kogosoftwarellc / open-api

A Monorepo of various packages to power OpenAPI in node
MIT License
892 stars 235 forks source link

[express-openapi] Apply middleware to all endpoints on a route #781

Closed LukDrews closed 2 years ago

LukDrews commented 2 years ago

I know how I can add a middleware to all routes and how I can add one to just one route. But I want to add a middleware for all endpoints on a route by adding it to a common base path.

These are my endpoints:

/api/server/a1/info
/api/server/a1/login
...
/api/server/g23/info
/api/server/g23/login

I want to define a middleware for server/* only once and all child endpoints should use it. Is this possible?

LukDrews commented 2 years ago

This question is more related to express itself. You would have to create an extra router at the common path and add the middleware to it. Then you have to mount the router to the app. And voila, all routes share the same middleware.