从代码级重新启动node.js应用



我有一个应用程序,最初创建static配置文件(一次),文件被写入后,我需要重新初始化/重启应用程序。是否需要重新启动node.js应用程序?

这是必需的,因为我有一个应用程序运行在两个runlevelsnode.js。初始一个完全启动synchronus,在此级别完成后,应用程序在先前启动的环境中处于异步运行级别。

我知道有像nodemon这样的工具,但这不是我需要的。

我试图通过正在工作的process.kill()杀死应用程序,但我无法听到杀死事件:

 // Add the listener
 process.on('exit', function(code) {
    console.log('About to exit with code:', code);
    // Start app again but how?
 });
 // Kill application
 process.kill();
或者有没有更好、更干净的方法来处理这个问题?

找到一个从app本身重新启动node.js的工作案例:

例子:

// Optional part (if there's an running webserver which blocks a port required for next startup
try {
  APP.webserver.close(); // Express.js instance
  APP.logger("Webserver was halted", 'success');
} catch (e) {
  APP.logger("Cant't stop webserver:", 'error'); // No server started
  APP.logger(e, 'error');
}

// First I create an exec command which is executed before current process is killed
var cmd = "node " + APP.config.settings.ROOT_DIR + 'app.js';
// Then I look if there's already something ele killing the process  
if (APP.killed === undefined) {
  APP.killed = true;
  // Then I excute the command and kill the app if starting was successful
  var exec = require('child_process').exec;
  exec(cmd, function () {
    APP.logger('APPLICATION RESTARTED', 'success');
    process.kill();
  });
}

我在这里看到的唯一错误是在控制台上丢失输出,但如果有任何东西被记录到日志文件中,这不是问题。

最新更新