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