简体   繁体   中英

express.js next middleware tier being called when explicitly ending the response

I came across the following behaviour:

When I have multiple express middleware mounted, the request continues to cascade down to the next middleware, even though I explicitly end the request by using res.json() in the previous middleware. Also note that I'm NOT calling next() anywhere in said middleware.

app.use('/v1/status', function (req, res, next) {
    res.json({ status: 'ok' });
});

app.use(function (req, res, next) {
    console.log('I always get called');
});

Is this 'new' behaviour? Because to my understanding (and the 4x docs) this should't happen.

Any help would be much appreciated.

express v4.13.0 on Mac OSX

So I found the problem with the help of the amazing @dougwilson. He advised me to print the url that was being requested and there I saw /favicon.ico being printed.

Upon closer inspection I saw that the first middleware actually completed the request, but that the favicon request was still pending. Thats why the second middleware was being called. It had nothing to do with res.json() not working properly.

Never ran into this problem before and it was driving me crazy! Thanks for your help everyone!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM