简体   繁体   English

Node.js中的模块范围-同时Express请求

[英]Module scope in Node.js - Simultaneous Express requests

I'm using the id variable across different functions. 我在不同的函数中使用id变量。 The id is defined outside of the functions. id在函数外部定义。 Could this cause the variable to overwritten when receiving multiple simultaneous requests? 接收到多个同时请求时,这可能导致变量被覆盖吗? Is the behavior the same when this is done in the browser instead of in Node.js? 在浏览器中而不是在Node.js中完成此行为时,行为是否相同?

var id;
server.get('/myapp', function (req, res, next) {
    id = uuid.v4();
}

Yes, it causes conflicts 是的,它会引起冲突

If your request handlers have any asynchronous operations in them which allow more than one request to be in flight at the same time, then YES, the use of a module level variable that more than one request may access will cause conflicts and one request could trounce the variable that another was using. 如果您的请求处理程序中包含任何异步操作,这些操作允许同时运行多个请求,则为“是”,使用一个以上可以访问的模块级别变量将导致冲突,并且一个请求可能被删除另一个人正在使用的变量。

In a nutshell, don't do this 简而言之,不要这样做

Module level variables are only for data that ALL requests can safely access and share. 模块级变量仅适用于所有请求都可以安全访问和共享的数据。 Data for a single request should be inside the request handler or a property on the request or response object or some similar-type object that is per-request and not shared among multiple requests. 单个请求的数据应该在请求处理程序中,或者在请求或响应对象或某个类似类型的对象上的属性中,每个对象均应按此属性,并且不应在多个请求之间共享。

Data needed only during the processing of a request 仅在处理请求期间需要的数据

So, if you just need access to some data for the duration of this particular request, then you can put it in a local variable inside the request handler and you can pass it to any other functions you may call that need it while processing that request. 因此,如果您只需要在此特定请求期间访问某些数据,则可以将其放在请求处理程序内的局部变量中,并将其传递给在处理该请求时可能需要调用的其他任何函数。 Or, you can assign it as a property on the req object which is unique for each separate request and then pass the req object to any other functions you are calling that need access to the data. 或者,您可以将其分配为req对象的属性,该属性对于每个单独的请求都是唯一的,然后将req对象传递给您正在调用的任何其他需要访问数据的函数。

Data needed from one request to the next, but different for each user 从一个请求到下一个请求所需的数据,但每个用户的数据不同

If you are trying to keep track of some data from one request to the next for one particular client, then you can't store it this way at all. 如果您试图跟踪某个特定客户端从一个请求到另一个请求的某些数据,则根本无法以这种方式存储数据。 You will likely need to use a session manager so you can create a lasting session that can be correlated with one particular browser from one request to the next. 您可能需要使用会话管理器,以便可以创建一个持久会话,该会话可以与一个特定浏览器从一个请求到另一个请求相关联。 express-session is a popular session manager that makes it pretty easy. express-session是一个流行的会话管理器,它非常容易。

The way a session manager works is that the first time a given browser hits your server, it is assigned a unique cookie. 会话管理器的工作方式是,给定浏览器第一次访问您的服务器时,会为其分配一个唯一的cookie。 That cookie contains an encrypted session ID. 该Cookie包含一个加密的会话ID。 Then, the session manager creates a session object that is indexed by that session ID. 然后,会话管理器创建一个由该会话ID索引的会话对象。 When a second request from the same browser comes in, then express-session gets that previously set cookie, decrypts the sessionID, looks up the corresponding session object and lets you then access any data you may have stored in the session object on behalf of this particular user. 当来自同一浏览器的第二个请求进入时,express-session会获取先前设置的cookie,解密sessionID,查找相应的会话对象,然后让您访问代表该对象存储在会话对象中的任何数据特定用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM