Skip to content

Instantly share code, notes, and snippets.

@totherik
Last active August 29, 2015 14:10
Show Gist options
  • Save totherik/3f21469118f8332225cc to your computer and use it in GitHub Desktop.
Save totherik/3f21469118f8332225cc to your computer and use it in GitHub Desktop.
'use strict';
var http = require('http');
var express = require('express');
var enrouten = require('enrouten');
var app, server;
app = express();
/**
* Index Mode
* Dependencies are provided, in order, to all scanned files within
* a directory, in addition to the existing `router` argument.
*/
app.use(enrouten({
"mode": {
"type": "index",
"options": "./controllers",
"dependencies": ["config", "misc"]
},
"providers": {
"config": {},
"misc": {}
}
}));
/**
* Directory Mode
* Dependencies are provided, in order, to configured index file in
* in addition to the existing `router` argument.
*/
app.use(enrouten({
"mode": {
"type": "directory",
"options": "./routes",
"dependencies": ["config"]
},
"providers": {
"config": {}
}
}));
/**
* Routes Mode
* Top-level dependencies are injected to *all* routes that omit a dependency
* declaration of their own. If a route definition declares dependencies, that list
* supercedes the global list.
*/
/*
UPDATE: This can't/does not need to change.
app.use(enrouten({
"mode": {
"type": "routes",
"options": [
{ path: '/', method: 'GET', handler: require('./routes/index') },
{ path: '/foo', method: 'GET', handler: require('./routes/foo'), dependencies: ["misc"] } // Overrides parent deps
],
"dependencies": ["config", "misc"] // Apply to all routes unless overridden
},
"providers": {
"config": {},
"misc": {}
}
}));
*/
module.exports = app;
'use strict';
var db = require('./mydb');
module.exports = function (router, config) {
var conn = db.createConnection(config.db);
router.get('/db', function (req, res) {
// do stuff with db
res.end('ok');
});
};
'use strict';
module.exports = function (router, config, misc) {
if (config.env.dev) {
router.get('/test', function (req, res) {
res.end('test');
});
}
};
'use strict';
var db = require('./mydb');
module.exports = function (router, misc) {
router.get('/', function (req, res) {
res.end('ok');
});
};
'use strict';
var db = require('./mydb');
module.exports = function (router, config, misc) {
router.get('/', function (req, res) {
res.end('ok');
});
};
@aredridel
Copy link

"dependencies", too, is non-obvious. To document this, we'd have to explain essentially all of DI. And why our flavor of DI.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment