Last active
November 11, 2020 18:34
-
-
Save kevinah95/ad899f7bb89a9e34bee6d2a1cea2d04c to your computer and use it in GitHub Desktop.
Print all routes in express REF: https://github.com/expressjs/express/issues/3308#issuecomment-300957572
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
function print (path: any, layer: any) { | |
if (layer.route) { | |
layer.route.stack.forEach(print.bind(null, path.concat(split(layer.route.path)))) | |
} else if (layer.name === 'router' && layer.handle.stack) { | |
layer.handle.stack.forEach(print.bind(null, path.concat(split(layer.regexp)))) | |
} else if (layer.method) { | |
console.log('%s /%s', | |
layer.method.toUpperCase(), | |
path.concat(split(layer.regexp)).filter(Boolean).join('/')) | |
} | |
} | |
function split (thing: any) { | |
if (typeof thing === 'string') { | |
return thing.split('/') | |
} else if (thing.fast_slash) { | |
return '' | |
} else { | |
var match = thing.toString() | |
.replace('\\/?', '') | |
.replace('(?=\\/|$)', '$') | |
.match(/^\/\^((?:\\[.*+?^${}()|[\]\\\/]|[^.*+?^${}()|[\]\\\/])*)\$\//) | |
return match | |
? match[1].replace(/\\(.)/g, '$1').split('/') | |
: '<complex:' + thing.toString() + '>' | |
} | |
} | |
app._router.stack.forEach(print.bind(null, [])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment