1

I have an app written using express with connections to db, logger and other long living objects that requires to be open and accessed by multiple js files.

What are some of the techniques I could use to make the object "global" with minimal overhead/maintanence?

Thanks

ed1t
  • 8,719
  • 17
  • 67
  • 110

2 Answers2

1

In my opinion, you could put it in a foo.js file and use require('foo.js') to load it. This object is created only one according to This question

Community
  • 1
  • 1
David
  • 241
  • 2
  • 11
0

Define like this //global.js

module.export = function(){


    //define your global object and function here.
}

and call require('global.js') in your script where you want

RIYAJ KHAN
  • 15,032
  • 5
  • 31
  • 53