How to get all registered routes in Express?

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.


This gets routes registered directly on the app (via app.VERB) and routes that are registered as router middleware (via app.use). Express 4.11.0

//////////////
app.get("/foo", function(req,res){
    res.send('foo');
});

//////////////
var router = express.Router();

router.get("/bar", function(req,res,next){
    res.send('bar');
});

app.use("/",router);


//////////////
var route, routes = [];

app._router.stack.forEach(function(middleware){
    if(middleware.route){ // routes registered directly on the app
        routes.push(middleware.route);
    } else if(middleware.name === 'router'){ // router middleware 
        middleware.handle.stack.forEach(function(handler){
            route = handler.route;
            route && routes.push(route);
        });
    }
});

// routes:
// {path: "/foo", methods: {get: true}}
// {path: "/bar", methods: {get: true}}

DEBUG=express:* node index.js

If you run your app with the above command, it will launch your app with DEBUG module and gives routes, plus all the middleware functions that are in use.

You can refer: ExpressJS - Debugging and debug.


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