Javascript 当我想查看更改时,如何在不重新启动 nodejs 的情况下编辑我的服务器文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2925940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I edit on my server files without restarting nodejs when i want to see the changes?
提问by Robert Hurst
I'm trying to setup my own nodejs server, but I'm having a problem. I can't figure out how to see changes to my application without restarting it. Is there a way to edit the application and see changes live with node.js?
我正在尝试设置自己的 nodejs 服务器,但遇到了问题。我不知道如何在不重新启动应用程序的情况下查看对它的更改。有没有办法编辑应用程序并查看 node.js 的实时更改?
采纳答案by isaacs
Check out Node-Supervisor. You can give it a collection of files to watch for changes, and it restarts your server if any of them change. It also restarts it if it crashes for some other reason.
查看Node-Supervisor。你可以给它一个文件集合来观察变化,如果其中任何一个发生变化,它会重新启动你的服务器。如果由于其他原因崩溃,它也会重新启动它。
"Hot-swapping" code is not enabled in NodeJS because it is so easy to accidentally end up with memory leaks or multiple copies of objects that aren't being garbage collected. Node is about making your programs accidentally fast, not accidentally leaky.
“热交换”代码在 NodeJS 中没有启用,因为很容易意外地以内存泄漏或未进行垃圾回收的对象的多个副本结束。Node 是让你的程序意外快速,而不是意外泄漏。
EDIT, 7 years after the fact: Disclaimer, I wrote node-supervisor, but had handed the project off to another maintainer before writing this answer.
编辑,事后 7 年:免责声明,我写了 node-supervisor,但在写这个答案之前已经把项目交给了另一个维护者。
回答by Kris Zyp
Nodules is a module loader for Node that handles auto-reloading of modules withoutrestarting the server (since that is what you were asking about):
Nodules 是 Node 的模块加载器,它可以在不重新启动服务器的情况下处理模块的自动重新加载(因为这就是您要问的):
http://github.com/kriszyp/nodules
http://github.com/kriszyp/nodules
Nodules does intelligent dependency tracking so the appropriate module factories are re-executed to preserve correct references when modules are reloaded without requiring a full restart.
Nodules 进行智能依赖跟踪,因此在重新加载模块时重新执行适当的模块工厂以保留正确的引用,而无需完全重新启动。
回答by Loolooii
Use this: https://github.com/remy/nodemon
使用这个:https: //github.com/remy/nodemon
Just run your app like this: nodemon yourApp.js
像这样运行你的应用程序: nodemon yourApp.js
回答by Sparkida
There should be some emphasis on what's happening, instead of just shotgunning modules at the OP. Also, we don't know that the files he is editing are all JS modules or that they are all using the "require" call. Take the following scenarios with a grain of salt, they are only meant to describe what is happening so you know how to work with it.
应该重点关注正在发生的事情,而不仅仅是在 OP 中使用霰弹枪模块。此外,我们不知道他正在编辑的文件都是 JS 模块,还是都在使用“require”调用。对以下场景持保留态度,它们仅用于描述正在发生的事情,以便您知道如何使用它。
Your code has already been loaded and the server is running with it
SOLUTIONYou need to have a way to tell the server what code has changed so that it can reload it. You could have an endpoint set up to receive a signal, a command on the command line or a request through tcp/http that will tell it what file changed and the endpoint will reload it.
//using Express var fs = require('fs'); app.get('reload/:file', function (req, res) { fs.readfile(req.params.file, function (err, buffer) { //do stuff... }); });
Your code may have "require" calls in it which loads and cachesmodules
SOLUTIONsince these modules are cached by require, following the previous solution, you would need a line in your endpoint to delete that reference
var moduleName = req.params.file; delete require.cache[moduleName]; require('./' + moduleName);
您的代码已经加载,服务器正在运行
解决方案您需要有一种方法来告诉服务器哪些代码已更改,以便它可以重新加载它。您可以将端点设置为接收信号、命令行上的命令或通过 tcp/http 的请求,该请求将告诉它更改了什么文件,端点将重新加载它。
//using Express var fs = require('fs'); app.get('reload/:file', function (req, res) { fs.readfile(req.params.file, function (err, buffer) { //do stuff... }); });
您的代码中可能有加载和缓存模块的“require”调用
解决方案由于这些模块是由 require 缓存的,按照前面的解决方案,您需要在端点中添加一行来删除该引用
var moduleName = req.params.file; delete require.cache[moduleName]; require('./' + moduleName);
There's a lot of caveats to get into behind all of this, but hopefully you have a better idea of what's happening and why.
所有这一切背后有很多警告需要注意,但希望您对正在发生的事情以及原因有更好的了解。
回答by Tahsin Turkoz
if you would like to reload a module without restarting the node process, you can do this by the help of the watchFilefunction in fsmodule and cache clearingfeature of require:
如果你想在不重启节点进程的情况下重新加载模块,你可以通过fs模块中的watchFile函数和require 的缓存清除功能来完成:
Lets say you loaded a module with a simple require:
假设您加载了一个带有简单要求的模块:
var my_module = require('./my_module');
In order to watch that file and reload when updated add the following to a convenient place in your code.
为了查看该文件并在更新时重新加载,请将以下内容添加到代码中方便的位置。
fs.watchFile(require.resolve('./my_module'), function () {
console.log("Module changed, reloading...");
delete require.cache[require.resolve('./my_module')]
my_module = require('./my_module');
});
If your module is required in multiple files this operation will not affect other assignments, so keeping module in a global variable and using it where it is needed from global rather than requiring several times is an option. So the code above will be like this:
如果您的模块在多个文件中需要,则此操作不会影响其他分配,因此将模块保存在全局变量中并在全局需要的地方使用它而不是多次需要。所以上面的代码会是这样的:
global.my_module = require ('./my_module');
//..
fs.watchFile(require.resolve('./my_module'), function () {
console.log("Module changed, reloading...");
delete require.cache[require.resolve('./my_module')]
global.my_module = require('./my_module');
});
回答by John Smith Optional
What's “Live Coding”?
In essence, it's a way to alter the program while it runs, without restarting it. The goal, however, is to end up with a program that works properly when we (re)start it. To be useful, it helps to have an editor that can be customized to send code to the server.
什么是“实时编码”?
本质上,这是一种在程序运行时更改程序的方法,而无需重新启动它。然而,目标是当我们(重新)启动它时,最终得到一个可以正常工作的程序。有用的是,拥有一个可以自定义的编辑器以将代码发送到服务器会有所帮助。
Take a look: http://lisperator.net/blog/livenode-live-code-your-nodejs-application/
看一看:http: //lisperator.net/blog/livenode-live-code-your-nodejs-application/
回答by nidhin
You can also use the tool PM2. Which is a advanced production process tool for node js. http://pm2.keymetrics.io/
您也可以使用工具 PM2。是node js的高级制作工艺工具。 http://pm2.keymetrics.io/
回答by Gaurav Ramanan
I think node-inspectoris your best bet.
我认为node-inspector是你最好的选择。
Similar to how you can Live Edit Client side JS codein Chrome Dev tools, this utilizes the Chrome (Blink) Dev Tools Interface to provide live code editing.
与如何在 Chrome 开发工具中实时编辑客户端 JS 代码类似,这利用 Chrome (Blink) 开发工具界面提供实时代码编辑。
https://github.com/node-inspector/node-inspector/wiki/LiveEdit
https://github.com/node-inspector/node-inspector/wiki/LiveEdit
回答by Dumi Jay
A simple direct solution with reference to all answers available here:
参考此处提供的所有答案的简单直接解决方案:
Node documentation says that fs.watchis more efficient than fs.watchFile& it can watch an entire folder.
Node 文档说fs.watch比fs.watchFile更有效,它可以监视整个文件夹。
(I just started using this, so not really sure whether there are any drawbacks)
(我刚开始使用这个,所以不太确定是否有任何缺点)
fs.watch("lib", (event_type, file_name) => {
console.log("Deleting Require cache for " + file_name);
delete require.cache[ require.resolve("./lib/" + file_name)];
});

