Skip to content

express.Router with with two methods on one path does not display middleswares list correctly #81

@tsippert

Description

@tsippert

Please check the following example to understand better what I do mean by the title of this issue.

const listEndpoints = require('express-list-endpoints')
const express = require("express");

let app = require('express')();

const userRouter = express.Router();
userRouter.get('/', [function authorize(req,res){}] , function listUsers(req,res){});
userRouter.post('/', [function authorize(req,res){}], function createUser(req,res){});
userRouter.get('/:id', [function authorize(req,res){}] , function getUserById(req,res){});

const teamRouter = express.Router();
teamRouter.get('/', [function authorize(req,res){}] , function listUsers(req,res){});
teamRouter.post('/', [function authorize(req,res){}], function createUser(req,res){});
teamRouter.get('/:id', [function authorize(req,res){}] , function getUserById(req,res){});

app.use('/users', userRouter);
app.use('/teams', teamRouter);

console.log(listEndpoints(app));

The response would be the following:

[
  {
    path: '/users',
    methods: [ 'GET', 'POST' ],
    middlewares: [ 'authorize', 'listUsers' ]
  },
  {
    path: '/users/:id',
    methods: [ 'GET' ],
    middlewares: [ 'authorize', 'getUserById' ]
  },
  {
    path: '/teams',
    methods: [ 'GET', 'POST' ],
    middlewares: [ 'authorize', 'listUsers' ]
  },
  {
    path: '/teams/:id',
    methods: [ 'GET' ],
    middlewares: [ 'authorize', 'getUserById' ]
  }
]

In the first endpoint, we have the path /users, with the methods GET and POST. That is correct.
Now, the middlewares list is incorrect: we should also have createUser added in the list.

I would have two suggestions as a possibility:

  1. To have one endpoint in the list for each method (paths would be duplicated then)
  2. The list of middlewares should be associated/mapped with the method that it is associated

Thank you for the time for reading this issue, and please leave any comment if you have any questions.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions