Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly pass mysql connection to routes with express.js

I am trying to figure out the best way to pass a mysql connection (using node-mysql) between my routes for express.js. I am dynamically adding each route (using a for each file loop in routes), meaning I can't just pass in the connection to routes that need it. I either need to pass it to every route or none at all. I didn't like the idea of passing it to ones that dont need it so I created a dbConnection.js that the routes can individually import if they need. The problem is that I dont think I am doing it correctly. As of now, my dbConnection.js contains:

var mysql = require('mysql'); var db = null; module.exports = function () {     if(!db) {             db = mysql.createConnection({                 socketPath: '/tmp/mysql.sock',             user: '*********',             password: '*********',             database: '**********'         });     }     return db; }; 

And I am importing it into each route using:

var db = require('../dbConnection.js'); var connection = new db(); 

But I would like to do it like this:

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

When I try it like this, however, I get an error saying connection has no method 'query' when I try to make a query.

like image 631
anonymousfox Avatar asked May 28 '13 19:05

anonymousfox


People also ask

Can Nodejs communicate with MySQL?

Once you have MySQL up and running on your computer, you can access it by using Node. js. To access a MySQL database with Node. js, you need a MySQL driver.

How does routing work in Express JS?

A route method is derived from one of the HTTP methods, and is attached to an instance of the express class. The following code is an example of routes that are defined for the GET and the POST methods to the root of the app. Express supports methods that correspond to all HTTP request methods: get , post , and so on.


1 Answers

I find it more reliable to use node-mysql's pool object. Here's how I set mine up. I use environment variable for database information. Keeps it out of the repo.

database.js

var mysql = require('mysql');  var pool = mysql.createPool({   host: process.env.MYSQL_HOST,   user: process.env.MYSQL_USER,   password: process.env.MYSQL_PASS,   database: process.env.MYSQL_DB,   connectionLimit: 10,   supportBigNumbers: true });  // Get records from a city exports.getRecords = function(city, callback) {   var sql = "SELECT name FROM users WHERE city=?";   // get a connection from the pool   pool.getConnection(function(err, connection) {     if(err) { console.log(err); callback(true); return; }     // make the query     connection.query(sql, [city], function(err, results) {       connection.release();       if(err) { console.log(err); callback(true); return; }       callback(false, results);     });   }); }; 

Route

var db = require('../database');  exports.GET = function(req, res) {   db.getRecords("San Francisco", function(err, results) {     if(err) { res.send(500,"Server Error"); return;     // Respond with results as JSON     res.send(results);   }); }; 
like image 106
Daniel Avatar answered Oct 05 '22 12:10

Daniel