Sunday 22 April 2018

Questions regarding module.exports

Hey guys,when I use module.exports in any of my modules, for example the following code:const mongoose = require('mongoose'); const Grid = require('gridfs-stream'); const database = 'mongodb://127.0.0.1:27017/test'; const connection = mongoose.createConnection(database, null, (err, db) => { if(err) { console.log(`Error occured during connection to the database ${err}`); } else { console.log(`Connected to the database: ${database}`); } }) module.exports = {connection}; I require the shown module in my entry.js-file.When exactly will the above code execute? When requiring it? If I don't require the export in my entry.js, the code won't execute ever, right?Do I need to require all the the dependencies I required in my exported module in my entry.js file? To my understanding I don't, as long as I'm not (in this case mongoose) accessing any of mongooses features, which would require the mongoose package, right?Let's assume I require the module above in another file, for example test.js, would I receive another instance(another connection) to the database or is the instance shared throughout my code?

Submitted April 22, 2018 at 08:46AM by Fasyx

No comments:

Post a Comment