In this example, I am going to create an example for how to show Number of visits in Node.Js.

Number of Visits in Node.js:

Here I am going to show how to show Number of Visits in Node.js.

First of all, create a js file at any location with the name CounterExample.

Write the following code in CounterExample.js file.

var http = require('http');

var VisitCount = 0;

http.createServer(function (request, response) {

console.log('Connecting to Server');

VisitCount++;

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

response.write('Welcome to Node.Js tutorial!\n');

response.write('Total Visits'+VisitCount+' \n');

response.end();

}).listen(3000);

console.log('Server started run at the http://localhost:3000');

Now open Node.Js command prompt and switch to your CounterExample.js file location and run the following command.

Node CounterExample.js

Number of Visits

Output:

Number of Visits

Every time you refresh the page the Number of visits will increase.

View More:

Conclusion:

I hope you loved this post. Your feedback, comments, and suggestions are always welcome to me.

Thank You.

Leave a Comment