List All Routes in express app #5858
-
Beta Was this translation helpful? Give feedback.
Replies: 30 comments 4 replies
-
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. |
Beta Was this translation helpful? Give feedback.
-
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: |
Beta Was this translation helpful? Give feedback.
-
**Creative and/or potentially crazy solutions are welcome :) |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
O I think I can share my sandbox app! (Way less code anyway.) Two main datapoints I'm trying to capture:
Sandbox:
Output: |
Beta Was this translation helpful? Give feedback.
-
Here is a very, very hacky way for that app. I can't stress how hacky this is, but here it is: function print (path, layer) {
if (layer.route) {
layer.route.stack.forEach(print.bind(null, path.concat(split(layer.route.path))))
} else if (layer.name === 'router' && layer.handle.stack) {
layer.handle.stack.forEach(print.bind(null, path.concat(split(layer.regexp))))
} else if (layer.method) {
console.log('%s /%s',
layer.method.toUpperCase(),
path.concat(split(layer.regexp)).filter(Boolean).join('/'))
}
}
function split (thing) {
if (typeof thing === 'string') {
return thing.split('/')
} else if (thing.fast_slash) {
return ''
} else {
var match = thing.toString()
.replace('\\/?', '')
.replace('(?=\\/|$)', '$')
.match(/^\/\^((?:\\[.*+?^${}()|[\]\\\/]|[^.*+?^${}()|[\]\\\/])*)\$\//)
return match
? match[1].replace(/\\(.)/g, '$1').split('/')
: '<complex:' + thing.toString() + '>'
}
}
app._router.stack.forEach(print.bind(null, [])) That produces:
|
Beta Was this translation helpful? Give feedback.
-
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!! |
Beta Was this translation helpful? Give feedback.
-
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? |
Beta Was this translation helpful? Give feedback.
-
It's the only I found that realy works so far ... good job |
Beta Was this translation helpful? Give feedback.
-
Thanks, @dougwilson! This is the only solution working. I hope the next version do not break this code :D |
Beta Was this translation helpful? Give feedback.
-
Same topic discussed and addressed on StackOveflow. |
Beta Was this translation helpful? Give feedback.
-
I've found this package, which seems to work properly: https://github.com/AlbertoFdzM/express-list-endpoints ! |
Beta Was this translation helpful? Give feedback.
-
I wrote a package to list middleware and routes mounted on an app quite a while ago: https://github.com/ErisDS/middleware-stack-printer Maybe its useful for someone else. |
Beta Was this translation helpful? Give feedback.
-
Why don't you try something like swagger using tsoa(it automatically generates a swagger.json file) which will have all the api liat including what parameters it takes. Just one note, tsoa requires Typescript. |
Beta Was this translation helpful? Give feedback.
-
@surendra-y - the problem I've had with swagger & friends (and tsoa looks similar) is that they all depend on some sort of non-authoritative, duplicate source of route information: annotations, jsdoc, etc. Which only works long term if you're disciplined enough where you probably don't need it in the first place. The great thing about doing it via reflection is:
That said, if whatever middleware does this also spits out the result in OpenAPI format - even better! |
Beta Was this translation helpful? Give feedback.
-
Here's how I parsed out the express routes to a json array.
Result |
Beta Was this translation helpful? Give feedback.
-
@StuAtGit, maybe this will help you? |
Beta Was this translation helpful? Give feedback.
-
@wesleytodd - 👍 |
Beta Was this translation helpful? Give feedback.
-
I use this package, which gives both terminal and web view |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
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:
|
Beta Was this translation helpful? Give feedback.
-
We are always listening 😀 |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
@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, Could you please take a look at this abnormal behaviour? |
Beta Was this translation helpful? Give feedback.
-
@brunolnetto I'll take a look |
Beta Was this translation helpful? Give feedback.
-
@brunolnetto 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: 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. |
Beta Was this translation helpful? Give feedback.
-
Hey y'all – to my understanding NestJS uses express for routing – how can I grab the
|
Beta Was this translation helpful? Give feedback.
-
Is there anyone that renders it as an HTML file? |
Beta Was this translation helpful? Give feedback.
-
Hi @wesleytodd and @blakeembrey! |
Beta Was this translation helpful? Give feedback.
Here is a very, very hacky way for that app. I can't stress how hacky this is, but here it is: