332

I want to pass some variable from the first middleware to another middleware, and I tried doing this, but there was "req.somevariable is a given as 'undefined'".


//app.js
..
app.get('/someurl/', middleware1, middleware2)
...

////middleware1
...
some conditions
...
res.somevariable = variable1;
next();
...

////middleware2
...
some conditions
...
variable = req.somevariable;
...
2
  • 2
    Should work. Unless it's a typo in the question you probably fail because you assign the value to res in middleware1 and try to get it from req in middleware2. Commented Sep 18, 2013 at 14:44
  • 2
    Local variables are available in middleware via req.app.locals expressjs.com/pt-br/api.html#app.locals Commented Dec 24, 2018 at 19:48

10 Answers 10

690

v4.x API docs

This is what the res.locals object is for. Setting variables directly on the request object is not supported or documented. res.locals is guaranteed to hold state over the life of a request.

Quote from the docs:

Use this property to set variables accessible in templates rendered with res.render. The variables set on res.locals are available within a single request-response cycle, and will not be shared between requests.

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.

app.use(function(req, res, next) {
    res.locals.user = req.user;  
    res.locals.authenticated = !req.user.anonymous;
    next();
});

To retrieve the variable in the next middleware:

app.use(function(req, res, next) {
    if (res.locals.authenticated) {
        console.log(res.locals.user.id);
    }
    next();
});
Sign up to request clarification or add additional context in comments.

13 Comments

res.locals is intended to be used by views that are eventually rendered during the lifecycle of the request, this is what the documentation is saying. If you are not using views it is overkill to put things on the locals object and it is not the convention. In these cases, I tend to encourage people to stick with the convention so that we can reduce cognitive load while learning concepts like Express or request lifecycles.
@real_ate that is the primary usage of res.locals, however it doesn't address OPs question. Passing request scoped variables to future middleware is sometimes a necessity, the times I've had it come up is typically around user authentication in an early middleware which sets claims that get routed in a downstream middleware.
The express documentation for writing middleware sets variables on the req object expressjs.com/en/guide/writing-middleware.html. Look at the section Middleware function requestTime
This was the way you'd do it before they added res.locals so might be old docs. res.locals is a namespace specifically for this. Mutating request could be dangerous as there are reserved properties and evolving standards.
@goonerify thats luckily not how response works. Responses are transmitted by calling a functional property of the response object such as res.json({}), etc. res.locals is only available on the back-end over the life of the request. expressjs.com/en/5x/api.html
|
290

Attach your variable to the res.locals object, not req.

Instead of

req.somevariable = variable1;

Have:

res.locals.somevariable = variable1;

As others have pointed out, res.locals is the recommended way of passing data through middleware.

10 Comments

use res.locals.variable = var; see the other answer
to retrieve the variable, use req.res.locals.variable in next middleware function
@WilliamWu its res.locals, not req.res.locals.
Sorry for asking, but now I'm confused. Is this contradictory answer. First you suggested to do it req.somevariable = variable1; and then you suggested to use res.locals. Maybe I missed something, if someone can explain me better?
Where is res.locals recommended? The API documentation you all keep referring to seems to indicate that the res.locals field is for passing data to views when using res.render. In fact, middleware like express.json() extends req, which seems to contradict the advice given here?
|
46

The most common pattern for passing variables on to other middleware and endpoint functions is attaching values to the request object req.

In your case, that would mean having middlewares such as these:

app.use(function (req, res, next) {
  req.someVariable = 123;
  next();
});

app.use(function (req, res, next) {
  console.log("The variable is", req.someVariable);
  next();
});

There are many common use cases of this pattern, and it is the standard way of doing it in the express community. See, for example:


It is worth noting that the currently most highly voted answer incorrectly recommends using res.locals for this purpose---which seems to stem from a misreading of the documentation. For that reason, I'll elaborate on why this is not the usual approach to the problem (although it isn't particularly harmful either).

The documentation

As supporting evidence for the res.locals approach being the appropriate one for the case, the now outdated documentation is cited:

An object that contains response local variables scoped to the request, and therefore available only to the view(s) rendered during that request / response cycle (if any). Otherwise, this property is identical to app.locals.

This property is useful for exposing request-level information such as the request path name, authenticated user, user settings, and so on.

Note the framing here: res.locals is for variables only available "to the view(s) rendered during that request" (emphasis added).

That is what res.locals relates to. res.render renders some template file with some given data as well as access to the locals. This was actually more clear in the v2 docs, and we've now updated the current Express documentation to be clearer:

Use this property to set variables accessible in templates rendered with res.render. The variables set on res.locals are available within a single request-response cycle, and will not be shared between requests.

In order to keep local variables for use in template rendering between requests, use app.locals instead.

This property is useful for exposing request-level information such as the request path name, authenticated user, user settings, and so on to templates rendered within the application.

(Emphasis added.)

The guide

Further evidence of extending req being the standard approach is found in the guide on Writing Middleware, which states:

Next, we’ll create a middleware function called “requestTime” and add a property called requestTime to the request object.

const requestTime = function (req, res, next) {
 req.requestTime = Date.now()
 next()
}

When this was mentioned in discussion in the answers on this here question, one user responded: "This was the way you'd do it before they added res.locals so might be old docs. res.locals is a namespace specifically for this."

This doesn't track with the history of the codebase, however: locals have been present since v2, which is significantly before e.g. express.json was included in the library, at which point it would have made sense to change the behvaior, if it was indeed correct to save values in res.locals.

Closing notes

Shoutout to @real_ate who wrote in the comments, but was overlooked.

3 Comments

People please consider using res.local.xxx as mentioned in other answers. Of course you can do this way also, it is javascript after all. But you may not want to pollute the req or res object. The libraries use req.xxx to make them look legit and ease of use bc the use case is simple and all introduced variables are documented as part of the framework. Real life SW is not always like that. Unless you are developing a framework, just follow the guidelines and save the source code maintain cost.
When using express to handle data only (and not doing any SSR) is it safe to assume that using response.locals is a viable way (altough not the proper one) of passing variables across middlewares within the request/response lifecycle? My use case is I want to parse and validate with one middleware then I want to reuse the parsed value without parsing again
@UmbertoFontanazza I mean... it's not dangerous, but it is confusing and not what is intended by the design. Both req.value and res.locals.value will be available within the lifecycle, but the parsed values have nothing to do with your response, if they are to be used in a different middleware, so storing them on the response object communicates the wrong intent.
46

I don't think that best practice will be passing a variable like req.YOUR_VAR. You might want to consider req.YOUR_APP_NAME.YOUR_VAR or req.mw_params.YOUR_VAR.

It will help you avoid overwriting other attributes.

5 Comments

How do you suggest to set req.YOUR_APP_NAME = {} initially? You just try to write to req.YOUR_APP_NAME.someVar you will get an error as req.YOUR_APP_NAME is not defined yet.
@Kousha You can write router middleware at the top of your routing script: router.use(function(req,res,next){req.YOUR_APP_NAME = {};next()})
is there a way to pass it as param with the next() function? like next(myObj)
@Kousha - Your undefined error has nothing to do with express, its you trying to assign a property to an object that does not exist. Typically to do this you would use req.APP_NS = req.APP_NS || {}; req.APP_NS.somevar = 'value'.
@philx_x, if you pass any parameter to next function, it will trigger express' error handler and drop out. check the docs.
8

That's because req and res are two different objects.

You need to look for the property on the same object you added it to.

Comments

7

The trick is pretty simple... The request cycle is still pretty much alive. You can just add a new variable that will create a temporary, calling

app.get('some/url/endpoint', middleware1, middleware2);

Since you can handle your request in the first middleware

(req, res, next) => {
    var yourvalue = anyvalue
}

In middleware 1 you handle your logic and store your value like below:

req.anyvariable = yourvalue

In middleware 2 you can catch this value from middleware 1 doing the following:

(req, res, next) => {
    var storedvalue = req.yourvalue
}

Comments

1

As mentioned above, res.locals is a good (recommended) way to do this. See here for a quick tutorial on how to do this in Express.

1 Comment

Just to be clear, the video doesn't show a solution to what is being asked. The question is about how to pass values from one middleware to another. The video explicitly states that res.locals is for passing values straight to template rendering. (But it's a fine video)
1

From https://expressjs.com/en/guide/writing-middleware.html :

const requestTime = function (req, res, next) {
  req.requestTime = Date.now()
  next()
}

Comments

1

I had this issue too and to overcome it, I saved the variable in Middelware2 passed from midlleware1. So, the solution based on the original question looks something like this:

//app.js
app.get('/someurl/', middleware1, middleware2)

// middleware1
// ...
req.somevariable = variable1;
next();

// middleware2
// ...
const variable = req.somevariable;

Comments

0

Along with middleware, I also use "utility" functions so I don't need to play with the req/res variables (which kind-of increases complexity).

// for ex. we need a condition based on the headers
const isUserAdmin = async(headers) => {
   const authHeaderValue = headers.authorization;
   // perform logic
   return true; // or false
}

app.get("/hello-world", async(req, res, next) => {
  const isAdmin = await isUserAdmin(req.headers)
  if(isAdmin) {
    res.send("You are admin.")
  } else {
    res.send("You do not have admin access.")
  }
})

Note that isUserAdmin a re-usable function so it can be re-used in any number of request handlers.

Good Luck...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.