I have a simple server running in node.js using connect:
var server = require('connect').createServer(); //actions... server.listen(3000);
In my code I have actual handlers, but thats the basic idea. The problem I keep getting is
EADDRINUSE, Address already in use
I receive this error when running my application again after it previously crashed or errors. Since I am not opening a new instance of terminal I close out the process with
ctr + z.
I am fairly certain all I have to do is close out the server or connection. I tried calling
process.on('exit', ...); with no luck.
process.on('exit', ..) isn't called if the process crashes or is killed. It is only called when the event loop ends, and since
server.close() sort of ends the event loop (it still has to wait for currently running stacks here and there) it makes no sense to put that inside the exit event...
On crash, do
process.on('uncaughtException', ..) and on kill do
That being said, SIGTERM (default kill signal) lets the app clean up, while SIGKILL (immediate termination) won't let the app do anything.
You can also go the command line route:
ps aux | grep node
to get the process ids.
kill -9 PID
Doing the -9 on kill sends a SIGKILL (instead of a SIGTERM). SIGTERM has been ignored by node for me sometimes.