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 theapp.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) andmethod
(routeObj.route?.stack[0].method).