1.0.0 • Published 4 years ago

@forgdev/get-routes-express v1.0.0

Weekly downloads
1
License
MIT
Repository
-
Last release
4 years ago

get-routes-express

get-routes-express gets all routes from an Express application.

Installation

$ npm install @forgdev/get-routes-express

Quick start

First you need to add a reference to get-routes to your application:

const { getRoutes } = require('@forgdev/get-routes-express');

If you use TypeScript, use the following code instead:

import { getRoutes } from '@forgdev/get-routes-express';

Then, call the getRoutes function with an Express app to get a list of all registered routes:

Usage

import express from 'express';
import { getRoutes } from '@forgdev/get-routes-express';

const app = express();

app.get('/users', (_: any, res: any) => res.send(['Juan']));
app.get('/users/:id', (_: any, res: any) => res.send('Juan'));
app.post('/users', (_: any, res: any) => res.send(['Juan']));

app.get('/roles', (_: any, res: any) => res.send(['admin']));
app.get('/info', (_: any, res: any) => res.send('info'));

app.get('/routes', (_: any, res: any) =>
  res.send(JSON.stringify(getRoutes(app))),
);

app.listen(3000);
// GET /routes
[
  {
    "method": "GET",
    "path": "/users"
  },
  {
    "method": "GET",
    "path": "/users/:id"
  },
  {
    "method": "POST",
    "path": "/users"
  },
  {
    "method": "GET",
    "path": "/roles"
  },
  {
    "method": "GET",
    "path": "/info"
  },
  {
    "method": "GET",
    "path": "/routes"
  }
]