Reputation: 9402
I have a module implementing a service that is using some middleware that it needs to do some processing, e.g.
module.exports = function(options) {
...
app.use(busboy())
...
return module
}
If I have another module that needs to use the same middleware, it is going to make the same call. Each module is a plug-in and doesn't know about any of the others, and it doesn't seem like good design to create a global variable for each piece of middleware that a plug-in might use. And per the answer to this question, it doesn't seem like a simple matter of checking if the middleware is already loaded.
Right now, no piece of middleware is used in more than one place, but if in the future it does, is this going to cause a problem? If so, what is the solution?
Upvotes: 0
Views: 272
Reputation: 203231
If you need to use particular middleware for particular routers/routes, include it for just those routers/routes, instead of application-wide.
So instead of this:
app.use(middleware);
You can use this:
let router = express.Router();
router.use(middleware);
router.get('/', ...);
// and return/export the router
Using routers also makes your app more modular.
Upvotes: 1