javabrain
javabrain

Reputation: 383

using file scope variables in node.js

Beginner question

Running on server side, in node.js:

If I use a file-scope (or even global) variable which is set by an export.function(), where that exported function is called via ajax from a client, if multiple requests come from different clients, is the variable now prone to unexpected results?

I.e. do I need to set up an array so every time the export.function() is called it adds a new file-scope instance for that particular ajax request? Or is this magically handled by node.js where every ajax request gets its own instance of the server?

Upvotes: 0

Views: 688

Answers (1)

Robert Levy
Robert Levy

Reputation: 29083

Requests will share the same instances so you'll need to guard against this. Note, however, that blocks of synchronous code will be executed completely before execution switches to handle another request so this simplifies the "guarding" you need to do.

Upvotes: 1

Related Questions