Node.JS in Action: Implement Visitor Counter in Node.js

This is the node.js in action article series. In this series we would like to see various realastic uses of node.js. The previous article explained how to implement a simple web server using node.js. You can read it here:

Node.js in Action : implement simple http server using node.js

In this article we will implement a simple hit counter application with a few lines of code. Let's discuss what we really want to do.

We would just like to implement a simple hit count for our web server. For example, we know that in ASP.NET application variables are globally shareable and static in nature, in other words if a user changes a value then all users will see the changed version.

In our example we will count the number of hits in our web server, and believe me, it's much simpler than you would expect.

I will assume that the Node.Js server is already up and running in our system; if not then please visit our first article and configure it.

Have a look at the following code.

var http = require('http');

var userCount = 0;

var server = http.createServer(function (req, res) {

    userCount++;

    res.writeHead(200, { 'Content-Type''text/plain' });

    res.write('Hello!\n');

    res.write('We have had ' + userCount + ' visits!\n');

    res.end();

 

});

server.listen(9090);

console.log('server running...')

 

The example is very simple with our previous Hello World example. In this example we have declared one global variable that is counting the number of hits in our web application. In the server body, at first we are incrementing the variable value and then we are sending the value as a response after setting 200 response statuses.

Let's run the application.

Go to the command prompt and navigate to your server location. Then fire the command:

node <server filename>

Visit counter in node.js

We are seeing that our node server is running. Now we will go to a browser and try to access the server.

Visit counter in node.js

At first hit, we see that it's showing that this is the first HTTP request to our server. Now, if we hit a couple of more times then we will see that the number has changed as in the following.

Visit counter in node.js

It's showing the number of HTTP requests after the node server was up and running. The question may come to your mind, for each and every hit, why is the variable not initialized? The reason is, the node.js process any work in event loop mechanism.

Once we create the server and it is up, it will never stop and it will continue to listen for events. In our case the server is listening for request and response events. So, when we make one HTTP request , it executes the code that is written within the event scope and since we have declared a variable out of this scope, it is never initialized the second time.

Conclusion

In this short article we have learned how to implement a hit counter in a HTTP server using a few lines of code. I hope this practical example will boost you in your node.js day. Happy learning.

Up Next
    Ebook Download
    View all
    Learn
    View all