The variable is doubled due to the node.js http callback function

I played with node.js and something strange happens when you run this code:

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

function onRequest(request, response) {  
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("You're number " + i++);
  response.end();
}

http.createServer(onRequest).listen(8888);

I expect it to behave like a pageview counter, but each time I refresh the browser tab, I get the result of what seems i=i+2instead of a simple increment. Can someone explain this behavior to me?

+5
source share
2 answers

Your browser hits your server for favicon.ico. Each request increases i, and the request is favicon.icocounted.

Use a tool like Fiddler or WireShark to see this behavior yourself.

+12

, favicon, .

+2

All Articles