ノードが終了したときにサーバーにいくつかの重要なデータを保存して、次回ロードするようにしたいと思います。
この質問の回答が示唆することを試しましたが、ファイルに書き込む前にサーバーが閉じているようです。
これは私が試している正確なコードです:
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}));