node.js 在 Express.js 中使用 next() 将变量传递给下一个中间件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18875292/
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 15:40:03  来源:igfitidea点击:

Passing variables to the next middleware using next() in Express.js

javascriptnode.jsexpressmiddlewarenext

提问by user2791897

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

好吧,我的问题是我想将一些变量从第一个中间件传递给另一个中间件,我尝试这样做,但是有“req.somevariable给定为‘未定义’”。



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


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


////middleware2
...
some conditions
...
variable = req.somevariable;
...

采纳答案by Amberlamps

Attach your variable to the reqobject, not res.

将您的变量附加到req对象,而不是res.

Instead of

代替

res.somevariable = variable1;

Have:

有:

req.somevariable = variable1;

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

正如其他人所指出的,res.locals是通过中间件传递数据的推荐方式。

回答by cchamberlain

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

这就是res.locals对象的用途。不支持或记录直接在请求对象上设置变量。res.locals保证在请求的生命周期内保持状态。

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.

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

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

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();
});

回答by Doron Segal

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_VARor req.mw_params.YOUR_VAR.

我认为最佳实践不会传递像req.YOUR_VAR. 您可能要考虑req.YOUR_APP_NAME.YOUR_VARreq.mw_params.YOUR_VAR

It will help you avoid overwriting other attributes.

它将帮助您避免覆盖其他属性。

回答by SLaks

That's because reqand resare two different objects.

那是因为reqres是两个不同的对象。

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

您需要在添加它的同一对象上查找该属性。

回答by Delino

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:

在中间件 1 中,您处理您的逻辑并存储您的值,如下所示:

req.anyvariable = yourvalue

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

在中间件 2 中,您可以通过执行以下操作从中间件 1 中捕获此值:

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

回答by nax3t

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

如上所述, res.locals 是一个很好的(推荐的)方法来做到这一点。有关如何在 Express 中执行此操作的快速教程,请参见此处