dopplesoldner
dopplesoldner

Reputation: 9499

node js - understanding variable scopes

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

Upvotes: 0

Views: 562

Answers (3)

Deathspike
Deathspike

Reputation: 8800

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);

Upvotes: 1

VeXii
VeXii

Reputation: 3109

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

Upvotes: 1

intuitivepixel
intuitivepixel

Reputation: 23322

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.

Upvotes: 1

Related Questions