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

前端 未结 1 671
情话喂你
情话喂你 2020-11-30 18:44

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 be

相关标签:
1条回答
  • 2020-11-30 19:18

    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

    0 讨论(0)
提交回复
热议问题