Reputation: 369
I have a NodeJS backend. I am using the mysql plugin (not the connector X API) to connect to the database. I want to open a connection at application start and make the connection available for all the APIs in the application rather than a separate connection per module. How can I do that?
The database connection is in a separate javascript file with the follwing code:
const mysql = require('mysql');
const connection = mysql.createConnection({
host: process.env.HOST,
user: process.env.USER,
password: process.env.PASSWORD,
database: process.env.DATABASE,
});
connection.connect((err) => {
if (err) throw err;
console.log('[SERVER] Database connection established..');
});
module.exports = connection;
I want something like creating the connection in index.js
rather than separate controller modules.
Edit: I have put a console.log at the first line of each file. This is the order the files are being loaded.
This is my index.js file (this is the entry point of the app)
const express = require('express');
const bodyParser = require('body-parser');
require('dotenv').config();
const db = require('./src/utils/DatabaseConnection');
db.establishConnection();
app.listen(port, () => {
console.log(`[SERVER] Up and running at http://localhost:${port}`);
});
and this is how I am calling the established connection in the RegisterController.js file
const database = require('../../utils/DatabaseConnection');
const connection = database.getConnection();
/* Rest of the code*/
Q.1 If I am calling the establishConnection()
method before the server starts, then why is [SERVER] Database connection established..
message being logged after the server running at port message?
Q.2 Is there a specific way to how the files of a Node.js application loaded in the memory?
Upvotes: 0
Views: 213
Reputation: 1215
It would be more readable to have a separate file for db connection.
Since you want it in the index.js itself, you can have it like this
app.use('/users/list', usersList(connection))
In this manner, you will have the connection as the parameter in the usersList
function. So, usersList
will be as
exports.usersList = (connection) => {
// your database operation
}
You can also pass additional params for the function.
I would still recommend you to have separate file for database config and connection. That separate file can look something like this
const mysql = require('mysql');
var _connection;
exports.createConnection = (_connection) => {
const connection = mysql.createConnection({
host: process.env.HOST,
user: process.env.USER,
password: process.env.PASSWORD,
database: process.env.DATABASE,
});
_connection = connection;
connection.connect((err) => {
if (err) throw err;
console.log('[SERVER] Database connection established..');
});
}};
exports.getConnection = () => {
return _connection;
}
Edit: About the logs getting printed later, it is because starting the server requires no connections or other system processes, However, establishing a connection works async and so by the time it is done, your server is already up.
Upvotes: 1