-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
132 lines (113 loc) · 3.06 KB
/
index.js
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
var fs = require('fs');
var path = require('path');
var requireDirectory = require('require-directory');
var routes = requireDirectory(module, './routes');
var stack = [];
/**
* Mount routes with directory.
*
* Examples:
*
* // mount routes in app.js
* require('./config/routes')(app);
*
* @param {Object} app
* @param {Object} routes
* @param {String} pre
* @return
* @api public
*/
function mount(app) {
var r = arguments[1] || routes;
var pre = arguments[2] || '';
for (var k in r) {
var file = '/' + pre + '' + k + '.js';
// console.log('mount route ' + file + " ");
var path = '';
if(typeof r[k] == 'object') {
// console.log('this is a obj');
mount(app, r[k], pre + k + '/');
}else if(k === 'index') {
path = '/'+ pre;
_use(app, file, path, r[k]);
}else {
path = '/' + pre + '' + k;
_use(app, file, path, r[k]);
}
}
}
function _use(app, file, path, handler) {
// console.dir(handler)
// console.log(handler.stack)
app.use(path, handler);
_track_routes(file, path, handler.stack);
}
function _track_routes(file, path, handle) {
for(var i in handle){
var _route = handle[i].route;
// console.log(_route);
// console.log(_route.stack);
// console.log(_route.methods);
if(!_route)continue; //添加了非空的处理逻辑,挂载中间件的路由不予显示在路由列表中
var params = _route.stack.params;
for(var j in _route.methods){
if(_route.path == '/'){
_cache_to_stack(file, path, j);
}else{
_cache_to_stack(file, path + _route.path, j);
}
}
}
}
function _cache_to_stack(file, path, method) {
// console.log(file+ ' ' +method + ' ' + path)
stack.push({
file : file,
method : method,
path : path
});
}
function _dump(routes_folder_path) {
var Table = require('cli-table');
var table = new Table({ head: ["File", "Method", "Path"] });
// console.log(stack)
console.log('\n******************************************************');
console.log('\t\tMoaJS Apis Dump');
console.log('******************************************************\n');
for (var k in stack) {
var obj = stack[k];
// console.dir(k)
// console.log(obj.file + obj.method + obj.path)
table.push(
[routes_folder_path + obj.file, obj.method, obj.path]
);
}
console.log(table.toString());
}
/**
* Mount routes with directory.
*
* Examples:
*
* // mount routes in app.js
* mount(app, 'routes2', true);
*
* @param {Object} app
* @param {String} routes_folder_path
* @param {Boolean} is_debug
* @return
* @api public
*/
function mount_with_folder(app, routes_folder_path) {
stack = [];// empty when enter
var r = arguments[1] || './routes';
var is_debug = arguments[2] || false;
r = path.resolve(path.dirname(require.main.filename), r)
// console.log('mount routes_folder_path = ' + r)
routes = requireDirectory(module, r);
mount(app) ;
if(is_debug){
_dump (routes_folder_path);
}
}
module.exports = mount_with_folder;