Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS / Express - Make Available MySQL Connection Object in Router File

I have the following in my app.js file:

var mysql = require('mysql');
var connection = mysql.createConnection({
    host: 'localhost',
    port: 3306,
    user: 'user',
    password: 'password',
    database: 'mydatabase'
});
connection.connect();

In routes/index.js, I currently have only the boilerplate code:

var express = require('express');
var router = express.Router();

module.exports = router;

How do I make available the connection object from the app.js file in routes/index.js?

like image 896
Lloyd Banks Avatar asked Aug 04 '14 18:08

Lloyd Banks


People also ask

How do I keep MySQL connection alive in node JS?

- the mysql connection pool is lazy, only creating and restoring connections as needed. with this keepalive, the pool is no longer lazy. once a connection is opened, the keepalive will keep it open. the pool no longer scales depending on traffic.

How do I create a MySQL connection pool in node JS?

Nodejs MySQL Integration: Pool Connectionsvar pool = mysql. createPool({ connectionLimit: 7, host: 'localhost', user: 'root', password: '', database: 'todoapp' });

Does Nodejs allow to communicate with MySQL?

MySQL Driver for Node Projects Assuming that the server is running, you can communicate with it programmatically through a Node application, using a driver. For an application to have this ability, you need to install a MySQL Driver. A driver is available on npm as mysql !


2 Answers

I ended up splitting the database connection logic from the app.js file. In a separate file called connection.js, I have the following:

var mysql = require('mysql');

var connection = mysql.createConnection({
    host: 'localhost',
    port: 3306,
    user: 'user',
    password: 'password',
    database: 'mydatabase'
});

module.exports = connection;

Then in my route file, I add

var connection = require('../connection');

to the top of the file where all my other modules are brought in. In my instance, the connection.js file is one level higher than my route file, hence the ../ in the require() function parameter.

like image 168
Lloyd Banks Avatar answered Oct 13 '22 00:10

Lloyd Banks


My preference is to do some simple dependency injection and pass the required resource into the router by wrapping the module in a function:

var express = require('express');

module.exports = function (connection) {
    var router = express.Router();
    //do stuff with the connection
    return router;

}

Then you just instantiate the router module in app.js as a function with the database connection as an argument:

app.use('/where/ever', require('./module-b')(connection)); 

Usually I wrap up the dependencies in an object:

app.use('/where/ever', require('./module-b')({db:connection})); 

This way you don't have to keep changing the function signature as dependencies are added. This gives you a super-lightweight inversion of control for your express applications.

like image 40
Robert Moskal Avatar answered Oct 12 '22 22:10

Robert Moskal