express middleware functions invoked twice when request is made from browser

后端 未结 2 1166
说谎
说谎 2020-12-12 06:42

Below is my nodejs code

const express = require(\'express\');

const app = express();

app.use(\'/\', (req, res, next) => {
    console.log(\"In intercept         


        
相关标签:
2条回答
  • 2020-12-12 07:09

    Found that it is happening due to /favicon.ico request made by browser. Adding specific handler (shown below) prevented default handler from being called twice

    app.use('/favicon.ico', (req, res, next) => {
        console.log('favicon handler');
        res.sendStatus(200);
    });
    
    0 讨论(0)
  • 2020-12-12 07:26

    The usual reasons you see multiple requests when a page loads from a browser are one of two things:

    1. The browser automatically requesting the favicon.ico file.
    2. The browser attempting to load some resource from the HTML file (script file, image, CSS file, etc..)

    You can see exactly what each request is for by adding:

    console.log(req.url);
    

    to your middleware.

    0 讨论(0)
提交回复
热议问题