Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting visitors in a node http server

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?

like image 252
user1780176 Avatar asked Aug 20 '13 03:08

user1780176


1 Answers

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.

like image 128
alex Avatar answered Sep 28 '22 00:09

alex