{"action":"created","issue":{"url":"https://api.github.com/repos/strongloop/express/issues/2760","labels_url":"https://api.github.com/repos/strongloop/express/issues/2760/labels{/name}","comments_url":"https://api.github.com/repos/strongloop/express/issues/2760/comments","events_url":"https://api.github.com/repos/strongloop/express/issues/2760/events","html_url":"https://github.com/strongloop/express/issues/2760","id":107625211,"number":2760,"title":"Two routers mounted at same path do not isolate their middleware from each other","user":{"login":"mjm","id":1181,"avatar_url":"https://avatars.githubusercontent.com/u/1181?v=3","gravatar_id":"","url":"https://api.github.com/users/mjm","html_url":"https://github.com/mjm","followers_url":"https://api.github.com/users/mjm/followers","following_url":"https://api.github.com/users/mjm/following{/other_user}","gists_url":"https://api.github.com/users/mjm/gists{/gist_id}","starred_url":"https://api.github.com/users/mjm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mjm/subscriptions","organizations_url":"https://api.github.com/users/mjm/orgs","repos_url":"https://api.github.com/users/mjm/repos","events_url":"https://api.github.com/users/mjm/events{/privacy}","received_events_url":"https://api.github.com/users/mjm/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/strongloop/express/labels/4.x","name":"4.x","color":"ededed"},{"url":"https://api.github.com/repos/strongloop/express/labels/question","name":"question","color":"cc317c"}],"state":"open","locked":false,"assignee":{"login":"dougwilson","id":67512,"avatar_url":"https://avatars.githubusercontent.com/u/67512?v=3","gravatar_id":"","url":"https://api.github.com/users/dougwilson","html_url":"https://github.com/dougwilson","followers_url":"https://api.github.com/users/dougwilson/followers","following_url":"https://api.github.com/users/dougwilson/following{/other_user}","gists_url":"https://api.github.com/users/dougwilson/gists{/gist_id}","starred_url":"https://api.github.com/users/dougwilson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dougwilson/subscriptions","organizations_url":"https://api.github.com/users/dougwilson/orgs","repos_url":"https://api.github.com/users/dougwilson/repos","events_url":"https://api.github.com/users/dougwilson/events{/privacy}","received_events_url":"https://api.github.com/users/dougwilson/received_events","type":"User","site_admin":false},"milestone":null,"comments":6,"created_at":"2015-09-22T00:40:09Z","updated_at":"2015-12-11T20:03:30Z","closed_at":null,"body":"Using `router.use` seems to be documented to allow you to add middleware that will only be called for routes added to that router, but that does not seem to be working for me. The issue seems to be because I'm mounting several routers at the same path. The decision for whether a middleware is run seems to be based on the path, not the router. This was confusing.\r\n\r\nThe use case here is that my application has several modules that are mounted under the same location, as the URLs for these don't always follow a common prefix. There's common middleware that is used on most, but not all, endpoints. I was attempting to refactor the application to use an express.Router for each module, with each router being able to .use whatever middleware is common to all of its routes.\r\n\r\nThe result has been that the same middleware is called multiple times, because several routers are all using it. They should be separated, if Router worked as described. Another case is that one module used a middleware to require auth on all of the endpoints in its router, but that spilled over to any other routers used after that one.\r\n\r\nIs this intended behavior? Is there a workaround?\r\n\r\nHere is a Gist demonstrating the issue: https://gist.github.com/mjm/497a23f68826f8ca473b"},"comment":{"url":"https://api.github.com/repos/strongloop/express/issues/comments/164035553","html_url":"https://github.com/strongloop/express/issues/2760#issuecomment-164035553","issue_url":"https://api.github.com/repos/strongloop/express/issues/2760","id":164035553,"user":{"login":"andrewosh","id":1043263,"avatar_url":"https://avatars.githubusercontent.com/u/1043263?v=3","gravatar_id":"","url":"https://api.github.com/users/andrewosh","html_url":"https://github.com/andrewosh","followers_url":"https://api.github.com/users/andrewosh/followers","following_url":"https://api.github.com/users/andrewosh/following{/other_user}","gists_url":"https://api.github.com/users/andrewosh/gists{/gist_id}","starred_url":"https://api.github.com/users/andrewosh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andrewosh/subscriptions","organizations_url":"https://api.github.com/users/andrewosh/orgs","repos_url":"https://api.github.com/users/andrewosh/repos","events_url":"https://api.github.com/users/andrewosh/events{/privacy}","received_events_url":"https://api.github.com/users/andrewosh/received_events","type":"User","site_admin":false},"created_at":"2015-12-11T20:03:30Z","updated_at":"2015-12-11T20:03:30Z","body":"Just want to leave my :+1: here. Let me know if there's something I'm doing wrong, but my use case is: \r\n\r\n1. I have two routers, one uses some authorization middleware and the other does not require authorization\r\n2. Both routers are mounted on the same path, and they might be responsible for handling different methods on the same endpoint\r\n\r\nIn this scenario, I'm seeing that the authorization middleware is being applied to the route/methods being handled by the second router as well. To be more specific, in the following chunk of code, authorization is being applied to the routes in `openRouter` as well: \r\n\r\n```\r\nvar authRouter = express.Router()\r\nauthRouter.use(authHandler)\r\nvar openRouter = express.Router()\r\n\r\nauthRouter.route('/applications/:template')\r\n  .get(this.cluster.getAllApplications.bind(this.cluster))\r\nopenRouter.route('/applications/:template')\r\n  .post(this.cluster.createApplication.bind(this.cluster))\r\n\r\napp.use('/', openRouter)\r\napp.use('/', authRouter)\r\n```\r\n\r\nAny thoughts on this? "}}