> On 28 Feb 2015, at 06:31, Satish B <[email protected]> wrote: > > 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.
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
--
Job board: http://jobs.nodejs.org/
New group rules:
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/nodejs/E942CD2C-ED16-4F7B-B63D-C5C8A8EA3045%40dinhe.net.
For more options, visit https://groups.google.com/d/optout.
smime.p7s
Description: S/MIME cryptographic signature
