【发布时间】:2015-08-27 02:45:09
【问题描述】:
我希望我的服务器在节点退出时保存一些基本数据并在下次加载它们。
我尝试了this question 建议的答案,但服务器似乎在能够写入文件之前关闭。
这是我正在尝试的确切代码:
process.stdin.resume();
function exitHandler(options, err) {
if(!serverUp){return;}
serverUp = false;
fs.writeFile('server.txt', String(search_index),function (err) {
console.log("debug") // <-- This doesn't happen
process.exit();
});
}
//do something when app is closing
process.on('exit', exitHandler.bind(null,{cleanup:true}));
//catches ctrl+c event
process.on('SIGINT', exitHandler.bind(null, {exit:true}));
//catches uncaught exceptions
process.on('uncaughtException', exitHandler.bind(null, {exit:true}));
【问题讨论】:
标签: javascript node.js text exit writefile