Member-only story
How to list all routes in ExpressJs
If you are not subscribed to Medium Membership, you can click this link:
If you are building an application in ExpressJs, You might need to define the routes for documenation (for example swagger), but sometimes due to the complexity (especially in large projects) of the application and routes in most of the applications are nested, it is very difficult to list all apis manually.
Fortunately there is an npm package which is express-list-endpoints
which make our job easier.
Here is the documentation for the same: https://github.com/AlbertoFdzM/express-list-endpoints.
I will give you a sample example:
const express = require('express');
const listEndpoints = require('express-list-endpoints');
const app = express();
app.listen(3000);
const endPoints = require('express-list-endpoints');
app.get('/users', () => {});
app.get('/books', () => {});
app.get('/routes', (req, res) => {
res.status(200).send(endPoints(app));
});
After running the code, if you start the application, the GET /routes
API will return the following result:
[
{
"path": "/users",
"methods": […