Skip to content
Myr

Regenerated 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 cef14ca · Browse files · 2 files+92 added−163 removed
This commit

State

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

Diff stats

2 files+92 added−163 removed

  • other:+92 added−163 removed

2 files+92 added−163 removed

modified:docs/guide.1+45 added-98 removed
343 unmodified lines
344.SS "Route Middleware"345Routes 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\.346.347.P348Typically async data retrieval might look similar to below, where we take the ":id" parameter, and attempt loading a user\.348Typically async data retrieval might look similar to below, where we take the \fI:id\fR parameter, and attempt loading a user\.349.350.P351app\.get(\'/user/:id\', function(req, res, next){352.350.IP "" 4351.352.nf353354app\.get(\'/user/:id\', function(req, res, next){357 loadUser(req\.params\.id, function(err, user){358 if (err) return next(err);359 res\.send(\'Viewing user \' + user\.name);360 });355 loadUser(req\.params\.id, function(err, user){356 if (err) return next(err);357 res\.send(\'Viewing user \' + user\.name);358 });359});360.361.fi362.363.IP "" 0364.365.P367});368.369.P366To 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\.367.372.P373function loadUser(req, res, next) {374.368.IP "" 4369.370.nf371379 // You would fetch your user from the db380 var user = users[req\.params\.id];381 if (user) {382 req\.user = user;383 next();384 } else {385 next(new Error(\'Failed to load user \' + req\.params\.id));386 }387.388.fi389.390.IP "" 0391.392.P372function loadUser(req, res, next) {373 // You would fetch your user from the db374 var user = users[req\.params\.id];375 if (user) {376 req\.user = user;377 next();378 } else {379 next(new Error(\'Failed to load user \' + req\.params\.id));380 }381}394.395.P382383app\.get(\'/user/:id\', loadUser, function(req, res){384 res\.send(\'Viewing user \' + req\.user\.name);385});386.398.IP "" 4399.400.nf401402 res\.send(\'Viewing user \' + req\.user\.name);403.387.fi388.389.IP "" 0390.391.P409});410.411.P392Multiple 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\.393.414.P415function andRestrictToSelf(req, res, next) {416.394.IP "" 4395.396.nf397421 req\.authenticatedUser\.id == req\.user\.id422 ? next()423 : next(new Error(\'Unauthorized\'));424.425.fi426.427.IP "" 0428.429.P398function andRestrictToSelf(req, res, next) {399 req\.authenticatedUser\.id == req\.user\.id400 ? next()401 : next(new Error(\'Unauthorized\'));402}431.432.P403404app\.get(\'/user/:id/edit\', loadUser, andRestrictToSelf, function(req, res){405 res\.send(\'Editing user \' + req\.user\.name);406});407.435.IP "" 4436.437.nf438439 res\.send(\'Editing user \' + req\.user\.name);440.408.fi409.410.IP "" 0411.412.P446});447.448.P413Keeping in mind that middleware are simply functions, we can define function that \fIreturns\fR the middleware in order to create a more expressive and flexible solution as shown below\.414.451.P452function andRestrictTo(role) {453.415.IP "" 4416.417.nf418458 return function(req, res, next) {459 req\.authenticatedUser\.role == role460 ? next()461 : next(new Error(\'Unauthorized\'));462 }463.464.fi465.466.IP "" 0467.468.P419function andRestrictTo(role) {420 return function(req, res, next) {421 req\.authenticatedUser\.role == role422 ? next()423 : next(new Error(\'Unauthorized\'));424 }425}470.471.P426427app\.del(\'/user/:id\', loadUser, andRestrictTo(\'admin\'), function(req, res){428 res\.send(\'Deleted user \' + req\.user\.name);429});430.474.IP "" 4475.476.nf477478 res\.send(\'Deleted user \' + req\.user\.name);479.431.fi432.433.IP "" 0434.435.P485});486.487.P436Commonly used "stacks" of middleware can be passed as an array (\fIapplied recursively\fR), which can be mixed and matched to any degree\.437.490.P491var a = [middleware1, middleware2]492.438.IP "" 4439.440.nf441442var a = [middleware1, middleware2]497, b = [middleware3, middleware4]498, all = [a, b];443 , b = [middleware3, middleware4]444 , all = [a, b];445446app\.get(\'/foo\', a, function(){});447app\.get(\'/bar\', a, function(){});448449app\.get(\'/\', a, middleware3, middleware4, function(){});450app\.get(\'/\', a, b, function(){});451app\.get(\'/\', all, function(){});452.453.fi454.455.IP "" 0456.457.P505app\.get(\'/foo\', a, function(){}); app\.get(\'/bar\', a, function(){});506.507.P508app\.get(\'/\', a, middleware3, middleware4, function(){}); app\.get(\'/\', a, b, function(){}); app\.get(\'/\', all, function(){});509.510.P458For this example in full, view the route middleware example \fIhttp://github\.com/visionmedia/express/blob/master/examples/route\-middleware/app\.js\fR in the repository\.459.460.SS "HTTP Methods"461We have seen \fIapp\.get()\fR a few times, however Express also exposes other familiar HTTP verbs in the same manor, such as \fIapp\.post()\fR, \fIapp\.del()\fR, etc\.
1186 unmodified lines
modified:docs/guide.html+47 added-65 removed
457 unmodified lines
458<h3 id="Route-Middleware">Route Middleware</h3>459460<p>Routes 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.</p>461462<p>Typically async data retrieval might look similar to below, where we take the ":id" parameter, and attempt loading a user.</p>462<p>Typically async data retrieval might look similar to below, where we take the <em>:id</em> parameter, and attempt loading a user.</p>463464<p> app.get('/user/:id', function(req, res, next){</p>465466<pre><code> loadUser(req.params.id, function(err, user){467 if (err) return next(err);468 res.send('Viewing user ' + user.name);469 });464<pre><code>app.get('/user/:id', function(req, res, next){465 loadUser(req.params.id, function(err, user){466 if (err) return next(err);467 res.send('Viewing user ' + user.name);468 });469});470</code></pre>471472<p> });</p>473472<p>To 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.</p>473476<p> function loadUser(req, res, next) {</p>474<pre><code>function loadUser(req, res, next) {475 // You would fetch your user from the db476 var user = users[req.params.id];477 if (user) {478 req.user = user;479 next();480 } else {481 next(new Error('Failed to load user ' + req.params.id));482 }483}484478<pre><code> // You would fetch your user from the db479 var user = users[req.params.id];480 if (user) {481 req.user = user;482 next();483 } else {484 next(new Error('Failed to load user ' + req.params.id));485 }485app.get('/user/:id', loadUser, function(req, res){486 res.send('Viewing user ' + req.user.name);487});488</code></pre>489488<p> }</p>489490<p> app.get('/user/:id', loadUser, function(req, res){</p>491492<pre><code> res.send('Viewing user ' + req.user.name);493</code></pre>494495<p> });</p>496490<p>Multiple 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.</p>491499<p> function andRestrictToSelf(req, res, next) {</p>492<pre><code>function andRestrictToSelf(req, res, next) {493 req.authenticatedUser.id == req.user.id494 ? next()495 : next(new Error('Unauthorized'));496}497501<pre><code> req.authenticatedUser.id == req.user.id502 ? next()503 : next(new Error('Unauthorized'));498app.get('/user/:id/edit', loadUser, andRestrictToSelf, function(req, res){499 res.send('Editing user ' + req.user.name);500});501</code></pre>502506<p> }</p>507508<p> app.get('/user/:id/edit', loadUser, andRestrictToSelf, function(req, res){</p>509510<pre><code> res.send('Editing user ' + req.user.name);511</code></pre>512513<p> });</p>514503<p>Keeping in mind that middleware are simply functions, we can define function that <em>returns</em> the middleware in order to create a more expressive and flexible solution as shown below.</p>504517<p> function andRestrictTo(role) {</p>505<pre><code>function andRestrictTo(role) {506 return function(req, res, next) {507 req.authenticatedUser.role == role508 ? next()509 : next(new Error('Unauthorized'));510 }511}512519<pre><code> return function(req, res, next) {520 req.authenticatedUser.role == role521 ? next()522 : next(new Error('Unauthorized'));523 }513app.del('/user/:id', loadUser, andRestrictTo('admin'), function(req, res){514 res.send('Deleted user ' + req.user.name);515});516</code></pre>517526<p> }</p>527528<p> app.del('/user/:id', loadUser, andRestrictTo('admin'), function(req, res){</p>529530<pre><code> res.send('Deleted user ' + req.user.name);531</code></pre>532533<p> });</p>534518<p>Commonly used "stacks" of middleware can be passed as an array (<em>applied recursively</em>), which can be mixed and matched to any degree.</p>519537<p> var a = [middleware1, middleware2]</p>520<pre><code>var a = [middleware1, middleware2]521 , b = [middleware3, middleware4]522 , all = [a, b];523539<pre><code>, b = [middleware3, middleware4]540, all = [a, b];524app.get('/foo', a, function(){});525app.get('/bar', a, function(){});526527app.get('/', a, middleware3, middleware4, function(){});528app.get('/', a, b, function(){});529app.get('/', all, function(){});530</code></pre>531543<p> app.get('/foo', a, function(){});544 app.get('/bar', a, function(){});</p>545546<p> app.get('/', a, middleware3, middleware4, function(){});547 app.get('/', a, b, function(){});548 app.get('/', all, function(){});</p>549532<p>For this example in full, view the <a href="http://github.com/visionmedia/express/blob/master/examples/route-middleware/app.js">route middleware example</a> in the repository.</p>533534<h3 id="HTTP-Methods">HTTP Methods</h3>535
736 unmodified lines