My source code:
var http = require("http");
var count=1;
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hi, you are number "+count+" visitors");
response.end();
count++;
}).listen(8888);
I got 1,3,5,7,..... in each visit. Why to increment the count by 2?
The request to favicon.ico
is triggering an extra request (I confirmed this by logging the details for each request and then making a normal request with Chrome).
You will need to look explicitly for the type of request (url, method, etc) you're wanting to match.
Also, keep in mind, if your server dies, which it probably will at some stage, your count will be reset. If you don't want that, you should persist it somewhere less volatile, such as a database.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With