Express Middleware Exception Handling

Exception Handling Middleware

Express exception handling middleware
This middleware has four functions, namely, in order err, req, res, next
time an exception occurs while the program will execute the callback function exception handling middleware
wrong priority principle, since the middleware execution order , generally written in the bottom
of the following codes are case


//测试异常中间件 get
app.get('/errTest',(req,res)=>{
    throw new Error("error Test");
});

//异常处理中间件,错误优先的原则 一般写在最下面
app.use((err,req,res,next)=>{
    res.status(200).send("========="+err.stack);
});

effect:

Guess you like

Origin www.cnblogs.com/ltfxy/p/12549041.html