John
John

Reputation: 129

Use a mysql connection across my express js app

I am pretty new with node.js and express so bear with me please.

I am wondering how i can get a mysql instance and use it in my controller. I have 4 files that look like this:

see my comment in the controller.js file

server.js :

var express = require('./config/express');
var app = express();
module.exports = app;
app.listen(3000);
console.log('server running');

express.js :

var express = require('express'),
bodyParser = require('body-parser'),
mysql = require('mysql');

module.exports = function() {
var app = express();

app.use(bodyParser.urlencoded({
    extended: true
}));

app.use(bodyParser.json());

app.set('views','./app/views');
app.set('view engine','ejs');

//NOT SURE WHAT TO DO HERE OR WHERE TO PUT THE CONNECTION DETAILS

var dbConnection = mysql.createConnection({
    host     : 'localhost',
    user     : 'someuser',
    database : 'somedb',
    password : 'somepass'
});

//connection.connect();
//
//connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
//    if (err) throw err;
//    console.log('The solution is: ', rows[0].solution);
//});
//
//connection.end();


require('../app/routes/index.server.routes.js')(app);

app.use(express.static('./public'));
return app;
};

routes.js :

module.exports = function(app) {
  var index = require('../controllers/index.server.controller');
  app.get('/', index.render);
  app.post('/', index.stuff);
};

controller.js :

 exports.render = function(req, res) {
   //DO DB STUFF HERE

   res.render('index', {
     title: 'this is the title'
   });
 };

exports.stuff = function(req, res) {
  res.render('index', {
    title: 'this is the title post'
  });
};

Upvotes: 1

Views: 1711

Answers (1)

gnerkus
gnerkus

Reputation: 12019

To use the connection instance in your controller, you'll need to pass it from the express.js file to the controller.js file. The first step is to pass the connection instance to the router:

express.js

require('../app/routes/index.server.routes.js')(app, connection);

This will make it available in the routes.js file. You then need to pass the same connection instance to the controller.

index.server.routes.js

module.exports = function(app, connection) {
  var index = require('../controllers/index.server.controller')(connection);
  app.get('/', index.render);
  app.post('/', index.stuff);
};

The controller will need to be refactored so it takes a connection instance as an argument:

index.server.controller.js

function IndexController(connection) {
    controllerMethods = {};
    controllerMethods.render = function (req, res) {
        // You can use the connection instance here.
        connection.connect()
        // Run your query
        connection.end()
        ...
    };

    // Define other methods for the controller

    // Return the object that holds the methods.
    return controllerMethods;
}

module.exports = IndexController;

Upvotes: 1

Related Questions