Express request is called twice

21,345

Solution 1

This annoyed me for a long time. It's most likely the Firebug extension which is sending a duplicate of each GET request in the background. Try turning off Firebug to make sure that's not the issue.

Solution 2

I faced the same issue while using Google Cloud Functions Framework (which uses express to handle requests) on my local machine. Each fetch request (in browser console and within web page) made resulted in two requests to the server. The issue was related to CORS (because I was using different ports), Chrome made a OPTIONS method call before the actual call. Since OPTIONS method was not necessary in my code, I used an if-statement to return an empty response.

if(req.method == "OPTIONS"){
    res.set('Access-Control-Allow-Origin', '*');
    res.set('Access-Control-Allow-Headers', 'Content-Type');
    res.status(204).send('');
}

Spent nearly 3hrs banging my head. Thanks to user105279's answer for hinting this.

Solution 3

If you have favicon on your site, remove it and try again. If your problem resolved, refactor your favicon url

Solution 4

I am to fix with listen.setTimeout and axios.defaults.timeout = 36000000

Node js

var timeout = require('connect-timeout'); //express v4

//in cors putting options response code for 200 and pre flight to false
app.use(cors({ preflightContinue: false, optionsSuccessStatus: 200 }));

//to put this middleaware in final of middleawares
app.use(timeout(36000000)); //10min
app.use((req, res, next) => {
   if (!req.timedout) next();
});


var listen = app.listen(3333, () => console.log('running'));
listen.setTimeout(36000000); //10min

React

import axios from 'axios';

axios.defaults.timeout = 36000000;//10min

After of 2 days trying

Solution 5

I'm doing more or less the same thing now, and noticed the same thing.

I'm testing my server by entering the api address in chrome like this:

http://127.0.0.1:1337/links/1

my Node.js server is then responding with a json object depending on the id.

I set up a console log in the get method and noticed that when I change the id in the address bar of chrome it sends a request (before hitting enter to actually send the request) and the server accepts another request after I actually hit enter. This happens with and without having the chrome dev console open.

IE 11 doesn't seem to work in the same way but I don't have Firefox installed right now.

Hope that helps someone even if this was a kind of old thread :)

/J

Share:
21,345
xavier.seignard
Author by

xavier.seignard

Hello, I'm a creative coder and hacker. https://drangies.fr I design and realize embedded systems for interactive and artistic installations. I also develop and provide guidance in the field of web development (Node.js, React, Vue.js, React Native, Industrialization of modern JavaScript, etc.)

Updated on April 02, 2021

Comments

  • xavier.seignard
    xavier.seignard about 3 years

    To learn node.js I'm creating a small app that get some rss feeds stored in mongoDB, process them and create a single feed (ordered by date) from these ones.

    It parses a list of ~50 rss feeds, with ~1000 blog items, so it's quite long to parse the whole, so I put the following req.connection.setTimeout(60*1000); to get a long enough time out to fetch and parse all the feeds.

    Everything runs quite fine, but the request is called twice. (I checked with wireshark, I don't think it's about favicon here).

    I really don't get it.

    You can test yourself here : http://mighty-springs-9162.herokuapp.com/feed/mde/20 (it should create a rss feed with the last 20 articles about "mde").

    The code is here: https://github.com/xseignard/rss-unify

    And if we focus on the interesting bits :

    I have a route defined like this : app.get('/feed/:name/:size?', topics.getFeed);

    And the topics.getFeed is like this :

    function getFeed(req, res) {
      // 1 minute timeout to get enough time for the request to be processed
      req.connection.setTimeout(60*1000);   
    
      var name = req.params.name;
      var callback = function(err, topic) {
      // if the topic has been found
      if (topic) {
        // aggregate the corresponding feeds
        rssAggregator.aggregate(topic, function(err, rssFeed) {
          if (err) {
            res.status(500).send({error: 'Error while creating feed'});
          }
          else {
            res.send(rssFeed);
          }
        },
        req);
      }
      else {
        res.status(404).send({error: 'Topic not found'});
      }};
      // look for the topic in the db
      findTopicByName(name, callback);
    }
    

    So nothing fancy, but still, this getFeed function is called twice.

    What's wrong there? Any idea?