计算我的网页的点击次数 [英] Counting the number of hits to my webpage

查看:120
本文介绍了计算我的网页的点击次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个网页,用于内部监控我公司的某些服务。我们希望记录页面上的点击次数并实时显示。我已经解决了这些问题:

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

确定访问网页的次数

如何使用JavaScript计算我网站上访问者的数量?

但我无法使用谷歌分析和其他此类工具。我希望它可以在nodejs中完成。问题是如果我在nodejs服务器上做这样的事情:

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?

我们也看了一下cookie,但是即使同一个用户再次访问我们也希望增加计数器,即使可能,我也无法理解可以避免竞争条件吗?

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?

推荐答案

假设您正在运行单个Node.js进程,那么您可以确定没有竞争条件。 Node.js是单线程的,因此所有的javascript代码都是同步执行的。因此,每个请求将一次运行一个控制器方法。

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.

这种方法的一个缺点是你的 userCount 一旦重新启动服务器就会丢失,因为它存储在进程的内存中。

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.

但是如果你使用多个Node.js进程那么这个方法会失败,因为每个Node.js进程将维护自己不同的 userCount 。唯一的失败证明方式是使用中央数据库(所有Node.js进程通用)已经处理竞争条件(大多数情况下)并将计数存储在那里。

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 (common to all Node.js process) which already handles race conditions (Most do) and store the count there.

这篇关于计算我的网页的点击次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆