Express request is called twice

2019-06-15 13:08发布

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?

7条回答
ゆ 、 Hurt°
2楼-- · 2019-06-15 13:21

I had the same issue doing this with Express 4. I believe it has to do with ow it resolves request params. The solution is to ensure your params are resolved by for example checking them in an if block.

app.get('/:conversation',(req, res) => {
  let url = req.params.conversation;

 //Only handle request when params have resolved
  if(url){
 res.redirect(301, 'http://'+ url + '.com')
  }

})
查看更多
仙女界的扛把子
3楼-- · 2019-06-15 13:24

Ensure you give res.send(); The axios call expects a value from the server and hence sends back a call request after 120 seconds.

查看更多
仙女界的扛把子
4楼-- · 2019-06-15 13:38

you might have to increase the timeout even more. I haven't seen the express source but it just sounds on timeout, it retries.

查看更多
走好不送
5楼-- · 2019-06-15 13:41

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

查看更多
仙女界的扛把子
6楼-- · 2019-06-15 13:44

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.

查看更多
forever°为你锁心
7楼-- · 2019-06-15 13:47

I met the same problem. Then I tried to add return, it didn't work. But it works when I add return res.redirect('/path');

查看更多
登录 后发表回答