Skip to content

Instantly share code, notes, and snippets.

@simonista
Created February 16, 2022 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonista/d2d3f7f57cbc6ed7b63b4fbe1f116b98 to your computer and use it in GitHub Desktop.
Save simonista/d2d3f7f57cbc6ed7b63b4fbe1f116b98 to your computer and use it in GitHub Desktop.
Print all the routes defined in an express app (excluding middleware)
function printRoutePaths(router, prefix) {
router.stack.forEach(function(r){
if (r.route?.path){
for (let key in r.route.methods) {
if (r.route.methods[key]) {
console.log(`${key.toUpperCase()} ${prefix}${r.route.path}`)
}
}
} else if (r.name === 'router') {
let newPrefix = r.regexp.toString().replace('/^\\', '').replace('\\/?(?=\\/|$)/i', '')
printRoutePaths(r.handle, `${prefix}${newPrefix}`)
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment