简体   繁体   English

在node.js中使用文件范围变量

[英]using file scope variables in node.js

Beginner question 初学者问题

Running on server side, in node.js: 在node.js的服务器端运行:

If I use a file-scope (or even global) variable which is set by an export.function(), where that exported function is called via ajax from a client, if multiple requests come from different clients, is the variable now prone to unexpected results? 如果我使用由export.function()设置的文件范围(甚至全局)变量,则该导出的函数是通过客户端的ajax调用的,如果多个请求来自不同的客户端,则该变量现在容易出乎意料的结果?

Ie do I need to set up an array so every time the export.function() is called it adds a new file-scope instance for that particular ajax request? 即我是否需要设置一个数组,以便每次调用export.function()时都会为该特定的ajax请求添加一个新的文件作用域实例? Or is this magically handled by node.js where every ajax request gets its own instance of the server? 还是由每个ajax请求获得其自己的服务器实例的node.js神奇地处理?

Requests will share the same instances so you'll need to guard against this. 请求将共享相同的实例,因此您需要注意这一点。 Note, however, that blocks of synchronous code will be executed completely before execution switches to handle another request so this simplifies the "guarding" you need to do. 但是请注意,同步代码块将在执行切换为处理另一个请求之前完全执行,因此这简化了您需要执行的“防护”。

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

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