Nodejs to find all routes example | expressJS

Sometimes, the NodeJS application needs to get all routes from all modules in an application..

this tutorial shows you how to retrieve all routes from the Express app.

As part of this, It prints the URL and Request method.

Nodejs code example to get all routes

nestjs uses express routes, use the express classes to get route information.

  • First, In main.js, get the server object using the app.getHttpServer() method
  • Get routes from the server._events.request._router property
  • Iterate routes stack using the map method
  • check each route if it is a route, and print the request path(routeObj.route?.path) and method(routeObj.route?.stack[0].method).
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
You'll get a notification every time a post gets published here.