This title itself is a proposition, because by default, each module in a Node.js application is shared with the same database connection. But if the posture is wrong, it can be ugly and may even go wrong.
You can ignore the following part and get to the point.
background
Recently, I am doing professional course design, the title is "Air Ticket Booking Management System". The requirement is relatively simple, so I tried to use the Node.js I was learning recently. I was also investigating what kind of Node.js framework is more suitable. After reading a few frameworks, I found that this is a killer. If you have time to read the documents and search information, it is better to write it directly. Finally, after writing, I will put the code on Github. Everyone is welcome to criticize and correct me.
In terms of database, I thought I was more familiar with and liked JSON (I admit it if I didn’t learn SQL well-_-#), so I chose MongoDB. Node + Mongo is an increasingly popular backend combination in recent years, and there is a lot of information on how to use it together on the Internet. But in order to save time (the course design is only over a week), and focus more on the system and logic, I used Mongoose, a Node.js extension specially used for MongoDB data modeling, to greatly reduce the code that operates the database.
Main topic
I have established two data models, one is the user (User) and the other is the flight (Flight), which are encapsulated into the two modules (Modules) of user.js, flight.js. Model is specifically responsible for interacting with the database. Both the user and flight modules need to connect to the database. At the beginning, my code was as follows:
// ------ user.js -----// require mongoose.js reference mongoose.jsvar M = require('mongoose');// connect to database Connect to database M.connect('mongodb://localhost/test');// ... some other code ...// ----- flight.js -----// require mongoose.js reference mongoose.jsvar M = require('mongoose');// connect to database Connect to database M.connect('mongodb://localhost/test');// ... some other code ...// ------ models.js -----var user = require('./user'), flight = require('./flight');// ----- index.js -----var Models = require('./models');Not to mention that this writing method is not DRY at all, this method itself is wrong. When I run index.js, I get the following error.
> node index.js> Connection error: { [Error: Trying to open unclosed connection.] state: 2 }The error is: Try to open an unclosed connection.
So we should connect to the database once in one place, and then other modules that need to connect to the database interact with the database through this module. It was like a wiring board, and he shouted without hesitation: "There is only one socket on the wall, don't rob it! Let me go! You... that's fine!"
Specific plan
We put the action of connecting to the database into a module and expose the connection to other modules in the entire application, and then other modules that need to connect to the database can refer to this connection.
// ----- database.js -----var M = require('mongoose');M.connect('mongodb://localhost/test');// reference to the database connection Create a reference for this connection var db = M.connection;// expose to modules that require database.js Expose this reference to other modules that reference the database module module module.exports = db;// ----- user.js ----- flight.js Similar -----/... some other code ...// We will pass the reference of the database connection as a parameter in models.js module.exports = function( db ){ if( db ){ // ... do things with the connection ... If you connect to the database, you can perform database-related operations }}// ----- models.js ----// require database module, retrieve the reference to database connection Reference the database module and get the reference to the database connection var db = require('./database');// Pass the reference to the database connection into the module that needs to connect to the database, and the task is completed! var user = require('./user')( db ), flight = require('./flight')( db );This is one way to get multiple modules of a Node.js application to share database connections. I saw it on StackOverflow. If you have a better way, please share it with you in the comments!