2019-05-30 23:51:26

Just for fun, I was trying to add a counter variable that increases every time the function is called that handles incoming requests. My code is as follows:

var count = 0; //for keeping track of how many visits the site gets
var http = require('http');

http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    count++;
    res.write("The site has " + count + " visits!");
    res.end();
}).listen(8080);

console.log("Running...");

I set count to 0. When the function is called the first time, it increments to 1. I revisit the page, and it increments to 3, then 5, 7, etc.
Why is it adding by two after the first time the the function runs?