Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

req.locals vs. res.locals vs. res.data vs. req.data vs. app.locals in Express middleware

There are some similar questions asked but my question is that if I want to propagate intermediate results that I get along the different routing middleware, what is the best way to do that?

app.use(f1); app.use(f2); app.use(f3);  function f1(req,res,next) {   //some database queries are executed and I get results, say x1   res.locals.dbResults = {...};   next(); }  function f2(req,res,next) {   // more processing based upon req.locals.dbResults    res.locals.moreResults = {....};   next(); } // ... 

I think that I can get the same propagation of data through the different middleware by using req.locals. Also, it appears that the request and response objects both have the locals properties initialized to an empty object at the start of the request.

Also, one can set res.mydata or req.mydata properties too?

In theory, app.locals can also be used for passing this data along through the different middleware as it will persist across middlewares but that would be contrary to the conventional use of app.locals. It is used more for application specific data. It will also be necessary to clear that data at the end of the request-response cycle so the same variables can be used for the next request.

What is the optimal and standard way to propagate intermediate results through middleware?

like image 547
Sunny Avatar asked Oct 31 '15 11:10

Sunny


People also ask

What is req locals in Express?

The res. locals is an object that contains the local variables for the response which are scoped to the request only and therefore just available for the views rendered during that request or response cycle.

What is req app in Express?

The req. app property holds the reference to the instance of the Express application that is using the middleware. Syntax: req.app. Parameter: No parameters. Return Value: Object.

What is res data?

Short for response , the res object is one half of the request and response cycle to send data from the server to the client-side through HTTP requests.


1 Answers

As you mentioned, both req.locals, res.locals or even your own defined key res.userData can be used. However, when using a view engine with Express, you can set intermediate data on res.locals in your middleware, and that data will be available in your view (see this post). It is common practice to set intermediate data inside of middleware on req.locals to avoid overwriting view data in res.locals, though this is not officially documented.

res.locals 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.

Source: http://expressjs.com/en/api.html#res.locals

like image 153
xaviert Avatar answered Sep 17 '22 15:09

xaviert