Express.js app.render() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Application

Definition

In Express.js, app.render(view, [locals], callback) renders a view to an HTML string and calls callback(err, html). Unlike res.render(), it does not send the response by itself.

Think of it as a utility for emails, PDF pipelines, or custom delivery. Internally, res.render() uses app.render(). You still need views + a view engine via app.set() and often app.engine().

Syntax

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

Express
app.render(view, callback)
app.render(view, locals, callback)
CallWhat it does
app.render(view, callback)Primary signature.
app.render(view, locals, callback)Related call form.

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.render(view, callback)
app.render(view, locals, callback)

Parameters

ParameterDescription
viewRequired. Name of the view to render (resolved under the views directory / view engine).
localsOptional. Optional. Local variables for the template (for example { name: 'Tobi' }).
callbackRequired. (err, html) — error first, then the rendered HTML string.

Return Value

app.render() does not return HTML synchronously. You always pass a callback of the form (err, html) and use the rendered string there. The call itself is for side-effect rendering, not a usable return value.

Express
app.render('email', { name: 'Ada' }, (err, html) => {
  if (err) throw err;
  // html is the rendered string — send email, cache, etc.
});

Express.js Examples

Example 1 — Render to String

Official callback shape: get HTML without sending a response yet.

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

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.render('hello', (err, html) => {
  if (err) throw err;
  console.log(html);
});

app.listen(3000);
Express
const express = require('express');
const path = require('path');
const app = express();

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

function buildWelcomeEmail(name, done) {
  app.render('email', { name }, (err, html) => {
    if (err) return done(err);
    done(null, html);
  });
}

buildWelcomeEmail('Tobi', (err, html) => {
  if (err) throw err;
  // sendMail({ to: '...', html })
  console.log(html);
});

app.listen(3000);

views/email.ejs:

email.ejs
<p>Welcome, <%= name %>!</p>

How It Works

  1. Locals object. { name } becomes template data.
  2. Engine fills the view. EJS substitutes <%= name %>.
  3. Callback gets HTML. Ready for an email client or queue.
  4. Safe values. Escape output in templates; never pass user-controlled keys in locals.

Example 3 — Render Then res.send

When you need the string first (minify, wrap, log), then send it.

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

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.get('/preview', (req, res, next) => {
  app.render('hello', { title: 'Preview' }, (err, html) => {
    if (err) return next(err);
    // optional: transform html here
    res.type('html').send(html);
  });
});

app.listen(3000);

How It Works

  1. Route starts. GET /preview needs HTML.
  2. app.render first. You receive the string in the callback.
  3. Optional work. Transform or log before responding.
  4. Send manually. res.send(html) finishes the response — or use res.render when you need none of that.

Runtime Support

app.render() is an Express.js application method. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers receive HTML only after you send it.

✓ Server · Express 4 & 5

app.render()

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.render() Express on Node.js

Bottom line: Install a view engine (for example ejs), set views, then call app.render.

Frequently Asked Questions

It renders a view template to an HTML string and passes the result to a callback as callback(err, html). It does not send the response by itself.
res.render() renders and usually sends HTML to the client. app.render() only produces the string — useful for emails, logging, or custom delivery. Internally, res.render() uses app.render().
Yes. Set views and view engine (and app.engine when needed) the same way you would for res.render().
Yes. Pass an object as the second argument: app.render('email', { name: 'Tobi' }, callback).
No. Official docs warn that the view argument performs filesystem operations — never put end-user input in the view name.

Next: app.route()

Chain .get/.post/.put handlers on a single path.

app.route() 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