在Node.js Express中随时随地访问当前的req对象 [英] Access current req object everywhere in Node.js Express

查看:120
本文介绍了在Node.js Express中随时随地访问当前的req对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如果回调中没有'req'参数时如何访问req对象.

I wonder how to access req object if there's no 'req' parameter in callback.

这是场景:
在ExpressJs中,我有一个通用函数,它用于处理带有'req'对象的东西,但不将req传递给它.

This is the scenario:
In ExpressJs, I have a common function, it uses to handle something with 'req' object, but not pass req into it.

module.exports = {
    get: function(){
        var req = global.currentRequest;
        //do something...
    }
}

我当前的解决方案是我为所有请求编写一个中间件,将'req'放入全局变量,然后可以使用'global.currentRequest'在任何地方访问'req'.

My current solution is that I write a middleware for all request, I put the 'req' in global variable, then I can access the 'req' everywhere with 'global.currentRequest'.

// in app.js
app.use(function (req, res, next) {
    global.currentRequest= req;
    next();
});

但是我不知道这是否很好?有人可以提出建议吗?
非常感谢!

But I don't know if it's good? Can anyone have suggestions?
Thanks a lot!

推荐答案

唯一正确的方法是将req对象作为参数传递给需要它的所有函数.

The only proper way is to pass the req object through as an argument to all functions that need it.

将其存储在全局中根本无法工作,因为如果任何请求在处理过程中都使用异步调用,则多个请求可以同时处理,并且这些多个请求会相互mp脚,从而难以跟踪错误.这里没有捷径.将当前请求作为参数传递给需要它的任何代码.

Stashing it in a global simply will not work because multiple requests can be in process at the same time if any requests use async calls as part of their processing and those multiple requests will stomp on each other making a hard to track down bug. There are no shortcuts here. Pass the current request as an argument to any code that needs it.

您永远无法将特定于请求的数据放入node.js中的全局变量.这样做将为同时进行的两个请求创造机会,使彼此之间相互requests脚,并使数据在请求之间变得混乱.请记住,这是一台服务器,它可能正在处理许多客户端的请求.您不能对服务器使用一次一次的同步思考.一个node.js服务器可能同时具有多个处于运行状态的请求,因此普通全局变量不能用于特定于请求的数据.

You cannot put request-specific data into a global in node.js, ever. Doing so will create an opportunity for two requests that are in-flight at the same time to stomp on each other and for data to get confused between requests. Remember, this is a server that is potentially handling requests for many clients. You cannot use synchronous, one-at-a-time thinking for a server. A node.js server may potentially have many requests all in flight at the same time and thus plain globals cannot be used for request-specific data.

这里没有捷径.您只需要将req对象传递给需要它的函数即可.如果那意味着您必须更改几个中间函数的函数签名,那么就可以了.那就是你要做的.这是解决此类问题的唯一正确方法.

There is no shortcut here. You will just have to pass the req object through to the function that needs it. If that means you have to change the function signature of several intervening functions, then so-be-it. That's what you have to do. That is the only correct way to solve this type of problem.

在某些情况下,您可能可以使用闭包来捕获"所需的req对象,然后在内部函数中使用它而不将其传递给那些内部函数,但这听起来不像是您的功能结构.我们必须查看更多您的真实/实际代码,才能知道是否有这种可能性.

There are some circumstances where you may be able to use a closure to "capture" the desired req object and then use it in inner functions without passing it to those inner functions, but it does not sound like that is your function structure. We'd have to see a lot more of your real/actual code to be able to know whether that's a possibility or not.

这篇关于在Node.js Express中随时随地访问当前的req对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆