You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
While using express-list-endpoints, I noticed that certain middlewares are not listed in the output table. Specifically, global middlewares applied using app.use are not shown in the endpoint list, even though they are executed as expected when calling the corresponding routes.
Code to Reproduce the Issue
Here is a simplified version of the code demonstrating the issue:
// Importing express and a utility to list all the endpoints in the applicationconstexpress=require('express');constlistEndpoints=require('express-list-endpoints');constapp=express();// Middleware definitions for various purposesconstm={// Logs a welcome message and proceeds to the next middlewarewelcome(req,res,next){console.log('welcome Middleware');next();},// Global middleware for all routes under '/api'doGlobalStuff(req,res,next){console.log('doGlobalStuff Middleware');next();},// Middleware specific to user-related routesdoUserStuff(req,res,next){console.log('doUserStuff Middleware');next();},// Middleware specific to product-related routesdoProductStuff(req,res,next){console.log('doProductStuff Middleware');next();},// Middleware for some specific logic within routesdoInsideStuff(req,res,next){console.log('doInsideStuff Middleware');next();},// Middleware to handle getting user informationgetUser(req,res,next){console.log('getUser Middleware');next();},// Middleware to handle getting product informationgetProduct(req,res,next){console.log('getProduct Middleware');next();},// Final middleware to send a response back to the clientfinalSend(req,res,next){console.log('finalSend Middleware');res.send('This is my answer');next();}};// Main route /apiconstapiRouter=express.Router();apiRouter.get('/',m.welcome,m.finalSend);// Sub-route /api/usersconstusersRouter=express.Router();usersRouter.get('/myuser',m.doInsideStuff,m.getUser,m.finalSend);// Sub-route /api/productsconstproductsRouter=express.Router();productsRouter.get('/myproduct',m.doInsideStuff,m.getProduct,m.finalSend);// Mounting user-related middleware and sub-routes under '/api/users'apiRouter.use('/users',m.doUserStuff);apiRouter.use('/users',usersRouter);// Mounting product-related middleware and sub-routes under '/api/products'apiRouter.use('/products',m.doProductStuff);apiRouter.use('/products',productsRouter);// Mounting the main API middleware and routes under '/api'app.use('/api',m.doGlobalStuff);app.use('/api',apiRouter);// Log all defined routes for the applicationconsole.log('Routes:');console.table(listEndpoints(app));// Start the server on port 3000constPORT=3000;app.listen(PORT,()=>{console.log(`Server started on http://localhost:${PORT}`);});
Test the main routes
curl -X GET http://localhost:3000/api
curl -X GET http://localhost:3000/api/users/myuser
curl -X GET http://localhost:3000/api/products/myproduct
Problem Details
In the code above:
The not anonymous middlewares doGlobalStuff, doUserStuff, and doProductStuff are applied using app.use and are executed correctly when their corresponding routes are accessed.
However, when using express-list-endpoints, these middlewares are not listed or associated with any route in the generated output table.
Expected Behavior
The listEndpoints function should ideally list all middlewares that are applied to routes, including global middlewares defined with app.use.
Current Behavior
Only route-specific middlewares defined directly in router.get() are shown in the table, while global middlewares defined with app.use are not displayed.
Request
Is this the intended behavior, or is there a way to make express-list-endpoints include middlewares defined with app.use in its output?
Thank you for your work and your support!
The text was updated successfully, but these errors were encountered:
infuzz
changed the title
Middleware Not Listed by express-list-endpoints
Some Middlewares Not Listed by express-list-endpoints
Oct 4, 2024
Environment Details
Description of the Issue
While using
express-list-endpoints
, I noticed that certain middlewares are not listed in the output table. Specifically, global middlewares applied usingapp.use
are not shown in the endpoint list, even though they are executed as expected when calling the corresponding routes.Code to Reproduce the Issue
Here is a simplified version of the code demonstrating the issue:
Test the main routes
Problem Details
In the code above:
doGlobalStuff
,doUserStuff
, anddoProductStuff
are applied usingapp.use
and are executed correctly when their corresponding routes are accessed.express-list-endpoints
, these middlewares are not listed or associated with any route in the generated output table.Expected Behavior
The
listEndpoints
function should ideally list all middlewares that are applied to routes, including global middlewares defined withapp.use
.Current Behavior
Only route-specific middlewares defined directly in
router.get()
are shown in the table, while global middlewares defined withapp.use
are not displayed.Request
Is this the intended behavior, or is there a way to make
express-list-endpoints
include middlewares defined withapp.use
in its output?Thank you for your work and your support!
The text was updated successfully, but these errors were encountered: