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?


Solution 1:

express 3.x

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

express 4.x

Applications - built with express()

app._router.stack

Routers - built with express.Router()

router.stack

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

Solution 2:

app._router.stack.forEach(function(r){
  if (r.route && r.route.path){
    console.log(r.route.path)
  }
})