Skip to content
Myr

Fixed docs

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent 090debe · Browse files · 1 file+51 added−51 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+51 added−51 removed

  • other:+51 added−51 removed

1 file+51 added−51 removed

modified:docs/guide.md+51 added-51 removed
197 unmodified lines
198### Route Middleware199200Routes may utilize route-specific middleware by passing one or more additional callbacks (or arrays) to the method. This feature is extremely useful for restricting access, loading data used by the route etc.201202Typically async data retrieval might look similar to below, where we take the ":id" parameter, and attempt loading a user. 202Typically async data retrieval might look similar to below, where we take the _:id_ parameter, and attempt loading a user. 203204 app.get('/user/:id', function(req, res, next){205 loadUser(req.params.id, function(err, user){206 if (err) return next(err);207 res.send('Viewing user ' + user.name);208 });209 });204 app.get('/user/:id', function(req, res, next){205 loadUser(req.params.id, function(err, user){206 if (err) return next(err);207 res.send('Viewing user ' + user.name);208 });209 });210211To keep things DRY and to increase readability we can apply this logic within a middleware. As you can see below, abstracting this logic into middleware allows us to reuse it, and clean up our route at the same time. 212213 function loadUser(req, res, next) {214 // You would fetch your user from the db215 var user = users[req.params.id];216 if (user) {217 req.user = user;218 next();219 } else {220 next(new Error('Failed to load user ' + req.params.id));221 }222 }213 function loadUser(req, res, next) {214 // You would fetch your user from the db215 var user = users[req.params.id];216 if (user) {217 req.user = user;218 next();219 } else {220 next(new Error('Failed to load user ' + req.params.id));221 }222 }223 224 app.get('/user/:id', loadUser, function(req, res){225 res.send('Viewing user ' + req.user.name);226 });227224 app.get('/user/:id', loadUser, function(req, res){225 res.send('Viewing user ' + req.user.name);226 });227228Multiple route middleware can be applied, and will be executed sequentially to apply further logic such as restricting access to a user account. In the example below only the authenticated user may edit his/her account.229230 function andRestrictToSelf(req, res, next) {231 req.authenticatedUser.id == req.user.id232 ? next()233 : next(new Error('Unauthorized'));234 }235 236 app.get('/user/:id/edit', loadUser, andRestrictToSelf, function(req, res){237 res.send('Editing user ' + req.user.name);238 });239240Keeping in mind that middleware are simply functions, we can define function that _returns_ the middleware in order to create a more expressive and flexible solution as shown below.241242 function andRestrictTo(role) {243 return function(req, res, next) {244 req.authenticatedUser.role == role230 function andRestrictToSelf(req, res, next) {231 req.authenticatedUser.id == req.user.id232 ? next()233 : next(new Error('Unauthorized'));247 }248 }234 }235 236 app.get('/user/:id/edit', loadUser, andRestrictToSelf, function(req, res){237 res.send('Editing user ' + req.user.name);238 });239250 app.del('/user/:id', loadUser, andRestrictTo('admin'), function(req, res){251 res.send('Deleted user ' + req.user.name);252 });240Keeping in mind that middleware are simply functions, we can define function that _returns_ the middleware in order to create a more expressive and flexible solution as shown below.241242 function andRestrictTo(role) {243 return function(req, res, next) {244 req.authenticatedUser.role == role245 ? next()246 : next(new Error('Unauthorized'));247 }248 }249 250 app.del('/user/:id', loadUser, andRestrictTo('admin'), function(req, res){251 res.send('Deleted user ' + req.user.name);252 });253254Commonly used "stacks" of middleware can be passed as an array (_applied recursively_), which can be mixed and matched to any degree.255256 var a = [middleware1, middleware2]257 , b = [middleware3, middleware4]258 , all = [a, b];256 var a = [middleware1, middleware2]257 , b = [middleware3, middleware4]258 , all = [a, b];259 260 app.get('/foo', a, function(){});261 app.get('/bar', a, function(){});262 263 app.get('/', a, middleware3, middleware4, function(){});264 app.get('/', a, b, function(){});265 app.get('/', all, function(){});266260 app.get('/foo', a, function(){});261 app.get('/bar', a, function(){});262263 app.get('/', a, middleware3, middleware4, function(){});264 app.get('/', a, b, function(){});265 app.get('/', all, function(){});266267For this example in full, view the [route middleware example](http://github.com/visionmedia/express/blob/master/examples/route-middleware/app.js) in the repository.268269### HTTP Methods270
643 unmodified lines