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



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

我目前在我的服务器中有以下内容.js

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

每次出现错误时,这都会阻止服务器崩溃,但是,它只是坐在那里......

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

我认为您无法从uncaughtException内部做出响应,因为即使没有请求发生,这也可能发生。

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

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

根据 ExpressJS 错误处理,在其他 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
});

相关内容

最新更新