app.locals

An object of variables available to all rendered templates for the lifetime of the app.

Since Express 4 Spec ↗

Syntax

app.locals.key = value

Returns

object — A persistent object merged into every render context.

Examples

app.locals.siteName = 'JS Schools';
app.locals.year = new Date().getFullYear();

app.get('/', (req, res) => res.render('home'));
Output
<title>JS Schools</title>
app.locals.formatDate = (d) => d.toISOString().slice(0, 10);

Notes

Use `app.locals` for app-wide constants and helper functions available in views. For per-request data (current user, request id) use `res.locals` instead, which does not persist between requests.

See also