When middleware receives 4 parameters during error handler processing, it is recognized as error, request, response, and nextFunction in order.
In my case, app.use((err,req,res,next)
=> does not recognize four parameters.
The editor recognizes err as request, req as response, and res as next. It doesn't recognize what 'next' is.(as with 3 parameters)
I'm using intelliJ, but it was the same when I checked in visual studio code. Node.js version is 14.15.4.
const express = require('express');
const app = express();
app.set('port',process.env.PORT||3000);
app.use((req,res,next)=>{
console.log('Run on all requests');
next();
})
app.get('/', (req, res, next) => {
console.log('only working at GET /');
next();
}, (req, res) => {
throw new Error('go to error middleware')
});
app.use((err,req, res, next) => {
console.error(err);
res.status(500).send(err.message);
});
app.listen(app.get('port'),()=>{
console.log(app.get('port'),'is waiting');
});
When I try, the only output here is '3000 is waiting'.
I don't know what the problem is. I'd appreciate your help.
question from:
https://stackoverflow.com/questions/66051752/node-js-expressjs-middleware-error-handling-does-not-recognize-parameter-error 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…