2012-04-19 35 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 
});