Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js app crashes when error is detected

My node.js (Express.js) application crashes every time an error occurs (syntax, undefined variable, etc.).

Is there any solution to just log it and not crash my app? I read node.js Error's documentation but can't find a solution. https://nodejs.org/api/errors.html

I generated application from express generator. I'm talking about this, here is syntax error, variable is undefined but server is running: http://prnt.sc/e2b7ce it's not crashed

like image 480
Vatex Official Avatar asked Jan 30 '17 17:01

Vatex Official


1 Answers

You could catch the error, but you should not do this with errors that are your fault, because it is in an unpredictible state after such an error its the best to restart the whole app (nodemon, forever). Errors like not reaching the db, validation, errors in promises, ... you should catch it.

To your second question, you can use the event for unhandled errors like that

process.on('uncaughtException', err => {})
like image 68
Julian Avatar answered Oct 03 '22 00:10

Julian