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.
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.
App-wide once.app.locals.siteName is set at startup and shared by all requests.
Per request.res.locals.pageTitle exists only for this /about handling.
Same app object.res.app.locals is identical to app.locals.
Choose by lifetime. Shared branding → app.locals; page/user data → res.locals.
Compatibility
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.
ServerNode.js runtime API
Node.js18+ for Express 5 · 14+ typical for Express 4
Supported
BunExpress-compatible server apps
Supported
DenoVia npm compatibility / adapters
Supported
res.localsExpress 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).