Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling or prevent errors from crashing my whole node.js app on Heroku

I've got a node.js app hosted on Heroku

Whenever my app encounters an error, the whole server crashes, making it unavailable for anyone.

Is there any way to return an error page instead, and keep the server alive?

I read this, Node.JS with forever on Heroku but it didn't answer my question....

like image 770
Alex Avatar asked Apr 19 '12 03:04

Alex


People also ask

Does heroku support node JS?

Initially, it supported only Ruby sites but now supports various languages, including JavaScript with Node. js. Heroku also has Docker support so that you can deploy just about anything to it. This tutorial will teach you how to build a small application using the Express framework for Node.


2 Answers

http://nodejs.org/docs/latest/api/process.html#process_event_uncaughtexception

You can listen for the uncaughtException event and then do your error handling. Ideally, your callbacks should return an err argument and then do the error handling.

like image 130
liangzan Avatar answered Oct 18 '22 11:10

liangzan


You can probably use DOMAIN, this will probably stop your server from crashing when an uncaughtException is thrown.

domain = require('domain'),
d = domain.create();

d.on('error', function(err) {
  console.error(err);
});

for more details go http://shapeshed.com/uncaught-exceptions-in-node/

beside using this method must try-catch your block.

like image 34
Airy Avatar answered Oct 18 '22 10:10

Airy