mirror of
https://github.com/expressjs/expressjs.com.git
synced 2026-02-22 03:51:33 +00:00
* Include 4.18 API doc updates in 5.x5c98ee4e94Co-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}.render5e918ea3a9Co-authored-by: Douglas Christopher Wilson <doug@somethingdoug.com> * Copy warning around securing locals to 5.xfcaca7fcc6Co-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 middlewarea5ca5b0edfCo-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>
1.2 KiB
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'