LordZardeck
LordZardeck

Reputation: 8293

file modules aware of global vars?

Are file modules in Node.js aware of global vars? If not, how can I get a variable to a file module I'm wanting to load? For example, right now I have a single script that runs 3 servers. One for a game lobby, one for the socket router, and one for the administration. I'd like to break the servers into separate file modules. The problem is, the socket router needs to have access to the game lobby, and the game lobby and administration modules need access to the socket router. Is this possible?

Upvotes: 1

Views: 74

Answers (1)

mike
mike

Reputation: 7177

Not really -- Variables defined in files/modules are local to the module.

You could create a common module that exports the needed server variables, and require that module in each server, but a better approach might be using Dependency Injection...

Each server needs to have a way of receiving it's dependent parameters (in this case, via exporting a "start" function with parameter of the servers it requires). On startup, a master server "injects" the references to those other servers.

var socketRouter = require('./socketRouter.js');
var gameLobby = require('./gameLobby.js');
var admin = require('./admin.js');
socketRouter.start(gameLobby);
gameLobby.start(socketRouter);
admin.start(socketRouter);

Upvotes: 2

Related Questions