/
index.js
55 lines (49 loc) · 1.45 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
/* jshint node:true, browser:false */
/* global require */
;(function(module) {
'use strict';
require('colors');
module.exports = function(app, logger) {
if(typeof logger === 'undefined' && typeof console !== 'undefined') {
logger = console;
}
var log = (function(l) {
if(typeof l.info === 'function') {
return l.info;
} else if(typeof l.log === 'function') {
return l.log;
} else {
throw new Error("I can't figure out how to log to that logger!");
}
})(logger);
var printRoutes = function(rts) {
if(rts instanceof Array) {
rts.forEach(function(r) {
if(typeof r.path === 'string') {
log(' ' + r.method.toUpperCase().green + ' ' + r.path.cyan);
} else {
log(' ' + r.method.toUpperCase().green + ' ' + r.path.toString().yellow);
}
});
}
};
log('ExpressJS routes (' + 'strings'.cyan + ' ' + 'regexes'.yellow + ')');
if(app.routes) {
var r = app.routes;
([r.get, r.post, r.put, r['delete'], r.head, r.options, r.trace]).forEach(printRoutes);
} else if(app._router && app._router.stack) {
app._router.stack
.filter(function(r) { return r.route && r.route.path && r.route.methods; })
.forEach(function(r) {
printRoutes(Object.keys(r.route.methods).map(function(method) {
return {
method: method,
path: r.route.path
};
}));
});
} else {
log('Your app doesn\'t look familiar to me, I cannot print its routes.');
}
};
})(module);