0

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?

Subhi Samara
  • 77
  • 1
  • 2
  • 10
  • You'd need to rely on lib internals for that, and I'm not sure if it's possible to do. Probably available on router object somewhere, e.g. router.stack. What's the purpose? If you just need to know them, just find the with regex. If you need a list at runtime, write a helper that wraps around app.use( router... and keeps an array of routes. – Estus Flask Nov 23 '21 at 21:37

2 Answers2

0

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("/hc", (req, res) => res.status(200).send("healthy!"));

app.use(router);

console.log(listEndpoints(app));
Etienne Martin
  • 8,274
  • 3
  • 32
  • 45
0

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:

boolfalse
  • 1,431
  • 2
  • 11
  • 10