Does Module.require(...).* Return A Copy Of Module.exports.* Or A Reference Of It?
In the following code, are the 'db' variables in session.js and user.js referencing to the same object in db.js, or are they copies of it (making separate connections to my db serv
Solution 1:
A required module is cached, so they will both point to the same object. Relevant Node.js documentation: Modules caching docs.
Solution 2:
Every call to require('../db.js')
returns the same object, so in your case there would just be a single database
connection pool created.
Note that database
is actually a pool of connections (5 by default) that can be freely shared across your code so this is likely what you want.
See the docs here.
Post a Comment for "Does Module.require(...).* Return A Copy Of Module.exports.* Or A Reference Of It?"