Global and Local Variables in Node.js

157 views
Skip to first unread message

Satish B

unread,
Feb 28, 2015, 5:24:22 PM2/28/15
to nod...@googlegroups.com
After working with Node.js for some months, I was shocked to know recently that the global variables are actually shared between the users!

Example: If 1 user sets the value of a global variable to 10, the value will be 10 to other user too.

In other programming languages like PHP, separate instances of these variables are created, unlike in Node.js


I want to know if variables inside the route are also shared between the users? I guess the req object makes it a separate instance for each user.
I've not got a proper way to test this, so asking the community.


Please let me know if I'm missing something here.


Thanks ..

Aria Stewart

unread,
Feb 28, 2015, 5:38:29 PM2/28/15
to nod...@googlegroups.com
They are not, generally, if you mean a 'route' as in this:

app.get('/path', function (req, res) {
var thisvariablerighthere = 1;
});

Javascript is lexically scoped, so each time a function is run, variables declared within it (look for the var keyword!) are given a new allocation.

Functions within that scope can see 'outward' to those variables, but things outside can't see 'in' and see the varying variables within a function.

The thing that surprises you coming from PHP is that unlike PHP, node _is_ the web server, so its toplevel variables actually last the duration of the server's run, not any individual request. PHP, on the other hand, its top level is matched lifetime-wise to a request.

Aria

Harald Hanche-Olsen

unread,
Mar 1, 2015, 10:13:53 AM3/1/15
to nod...@googlegroups.com
Aria Stewart wrote:
>> On 28 Feb 2015, at 06:31, Satish B<technotip...@gmail.com> wrote:
>> […]
>> I want to know if variables inside the route are also shared between the users? I guess the req object makes it a separate instance for each user.
>> I've not got a proper way to test this, so asking the community.
>
>
> They are not, generally, if you mean a 'route' as in this:
>
> app.get('/path', function (req, res) {
> var thisvariablerighthere = 1;
> });

Do note the presence of the “var” keyword. Without it, you may well be
accessing global state.

Perhaps running node in strict mode is a good idea? Put "use strict"; at
the top of your scripts to turn it on.

– Harald

Satish B

unread,
Mar 1, 2015, 10:13:54 AM3/1/15
to nod...@googlegroups.com
Thanks for the clarification Aria.

Coming from PHP background I overlooked these issues. My bad.


I was not able to open .p7s file. I tried it to open in my ms outlook, but failed.
Can you please upload it in .txt format, maybe zipped ?



Thanks ..

Tom Boutell

unread,
Mar 1, 2015, 10:13:54 AM3/1/15
to nod...@googlegroups.com
Aria's answer is great. This is an issue that confuses a lot of people coming from PHP. Try to remember: "there is no magic in node." If you want to scope something to one request, either use a local variable declared in the route function, or attach it to req as a property.

You can use global data intentionally for many useful things, like caching.

Aria Stewart

unread,
Mar 1, 2015, 11:04:24 AM3/1/15
to nod...@googlegroups.com

> On 1 Mar 2015, at 03:35, Satish B <technotip...@gmail.com> wrote:
>
> Thanks for the clarification Aria.
>
> Coming from PHP background I overlooked these issues. My bad.
>
>
> I was not able to open .p7s file. I tried it to open in my ms outlook, but failed.
> Can you please upload it in .txt format, maybe zipped ?


A p7s file is a digital signature. It's only visible because Google insists on mangling messages sent to the mailing list. Ignore it. Sorry for the noise.

Aria
Reply all
Reply to author
Forward
0 new messages