Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Express request is called twice

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?

like image 543
xavier.seignard Avatar asked Oct 31 '12 02:10

xavier.seignard


People also ask

How many requests can express handle per second?

There's a benchmark made by Fastify creators, it shows that express. js can handle ~15K requests per second, and the vanilla HTTP module can handle 70K rps.

Does express use multiple threads?

js is single threaded. You can't have another thread do some work for you. However you can send another request to the server as the uploading work happens asynchronously and your request will be processed along with uploading function.

What is request express?

Express. js Request and Response objects are the parameters of the callback function which is used in Express applications. The express. js request object represents the HTTP request and has properties for the request query string, parameters, body, HTTP headers, and so on.

How multiple requests are handled by NodeJS?

How NodeJS handle multiple client requests? NodeJS receives multiple client requests and places them into EventQueue. NodeJS is built with the concept of event-driven architecture. NodeJS has its own EventLoop which is an infinite loop that receives requests and processes them.


2 Answers

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.

like image 117
Mushimo Avatar answered Sep 21 '22 16:09

Mushimo


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.

like image 37
MWaheed Avatar answered Sep 18 '22 16:09

MWaheed