0

Whenever I make http requests to a server using node.js, the console.logs inside the http request function output twice. I set up a sever with the following code, and used localhost:8888 on firefox to make a request (localhost:8888):

var http = require('http');
var url = require('url');
function onRequest(request, response) {
    var pathname = url.parse(request.url, true).pathname;
    console.log("Your url pathname is " + pathname);
    response.write("Did you get your response?");
}
var new_server = http.createServer(onRequest).listen(8888);

The console prints:

Your url pathname is /
Your url pathname is /favicon.ico

My questions are:

  1. Why is the request sent twice?
  2. Why is the pathname favicon.ico on the second request despite the fact that I did not specify anything after the socket number in the request url?
  3. Is there any way you can fix these two issues?

Thank you.

saad
  • 211
  • 3
  • 13
  • possible duplicate of [nodejs - http.createServer seems to call twice](http://stackoverflow.com/questions/11961902/nodejs-http-createserver-seems-to-call-twice) – loganfsmyth Aug 20 '14 at 16:46

1 Answers1

5

Obviously the request isn't sent twice, it's two requests.

It's the browser asking for the favicon. That's a thing browsers do.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302