Express.js res.locals Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Response

Definition

In Express.js, res.locals is an object of response local variables scoped to the current request. Values you set on it are available in templates rendered with res.render() during that request only — they are not shared between requests.

Use it for request-level data: authenticated user, path context, flash-style flags, or page title. For data that must survive across requests, use app.locals instead.

Syntax

Read res.locals in your Express.js route handler. Start with this line.

Express
res.locals              // Object
res.locals.name = value
app.use((req, res, next) => {
res.locals.user = req.user;
res.locals.authenticated = !req.user.anonymous;
next();
});
CallWhat it does
res.locals // ObjectPrimary signature.
res.locals.name = valueRelated call form.
app.use((req, res, next) => {Related call form.
res.locals.user = req.user;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();

res.locals              // Object
res.locals.name = value
app.use((req, res, next) => {
res.locals.user = req.user;
res.locals.authenticated = !req.user.anonymous;
next();
});

Parameters

This Express.js property is read from the request or response — it has no call parameters.

Return Value

res.locals is a plain object scoped to the current request. Assign properties on it for templates and downstream middleware — it is not a function with a return value.

Express
res.locals.user = req.user;
res.locals.pageTitle = 'Dashboard';
res.render('dashboard');

Express.js Examples

Example 1 — Expose User in Middleware

Official pattern: make user and authenticated available to every template for this request.

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

// Demo: pretend a previous middleware set req.user
app.use((req, res, next) => {
  req.user = { name: 'Mari', anonymous: false };
  next();
});

app.use((req, res, next) => {
  res.locals.user = req.user;
  res.locals.authenticated = !req.user.anonymous;
  next();
});

app.get('/me', (req, res) => {
  res.json({
    user: res.locals.user,
    authenticated: res.locals.authenticated
  });
});

app.listen(3000);

How It Works

  1. Attach request data. Earlier middleware (or your auth layer) sets req.user.
  2. Copy into locals. res.locals.user = req.user exposes it to templates.
  3. Derive flags. authenticated is a simple boolean templates can branch on.
  4. Call next(). Later routes and res.render see the same res.locals for this request only.

Example 2 — Page Locals Before res.render

Set page-specific values on res.locals, then render. Templates read them as local variables.

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

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

app.get('/dashboard', (req, res) => {
  res.locals.pageTitle = 'Dashboard';
  res.locals.notifications = 3;
  res.render('dashboard');
  // views/dashboard.ejs can use <%= pageTitle %> and <%= notifications %>
});

app.listen(3000);

How It Works

  1. Enable views. view engine tells Express how to compile templates.
  2. Set request-only fields. Title and counts belong on res.locals, not the global app.
  3. Render. res.render('dashboard') merges locals into the template scope.
  4. One cycle. The next request starts with a fresh res.locals object.

Example 3 — app.locals vs res.locals

Site-wide name on the app; page title on the response.

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

app.locals.siteName = 'CodeToFun';

app.get('/about', (req, res) => {
  res.locals.pageTitle = 'About';
  res.json({
    site: app.locals.siteName,       // same for every request
    page: res.locals.pageTitle,      // this request only
    also: res.app.locals.siteName    // same object as app.locals
  });
});

app.listen(3000);

How It Works

  1. App-wide once. app.locals.siteName is set at startup and shared by all requests.
  2. Per request. res.locals.pageTitle exists only for this /about handling.
  3. Same app object. res.app.locals is identical to app.locals.
  4. Choose by lifetime. Shared branding → app.locals; page/user data → res.locals.

Runtime Support

res.locals is an Express.js response property. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers do not have this API.

✓ Server · Express 4 & 5

res.locals

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.locals Express on Node.js

Bottom line: Study and run these examples with Node.js and the express package. Add a view engine when practicing res.render.

Frequently Asked Questions

It is an object of response local variables scoped to the current request. Values are available to views rendered with res.render during that request only.
res.locals lasts for one request/response cycle. app.locals lasts for the life of the application process and is shared across requests.
In middleware or routes, before calling res.render — for example authenticated user, flash messages, or the current page title.
Yes. Official docs warn that locals keys should not be user-controlled; they can affect the view engine or enable XSS. Keep key names under your control and escape values in templates.
No. It is server-side data for Express templates. Browsers only receive the rendered HTML (or other response body).

Next: res.location()

Set the Location header for redirects without sending the response yet.

res.location() tutorial →

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