有人可以向我解释一下 Express JS 代码中两种异常错误处理方式的不同吗:
const express = require('express');
const app = express();
app.get('/test', (req, res, next) => {
// the first way:
throw new Error('my error message');
// the second way:
next(new Error('my error message'));
});
app.use((err, req, res, next) => {
res.status(err.status || 500).send(err.message || 'Internal Server Error');
});
app.listen(3000, () => console.log('Welcome to ExpressJS'));
Run Code Online (Sandbox Code Playgroud)
它返回与错误中间件处理的相同结果,但这里有什么区别?
| 归档时间: |
|
| 查看次数: |
926 次 |
| 最近记录: |