Last updated: Apr 5, 2024
Reading timeยท5 min
You can learn more about the related topics by checking out the following tutorials:
Note: the second subheading shows how to pass custom parameters to middleware functions in Express.
You can set a property on the res.locals
object to pass variables to the
next middleware in Express.js.
The properties that are set on res.locals
are available within a single
request-response cycle.
import express from 'express'; // ๐๏ธ if you use CommonJS require() // const express = require('express') const app = express(); function middleware1(req, res, next) { const site = 'bobbyhadz.com'; // ๐๏ธ 1) set property on res.locals res.locals.site = site; next(); } function middleware2(req, res, next) { // ๐๏ธ 2) access property from previous middleware console.log(res.locals.site); next(); } app.get('/', middleware1, middleware2, function (req, res) { res.json({ // ๐๏ธ 3) access property from first middleware site: res.locals.site, hello: 'world', }); }); const port = 5000; app.listen(port, () => { console.log(`Example app listening on port ${port}`); });
If I run the npx nodemon index.js
command and visit
http://localhost:5000/, I can see that the request
succeeds.
npx nodemon index.js
The value of the res.locals.site
property is also logged to the terminal.
We have 2 middleware functions - middleware1
and middleware2
.
The first middleware function sets a site
property on the
res.locals object.
function middleware1(req, res, next) { const site = 'bobbyhadz.com'; // ๐๏ธ 1) set property on res.locals res.locals.site = site; next(); }
Properties that are set on the res.locals
object are available within a single
request-response cycle and are not shared between requests.
Once you set a property on the res.locals
object, you can access it in the
next middleware.
function middleware2(req, res, next) { // ๐๏ธ 2) access property from previous middleware console.log(res.locals.site); next(); }
You can also access the res.locals.site
property in your last route handler.
app.get('/', middleware1, middleware2, function (req, res) { res.json({ // ๐๏ธ access property from first middleware site: res.locals.site, hello: 'world', }); });
The res.locals
object should be your preferred approach rather than setting
properties on the request (req
) object.
Here is an example from the Express.js docs on how res.locals
is used.
app.use(function (req, res, next) { // Make `user` and `authenticated` available in next middlewares res.locals.user = req.user res.locals.authenticated = !req.user.anonymous next() })
Setting properties on the res.locals
object is useful for exposing
request-level information.
req
) object is not recommended because you might override properties that were already set on the object.However, if you decide to set properties on the req
object, use the following
approach.
import express from 'express'; // ๐๏ธ if you use CommonJS require() // const express = require('express') const app = express(); function middleware1(req, res, next) { const site = 'bobbyhadz.com'; // ๐๏ธ 1) set property on req req.site = site; next(); } function middleware2(req, res, next) { // ๐๏ธ 2) access property from previous middleware console.log(req.site); next(); } app.get('/', middleware1, middleware2, function (req, res) { res.json({ // ๐๏ธ 3) access property from first middleware site: req.site, hello: 'world', }); }); const port = 5000; app.listen(port, () => { console.log(`Example app listening on port ${port}`); });
The code sample sets the site
property on the request (req
) object which
makes the property available in the next middlewares.
However, as previously noted, it's better to set properties on the res.locals
object because you don't risk overriding existing properties.
If you need to pass custom parameters to middleware functions in Express:
req
, res
and next
parameters.import express from 'express'; // ๐๏ธ if you use CommonJS require() // const express = require('express') const app = express(); // ๐๏ธ takes `site` parameter function middleware1(site) { // ๐๏ธ returns true middleware function return (req, res, next) => { console.log(site); res.locals.site = site; next(); }; } app.get('/', middleware1('bobbyhadz.com'), function (req, res) { res.json({ // ๐๏ธ access property from first middleware site: res.locals.site, hello: 'world', }); }); const port = 5000; app.listen(port, () => { console.log(`Example app listening on port ${port}`); });
If I run the npx nodemon index.js
command and visit
http://localhost:5000/, I can see that the request
succeeds.
npx nodemon index.js
The value of the site
variable is also logged to the terminal.
The middleware1
function takes a site
variable and returns another function.
function middleware1(site) { // ๐๏ธ returns true middleware function return (req, res, next) => { console.log(site); res.locals.site = site; next(); }; }
The inner function is the true middleware function that takes the req
, res
and next
parameters.
This way, we can invoke the middleware1()
function with a value for the site
parameter.
When the middleware1()
function is invoked, it returns the inner function
but with access to the site
parameter.
app.get('/', middleware1('bobbyhadz.com'), function (req, res) { res.json({ // ๐๏ธ access property from first middleware site: res.locals.site, hello: 'world', }); });
We set the site
property on the res.locals
object, however, you can also set
the property on the req
object.
import express from 'express'; // ๐๏ธ if you use CommonJS require() // const express = require('express') const app = express(); function middleware1(site) { return (req, res, next) => { console.log(site); req.site = site; next(); }; } app.get('/', middleware1('bobbyhadz.com'), function (req, res) { res.json({ // ๐๏ธ access property from first middleware site: req.site, hello: 'world', }); }); const port = 5000; app.listen(port, () => { console.log(`Example app listening on port ${port}`); });
I prefer setting properties on res.locals
to avoid overriding properties on
the request (req
) object by mistake.
The key moment of passing custom parameters to a middleware function is that:
req
, res
and next
).You can learn more about the related topics by checking out the following tutorials: