How to get all registered routes in Express?

Sometimes, we want to get all registered routes in Express.

In this article, we’ll look at how to get all registered routes in Express.

How to get all registered routes in Express?

To get all registered routes in Express, we can use the app._router.stack property.

For instance, we write

app._router.stack.forEach((r) => {
  if (r?.route?.path){
    console.log(r?.route?.path)
  }
})

to loop through the array returned by app._router.stack, which has all the routes in an array.

We get the path of each route from r.route.path.

Conclusion

To get all registered routes in Express, we can use the app._router.stack property.