2016-03-20 23 views
0

我有一個node.js + Express + express-handlebars應用程序。當用戶訪問不存在的頁面時,我想將用戶重定向到404頁面,並在存在內部服務器錯誤或異常(不停止服務器)的情況下將它們重定向到500頁面。在我的app.js中,我已經在最後編寫了中間件來執行這些任務。在Node.js和Express中處理404,500和異常

app.get('*', function(req, res, next) { 
    var err = new Error(); 
    err.status = 404; 
    next(); 
}); 

//Handle 404 
app.use(function(err, req, res, next){ 
    res.sendStatus(404); 
    res.render('404'); 
    return; 
}); 

//Handle 500 
app.use(function(err, req, res, next){ 
    res.sendStatus(500); 
    res.render('500'); 
}); 

//send the user to 500 page without shutting down the server 
process.on('uncaughtException', function (err) { 
    console.log('-------------------------- Caught exception: ' + err); 
    app.use(function(err, req, res, next){ 
     res.render('500'); 
    }); 
}); 

但是隻有404的代碼有效。因此,如果我嘗試去一個網址

localhost:8000/fakepage 

它成功地將我重定向到我的404頁面。 505不起作用。並且,對於異常處理,服務器確實保持運行,但它並沒有將我重定向到console.log後的500錯誤頁面。

我很困惑於許多在線的解決方案,人們似乎爲此實現了不同的技術。

這裏有一些我看着

http://www.hacksparrow.com/express-js-custom-error-pages-404-and-500.html

Correct way to handle 404 and 500 errors in express

How to redirect 404 errors to a page in ExpressJS?

https://github.com/expressjs/express/blob/master/examples/error-pages/index.js

回答

4
資源

上uncaughtexception的過程是應用程序的過程 - 不是每請求錯誤處理器。注意它在回調中需要一個err,並且res不會被傳遞。它是一個全局應用程序異常處理程序在全局代碼拋出的情況下,這樣做很好。

一種選擇是你可以擁有你所有的正常路線(在你的例子中沒有看到),然後是一個非錯誤處理程序最終*路線爲404。這總是最後一條路線,意味着它已經通過所有其他路線找到一個匹配...因此沒有找到。這不是一個例外處理的情況 - 你確定地知道他們要求的路徑沒有匹配,因爲它已經通過了。

How to redirect 404 errors to a page in ExpressJS?

然後ERR路線可以返回500

http://expressjs.com/en/guide/error-handling.html

問題是你有兩個錯誤的路線,因此總是命中其中硬編碼返回404

第一個

快遞4工具創建此模式:

var users = require('./routes/users'); 

// here's the normal routes. Matches in order 
app.use('/', routes); 
app.use('/users', users); 

// catch 404 and forward to error handler 
// note this is after all good routes and is not an error handler 
// to get a 404, it has to fall through to this route - no error involved 
app.use(function(req, res, next) { 
    var err = new Error('Not Found'); 
    err.status = 404; 
    next(err); 
}); 

// error handlers - these take err object. 
// these are per request error handlers. They have two so in dev 
// you get a full stack trace. In prod, first is never setup 

// development error handler 
// will print stacktrace 
if (app.get('env') === 'development') { 
    app.use(function(err, req, res, next) { 
     res.status(err.status || 500); 
     res.render('error', { 
      message: err.message, 
      error: err 
     }); 
    }); 
} 

// production error handler 
// no stacktraces leaked to user 
app.use(function(err, req, res, next) { 
    res.status(err.status || 500); 
    res.render('error', { 
     message: err.message, 
     error: {} 
    }); 
}); 
+0

var profile = require('./ routes/profile); app.use('/ profie',個人資料);這是我定義所有路線的方式。 – codeinprogress

+0

這很好 - 這些路由應該在404路由和錯誤路由之前設置 - 基本上在上面設置/和/用戶路由的地方。路線排序是至關重要的 – bryanmac

+0

這正是上面的例子。它通過要求用戶加載用戶功能並通過傳遞用戶來設置/用戶路由。 *然後*它設置所有路由後添加404通配符路由,然後最後錯誤500路由 – bryanmac