Node.js make an initialized object available in all modules

I have an initialized object that I initialized in the app.js file, and I would like to make this initialized object available in all modules. How could I do this? Passing this object to all modules is one way to do it, and I wonder if I am missing something or something needs to be done in different ways?

I saw that mongoose actually supports the default connection, which I need to run in app.js once and anywhere in other modules, I just can use it without requiring it to be passed. Do I have something like this?

I also checked the global doc object from node.js http://nodejs.org/api/globals.html and wonder what should I use global for the problem.

thanks

+4
source share
3 answers

A little tip:

  • You very rarely need to use global. If you think you need it, you probably won't.
  • Singletones are usually anti-patterns in Node.js, but sometimes (logging, config) they will do the job just fine.
  • Walking around is sometimes useful and rewarding.

Here is an example of how you can use a singleton for logging:

Library / logger.js

var bunyan = require('bunyan'), mixIn = require('mout/object/mixIn'), // add some default options here... defaults = {}, // singleton logger, createLogger = function createLogger(options) { var opts; if (logger) { return logger; } opts = mixIn({}, defaults, options); logger = bunyan.createLogger(opts); return logger; }; module.exports = createLogger; 

Library / module.js

 var logger = require('./logger.js'), log = logger(); log.info('Something happened.'); 

Hope this helps.

+9
source

The solution, as you suggest, is to add an object as a property for the global object. However, I would recommend not to do this and put the object in your own module, which require d from any other module that it needs. You will receive benefits later in several ways. First, it is always explicit where this object comes from and where it is initialized. You will never have a situation when you try to use an object before its initialization (provided that the module that defines it also initializes it). It will also help make your code more reliable.

+4
source

There are many solutions to the problem, depending on how large your application is. The two solutions you mentioned are the most obvious. I would rather go for a third, based on the re-architecture of your code. The solution that I provide looks the same as the artist template.

First, create actions that require your common module, which are in this particular form -

 var Action_One = function(commonItems) { this.commonItems = commonItems; }; Action_One.prototype.execute = function() { //..blah blah //Your action specific code }; var Action_Two = function(commonItems) { this.commonItems = commonItems; }; Action_Two.prototype.execute = function() { //..blah blah //Your action_two specific code }; 

Now create an action initializer that will programmatically initialize your actions as follows:

 var ActionInitializer = function(commonItems) { this.commonItems = commonItems; }; ActionInitializer.prototype.init = function(Action) { var obj = new Action(this.commonItems); return obj; }; 

The next step is to create an action performer -

 //You can create a more complex executor using `Async` lib or something else var Executor = function(ActionInitializer, commonItems) { this.initializer = new ActionInitializer(commonItems); this.actions = []; }; //Use this to add an action to the executor Executor.prototype.add = function(action) { var result = this.initializer.init(action); this.actions.push(result); }; //Executes all the actions Executor.prototype.executeAll = function() { var result = []; for (var i = this.action.length - 1; i >= 0; i--) { result[i] = this.action[i].execute(); } this.action = [] return result; }; 

The idea was to split each module so that in this case there was only one Executor module, which depends on common properties. Now let's see how this will work -

 var commonProperties = {a:1, b:2}; //Pass the action initilizer class and the common property object to just this one module var e = new Executor(ActionInitializer, commonProperties); e.add(Action_One); e.add(Action_Two); e.executeAll(); console.log(e.results); 

Thus, your program will be cleaner and more scalable. Shoot questions if it's not clear. Happy coding!

0
source

Source: https://habr.com/ru/post/1500845/


All Articles