Express.js app.locals Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Application

Definition

In Express.js, app.locals is an object of application-level local variables. Values you assign to it are available in every template rendered with res.render() (and app.render()) for the life of the process.

Use it for shared site data (title, contact email) or helper functions. In middleware, read the same object as req.app.locals. Per-request data belongs on res.locals instead.

Syntax

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

Express
app.locals                 // Object
app.locals.name = value
req.app.locals.name        // same object in middleware
CallWhat it does
app.locals // ObjectPrimary signature.
app.locals.name = valueRelated call form.
req.app.locals.name // same object in middlewareRelated 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.locals                 // Object
app.locals.name = value
req.app.locals.name        // same object in middleware

Parameters

ParameterDescription
Strings / numbersRequired. Site name, version, contact email
ObjectsRequired. Grouped config for layouts
FunctionsRequired. Helpers callable from templates

Return Value

app.locals is an object you read and write — not a function call. Assign properties on it; templates and handlers see those values. There is no separate “return” beyond the object itself.

Express
app.locals.title = 'CodeToFun';
app.locals.year = 2026;

// In a view / handler:
res.locals.page = 'home'; // request-scoped overlay
// app.locals.title stays available app-wide

Express.js Examples

Example 1 — Site Title in Views

Official-style assignments available to every res.render.

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

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

app.locals.title = 'My App';
app.locals.email = '[email protected]';

app.get('/', (req, res) => {
  res.render('home');
});

app.listen(3000);

How It Works

  1. Configure views. Set engine and views folder.
  2. Assign locals once. title and email live on the app.
  3. Render without repeating them. res.render('home') still exposes those names in the template.
  4. Persist. Every later request sees the same values until you change them.

Example 2 — app.locals vs res.locals

App-wide site name plus a per-request user name.

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

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

app.locals.site = 'CodeToFun';

app.get('/hi', (req, res) => {
  res.locals.user = req.query.name || 'Guest';
  res.render('hi');
});

app.listen(3000);

How It Works

  1. App scope. site is set once on app.locals.
  2. Request scope. user is set on res.locals inside the handler.
  3. Both appear in the view. The template can use site and user together.
  4. Next request. user resets; site stays CodeToFun.

Example 3 — Helper on app.locals

Attach a function templates (and middleware) can call — same idea as the docs’ helper pattern.

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

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

app.locals.upper = (text) => String(text).toUpperCase();

app.get('/', (req, res) => {
  console.log(req.app.locals.upper('hi')); // HI
  res.render('home', { message: 'welcome' });
});

app.listen(3000);

How It Works

  1. Store a function. app.locals.upper is a shared helper.
  2. Use in middleware. req.app.locals is the same object.
  3. Use in the template. EJS can call upper(message) while rendering.
  4. Keep keys trusted. Helper names are fixed by your code, not by users.

Runtime Support

app.locals is an Express.js application property. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers only see rendered HTML.

✓ Server · Express 4 & 5

app.locals

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

Bottom line: Set locals in Node.js, configure a view engine, then run the app with node and open a rendered page.

Frequently Asked Questions

It is an object of application-level local variables. Properties you set on it are available in templates rendered with res.render for the life of the app process.
app.locals lasts for the whole application. res.locals lasts only for one request/response cycle.
Use req.app.locals (or res.app.locals). That is the same object as app.locals.
Yes. Express documents this pattern — for example attaching a formatting function that templates can call.
No. It is server-side data for Express templates. Browsers only receive the rendered HTML.

Next: app.METHOD()

Learn the family of HTTP method routing helpers on the app.

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