Categories
JavaScript Answers

How to get all registered routes in Express?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *