Subhi Samara
Subhi Samara

Reputation: 87

List of all routes in express app (express-promise-router)

In my backend app I am using express-promise-router to manage my routes. For example:

const express = require( 'express' );
const router = require( 'express-promise-router' )();
let app = express();
app.use( router.get( "/hc" , ( req,res,next )=> res.status( 200 ).send( "healthy!" ) ) );

I want to list all the routes in the backend app. since I am using the express-promise-router, I can't use the app._router.stack

The problem I am facing is that I used "sub-routing":

app.use('/api/v1', require('./router'));

in router.js:

router.use('/api/v1/user', require('./user_r'));

in user_r.js:

router.get(...)

all the dependencies I tried, such as express-list-routes and express-list-endpoints weren't useful because of the hierarchy.

I did though try suggestions from here:

Any suggestions how to get it done?

Upvotes: 0

Views: 3456

Answers (2)

Etienne Martin
Etienne Martin

Reputation: 11619

I've used the package express-list-endpoints in the past, worked great.

Usage:

const express = require("express");
const expressPromiseRouter = require("express-promise-router");
const listEndpoints = require("express-list-endpoints");

const app = express();
const router = expressPromiseRouter();

router.get("/ping", (req, res) => res.send("pong!"));

app.use(router);

console.log(listEndpoints(app));

Upvotes: 1

boolfalse
boolfalse

Reputation: 2131

You can use express-list-endpoints module for that:

const listEndpoints = require('express-list-endpoints');
let app = require('express')();
// ... app usage
console.log(listEndpoints(app));

Also, may you already seen these, but this can be useful later:

Upvotes: 0

Related Questions