Last active
August 29, 2015 14:10
-
-
Save totherik/3f21469118f8332225cc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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'); | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
module.exports = function (router, config, misc) { | |
if (config.env.dev) { | |
router.get('/test', function (req, res) { | |
res.end('test'); | |
}); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var db = require('./mydb'); | |
module.exports = function (router, misc) { | |
router.get('/', function (req, res) { | |
res.end('ok'); | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var db = require('./mydb'); | |
module.exports = function (router, config, misc) { | |
router.get('/', function (req, res) { | |
res.end('ok'); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
"dependencies", too, is non-obvious. To document this, we'd have to explain essentially all of DI. And why our flavor of DI.)