0

Below is my NodeJS program.

var http = require("http");
var x = 0;
http.createServer(function(request, response){
    response.writeHead(200);
    console.log("x = " + x);
    x += 1;
    response.write("Hello Michael. This page has been requested [" + x + "] times!");
    response.end();
}).listen(8080);

When I first hit the application in a browser, the output to the browser is: Hello Michael. This page has been requested [1] times!

However, in the console (i.e. cmd.exe) I see:

x = 0 x = 1

If I reload the browser, I see: Hello Michael. This page has been requested [3] times!

The console now outputs:

x = 2 x = 3

Why is my function that is handling the HTTP request executing twice? My expected output would have been to see the number shown in the browser match the value in the console.

Michael Freake
  • 1,197
  • 2
  • 14
  • 35

1 Answers1

3

Check request.url, you will see one of them is for /favicon.ico and the other is the actual request.

mscdex
  • 104,356
  • 15
  • 192
  • 153