问题:
有没有办法在每个 session /http 请求中创建变量存储?该变量必须是全局可访问的,并且每个 HTTP 请求/连接/ session 都不同,并且不需要在函数之间传递。
例如(只是为了说明):
setVariableThatCanBeAccessedByThisHTTPRequestOnly( 'a string data' );
任何东西都应该有效,但我宁愿避免将它从一个函数传递到另一个函数。
//I'm trying to get rid of passing req parameter to all custom functions.
//I'd like to store it in a variable somehow, that it can be accessed globally, but only within that session/request.
exports.functionName = function( req, res ) {
customFunctionThatRequiresReq( req );
};
原始问题
我最近一直在玩 node.js,有点担心它的全局范围。假设我们有一个 server.js 文件:
username = ""; //Global scope
然后当建立连接并且服务器收到请求时,它将执行以下操作:
username = selectUsernameWithConditions(); //This will return a username
我的问题是:如果两台不同的计算机向服务器发送请求,username
的值是否会独立不同?我的意思是,处理第一个请求时的 username
与处理第二个请求时的 username
是否不同,或者它们只是一个变量并且将被覆盖?
如果它们被覆盖,存储数据并使它们仅在该请求和/或 session 中全局可访问的最佳方式是什么?例如下面的代码:
username = selectUsernameWithConditions(); //This will return a username
将为不同的请求分配不同的用户名
,并且不会相互覆盖。
最佳答案
是的,但有一些注意事项。
您正在寻找一个名为 continuation-local-storage 的模块.
这允许您为当前请求的其余回调保留任意数据,并以全局方式访问它。
我写了一篇关于它的博客文章 here .但要点是:
- 安装 cls:
npm install --save continuation-local-storage
为您的应用创建命名空间(在您应用的主文件顶部)
var createNamespace = require('continuation-local-storage').createNamespace, namespace = createNamespace('myAppNamespace');
创建一个在 cls(continuation-local-storage)命名空间中运行下游函数的中间件
var getNamespace = require('continuation-local-storage').getNamespace, namespace = getNamespace('myAppNamespace'), app.use(function(req, res, next) { // wrap the events from request and response namespace.bindEmitter(req); namespace.bindEmitter(res); // run following middleware in the scope of the namespace we created namespace.run(function() { namespace.set(‘foo’, 'a string data'); next(); }); });
由于您在
namespace.run
中运行了next
,因此任何下游函数都可以访问命名空间中的数据var getNamespace = require('continuation-local-storage').getNamespace, namespace = getNamespace('myAppNamespace'); // Some downstream function that doesn't have access to req... function doSomething() { var myData = namespace.get('foo'); // myData will be 'a string data' }
需要注意的是,某些模块可能会“丢失”由 cls 创建的上下文。这意味着当你去查找命名空间上的“foo”时,它不会有它。有几种方法可以解决这个问题,即使用另一个模块,如 cls-redis 、cls-q 或绑定(bind)到命名空间。
关于javascript - 每个 HTTP/Session 请求的 GLOBAL 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25151952/