Express.js res.render() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Response

Definition

In Express.js, res.render() loads a view template, fills it with data (locals), and sends the HTML to the client. It is the usual way to return pages when you use EJS, Pug, Handlebars (hbs), and similar engines.

Configure the engine with app.set('view engine', …) and the folder with app.set('views', …). Shared request data lives on res.locals. To send HTML you already built, use res.send(). See also the template engines guide.

Syntax

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

Express
res.render(view [, locals] [, callback])
CallWhat it does
res.render(view [, locals] [, 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();

res.render(view [, locals] [, callback])

Parameters

ParameterDescription
viewOptional. Template path relative to views (or absolute). Extension optional if view engine is set.
localsOptional. Optional data for the template. Merges with app.locals and res.locals.
callbackOptional. Optional (err, html). When present, Express does not auto-send the response.
view engineRequired. Default engine name (e.g. ejs, pug)
viewsRequired. Template directory (default: ./views)
view cacheRequired. Caches compiled templates (on by default in production)

Return Value

Without a callback, res.render() renders a view and sends HTML for you (async). With a callback (err, html), Express does not auto-send — your callback owns the response. Either way, do not treat it as a synchronous HTML string return.

Express
res.render('user', { name: 'Ada' });

res.render('user', { name: 'Ada' }, (err, html) => {
  if (err) return next(err);
  res.send(html);
});

Express.js Examples

Example 1 — Render a View with Locals

Set the engine once, then render a template and pass page data.

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

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

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

app.listen(3000);
views/home.ejs
<!DOCTYPE html>
<html lang="en">
<head><title><%= title %></title></head>
<body>
  <h1><%= message %></h1>
</body>
</html>

How It Works

  1. Engine is registered. view engine = ejs so 'home' maps to home.ejs.
  2. Views folder is set. Express looks in ./views.
  3. Locals fill the template. title and message replace <%= … %>.
  4. HTML is sent. No callback → Express finishes the response.

Example 2 — Combine res.locals and Render Locals

Put shared request data on res.locals; keep page-only fields in the render call.

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

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

app.use((req, res, next) => {
  res.locals.siteName = 'CodeToFun';
  next();
});

app.get('/profile', (req, res) => {
  res.render('profile', {
    name: 'Ada',
    role: 'Admin'
  });
});

app.listen(3000);
views/profile.ejs
<h1><%= siteName %></h1>
<p><%= name %> — <%= role %></p>

How It Works

  1. Middleware sets shared locals. siteName is available on every render for this request.
  2. Route adds page locals. name and role are only for /profile.
  3. Template sees both. Express merges app.locals, res.locals, and render locals.
  4. App-wide data belongs on app.locals instead of middleware when it never changes per request.

Example 3 — Callback Mode (Manual Send)

When you need the HTML string first (logging, email, custom headers), pass a callback.

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

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

app.get('/preview', (req, res) => {
  res.render('preview', { draft: true }, (err, html) => {
    if (err) {
      return res.status(500).send('Render error');
    }
    res.type('html').send(html);
  });
});

app.listen(3000);

How It Works

  1. Callback disables auto-send. You own the response.
  2. Handle err first. Return early so you never send twice.
  3. Send html yourself. Often via res.send(html) or res.type('html').
  4. Use for post-processing. Same pattern as app.render(), which never sends on its own.

Runtime Support

res.render() is an Express.js response method. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers only receive the finished HTML.

✓ Server · Express 4 & 5

res.render()

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

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

Bottom line: Study and run these examples with Node.js, express, and a view engine such as ejs. Copy a snippet into a local file and start the server with node.

Frequently Asked Questions

It finds a view template, fills it with local variables, and sends the rendered HTML to the client. Without a callback, Express sends the response for you.
Yes. Set app.set('view engine', 'ejs') (or pug, hbs, and so on) and keep templates under the views folder (or app.set('views', …)).
res.send() sends a string/buffer/object you already have. res.render() loads a template file, merges data, then sends HTML.
Express does not auto-send. Your callback gets (err, html). You must res.send(html) yourself, and handle errors.
No. It runs on your Express server. The browser only receives the finished HTML response.

Next: res.send()

Send a string, buffer, or object as the HTTP response body.

res.send() 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