I am developing Node.js application using Node-dev. Each time I save the script of the application (press Control-S) node-dev will restart my app. Now the question, can I detect the restart withing the application, so that I can save some data before the program is terminated?
process.on('SIGINT', function() {
console.log('SIGINT');
});
The code above does not detect this kind of restart.
I am using Windows XP.
In your child, add a listener on the
exitevent ofprocess:As you can see from the source,
node-devforks to launch your script. When one file changes, the parent launches itsstop()function, waits for the child to exit and then restarts it:The
stop()function sends a message:The ipc object uses the
child.send()method:node-devdoesn't work by sending aSIGINTsignal, but works using IPCs. It would seem that you can then detect the restart of your application by adding a listener on themessageevent ofprocess... but this doesn't work (ie. themessageevent is never emitted on theprocessobject). My guess is thatprocess.exit()is synchronous and that our handler doesn't get the chance to run.Edit: here is a Gist with an example of parent/child communication using IPC in Node, if that helps.