Alberto De Caro
Alberto De Caro

Reputation: 5213

Doubled requests on nodejs http server

Scenario

I am running a very simple server on nodejs:

var http = require("http");
var ConnectionsNumber = 0;

function onRequest(request, response)
{
    console.log((new Date).toUTCString() + " - Request received");
    console.log(request.headers);
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.write("Listening\n");
    response.end();
    ConnectionsNumber++;
    console.log('Requests so far: ' + ConnectionsNumber);
}

http.createServer(onRequest).listen(8888);
console.log((new Date).toUTCString() + " - Server started");

But when I start the server and make requests (via Chrome) the server receives two requests at once. Here is the log:

Wed, 03 Oct 2012 16:03:27 GMT - Server started
Wed, 03 Oct 2012 16:03:34 GMT - Request received
{ host: 'localhost:8888',
  connection: 'keep-alive',
  'cache-control': 'max-age=0',
  'user-agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.79 Safari/537.4',
  accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  'accept-encoding': 'gzip,deflate,sdch',
  'accept-language': 'it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4',
  'accept-charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3' }
Requests so far: 1
Wed, 03 Oct 2012 16:03:34 GMT - Request received
{ host: 'localhost:8888',
  connection: 'keep-alive',
  accept: '*/*',
  'user-agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.79 Safari/537.4',
  'accept-encoding': 'gzip,deflate,sdch',
  'accept-language': 'it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4',
  'accept-charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3' }
Requests so far: 2

Question

What is happening?

Edit

Here is the winner:

var http = require("http");
var ConnectionsNumber = 0;

function onRequest(request, response)
{
    console.log((new Date).toUTCString() + " - Request received");
    console.log(request.url);

    response.writeHead(200, {"Content-Type": "text/plain"});
    response.write("Listening\n");
    response.end();

    ConnectionsNumber++;
    console.log('Requests so far: ' + ConnectionsNumber);
}

http.createServer(onRequest).listen(8888);
console.log((new Date).toUTCString() + " - Server started");

And the corresponding log:

Wed, 03 Oct 2012 20:00:04 GMT - Server started
Wed, 03 Oct 2012 20:00:14 GMT - Request received
/
Requests so far: 1
Wed, 03 Oct 2012 20:00:14 GMT - Request received
/favicon.ico
Requests so far: 2

That's all folks.

Upvotes: 0

Views: 214

Answers (1)

jncraton
jncraton

Reputation: 9132

Chrome requests a favicon by default. That is probably why you are seeing a second request.

Upvotes: 4

Related Questions