labithiotis / express-list-routes

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

express-list-routes

List all routes used in Express[3,4,5]

NPM Version NPM Downloads

Example App

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);

List all Routes with prefix

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

Or only log out nested router routes

expressListRoutes(router);
// Logs out the following:
// POST   /admin/user
// GET    /admin/user
// PUT    /admin/user

Use combined paths to pragmatically do something

expressListRoutes returns array of all routes found in express.

const paths = expressListRoutes(req.app, { logger: false });
paths.forEach((endpoint) => {
  if (endpoint.path.endsWith('/')) {
    ...
  }
});

Installation

npm install express-list-routes

Options

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
  }

FAQ

Errors with importing this library You may need to enable esModuleInterop in your tsconfig.json to support default exports.

About


Languages

Language:JavaScript 100.0%