Skip to content

Instantly share code, notes, and snippets.

@boutell
Created September 10, 2016 19:50
Show Gist options
  • Save boutell/ca875a10302b234172bfe5c0b5f760c1 to your computer and use it in GitHub Desktop.
Save boutell/ca875a10302b234172bfe5c0b5f760c1 to your computer and use it in GitHub Desktop.
// in app.js
var _ = require('lodash');
var Promise = require('bluebird');
// A global context object for accessing the other modules
var modules = {};
// Would be loaded from a server-specific file, each module has some options in its own property sometimes
var options = {};
var model = loadModule('model');
var express = loadModule('express');
var routes = loadModule('routes');
model.init()
.then(express.init())
.then(routes.init())
.then(function() {
console.log('Ready and listening');
}).catch(function() {
console.error('bad news, everyone!');
});
function loadModule(name) {
context[name] = require('./lib/' + name)(context, options[name] || {});
return context[name];
}
// in model.js
var Promise = require('bluebird');
var mongodb = require('mongodb');
module.exports = function(context, options) {
var self = {};
self.context = context;
self.options = options;
self.init = function() {
return mongodb.MongoClient.connect(options.uri).then(function(db) {
self.db = db;
});
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment