Getting all registered routes in express.js

Published on Wednesday, April 6, 2016

To get all routes registered in express.js, this snippet gets the work done:

const routes = [];

app._router.stack.forEach((middleware) => {
if (middleware.route) {
routes.push(
`${Object.keys(middleware.route.methods)} -> ${middleware.route.path}`
);
}
});

console.log(JSON.stringify(routes, null, 2));

Possible result:

[
"post -> /customers"
"put -> /customers/:id",
"delete -> /customers/:id"
]
What are your thoughts about
"Getting all registered routes in express.js"?
Drop me a line - I'm looking forward to your feedback!