Created
February 16, 2022 15:55
-
-
Save simonista/d2d3f7f57cbc6ed7b63b4fbe1f116b98 to your computer and use it in GitHub Desktop.
Print all the routes defined in an express app (excluding middleware)
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 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