What would be the most appropriate way of sharing the database connection in the below snippet ( the db
variable) with my routers/controllers without turning the
Try look at this way:
app.js:
var mongo = require('mongoskin'),
db = mongo.db(config.db.adress);
app.use(function(req, res, next) {
db.open(function(err, data) {
(err) ? res.send('Internal server error', 500) : next();
});
});
require('./controllers/users')(app, db);
controllers/users.js:
module.exports = function (app, db) {
app.post('/users', function(req, res, next) {
// Your create function
// Link to db exists here
});
};