我有一个最初创建static
个配置文件的应用程序(一次),在写完文件之后我需要重新初始化/重新启动应用程序。
是否可以从自身重新启动 node.js 应用程序?
这是必需的,因为我在 node.js 中的两个runlevels
中运行了一个应用程序。
最初的一个以完整的synchronus
开始,在此级别完成之后,app在先前启动的环境中处于异步运行级别。
我知道有像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();
或者有更好,更清洁的方法来处理这个问题吗?
答案 0 :(得分:7)
找到一个工作案例,以便从应用程序本身重新启动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();
});
}
我在这里看到的唯一问题是在控制台上放松输出但是如果有任何内容记录到日志文件中则不是问题。