node.js Express中间件中的req.locals vs. res.locals vs. res.data vs. req.data vs. app.locals

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33451053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 19:27:15  来源:igfitidea点击:

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

node.jsexpressmiddleware

提问by Sunny

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.

我认为我可以通过使用req.locals通过不同的中间件获得相同的数据传播。此外,请求和响应对象似乎都在请求开始时将 locals 属性初始化为空对象。

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

另外,也可以设置 res.mydata 或 req.mydata 属性吗?

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.

理论上,app.locals 也可以用于通过不同的中间件传递这些数据,因为它会跨中间件持续存在,但这与 app.locals 的传统用法相反。它更多地用于特定于应用程序的数据。还需要在请求-响应周期结束时清除该数据,以便相同的变量可用于下一个请求。

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

通过中间件传播中间结果的最佳和标准方法是什么?

回答by xaviert

As you mentioned, both req.locals, res.localsor even your own defined key res.userDatacan be used. However, when using a view engine with Express, you can set intermediate data on res.localsin 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.localsto avoid overwriting view data in res.locals, though this is not officially documented.

正如您所提到的,可以使用两者req.localsres.locals甚至您自己定义的密钥res.userData。但是,当在 Express 中使用视图引擎时,您可以res.locals在中间件中设置中间数据,并且该数据将在您的视图中可用(请参阅此帖子)。通常的做法是在中间件内设置中间数据,req.locals以避免覆盖 中的视图数据res.locals,尽管这没有正式记录。

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

res.locals一个包含作用域为请求的响应局部变量的对象,因此仅可用于在该请求/响应周期(如果有)期间呈现的视图。否则,此属性与 相同app.locals

此属性可用于公开请求级信息,例如请求路径名、经过身份验证的用户、用户设置等。

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