Demon’s Code

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.

  1. var config = lib.extend({
  2.     name:         'unnamed!',
  3.     port:         3000,
  4.     mountpoint:   '/ft',
  5.     timeout:      250,
  6.     wait:         1500,
  7.     path:         './Data'
  8. }, 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.

  1. var EventEmitter = require('events').EventEmitter;
  3. var system = new EventEmitter;
  5. module.exports = system;
  1. var system  = require('./system');
  2. system.config = config;
  3. /* now every other code has access to the config
  4.    via system.config or through  system.on('initialize', function(config) {});
  5. */
  6. 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

No comments for this entry yet...

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...


All entries, chronologically...