Any ideas on how I could implement an auto-reload of files in Node.js?
(关于如何在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:(显然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 );
And in the app.js file I have:
(在app.js文件中我有:)
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()
语句中得到一个错误,说没有定义'require'。)
process.compile
is evaling the app.js , but has no clue about the node.js globals.(process.compile
正在评估app.js ,但对node.js全局变量没有任何线索。)