Created
February 9, 2022 14:41
-
-
Save iMagesh/833fd5c4f9ef4bfac28411f67659cc2e to your computer and use it in GitHub Desktop.
Code to list all the routes of your express app (node)
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
//If you are using express router | |
var route, routes = []; | |
app._router.stack.forEach(function(middleware){ | |
if(middleware.route){ // routes registered directly on the app | |
routes.push(middleware.route); | |
} else if(middleware.name === 'router'){ // router middleware | |
middleware.handle.stack.forEach(function(handler){ | |
route = handler.route; | |
route && routes.push(route); | |
}); | |
} | |
}); | |
// if you are not using express router | |
app._router.stack.forEach(function(r){ | |
if (r.route && r.route.path){ | |
console.log(r.route.path) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment