Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
268 views
in Technique[技术] by (71.8m points)

javascript - Node.js ExpressJS middleware error handling does not recognize parameter 'error'

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)

Detail Image

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...