Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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');
like image 680
Matty Avatar asked Oct 26 '11 15:10

Matty


1 Answers

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.

like image 114
Raynos Avatar answered Nov 06 '22 01:11

Raynos