node.js中的全局变量是服务器范围内的,还是仅适用于该会话或用户

时间:2020-12-28 16:49:04

I have a very basic question and apologise if it is dumb - are global variables in node.js server-wide or is it just for that session or user? For instance, I'm thinking of creating a global boolean variable under a routes.js file to store whether twitter credentials have been verified, and if it has, then it shouldn't re-verify.

我有一个非常基本的问题,如果它是愚蠢的道歉 - 在服务器范围内的node.js中是全局变量还是仅适用于该会话或用户?例如,我正在考虑在routes.js文件下创建一个全局布尔变量来存储是否已经验证了twitter凭证,如果有,则不应该重新验证。

However, I'm wondering whether the variable will be created for every single session/user or will it be created once for the entire server (i.e. if one person verifies, which sets verifiedCredentials to true, will the variable be true for all other users?).

但是,我想知道是否为每个会话/用户创建变量,或者是否为整个服务器创建一次变量(即,如果一个人验证,将verifiedCredentials设置为true,则变量对于所有其他用户都是真的) ?)。

routes.js (Server side):
'use strict';

var verifiedCredentials = false;

/**
 * Application routes
 */
module.exports = function(app) {

  // Server API Routes
  app.get('/api/awesomeThings', function(req, res) {
    if (!verifiedCredentials) {
      //do some verification
      verifiedCredentials = true;
    }
  });
}

1 个解决方案

#1


3  

It's not so hard to test that.

测试它并不是那么难。

But as you didn't, here's the result : global variables are really global, there's no session/user context in the core of node.

但是你没有,结果如下:全局变量实际上是全局变量,节点核心没有会话/用户上下文。

In fact node isn't really a http server program, it's an event based engine on which you can run many things and which happens to be often used to run http based applications (but it can be used for applications for which there's not even a notion of session or user).

事实上,node并不是一个真正的http服务器程序,它是一个基于事件的引擎,你可以在其上运行很多东西,而且它经常被用来运行基于http的应用程序(但是它可以用于那些甚至没有会话或用户的概念)。

If you do need to attach variables to sessions, then, as you're using express, just ask it to manage sessions and store your variable as in

如果您确实需要将变量附加到会话,那么,当您使用express时,只需要它来管理会话并将变量存储为

req.session.verifiedCredentials = false;

Note that if you're trying to manage authentication, as your variable name lets think, you might be interested by already tested modules doing it, for example passport.

请注意,如果您正在尝试管理身份验证,因为您的变量名称可以让您思考,您可能会对已经过测试的模块感兴趣,例如护照。

#1


3  

It's not so hard to test that.

测试它并不是那么难。

But as you didn't, here's the result : global variables are really global, there's no session/user context in the core of node.

但是你没有,结果如下:全局变量实际上是全局变量,节点核心没有会话/用户上下文。

In fact node isn't really a http server program, it's an event based engine on which you can run many things and which happens to be often used to run http based applications (but it can be used for applications for which there's not even a notion of session or user).

事实上,node并不是一个真正的http服务器程序,它是一个基于事件的引擎,你可以在其上运行很多东西,而且它经常被用来运行基于http的应用程序(但是它可以用于那些甚至没有会话或用户的概念)。

If you do need to attach variables to sessions, then, as you're using express, just ask it to manage sessions and store your variable as in

如果您确实需要将变量附加到会话,那么,当您使用express时,只需要它来管理会话并将变量存储为

req.session.verifiedCredentials = false;

Note that if you're trying to manage authentication, as your variable name lets think, you might be interested by already tested modules doing it, for example passport.

请注意,如果您正在尝试管理身份验证,因为您的变量名称可以让您思考,您可能会对已经过测试的模块感兴趣,例如护照。