0

I have a main module (app.js) which initializes a client (for making REST api calls) and have functions in that main module that use this client. As my code is growing bigger, I'd like to modularize my main by putting the functions into modules (say module_A.js). What is the best practice to have this client initialized and shared across the various modules? One way I thought about is to create a client module which I require in each modules - would the client not be initialized multiple times then? Christian

1
  • The best way is to create separate modules as you are already doing. which is the optimal way. Yes I feel if you initialize the client in separate module it would be initialize every time its called in different modules. Why not just initialize it one in code and assign it to global object in node and the access the same everywhere. like global.clinst=new client(); and then access across module as global.clinst.getReq(); is a solution i feel . Feel free to comment.. There can be other way as well such as you could initialize it at router level and then pass the function call. Commented May 2, 2017 at 9:17

2 Answers 2

1

No. As far as I know a module is a singleton so it will only ever be created once. When another module requires the module module_A they get the already existing reference to it.

module_A.js

console.log("should only be called ONCE");
var module_object = {
    shared_variable: "initial text"
};

module.exports = module_object;

caller1.js

var testmod = require("./module_A");

console.log("TEST IS:" + testmod.shared_variable);
testmod.shared_variable += " - included in caller1";
console.log("TEST IS:" + testmod.shared_variable);

caller2.js

var testmod = require("./module_A");

console.log("TEST IS:" + testmod.shared_variable);
testmod.shared_variable += " - included in caller2";
console.log("TEST IS:" + testmod.shared_variable);

In the test above the log line with "ONCE" should only be invoked once, no matter for how long your server is running.

I am not sure how many times it will run if you have multiple instances though (like in a cluster with one instance pr. cpu), but you can test that it is necessary.

Sign up to request clarification or add additional context in comments.

Comments

0

Thanks for your answers. After investigating a bit, I think the solution is to initialize each module (module_A, module_B, etc.) with the rest client that is created in the main. That would be something like this:

  • in the main app.js:

    var apiClient = ... var module_A = require('./modules/module_A.js') test.init(apiClient);

  • in module_A.js

    var client = null;
    
    exports.init = function init(aClient){
      client = aClient;
    }
    
    exports.myFunction = function(callback){
      client.doSomething(function(data){
          ...
    

    }); }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.