diff --git a/_includes/api/en/4x/router-use.md b/_includes/api/en/4x/router-use.md index a4d8b78219..273fe56a57 100644 --- a/_includes/api/en/4x/router-use.md +++ b/_includes/api/en/4x/router-use.md @@ -77,3 +77,26 @@ app.use(express.static(__dirname + '/uploads')); The `router.use()` method also supports named parameters so that your mount points for other routers can benefit from preloading using named parameters. + +__NOTE__: Although these middleware functions are added via a particular router, _when_ +they run is defined by the path they are attached to (not the router). Therefore a piece +of middleware added via one router may run for routes of other routers if its routes +match. For example, if two different routers are mounted on the same path: + +{% highlight js %} +var authRouter = express.Router(); +var openRouter = express.Router(); + +authRouter.use(require('./authenticate').basic(usersdb)); + +authRouter.get('/:user_id/edit', function(req, res, next) { ... edit user ui ... }); +openRouter.get('/', function(req, res, next) { ... list users ... }) +openRouter.get('/:user_id', function(req, res, next) { ... view user ... }) + +app.use('/users', authRouter); +app.use('/users', openRouter); +{% endhighlight %} + +Even though the authentication middleware was added via the `authRouter` it will run on the routes defined by the `openRouter` as well since both routers were mounted on `/users`. + +If this behavior is not desired then the paths must be different for each router.