List all routes used in Express[3,4,5]
const express = require('express');
const expressListRoutes = require('express-list-routes');
const app = express();
app.get('/health', fn)
app.use('/admin', router);
router.route('/user')
.post(fn)
.get(fn)
.put(fn);
expressListRoutes(app, { prefix: '/api/v1' });
// Logs out the following:
// GET /api/v1/health
// POST /api/v1/admin/user
// GET /api/v1/admin/user
// PUT /api/v1/admin/user
expressListRoutes(router);
// Logs out the following:
// POST /admin/user
// GET /admin/user
// PUT /admin/user
expressListRoutes
returns array of all routes found in express.
const paths = expressListRoutes(req.app, { logger: false });
paths.forEach((endpoint) => {
if (endpoint.path.endsWith('/')) {
...
}
});
npm install express-list-routes
You can pass a second argument to set some options
{
prefix: '', // A prefix for router Path
spacer: 7 // Spacer between router Method and Path
logger: console.info // A custom logger function or a boolean (true for default logger, false for no logging)
color: true // If the console log should color the method name
}