英文:
Does Node.js web server persist loaded modules and data?
问题
I am using node.js for the first time. It is running as a web server:
http.createServer(app).listen(process.env.PORT);
我第一次使用Node.js。它正在作为一个Web服务器运行:
http.createServer(app).listen(process.env.PORT);
I am used to working with JS on the front-end and building all my imported modules into a single js file which is then loaded in the browser.
我习惯于在前端使用JS,并将所有导入的模块构建到一个单独的JS文件中,然后在浏览器中加载它。
In my (copied sample) node.js app I find a lot of require("") e.g.
在我的(复制的示例)Node.js应用程序中,我发现很多require(""),例如:
var express = require('express');
var express = require('express');
It seems that (unlike Apache which spawns multiple processes) there is only one node.js process.
似乎(与生成多个进程的Apache不同),只有一个Node.js进程。
Does node.js do all the requires once and then use that 'bundle' to serve subsequent requests without re-requiring them?
Node.js是否只执行一次所有的require,然后使用那个“bundle”来处理后续的请求,而无需再次require它们?
Also does it persist variables. For example consider the following:
它还会保留变量吗?例如,考虑以下情况:
var day;
if (getTodayFromSystem() == 'Monday')
{
day = 'Monday';
}
var day;
if (getTodayFromSystem() == 'Monday')
{
day = 'Monday';
}
On Monday day will be Monday. But, assuming this code does stay loaded in memory, on Tuesday will day still be Monday or will it be undefined? (I used to work with mod_perl which persists loaded scripts in memory and in such as case var day would persist across requests).
在星期一,day将是Monday。但是,假设此代码确实保留在内存中加载,那么在星期二,day仍然是Monday吗,还是会变为未定义?(我曾经使用过mod_perl,在这种情况下,var day会在请求之间保持不变)。
Thank you
谢谢
英文:
I am using node.js for the first time. It is running as a web server:
http.createServer(app).listen(process.env.PORT);
I am used to working with JS on the front-end and building all my imported modules into a single js file which is then loaded in the browser.
In my (copied sample) node.js app I find a lot of require("") e.g.
var express = require('express');
It seems that (unlike Apache which spawns multiple processes) there is only one node.js process.
Does node.js do all the requires once and then use that 'bundle' to server subsequent requests without re-requiring them?
Also does it persist variables. For example consider the following:
var day;
if (getTodayFromSystem() == 'Monday')
{
day = 'Monday';
}
On Monday day will be Monday. But, assuming this code does stay loaded in memory, on Tuesday will day still be Monday or will it be undefined? (I used to work with mod_perl which persists loaded scripts in memory and in such as case var day would persist across requests).
Thank you
答案1
得分: 1
模块只加载并执行一次。
因此,如果这段代码在你的模块的顶层,你会遇到问题,因为它只会执行一次。
如果它在一个在HTTP请求中调用的函数内部,那么就安全了。
index.js:
let idx = 2;
while (idx--) {
const imported = require('./module.js');
console.log(imported);
}
module.js:
console.log('exec module.js');
const test = Math.random();
module.exports = test;
输出结果:
exec module.js
0.18808463493203975
0.18808463493203975
英文:
Modules are loaded and executed only once.
So if this
var day;
if (getTodayFromSystem() == 'Monday')
{
day = 'Monday';
}
in the top level of your module you are in trouble because it will be executed only once.
If it's inside a function that is called on a HTTP request for example, you are safe.
index.js:
let idx = 2;
while (idx--) {
const imported = require('./module.js');
console.log(imported);
}
module.js:
console.log('exec module.js');
const test = Math.random();
module.exports = test;
The output:
exec module.js
0.18808463493203975
0.18808463493203975
答案2
得分: 1
是的,Node.js 使用 CommonJS 模块系统来处理模块依赖,使用 require()。
因此,如果在 Node.js 应用程序执行过程中先前已经 require() 了一个模块,那么后续的请求将使用缓存的版本。
因此,模块在应用程序启动时只会被加载和初始化一次。
关于变量的持久性,在你的示例中,如果变量 day 被赋值为 'Monday',并且 Node.js 应用程序继续运行而不被重启,那么 day 的值将保持为 'Monday',即使在随后的几天里,除非你在代码中明确修改它。
然而,重要的是要注意,Node.js 设计成可以并行处理多个请求。每个请求都由单个 Node.js 进程内的一个单独线程处理。这意味着在一个请求内声明的变量不会在不同请求间保持持久性,因为每个请求都是在隔离环境中处理的。
因此,在你的示例中,如果代码是响应周二的新请求执行的,那么除非在该特定请求的逻辑中重新定义它,否则 day 变量将是未定义的。
我希望对你有帮助...
英文:
Yes, Node.js uses the CommonJS module system to handle module dependencies using require().
So, if a module has been previously required during the execution of the Node.js application, the cached version will be used for subsequent requests.
Therefore, modules are loaded and initialized only once during the application startup.
Regarding variable persistence, in your example, if the variable day is assigned the value 'Monday' and the Node.js application continues to run without being restarted, the value of day will remain 'Monday' even on subsequent days unless you explicitly modify it in the code.
However, it is important to note that Node.js is designed to handle multiple requests in parallel. Each request is handled by a separate thread within the context of the single Node.js process. This means that variables declared within a request do not persist across different requests since each request is handled in isolation.
So, in your example, if the code is executed in response to a new request on Tuesday, the day variable will be undefined unless you redefine it within the logic of that specific request.
Some things I went to review here:https://nodejs.dev/en/learn/
I hope it helped...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论