Sharing objects and avoiding globals in node.js

后端 未结 4 1845
眼角桃花
眼角桃花 2021-02-03 15:03

What would be the most appropriate way of sharing the database connection in the below snippet ( the db variable) with my routers/controllers without turning the

4条回答
  •  余生分开走
    2021-02-03 15:47

    As suggested by @Jed Watson, the moongoose module uses a singleton (anti?) pattern, which is enforced by the require/export mechanism. Here is the specific bit of code :

    (as found here : https://github.com/LearnBoost/mongoose/blob/master/lib/index.js)

    /*!
     * The exports object is an instance of Mongoose.
     *
     * @api public
     */
    
    var mongoose = module.exports = exports = new Mongoose;
    

提交回复
热议问题