As others have pointed out, there really is no way to do this directly in Node. You really need to run it using foreverjs . The reason you need to run it using the monitor, as forever, is because the error caused by your code often leads to the completion of the entire Node process for exiting and exiting. The monitor will search for it and immediately restart the process.
It is also important to note that when you restart the process, your server will not respond to the request, so plan ahead if you expect this to be a problem and make sure that you have several server processes running under load balancer.
source share