reload files in Node.js?
EDIT: Use http://github.com/isaacs/node-supervisor; This is an old question and the code example is made with an outdated Node.js API.
Any ideas on how I could implement an auto-reload of files in Node.js? I'm tired of restarting the server every time I change a file. Apparently Node.js' require()
function does not reload files if they already have been required, so I need to do something like this:
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 );
And in the app.js file I have:
var file = require('./file');
this.app = function(req, res) {
file.serveFile( req, res, 'file.js');
}
But this also isn't working - I get an error in the process.compile()
statement saying that 'require' is not defined. process.compile
is evaling the app.js, but has no clue about the node.js globals.
A good, up to date alternative to supervisor
is nodemon
:
Monitor for any changes in your node.js application and automatically restart the server - perfect for development
To use nodemon
:
$ npm install nodemon -g
$ nodemon app.js
node-supervisor is awesome
usage to restart on save:
npm install supervisor -g supervisor app.js
by isaacs - http://github.com/isaacs/node-supervisor
我找到了一个简单的方法:
delete require.cache['/home/shimin/test2.js']
链接地址: http://www.djcxy.com/p/32684.html
下一篇: 在Node.js中重新加载文件?