dis*_*cer 415 javascript node.js
关于如何在Node.js中实现文件自动重载的任何想法?每次更改文件时,我都厌倦了重启服务器.显然Node.js的require()功能如果已经被要求就不会重新加载文件,所以我需要做这样的事情:
var sys = require('sys'),
http = require('http'),
posix = require('posix'),
json = require('./json');
var script_name = '/some/path/to/app.js';
this.app = require('./app').app;
process.watchFile(script_name, function(curr, prev){
posix.cat(script_name).addCallback(function(content){
process.compile( content, script_name );
});
});
http.createServer(this.app).listen( 8080 );
Run Code Online (Sandbox Code Playgroud)
在app.js文件中我有:
var file = require('./file');
this.app = function(req, res) {
file.serveFile( req, res, 'file.js');
}
Run Code Online (Sandbox Code Playgroud)
但这也没有用 - 我在process.compile()声明中得到一个错误,说没有定义'require'.process.compile正在评估app.js,但对node.js全局变量没有任何线索.
Mar*_*tuc 500
一个好的,最新的替代品supervisor是nodemon:
监视node.js应用程序中的任何更改并自动重新启动服务器 - 非常适合开发
使用nodemon:
$ npm install nodemon -g
$ nodemon app.js
Run Code Online (Sandbox Code Playgroud)
小智 308
node-supervisor太棒了
用于在保存时重启:
npm install supervisor -g supervisor app.js
by isaacs - http://github.com/isaacs/node-supervisor
小智 83
我找到了一个简单的方法:
delete require.cache['/home/shimin/test2.js']
Run Code Online (Sandbox Code Playgroud)
小智 25
Nodemon 长期以来一直是重新启动服务器以进行文件更改的首选方法。现在,在 Node.js 19 中,他们引入了一个--watch标志,它具有相同的功能[实验]。文档
node --watch index.js
Run Code Online (Sandbox Code Playgroud)
JnB*_*ymn 18
nodemon在google搜索中首先出现,似乎可以解决这个问题:
npm install nodemon -g
cd whatever_dir_holds_my_app
nodemon app.js
Run Code Online (Sandbox Code Playgroud)
mic*_*nic 18
如果有人仍然只是使用标准模块来解决这个问题我做了一个简单的例子:
var process = require('process');
var cp = require('child_process');
var fs = require('fs');
var server = cp.fork('server.js');
console.log('Server started');
fs.watchFile('server.js', function (event, filename) {
server.kill();
console.log('Server stopped');
server = cp.fork('server.js');
console.log('Server started');
});
process.on('SIGINT', function () {
server.kill();
fs.unwatchFile('server.js');
process.exit();
});
Run Code Online (Sandbox Code Playgroud)
此示例仅适用于一个文件(server.js),但可以使用文件数组调整为多个文件,使用for循环获取所有文件名,或者通过查看目录:
fs.watch('./', function (event, filename) { // sub directory changes are not seen
console.log(`restart server`);
server.kill();
server = cp.fork('server.js');
})
Run Code Online (Sandbox Code Playgroud)
此代码是针对Node.js 0.8 API编写的,它不适合某些特定需求,但可以在一些简单的应用程序中使用.
更新:此功能在我的模块simpleR,GitHub repo中实现
您可以安装Node-Supervisor
npm install supervisor
Run Code Online (Sandbox Code Playgroud)
请参阅http://github.com/isaacs/node-supervisor
编辑:我的答案已经过时了.Node.js是一种快速变化的技术.
我也想知道重装模块.我修改了node.js,并在nalply/node的 Github上发布了源代码.唯一的区别是功能require.它有一个可选的第二个参数reload.
require(url, reload)
Run Code Online (Sandbox Code Playgroud)
要app.js在当前目录中重新加载使用
app = require("./app", true);
Run Code Online (Sandbox Code Playgroud)
写这样的东西,你有自动重载:
process.watchFile(script_name, function(curr, prev) {
module = reload(script_name, true);
});
Run Code Online (Sandbox Code Playgroud)
我看到的唯一问题是变量module,但我现在正在努力.
nodemon是一个伟大的。我只是添加更多用于调试和监视选项的参数。
package.json
"scripts": {
"dev": "cross-env NODE_ENV=development nodemon --watch server --inspect ./server/server.js"
}
Run Code Online (Sandbox Code Playgroud)
命令: nodemon --watch server --inspect ./server/server.js
鉴于:
--watch server重新启动应用程序时更改.js,.mjs,.coffee,.litcoffee,并.json在文件中的server文件夹(包括子文件夹)。
--inspect 启用远程调试。
./server/server.js 入口点。
然后将以下配置添加到launch.json(VS Code),并随时开始调试。
{
"type": "node",
"request": "attach",
"name": "Attach",
"protocol": "inspector",
"port": 9229
}
Run Code Online (Sandbox Code Playgroud)
请注意,最好将其安装nodemon为项目的dev依赖项。因此,您的团队成员无需安装它或记住命令参数,他们只需npm run dev开始进行黑客攻击。
在nodemon文档上查看更多信息:https : //github.com/remy/nodemon#monitoring-multiple-directories
You can use nodemon from NPM. And if you are using Express generator then you can using this command inside your project folder:
nodemon npm start
Run Code Online (Sandbox Code Playgroud)
或使用调试模式
DEBUG=yourapp:* nodemon npm start
Run Code Online (Sandbox Code Playgroud)
也可以直接运行
nodemon your-app-file.js
Run Code Online (Sandbox Code Playgroud)
希望这有帮助。
使用 Node.js 19,您可以使用该选项监视文件更改--watch。文件更改后,进程会自动重新启动,以反映新的更改。
node --watch server.js
Run Code Online (Sandbox Code Playgroud)
这个问题的另一个解决方案是永远使用
Forever的另一个有用功能是,当任何源文件发生变化时,它可以选择性地重启您的应用程序.这使您无需在每次添加功能或修复错误时手动重新启动.要在此模式下启动Forever,请使用-w标志:
forever -w start server.js
Run Code Online (Sandbox Code Playgroud)
小智 5
node-dev很棒。npminstall node-dev
重新加载服务器时,它甚至会发出桌面通知,并会在消息中显示成功或错误。
使用以下命令在命令行中启动您的应用程序:
node-dev app.js