每个HTTP/会话请求的GLOBAL数据? [英] GLOBAL data per HTTP/Session request?

查看:93
本文介绍了每个HTTP/会话请求的GLOBAL数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

是否有任何方法可以在每个会话/http请求中创建变量存储?该变量必须是全局可访问的,并且每个HTTP请求/连接/会话均不同,并且不必在函数之间传递.

例如(仅作说明):

setVariableThatCanBeAccessedByThisHTTPRequestOnly( 'a string data' );

任何东西都应该起作用,但是我宁愿不要将其从一个函数传递到另一个函数.

Anything should work, but I'd prefer to stay away from passing it from function to function.

//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,对它的GLOBAL范围有点担心.假设我们有一个server.js文件:

I've been playing with node.js lately, and have a little concern about its GLOBAL scope. Let's say we have a server.js file:

username = ""; //Global scope

然后,当建立连接并且服务器收到请求时,它将执行以下操作:

Then when a connection is established and the server receives a request, it will do the followings:

username = selectUsernameWithConditions(); //This will return a username

我的问题是:如果2台不同的计算机正在将请求发送到服务器,则username的值是否将独立地不同?我的意思是,处理第一个请求时的username与处理第二个请求时的username是否不同,或者它们只是一个变量并将被覆盖?

My question is: If 2 different computers are sending the requests to the server, will the value of username be independently different? I mean, does the username when the first request is processed different from the username when the second request is processed, or they are just one variable and will be overridden?

如果它们被覆盖,什么是存储数据并使它们仅在该请求和/或会话内可全局访问的最佳方法?例如,以下代码:

If they are overridden, what is the best way to store data and make them globally accessible within that requests and/or session only? For example, the following code:

username = selectUsernameWithConditions(); //This will return a username

将为不同的请求分配不同的username,并且不会互相覆盖.

Will assign username differently for different requests and not overriding each other.

推荐答案

是的,但有一些警告. 您正在寻找一个名为 continuation-local-storage 的模块.
这使您可以为当前请求的其余回调保留任意数据,并以全局方式访问它.
我写了一篇关于它的博客文章此处.但是要点是:

Yes, with some caveats. You're looking for a module called continuation-local-storage.
This allows you to keep arbitrary data for the remainder of callbacks for the current request, and access it in a global fashion.
I wrote a blog post about it here. But the gist is this:

  1. 安装cls:npm install --save continuation-local-storage
  2. 为您的应用创建名称空间(在应用主文件的顶部)

  1. Install cls: npm install --save continuation-local-storage
  2. Create a namespace for your app (at the top of the main file for your app)

var createNamespace = require('continuation-local-storage').createNamespace, 
    namespace = createNamespace('myAppNamespace');

  • 创建一个在cls(continuation-local-storage)名称空间中运行下游功能的中间件

  • Create a middleware that runs downstream functions in the cls (continuation-local-storage) namespace

    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,所以任何下游函数都可以访问命名空间中的数据

  • Since you ran next within namespace.run, any downstream function can access data in the namespace

    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,或绑定到名称空间.

  • There is the caveat that certain modules can "lose" the context created by cls. This means that when you go to lookup 'foo' on the namespace, it won't have it. There are a few ways to deal with this, namely using another module like cls-redis, cls-q, or binding to the namespace.

    这篇关于每个HTTP/会话请求的GLOBAL数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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