I am new to javascript and node, and have been trying to create an application using node.js and express.
I have put appropriate callbacks for errors but they are not put everywhere, and sometimes node.js server just stops because of some exception that comes in.
It has been recommended to use 'upstart' or 'forever' to keep the node.js running always and start it whether node.js stops.
It seems express provides a catch all for all the exceptions by the following from the link http://expressjs.com/guide.html#error-handling
app.use(function(err, req, res, next){
console.error(err.stack);
res.send(500, 'Something broke!');
});
It is working for me now. However i read somewhere that catching error like this is not a good idea as it can have side effects and can leave the variables in an inconsistent state? Also catching exception like this does not work for all the cases, and there can be some cases when the server can still stop for some exception. Is it true?
My questions are probably this.
1) Can the exception handling like above can leave some variables in an inconsistent state? 2) Can there be some exceptions which will still stop the server?
Thanks & Regards
Tuco
1) Whether this exception handling method can leave some variables in an inconsistant state depends on your implementation.
If you scope all the variables to the specific request and functions and dont access global or higher variables like the global app object, you will be fine. Unused memory gets cleaned up right after the exception by garbage collector.
2) Yes, it is possible for some exception to stop the server. Therefore you should set a global exception handling handler for the process like this: http://nodejs.org/api/process.html#process_event_uncaughtexception