处理或防止错误导致Heroku上的整个node.js应用程序崩溃

时间:2022-11-11 23:03:11

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

我有一个在Heroku上托管的node.js应用程序

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....

我读了这个,Node.JS永远在Heroku上,但它没有回答我的问题....

2 个解决方案

#1


8  

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

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.

您可以侦听uncaughtException事件,然后执行错误处理。理想情况下,您的回调应返回一个错误参数,然后执行错误处理。

#2


4  

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

你可以使用DOMAIN,这可能会在抛出uncaughtException时阻止你的服务器崩溃。

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/

欲了解更多详情,请访问http://shapeshed.com/uncaught-exceptions-in-node/

beside using this method must try-catch your block.

除了使用这种方法,必须尝试抓住你的块。

#1


8  

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

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.

您可以侦听uncaughtException事件,然后执行错误处理。理想情况下,您的回调应返回一个错误参数,然后执行错误处理。

#2


4  

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

你可以使用DOMAIN,这可能会在抛出uncaughtException时阻止你的服务器崩溃。

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/

欲了解更多详情,请访问http://shapeshed.com/uncaught-exceptions-in-node/

beside using this method must try-catch your block.

除了使用这种方法,必须尝试抓住你的块。