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 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.
Object.keys(require.cache).forEach(function(key) { delete require.cache[key]; });
– Celebrityapp.js
as a server file should be enough? – Chicory