0

我尝试使用 errorhandler 中间件处理错误,但它不起作用,为什么?如果我使用自定义句柄错误一切都好。你能帮助我吗?我是 node.js 的新手,我的起步很艰难,呵呵。

var express = require('express'),
timeout = require('connect-timeout'),
errorhandler = require('errorhandler');
var app = express();
//app.use(errorhandler({log:errorFunction}))

app.use(express.static('public'));
app.set('port', process.env.PORT || 3000);
app.use(timeout('2s'))


app.get('/slow-request',function(req,res,next){

setTimeout(function(){

    if(req.timedout){return false}
    console.log("Time is ok, i can use next()")
    },3000)

})

/*
function errorFunction(err,str,req)
{
    console.log("ERRORfromerrorHandler!!!!!");//never called? why?
}
*/

app.use(function(err, req, res, next) {
res.status(err.status || 500);
console.log("ERRORfromMyCustomErrorFunction!!!!!")// it is ok
});


var server = app.listen(app.get('port'), function() {
  console.log('Express server listening on port ' + server.address().port);
});
4

2 回答 2

0

您的错误处理程序很好。

而不是 using setTimeOutthrow一个错误和你的错误处理程序将捕获它。

于 2015-06-29T07:31:53.583 回答
0

认为你理解我,我的错误处理程序很好,我知道!!它是显示“ERRORformMyCustomErrorFunction”;p(它完美:P)但是....我询问错误处理程序(快速中间件)(仅)为什么不在控制台上显示“ERRORfromerrorHandler!!!!!!” 仅此对我不起作用,/ .... /之间是什么。

于 2015-06-29T08:08:24.727 回答