Isolate middleware to invoking router
Is it possible to have middleware only be used by the router which attaches it?
It would be great to be able to do the following
var firstRouter = express.Router();
firstRouter.use(someMiddleware);
firstRouter.get("/foo", ...);
var secondRouter = express.Router();
secondRouter.get("/bar", ...);
app.use(firstRouter);
app.use(secondRouter);
and have someMiddleware called with /foo but not with /bar.
Obviously in this simplified example the routes can be easily rearranged, but in a full app there are often times when arranging routes by the middleware they require makes much less sense than arranging them by shared purpose.
For example, in our app with have a set of routes associated with users, and a set of routes associated with schools. Some of our middleware runs on a subset of each of these routes, but it much cleaner to put the users in one router and the schools in a second than to arrange them by middleware needs.
Please avoid posting app support questions. This is a bug tracker.
You would mount each router respectively under /foo and /bar to limit the scope of the router accordingly. If you mount routers at top level (with no route) then obviously they would be run for all routes.
Alternatively, you can specify a route within the firstRouter .use call.