87

If I write the following program in node:

  http.createServer(function (req, res) {

    if( req.method == 'GET' ) {
      var body = ''; req.on('data', function(data) { body += data });
      req.on('end',  function() {
        console.log('request ended')
      });
    }

    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('142\n');
  }).listen(3500);

And then hit the server with http://xxx.xx.xxx.xx:35010 I see a request ended twice on my console -- I'm not sure why a single HTTP request is causing this to execute twice.

Will
  • 5,370
  • 9
  • 35
  • 48
  • 1
    possible duplicate of [node.js page refresh calling resources twice?](http://stackoverflow.com/questions/8283801/node-js-page-refresh-calling-resources-twice) – GSee Mar 29 '13 at 18:54
  • @Jessemon If a question is a duplicate, vote to close as a duplicate instead of editing the question. – GSee Mar 29 '13 at 18:57

3 Answers3

186

That is normal - your browser makes more than one call.

Most browsers make a call to grab /favicon.ico for example.

Try to log the url:

console.log(req.url);

and you'll see what's being called.

ᔕᖺᘎᕊ
  • 2,971
  • 3
  • 23
  • 38
3on
  • 6,291
  • 3
  • 26
  • 22
  • This might be silly but whats the exact reason for response.write("Hi") not being displayed twice, if added ? – Navin Nagpal Nov 11 '14 at 07:22
  • Each query gets it once. The favicon request get is and discard it has it is not an icon content, and the regular http request for html page displays it. – 3on Nov 11 '14 at 19:04
  • How's it normal i'm trying to do a increment inside my createServer code and being called two times, it totally mess up the results. – Muhammad Babar Feb 20 '18 at 07:08
  • I have the same problem, but I do not understand the answers speaking the link with favicon: in the second call, I find in "req.url" the same path as in the first call. This bug becomes annoying as soon as I dedug the Node server, because I'm juggling between 2 threads which do the same things in parallel. – Didier68 Jul 27 '20 at 14:06
21

Generally, favicon.ico is fetched by the browsers. So, the two calls.

Solution to this issue can be checking for request URL if it's fetching favicon.ico or not.

http.createServer(function (req, res) {
    if (req.url != '/favicon.ico') {
        // do your stuffs
    }
}).listen(3500);
Mukesh Chapagain
  • 25,063
  • 15
  • 119
  • 120
0

Another thing to watch out for is that modern browsers may prefetch or preload pages. Chrome will sometimes even do this before you hit enter in the address bar!

It can be disabled under Privacy and security, but this will affect all browsing.

enter image description here

You can also check for header Purpose: prefetch and just return an error. (I'm not sure what the official response should be in production.)

This is unlikely to be happening frequently, but if you're testing an API it can be at best annoying and at worse dangerous for an unexpected request to suddenly be made.

Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689