Express.js app.engine() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Application

Definition

app.engine(ext, callback) registers a template engine for a file extension. When you call res.render() (or app.render()), Express uses that callback to turn a view file into HTML.

By default, Express can require() engines that export __express (for example Pug). Use app.engine() when you want to map another extension (EJS → .html) or adapt an engine that does not follow the Express convention.

Syntax

Call app.engine in your Express.js app. Start with this line.

Express
app.engine(ext, callback)
CallWhat it does
app.engine(ext, callback)Primary signature.

Install with npm install express. Create the app with const app = express() before you use this API in an Express.js tutorial project.

Library declarations
Express
const express = require("express");
const app = express();

app.engine(ext, callback)

Parameters

ParameterDescription
extRequired. File extension without a leading dot — for example 'html', 'pug', or 'ejs'.
callbackRequired. Render function: (path, options, cb) where cb is (err, html). Often require('ejs').renderFile or require('pug').__express.

Return Value

app.engine() returns the same application object (app), which is usually ignored. The registered callback must match (path, options, callback).

Express
app.engine('html', engine);
// => app

app.engine('html', engine).set('view engine', 'html');

Express.js Examples

Example 1 — Map EJS to .html

Official-style mapping: render .html files with EJS’s renderFile.

Express
const express = require('express');
const app = express();

app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.set('views', './views');

app.get('/', (req, res) => {
  res.render('index', { title: 'Home', message: 'Hello' });
});

app.listen(3000);

How It Works

  1. Register the engine. app.engine('html', require('ejs').renderFile) maps .html to EJS.
  2. Set the default extension. view engine is html, so res.render('index') looks for index.html.
  3. Point at the folder. views tells Express where templates live.
  4. Render. The route passes locals; EJS replaces <%= ... %> and sends HTML.

Example 2 — Pug with __express

Engines that export __express can be registered explicitly (Express may also auto-load them).

Express
const express = require('express');
const app = express();

app.engine('pug', require('pug').__express);
app.set('view engine', 'pug');
app.set('views', './views');

app.get('/', (req, res) => {
  res.render('index', { title: 'Hey', message: 'Hello there!' });
});

app.listen(3000);

How It Works

  1. Wire Pug. require('pug').__express is the Express-compatible render function.
  2. Default extension. view engine is pug, so res.render('index') uses index.pug.
  3. Locals in. title and message become template variables.
  4. Shortcut. With Pug installed, app.set('view engine', 'pug') alone often works — Express can load __express for you.

Example 3 — Tiny Custom Engine

Show the expected callback shape with a minimal string-replace engine.

Express
const express = require('express');
const fs = require('fs');
const app = express();

app.engine('txt', (filePath, options, callback) => {
  fs.readFile(filePath, 'utf8', (err, content) => {
    if (err) return callback(err);
    const html = content.replace('{{message}}', options.message || '');
    callback(null, html);
  });
});

app.set('view engine', 'txt');
app.set('views', './views');

app.get('/', (req, res) => {
  res.render('hello', { message: 'Custom engine works' });
});

app.listen(3000);

How It Works

  1. Match the signature. Express calls (filePath, options, callback).
  2. Read the file. The engine loads the template from disk.
  3. Produce HTML. Replace placeholders, then call callback(null, html) (or callback(err) on failure).
  4. Render as usual. res.render('hello') finds hello.txt via view engine.

Runtime Support

app.engine() is an Express.js application method. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers only receive rendered HTML.

✓ Server · Express 4 & 5

app.engine()

Works wherever Express runs: Node.js is the primary target. Bun and Deno can run many Express apps with compatibility layers.

Server Node.js runtime API
Node.js 18+ for Express 5 · 14+ typical for Express 4
Supported
Bun Express-compatible server apps
Supported
Deno Via npm compatibility / adapters
Supported
app.engine() Express on Node.js

Bottom line: Install an engine package (ejs, pug, …), register it if needed, then run your app with node.

Frequently Asked Questions

It registers a template engine callback for a file extension so Express can render matching view files.
Not always. Engines that export __express (like Pug or EJS for .ejs files) can be loaded automatically after you set view engine. Use app.engine() to map another extension or wire engines that need an adapter.
app.engine() teaches Express how to render an extension. app.set('view engine', ext) chooses the default extension used by res.render() when you omit one.
Express expects (path, options, callback), where callback is (err, html). EJS renderFile and engine.__express follow that shape.
No. It configures your Express server on Node.js. Browsers only receive the rendered HTML.

Next: app.get()

Read application settings or handle HTTP GET routes.

app.get() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful