You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi! I've been fighting this for a bit, but I'm trying to gather a list of all of the endpoints in our express applications. I've tried 'express-list-router' and the code below, but none give the full paths of the endpoint and the allowed methods to use them. I've read the documentation, but can't seem to find the properties/methods to access this information. Thank you in advance for your time and assistance!
Reacted by Jianhua Cheng, Ruslan Konev, Sam He, Mathieu TUDISCO, Rayann Nayran, Chiawen Chen, Alexander Shevchenko, Raz Luvaton, Chuck Schleutker, Suman Sarkar and 3 more
It is not possible to get a fill list with the built-in router. Since paths are regular expression-based, there is no easy way in which to reverse them into a path.
Reacted by Mark Stosberg, Pratik Chowdhury and Andy Zawada
Hmmm, I'm auditing a public facing application. Tons of repos and submodules, it would be almost laughable to do it manually. Is my only option then to catch them downstream via: req.method and req.originalUrl?
There is no general solution that would guarantee you are not missing anything. Is it possible to share the app at all? We could at least see what kind of structure the program is using to see if it's possible at all. What you posted at first is a pretty simplistic method, and if that's not giving what you're looking for, it's hard to really understand why without seeing something.
POST /api/v1/user
GET /api/v1/user
PUT /api/v1/user
GET /
Reacted by Urbino Pescada, Vilintritenmert, Pkmmte Xeleon, noorzaie, Michael Giba, Zava, Peter Lai, Alexandru Olaru, Tomek Kolasa, knnth and 140 more
Reacted by Alex and 98587329
Reacted by Bryan Cote, Rocco Musolino, Pkmmte Xeleon, Alexandru Olaru, knnth, Charlie Deuter, Chris Rueber, simonjeesam, Bruno Bevilaqua, Park Hee Chan and 13 more
Reacted by Bryan Cote, Ryan W, Pkmmte Xeleon, Salvia, Ante Barišić, knnth, MilosN., Neas, Dhruvkumar Joshi, Kris Lefeber and 21 more
Reacted by Bryan Cote, Jairo Malanay, Michael Francoeur, Vilintritenmert, Pkmmte Xeleon, theNotoriousHxc, knnth, Bruno Alexandre, Neas, Seanghay Yath and 36 more
Reacted by AI, Sean Cannon, raul0ligma, Daniil Dubchenko, Ani Sinanaj, Uma Sankar, Fernando de Oliveira Santos, weplaynepal, Devy Mutiara Sari and 月太げつたい🌸
It's been working perfectly for our simpler projects, pretty confident it'll work for our more convoluted apps as well, but I will keep you posted. :)
I wanted to check in with you before I did it, but there's this stackoverflow post of others who have tried to get this working, would it be ok if I posted your hacky albeit effective solution?
Hi if someone is still searching, I rewrote the solution of @dougwilson in Typescript and made it output an array of strings of your routes so you can do with them what you wish.
Actually I wrote it as an middleware and in a singleton pattern. This is useful if you dont add routes on runtime, every route has only one Routehandler. So you can save the routes into a Map one time and can have a fast lookup. I used it to match the routes to the RouteHandlers and now I can lookup what route a routehandler has.
@nklisch I appreciate your library very much. However, I am afraid it does not dig up to the middleware level. Take, for example, my app available here: I use three libraries, swagger-stats, express-status-monitor and swagger-jsdoc, which inject some route, respectively /swagger-stats/stats, /status and explicitly-defined /swagger route. However, it is not able to find them for a reason unknown.
Could you please take a look at this abnormal behaviour?
@brunolnetto
For swagger ui try:
router.use('/swagger"', swaggerUi.serve);
router.get('/swagger", swaggerUi.setup(swaggerDocument));
instead of just one app.use() line.
This means it is impossible for someone to scan the Express stack to figure out these routes, as they are hidden inside custom logic, inside a middleware.
Hey y'all – to my understanding NestJS uses express for routing – how can I grab the @Version decorator when getting routes? FWIW I'm using URI based versioning, so the routes look something like:
/foo/1.0/bar/baz however, when querering the router (using this is an example #3308 (comment)) – I see: /bar/baz (which is missing both the versioning prefix (foo) and the version number 1.0. Any help would be greatly appreciated...
Activity
dougwilson commentedon May 11, 2017
It is not possible to get a fill list with the built-in router. Since paths are regular expression-based, there is no easy way in which to reverse them into a path.
bryanCoteChang commentedon May 12, 2017
Hmmm, I'm auditing a public facing application. Tons of repos and submodules, it would be almost laughable to do it manually. Is my only option then to catch them downstream via:
req.methodandreq.originalUrl?bryanCoteChang commentedon May 12, 2017
**Creative and/or potentially crazy solutions are welcome :)
dougwilson commentedon May 12, 2017
There is no general solution that would guarantee you are not missing anything. Is it possible to share the app at all? We could at least see what kind of structure the program is using to see if it's possible at all. What you posted at first is a pretty simplistic method, and if that's not giving what you're looking for, it's hard to really understand why without seeing something.
bryanCoteChang commentedon May 12, 2017
O I think I can share my sandbox app! (Way less code anyway.) Two main datapoints I'm trying to capture:
Sandbox:
Output:
/user: post, get, put,
/: get,
Expected Output:
/api/v1/user: post, get, put,
/: get,
dougwilson commentedon May 12, 2017
Here is a very, very hacky way for that app. I can't stress how hacky this is, but here it is:
That produces:
bryanCoteChang commentedon May 12, 2017
Hey, I'll take hacky over nothing any day . :)
I'll have to try this against our other apps and get back to you! Thanks for looking into this so quickly!!
bryanCoteChang commentedon May 15, 2017
It's been working perfectly for our simpler projects, pretty confident it'll work for our more convoluted apps as well, but I will keep you posted. :)
I wanted to check in with you before I did it, but there's this stackoverflow post of others who have tried to get this working, would it be ok if I posted your hacky albeit effective solution?
urbinopescada commentedon Jul 31, 2017
It's the only I found that realy works so far ... good job
knnth commentedon Nov 4, 2017
Thanks, @dougwilson! This is the only solution working. I hope the next version do not break this code :D
daniele-orlando commentedon Mar 7, 2018
Same topic discussed and addressed on StackOveflow.
4 remaining items
dondre commentedon Oct 13, 2019
Here's how I parsed out the express routes to a json array.
const endpoints = app._router.stack.filter(x=> x.route && x.route.path && Object.keys(x.route.methods) != 0).map(layer => ({ method :layer.route.stack[0].method.toUpperCase(), path: layer.route.path}));Result
[{ method: "GET", path: "/api/example }, {...}]wesleytodd commentedon Oct 15, 2019
@StuAtGit, maybe this will help you?
https://github.com/wesleytodd/express-openapi/
StuAtGit commentedon Oct 15, 2019
@wesleytodd - 👍
That's a very clever solution. The kind of thing that seems obvious once you see, but as seen in this thread, not until then ;)
NaveenDA commentedon Oct 24, 2019
I use this package, which gives both terminal and web view
https://www.npmjs.com/package/express-routes-catalogue
Uzlopak commentedon Jan 17, 2020
@dougwilson
Seems like that there are also some parasites on the internet, who steal your idea and sell them as their own...
https://medium.com/@stupid_arnob/get-all-api-path-with-method-in-a-single-api-request-f6116254ea1a
TLouage commentedon Apr 24, 2020
Hi if someone is still searching, I rewrote the solution of @dougwilson in Typescript and made it output an array of strings of your routes so you can do with them what you wish.
And then you can call it like this:
ghinks commentedon Apr 24, 2020
We are always listening 😀
Uzlopak commentedon Apr 25, 2020
Actually I wrote it as an middleware and in a singleton pattern. This is useful if you dont add routes on runtime, every route has only one Routehandler. So you can save the routes into a Map one time and can have a fast lookup. I used it to match the routes to the RouteHandlers and now I can lookup what route a routehandler has.
nklisch commentedon Jul 29, 2022
I wrote up a dependency free Typescript npm package that parses complex express apps and outputs a list of data per route and allows attaching meta-data to each route, if desired.
https://www.npmjs.com/package/express-route-parser
It also handles the cases that wesley todd's open api npm package runs into on some project configurations.
brunolnetto commentedon Nov 14, 2022
@nklisch I appreciate your library very much. However, I am afraid it does not dig up to the middleware level. Take, for example, my app available here: I use three libraries,
swagger-stats,express-status-monitorandswagger-jsdoc, which inject some route, respectively/swagger-stats/stats,/statusand explicitly-defined/swaggerroute. However, it is not able to find them for a reason unknown.Could you please take a look at this abnormal behaviour?
nklisch commentedon Nov 14, 2022
@brunolnetto I'll take a look
nklisch commentedon Nov 14, 2022
@brunolnetto
For swagger ui try:
router.use('/swagger"', swaggerUi.serve);
router.get('/swagger", swaggerUi.setup(swaggerDocument));
instead of just one app.use() line.
But for swagger-stats, I looked into how they do their routing, and unfortunately they have a custom solution that doesn't use Expresses built-in route matching, seen here:
https://github.com/slanatech/swagger-stats/blob/master/lib/swsInterface.js#L141-L149
This means it is impossible for someone to scan the Express stack to figure out these routes, as they are hidden inside custom logic, inside a middleware.
m-graf commentedon Mar 13, 2023
Hey y'all – to my understanding NestJS uses express for routing – how can I grab the
@Versiondecorator when getting routes? FWIW I'm using URI based versioning, so the routes look something like:/foo/1.0/bar/bazhowever, when querering the router (using this is an example #3308 (comment)) – I see:/bar/baz(which is missing both the versioning prefix (foo) and the version number1.0. Any help would be greatly appreciated...olawalejuwonm commentedon Apr 13, 2023
Is there anyone that renders it as an HTML file?