Golo Roden Golo Roden - 1 year ago 107
Node.js Question

How to get all registered routes in Express?

I have a web application built using Node.js and Express. Now I would like to list all registered routes with their appropriate methods.

E.g., if I have executed

app.get('/', function (...) { ... });
app.get('/foo/:id', function (...) { ... });
app.post('/foo/:id', function (...) { ... });

I would like to retrieve an object (or something equivalent to that) such as:

get: [ '/', '/foo/:id' ],
post: [ '/foo/:id' ]

Is this possible, and if so, how?

UPDATE: Meanwhile, I have created an npm package called get-routes that extracts the routes from a given application, which solves this issue. Currently, only Express 4.x is supported, but I guess for now this is fine. Just FYI.

Answer Source

express 3.x

Okay, found it myself ... it's just app.routes :-)

express 4.x

Applications - built with express()


Routers - built with express.Router()


Note: The stack includes the middleware functions too, it should be filtered to get the "routes" only.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download