so here's a quick question. Basically I'm making an express application that connects to a MySQL server. Pretty simple. But how do I stop adding all the connection code between files so I don't have to change multiple files if I change password or database.
Example:
app.coffee
client = mysql.createClient
user: config.db.user,
password: config.db.pass
app.get '/', routes.index
routes/index.coffee
client = mysql.createClient
user: config.db.user,
password: config.db.pass
exports.index = (req, res) ->
res.render 'index'
I've tried to create a file called lib/mysql.coffee
which is the following:
config = require '../config'
mysql = require 'mysql'
module.exports = ( ->
mysql.createClient
host: config.db.host
user: config.db.user
password: config.db.pass
database: config.db.name
)
So in app, I can go:
mysql = require './lib/mysql'
mysql.query 'SHOW TABLES', (err,r,f) ->
console.log err
but it keeps giving:
TypeError: Object function () {
return mysql.createClient({
host: config.db.host,
user: config.db.user,
password: config.db.pass,
database: config.db.name
});
} has no method 'query'