routes.js
2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* Module dependencies.
*/
var scripts = require('../utils/scripts')
, path = require('path')
, fs = require('fs')
, existsSync = fs.existsSync || path.existsSync // node <=0.6
, debug = require('debug')('bootable-di');
var IoC = require('electrolyte');
var async = require('asyncawait/async');
var await = require('asyncawait/await');
/**
* Route drawing phase.
*
* This phase will `require` a routes file, and invoke the exported function,
* allowing the application to draw its routes. Any annotated dependencies
* (using `exports['@require']`) will be injected into the function when
* invoked.
*
* This phase is typically the last phase before instructing the server to
* listen. Any initializers should be run prior to drawing routes, ensuring
* that the application is fully prepared to handle requests.
*
* Examples:
*
* app.phase(bootabledi.routes('routes.js'));
*
* @param {String|Object} options
* @return {Function}
* @api public
*/
module.exports = function (options, container) {
if (!container) {
try {
container = require('electrolyte');
} catch (_) {
// workaround when `npm link`'ed for development
var prequire = require('../utils/parent-require');
container = prequire('electrolyte');
}
}
if ('string' == typeof options) {
options = { filename: options };
}
options = options || {};
var filename = options.filename || 'routes'
, extensions = options.extensions;
return function routes(done) {
var script = scripts.resolve(path.resolve(filename), extensions);
if (!existsSync(script)) { return; }
var mod = require(script);
var deps = mod['@require'] || []
, args = [], self = this;
// for (var i = 0, len = deps.length; i < len; ++i) {
// debug('create %s', deps[i]);
// var inst = container.create(deps[i], this);
// args.push(inst);
// }
var loadDeps = async(function () {
for (var i = 0, len = deps.length; i < len; ++i) {
debug('create %s', deps[i]);
var inst = await(IoC.create(deps[i]));
args.push(inst);
}
});
loadDeps().then(function () {//异步加载依赖
mod.apply(self, args);
done();
});
};
};