2012-05-12 34 views
74

我曾嘗試:如何指定HTTP錯誤代碼?

app.get('/', function(req, res, next) { 
    var e = new Error('error message'); 
    e.status = 400; 
    next(e); 
}); 

和:

app.get('/', function(req, res, next) { 
    res.statusCode = 400; 
    var e = new Error('error message'); 
    next(e); 
}); 

但總是500錯誤代碼被公佈。

+1

我對相關問題的回答可能有所幫助:http://stackoverflow.com/questions/10170857/can-i-reuse-the-express-js-錯誤視圖/ 10556093#10556093 – Pickels

+1

您能否更新接受的回覆? –

回答

141

每快遞(版本4+)文檔,你可以使用:

res.status(400); 
res.send('None shall pass'); 

http://expressjs.com/4x/api.html#res.status

< = 3.8

res.statusCode = 401; 
res.send('None shall pass'); 
+14

+1使用最新版本的API。 如果你想發送更多的線,只是鏈: 'res.status(400).json({error:'message'})' – TyMayn

+0

如果我沒有響應變量呢?在一個貓鼬驗證中,如果你沒有響應變量,你必須提出一個錯誤'done(new Error('My error text'))' – Mikel

+0

@Mikel,那麼你不能發送響應。 –

13

您可以使用res.send('OMG :(', 404);只是res.send(404);

+0

但我想要將錯誤代碼發送到eventHandler中間件,因此可以顯示express的自定義錯誤頁面。 –

+6

對於任何在2016年閱讀此文章的人:根據Express 4.x,'res.send(404)'已棄用。現在是'res.sendStatus(404)'。 http://expressjs.com/en/api.html#res.sendStatus – 0xRm

11

一些捆綁的ErrorHandler中間件的版本(可能是舊的?)快遞的版本似乎有狀態代碼硬編碼。這裏記錄的版本:http://www.senchalabs.org/connect/errorHandler.html另一方面可以讓你做你想做的事情。所以,也許嘗試升級到最新版本的express/connect。

8

老問題,但仍在Google上發佈。在快車(3.4.0)的最新版本,你可以調用next之前改變res.statusCode(ERR):

res.statusCode = 404; 
next(new Error('File not found')); 
+0

下一步做什麼? – nottinhill

+0

'next'正在調用express.js通常試圖呈現錯誤頁面的下一個處理函數。 – Kurotsuki

5

從我在Expre看到ss 4.0這適用於我。這是身份驗證所需的中間件的示例。

function apiDemandLoggedIn(req, res, next) { 

    // if user is authenticated in the session, carry on 
    console.log('isAuth', req.isAuthenticated(), req.user); 
    if (req.isAuthenticated()) 
     return next(); 

    // If not return 401 response which means unauthroized. 
    var err = new Error(); 
    err.status = 401; 
    next(err); 
} 
45

簡單的一個襯墊;

res.status(404).send("Oh uh, something went wrong"); 
4

在快遞4.0得償所願吧:)

res.sendStatus(statusCode) 
// Sets the response HTTP status code to statusCode and send its string representation as the response body. 

res.sendStatus(200); // equivalent to res.status(200).send('OK') 
res.sendStatus(403); // equivalent to res.status(403).send('Forbidden') 
res.sendStatus(404); // equivalent to res.status(404).send('Not Found') 
res.sendStatus(500); // equivalent to res.status(500).send('Internal Server Error') 

//If an unsupported status code is specified, the HTTP status is still set to statusCode and the string version of the code is sent as the response body. 

res.sendStatus(2000); // equivalent to res.status(2000).send('2000') 
3

我想集中在這樣的錯誤響應創建:

app.get('/test', function(req, res){ 
    throw {status: 500, message: 'detailed message'}; 
}); 

app.use(function (err, req, res, next) { 
    res.status(err.status || 500).json({status: err.status, message: err.message}) 
}); 

所以我一直相同的錯誤輸出格式。

PS:當然,你可以創建一個對象來extend the standard error這樣的:

const AppError = require('./lib/app-error'); 
app.get('/test', function(req, res){ 
    throw new AppError('Detail Message', 500) 
}); 

'use strict'; 

module.exports = function AppError(message, httpStatus) { 
    Error.captureStackTrace(this, this.constructor); 
    this.name = this.constructor.name; 
    this.message = message; 
    this.status = httpStatus; 
}; 

require('util').inherits(module.exports, Error); 
0

快遞棄用res.send(身體狀況)。 使用res.status(status).send(body)代替