Skip to content

Instantly share code, notes, and snippets.

@iMagesh
Created February 9, 2022 14:41
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 iMagesh/833fd5c4f9ef4bfac28411f67659cc2e to your computer and use it in GitHub Desktop.
Save iMagesh/833fd5c4f9ef4bfac28411f67659cc2e to your computer and use it in GitHub Desktop.
Code to list all the routes of your express app (node)
//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