最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Counting the number of hits to my webpage - Stack Overflow

programmeradmin8浏览0评论

I have developed a webpage for the internal monitoring of some services of my pany. We wish to record the number of hits on the page and display it real time. I have gone through the questions :

Determine how many times webpage has been accessed

How can I count the number of the visitors on my website using JavaScript?

But I can't use google analytics and other such tools. I am hoping it can be done in nodejs. The problem is if I do something like this on nodejs server :

var express = require('express');
var app = express();

var bodyParser = require('body-parser');
var request = require('request');
var cfenv = require('cfenv');
app.use(express.static(__dirname + "/public"));
app.use(bodyParser.json());
var userCount = 0;
app.post('/counterIncrement', function(req, res){
console.log("I received a request to refresh the counter");
userCount = userCount + 1;
res.json({countNum: userCount});
});
var router1 = require('./public/routes/Route1');
router1(app);

var router2 = require('./public/routes/Route2');
router2(app);

var router3 = require('./public/routes/Route3');
router3(app);

var appEnv = cfenv.getAppEnv();

app.listen(appEnv.port, appEnv.bind, function() {

   console.log("server starting on " + appEnv.url); 
});

And everytime someone visits our webpage, we send a request from controller to server to increase the counter. Would this not create a race condition when multiple people access the webpage at the same time? Is there some other way possible?

We had a look at cookies also, but we want to increase the counter even when the same user visits again, and even if thats possible, I am unable to understand how the race condition can be avoided?

I have developed a webpage for the internal monitoring of some services of my pany. We wish to record the number of hits on the page and display it real time. I have gone through the questions :

Determine how many times webpage has been accessed

How can I count the number of the visitors on my website using JavaScript?

But I can't use google analytics and other such tools. I am hoping it can be done in nodejs. The problem is if I do something like this on nodejs server :

var express = require('express');
var app = express();

var bodyParser = require('body-parser');
var request = require('request');
var cfenv = require('cfenv');
app.use(express.static(__dirname + "/public"));
app.use(bodyParser.json());
var userCount = 0;
app.post('/counterIncrement', function(req, res){
console.log("I received a request to refresh the counter");
userCount = userCount + 1;
res.json({countNum: userCount});
});
var router1 = require('./public/routes/Route1');
router1(app);

var router2 = require('./public/routes/Route2');
router2(app);

var router3 = require('./public/routes/Route3');
router3(app);

var appEnv = cfenv.getAppEnv();

app.listen(appEnv.port, appEnv.bind, function() {

   console.log("server starting on " + appEnv.url); 
});

And everytime someone visits our webpage, we send a request from controller to server to increase the counter. Would this not create a race condition when multiple people access the webpage at the same time? Is there some other way possible?

We had a look at cookies also, but we want to increase the counter even when the same user visits again, and even if thats possible, I am unable to understand how the race condition can be avoided?

Share Improve this question edited Jun 14, 2017 at 8:51 Nagireddy Hanisha asked Jun 14, 2017 at 8:36 Nagireddy HanishaNagireddy Hanisha 1,4604 gold badges20 silver badges40 bronze badges 2
  • insert user Ip address or mac id of puter when u will need count.Count those inserted records.Inserting mac id u can also count unique visitor on your website – Dexter Commented Jun 14, 2017 at 8:44
  • 1 Please don't put tags in question titles – Liam Commented Jun 14, 2017 at 8:44
Add a ment  | 

2 Answers 2

Reset to default 5

Assuming You're running a single Node.js process then you can be sure that there'll be no race conditions. Node.js is single threaded and hence all the javascript code is executed synchronously. So each request will run your controller method one at a time.

One drawback of this approach would be that your userCount would be lost as soon as you restart your server as its stored in the process' memory.

However if you're using more than one Node.js process then this method would fail as each Node.js process will maintain its own different userCount. The only fail proof way then would be to use a central database (mon to all Node.js process) which already handles race conditions (Most do) and store the count there.

We wish to record the number of hits on the page and display it real time

In that case, use WebSockets for real time, which seems a more convenient solution in your case. With WebSockets, when someone closes the page, you can decrement the number of "live" users as well. You don't even need to store the number in the database, unless you want to keep track of every page hit.

The userCount trick you're doing is interesting, but also has a dark side: if the server restarts, you lose the number.

UPDATE:

Here's how you do it:

  1. When you receive a page hit, you open a WebSocket connection.
  2. On opening the connection successfully, increment the live number counter.
  3. On opening the connection successfully, increment a number in the database to keep track of all hits.
  4. (Optional) On closing a connection, decrement the live number counter.
发布评论

评论列表(0)

  1. 暂无评论