Files
expressjs.com/_includes/api/en/5x/app-locals.md
krzysdz 008255253d Update 5x API docs with missing 4x changes (#1886)
* Include 4.18 API doc updates in 5.x

5c98ee4e94

Co-authored-by: Douglas Christopher Wilson <doug@somethingdoug.com>

* Copy acceptsLanguages documentation improvements to 5.x

https://github.com/expressjs/expressjs.com/pull/1402

Co-authored-by: Jon Ege Ronnenberg <jon.ronnenberg@gmail.com>

* Add warning boxes to {app,res}.render

5e918ea3a9

Co-authored-by: Douglas Christopher Wilson <doug@somethingdoug.com>

* Copy warning around securing locals to 5.x

fcaca7fcc6

Co-authored-by: Douglas Christopher Wilson <doug@somethingdoug.com>

* Copy res.cookie `partitioned` option docs

https://github.com/expressjs/expressjs.com/pull/1456

Co-authored-by: Rich Hodgkins <rhodgkins@gmail.com>

* Update req.body to point to built-in middleware

a5ca5b0edf

Co-Authored-By: Douglas Wilson <doug@somethingdoug.com>

* Copy setting multiple cookies example to 5.x

https://github.com/expressjs/expressjs.com/pull/1063

Co-Authored-By: Mo <hematy61@gmail.com>

---------

Co-authored-by: krzysdz <krzysdz@users.noreply.github.com>
Co-authored-by: Douglas Christopher Wilson <doug@somethingdoug.com>
Co-authored-by: Jon Ege Ronnenberg <jon.ronnenberg@gmail.com>
Co-authored-by: Rich Hodgkins <rhodgkins@gmail.com>
Co-authored-by: Mo <hematy61@gmail.com>
2025-05-01 18:55:30 -05:00

1.2 KiB

app.locals

The app.locals object has properties that are local variables within the application, and will be available in templates rendered with res.render.

The `locals` object is used by view engines to render a response. The object keys may be particularly sensitive and should not contain user-controlled input, as it may affect the operation of the view engine or provide a path to cross-site scripting. Consult the documentation for the used view engine for additional considerations.
console.dir(app.locals.title)
// => 'My App'

console.dir(app.locals.email)
// => 'me@myapp.com'

Once set, the value of app.locals properties persist throughout the life of the application, in contrast with res.locals properties that are valid only for the lifetime of the request.

You can access local variables in templates rendered within the application. This is useful for providing helper functions to templates, as well as application-level data. Local variables are available in middleware via req.app.locals (see req.app)

app.locals.title = 'My App'
app.locals.strftime = require('strftime')
app.locals.email = 'me@myapp.com'