0

I am fairly new to node.js and trying to get my head around the asynchronous processing loop etc.

So let's assume I have an array var counter = [];defined at the top of my server.js file

Then I have a POST handler as follows

app.post("/test_post", function(req, res) {
    console.log(req.body);
    counter ++;
})

I am trying to understand the scope of the counter variable - Will it be different for every client or will it be common across clients.

Also, I am looking for a way to increment counter for the same client, so in other words, I'd like a counter for each visiting client.

How can I achieve this? Thanks

3 Answers 3

1

I am trying to understand the scope of the counter variable - Will it be different for every client or will it be common across clients.

It will be common across clients since you are defining it globally.

Also, I am looking for a way to increment counter for the same client, so in other words, I'd like a counter for each visiting client.

Basically what you need to do is to listen on connection & disconnection of every client and save the counter in the respective client object.

Have a look at this implementation it may help you.

Sign up to request clarification or add additional context in comments.

Comments

1

you're counter variable is going to be initialised one time and you can then increment it.

note in posted example counter is an Array var counter = []; so you can increment that. change it to an number and it should work for you

Comments

1

Unlike scripting languages such as PHP. NodeJS is a long running process and should be compared with Java or .NET. The module, thus your file that could expose content through module, is a singleton. Therefore the scope of the counter variable is to be a field in the node process for this particular module. If you have multiple node processes running the same module, the counter variable is different (e.g. when forking the process). The same applies to a variable in a different module, which in terms is a completely different memory address, hence unlike in the vanilla browser environment, it is impossible to get name conflicts.

To answer your question regarding the tracking of number of visits, you have to establish a recognition pattern of the connecting client. HTTP is stateless, and therefore there is no existing state for the HTTP connection, however can be achieved using sessions/cookies. Moreover if you intend to track based on non-cookie powered technologies, you can use browser fingerprinting or simply the IP address. Storing this in a process-scope variable is a bad idea, because you would lose such information the moment the process is restarted. Therefore you should be using a database, of course, assuming you want this information to be long lived.

This is a really naive implementation using process-scope storage and may explode memory...

var express = require('express');
var app = express();
var counters = {};
app.get('/', function (req, res) {
    if (typeof counters[req.ip] !== 'undefined') {
        counters[req.ip]++;
    } else {
        counters[req.ip] = 1;
    }
    res.send('You visited #' + counters[req.ip] + ' times!');
});
app.listen(80);

2 Comments

Thanks for the detailed explanation. One concern I have is regarding the browser fingerprinting is that it is a non-trivial solution and also doesnt guarentee good reliability. Would you consider the "socket" approach recommended by @intuitivepixel above?
Fingerprinting is indeed non reliable, and it makes more sense to use IP or using a cookie/session. The IP can change in some countries, but cookies are reliable (yet can be disabled). The suggestion posted by @intuitivepixel is using socket.io. That is a library written for bi-directional communication without page refreshes using web socket or flash socket. It simply doesn't solve your use case as it has an entirely different purpose.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.