app.locals is an object for storing variables that live for as long as the application
runs, and are available to every route and middleware — unlike res.locals, which resets
per request.
Syntax
app.locals
Example: a single value
const express = require("express");
const app = express();
app.locals.supportEmail = "help@pixelmart.example";
app.get("/", (req, res) => {
res.send(app.locals.supportEmail);
});
app.listen(4900, () => console.log("Listening on 4900"));
Example: multiple values
app.locals.storeName = "PixelMart";
app.locals.currency = "BDT";
app.locals.taxRate = 0.05;
app.get("/config", (req, res) => {
res.json(app.locals);
});
Benefits
- Application-wide availability — no need to pass the same value through every route.
- Centralized configuration — a natural place for settings, feature flags, or shared constants.
- Simpler code — one source of truth for values used across the app.