I'd like my server to save some essential data when node exits and load them next time.
I tried what the answer of this question suggested, but the server seems to close before it is able to write to the file.
This is the exact code I am trying:
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}));