Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some Middlewares Not Listed by express-list-endpoints #98

Open
infuzz opened this issue Oct 4, 2024 · 0 comments
Open

Some Middlewares Not Listed by express-list-endpoints #98

infuzz opened this issue Oct 4, 2024 · 0 comments

Comments

@infuzz
Copy link

infuzz commented Oct 4, 2024

Environment Details

  • express: 4.21.0
  • express-list-endpoints: 7.1.0
  • node: v21.7.3

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 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 application
const express = require('express');
const listEndpoints = require('express-list-endpoints');

const app = express();

// Middleware definitions for various purposes
const m = {
  // Logs a welcome message and proceeds to the next middleware
  welcome(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 routes
  doUserStuff(req, res, next) {
    console.log('doUserStuff Middleware');
    next();
  },
  // Middleware specific to product-related routes
  doProductStuff(req, res, next) {
    console.log('doProductStuff Middleware');
    next();
  },
  // Middleware for some specific logic within routes
  doInsideStuff(req, res, next) {
    console.log('doInsideStuff Middleware');
    next();
  },
  // Middleware to handle getting user information
  getUser(req, res, next) {
    console.log('getUser Middleware');
    next();
  },
  // Middleware to handle getting product information
  getProduct(req, res, next) {
    console.log('getProduct Middleware');
    next();
  },
  // Final middleware to send a response back to the client
  finalSend(req, res, next) {
    console.log('finalSend Middleware');
    res.send('This is my answer');
    next();
  }
};

// Main route /api
const apiRouter = express.Router();
apiRouter.get('/', m.welcome, m.finalSend);

// Sub-route /api/users
const usersRouter = express.Router();
usersRouter.get('/myuser', m.doInsideStuff, m.getUser, m.finalSend);

// Sub-route /api/products
const productsRouter = 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 application
console.log('Routes:');
console.table(listEndpoints(app));

// Start the server on port 3000
const PORT = 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!

@infuzz infuzz changed the title Middleware Not Listed by express-list-endpoints Some Middlewares Not Listed by express-list-endpoints Oct 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant