Matty
Matty

Reputation: 34513

NodeJS server incrementing variable by two for every request

When this code is run, i is incremented by two every time and I can't pinpoint in the documentation or otherwise why this would be the case. I'd expect the increment to be by one for each request, but it's not. Why is this behaving the way it is?

var http = require('http');
var i = 0;
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Number: ' + i + '\n');
  i++;
}).listen(8000, '127.0.0.1');

Upvotes: 12

Views: 5407

Answers (1)

Raynos
Raynos

Reputation: 169451

console.log(req.url);

You will notice the urls are / and /favicon.ico

Browsers like making requests to favicon.ico for you. That's why you get 2 requests.

Upvotes: 29

Related Questions