I would like to export the variable db so it would be available in app.js
app.js
'use strict';
var config = require('./config');
console.log(config);
var database = require('./services/database')(config);
database.connect(...) // database is undefined
Exception: TypeError: Cannot call method 'connect' of undefined
config.js
var config = module.exports;
config.mongodb = {
port: process.env.MONGODB_PORT || 27017,
host: process.env.MONGODB_HOST || 'localhost',
db: "test"
};
services/database.js
module.exports = function(config) {
var mongoskin = require('mongoskin');
console.log(config);
var dburl = config.mongodb.host + ":" + config.mongodb.port + "/" + config.mongodb.db + "?auto_reconnect";
console.log(dburl);
var db = mongoskin.db(dburl);
// I want to export db
}