使用process.on('uncaughtException显示500错误页面

Ale*_*lex 6 error-handling node.js express

我正在开发一个快速应用程序.

我目前在server.js中有以下内容

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

每当出现错误时停止服务器崩溃,但是,它只是坐在那里......

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

Aar*_*ell 8

我不认为你可以从uncaughtException做出回应,因为即使没有请求发生也可能发生.

Express本身提供了一种处理路径错误的方法,如下所示:

app.error(function(err, req, res, next){
    //check error information and respond accordingly
});
Run Code Online (Sandbox Code Playgroud)

  • 不行.`app.error`没有定义.自从这个答案发布(> 2年前)后,ExpressJS API可能已更改.ExpressJS异常处理涵盖在:http://expressjs.com/guide.html#error-handling (2认同)