Tag: node.js javascript global
node.js and global objects
by Nox on Jan.10, 2011, under JavaScript, node.js
Today I had a little problem with a node.js app.
I tried to seperate some parts of the app to different files. The problem that I ran into was that the app was controlled by a config file which was loaded synchronously and then carried on with the app.
-
var config = lib.extend({
-
name: 'unnamed!',
-
port: 3000,
-
mountpoint: '/ft',
-
timeout: 250,
-
wait: 1500,
-
path: './Data'
-
}, require('./' + config));
Now after seperating the server and client parts of this app into two different files I had no access anymore to the config file without always carrying the config object with everything. This let me to a simple solution.
-
var EventEmitter = require('events').EventEmitter;
-
-
var system = new EventEmitter;
-
-
module.exports = system;
-
var system = require('./system');
-
system.config = config;
-
/* now every other code has access to the config
-
via system.config or through system.on('initialize', function(config) {});
-
*/
-
system.emit('initialize', config);
Oh... and the reason for this junk is... The global-object does not seem to work. I tried global.config = config, and it did not work.
Have fun with this
Nox