2012-04-19 33 views
6

我开发一个明确的应用程序。使用process.on('uncaughtException显示500错误页面

目前,我有我的server.js以下

process.on('uncaughtException', function (err) { 
    console.log("UNCAUGHT EXCEPTION "); 
    console.log("[Inside 'uncaughtException' event] " + err.stack || err.message); 
}); 

哪个停止服务器崩溃每有一个错误,但是,它只是坐在那里的时间...

是可以代替的console.log的,与错误的详细信息发送500错误页面?

回答

8

我不认为您可以从uncaughtException内做出回应,因为即使没有请求发生,也可能发生这种情况。

表达自己提供了一种handle errors路线内,像这样:

app.error(function(err, req, res, next){ 
    //check error information and respond accordingly 
}); 
+2

不工作。未定义'app.error'。自从这个答案发布后(> 2年前),ExpressJS API可能会发生变化。 ExpressJS异常处理涵盖在:http://expressjs.com/guide.html#error-handling – 2014-07-04 15:21:49

+0

app.error不是函数。 – 2016-04-11 06:53:44

4

ExpressJS Error Handling,下方的其他app.use语句添加app.use(function(err, req, res, next){ // your logic });

例子:

app.use(function(err, req, res, next){ 
    console.log(err.stack); 
    // additional logic, like emailing OPS staff w/ stack trace 
});