Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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全局变量没有任何线索。)

  ask by disc0dancer translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
292 views
Welcome To Ask or Share your Answers For Others

1 Answer

A good, up to date alternative to supervisor is nodemon :

(对于supervisor来说,一个好的,最新的替代方案是nodemon :)

Monitor for any changes in your node.js application and automatically restart the server - perfect for development

(监视node.js应用程序中的任何更改并自动重新启动服务器 - 非常适合开发)

To use nodemon :

(要使用nodemon :)

$ npm install nodemon -g
$ nodemon app.js

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...