Wednesday 27 November 2019

Beginner learn the correct way of module.exports and global variables

What I get from most of the answers I could find is we should not use global variable lightly.Using NodeJS, we can use module.exports and then require in files that are needed.What I would like to know is would the variable get initialize again on multiple require ?For example on database connection using Sequelize:let sequelize = new Sequelize(...) module.exports = sequelize If file1.js require, it will then creates connection to database.What happens on other files that require it later on? Will it initialize connection again? If so, how do I make it like Singleton?​Sorry if this has been asked before or my understanding is wrong.Thank you in advance.

Submitted November 28, 2019 at 01:14AM by xjellyfishx

No comments:

Post a Comment