What is the best way to pass common variables into separate modules in Node.js?

后端 未结 4 675
慢半拍i
慢半拍i 2020-12-07 08:00

I use separate router files as modules for main app and auth app. I can\'t get the best way to pass variables(db client) into routers. I don\'t want to hardcode it or pass i

4条回答
  •  忘掉有多难
    2020-12-07 08:51

    It is completely outdated, but you can use global in a script :

     global.foo = new Foo();
    

    in another script :

     foo.bar();
    

    You can also use already existing constant :

     Object.foo = new Foo();
    

    And here :

     Object.foo.bar();
    

提交回复
热议问题