使用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错误页面的错误细节?

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

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

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

每个ExpressJSerror handling ,添加app.use(function(err, req, res, next){ // your logic }); 低于你的其他app.use语句。

例:

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