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:
- To have one endpoint in the list for each method (paths would be duplicated then)
- 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.
Please check the following example to understand better what I do mean by the title of this issue.
The response would be the following:
In the first endpoint, we have the path
/users, with the methodsGETandPOST. That is correct.Now, the middlewares list is incorrect: we should also have
createUseradded in the list.I would have two suggestions as a possibility:
Thank you for the time for reading this issue, and please leave any comment if you have any questions.