Created
June 1, 2015 18:53
-
-
Save kreegr/cdf7054b5765572eff6c to your computer and use it in GitHub Desktop.
Outputs the routes for a restify or express 4 nodeJS app
This file contains hidden or 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
module.exports = function (routes, src) { | |
var Table = require('cli-table'); | |
var table = new Table({ head: ["", "Name", "Path"] }); | |
if(src == 'restify') { | |
for (var key in routes) { | |
if (routes.hasOwnProperty(key)) { | |
var val = routes[key]; | |
var _o = {}; | |
_o[val.method] = [val.name, val.spec.path ]; | |
table.push(_o); | |
} | |
} | |
} else { | |
// thanks to http://stackoverflow.com/users/442821/caleb | |
// http://stackoverflow.com/a/28199817 | |
var _mwRoutes = []; | |
routes.forEach(function(middleware){ | |
var val = middleware.route; | |
if(val){ // routes registered directly on the app | |
_mwRoutes.push(val); | |
var _o = {}; | |
_o[val.stack[0].method] = [val.path, val.path ]; | |
table.push(_o); | |
} else if(middleware.name === 'router'){ // router middleware | |
middleware.handle.stack.forEach(function(handler){ | |
var route = handler.route; | |
route && _mwRoutes.push(route); | |
var _o = {}; | |
_o[route.stack[0].method] = [route.path, route.path]; | |
table.push(_o); | |
}); | |
} | |
}); | |
} | |
return table; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment