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
Provenance
- Commit
0faac20- Tree
244afea- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
cef14ca
2 files changed+92 added−163 removed
modified:docs/guide.1+45 added-98 removed
343 unmodified lines344 .SS "Route Middleware"345 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\.346 .347 .P348-Typically async data retrieval might look similar to below, where we take the ":id" parameter, and attempt loading a user\.348+Typically async data retrieval might look similar to below, where we take the \fI:id\fR parameter, and attempt loading a user\.349 .350-.P351-app\.get(\'/user/:id\', function(req, res, next){352-.350 .IP "" 4351 .352 .nf353 354+app\.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-.P366 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\.367 .372-.P373-function loadUser(req, res, next) {374-.368 .IP "" 4369 .370 .nf371 379- // 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-.P372+function 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-.P382+383 app\.get(\'/user/:id\', loadUser, function(req, res){384+ res\.send(\'Viewing user \' + req\.user\.name);385+});386 .398-.IP "" 4399-.400-.nf401-402- res\.send(\'Viewing user \' + req\.user\.name);403-.387 .fi388 .389 .IP "" 0390 .391 .P409-});410-.411-.P392 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\.393 .414-.P415-function andRestrictToSelf(req, res, next) {416-.394 .IP "" 4395 .396 .nf397 421- req\.authenticatedUser\.id == req\.user\.id422- ? next()423- : next(new Error(\'Unauthorized\'));424-.425-.fi426-.427-.IP "" 0428-.429-.P398+function andRestrictToSelf(req, res, next) {399+ req\.authenticatedUser\.id == req\.user\.id400+ ? next()401+ : next(new Error(\'Unauthorized\'));402 }431-.432-.P403+404 app\.get(\'/user/:id/edit\', loadUser, andRestrictToSelf, function(req, res){405+ res\.send(\'Editing user \' + req\.user\.name);406+});407 .435-.IP "" 4436-.437-.nf438-439- res\.send(\'Editing user \' + req\.user\.name);440-.408 .fi409 .410 .IP "" 0411 .412 .P446-});447-.448-.P413 Keeping 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-.P452-function andRestrictTo(role) {453-.415 .IP "" 4416 .417 .nf418 458- return function(req, res, next) {459- req\.authenticatedUser\.role == role460- ? next()461- : next(new Error(\'Unauthorized\'));462- }463-.464-.fi465-.466-.IP "" 0467-.468-.P419+function andRestrictTo(role) {420+ return function(req, res, next) {421+ req\.authenticatedUser\.role == role422+ ? next()423+ : next(new Error(\'Unauthorized\'));424+ }425 }470-.471-.P426+427 app\.del(\'/user/:id\', loadUser, andRestrictTo(\'admin\'), function(req, res){428+ res\.send(\'Deleted user \' + req\.user\.name);429+});430 .474-.IP "" 4475-.476-.nf477-478- res\.send(\'Deleted user \' + req\.user\.name);479-.431 .fi432 .433 .IP "" 0434 .435 .P485-});486-.487-.P436 Commonly used "stacks" of middleware can be passed as an array (\fIapplied recursively\fR), which can be mixed and matched to any degree\.437 .490-.P491-var a = [middleware1, middleware2]492-.438 .IP "" 4439 .440 .nf441 442+var a = [middleware1, middleware2]497-, b = [middleware3, middleware4]498-, all = [a, b];443+ , b = [middleware3, middleware4]444+ , all = [a, b];445+446+app\.get(\'/foo\', a, function(){});447+app\.get(\'/bar\', a, function(){});448+449+app\.get(\'/\', a, middleware3, middleware4, function(){});450+app\.get(\'/\', a, b, function(){});451+app\.get(\'/\', all, function(){});452 .453 .fi454 .455 .IP "" 0456 .457 .P505-app\.get(\'/foo\', a, function(){}); app\.get(\'/bar\', a, function(){});506-.507-.P508-app\.get(\'/\', a, middleware3, middleware4, function(){}); app\.get(\'/\', a, b, function(){}); app\.get(\'/\', all, function(){});509-.510-.P458 For 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"461 We 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 linesmodified:docs/guide.html+47 added-65 removed
457 unmodified lines458 <h3 id="Route-Middleware">Route Middleware</h3>459 460 <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>461 462-<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>463 464-<p> app.get('/user/:id', function(req, res, next){</p>465-466-<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>471 472-<p> });</p>473-472 <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>473 476-<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+}484 478-<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- }485+app.get('/user/:id', loadUser, function(req, res){486+ res.send('Viewing user ' + req.user.name);487+});488 </code></pre>489 488-<p> }</p>489-490-<p> app.get('/user/:id', loadUser, function(req, res){</p>491-492-<pre><code> res.send('Viewing user ' + req.user.name);493-</code></pre>494-495-<p> });</p>496-490 <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>491 499-<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+}497 501-<pre><code> req.authenticatedUser.id == req.user.id502- ? next()503- : next(new Error('Unauthorized'));498+app.get('/user/:id/edit', loadUser, andRestrictToSelf, function(req, res){499+ res.send('Editing user ' + req.user.name);500+});501 </code></pre>502 506-<p> }</p>507-508-<p> app.get('/user/:id/edit', loadUser, andRestrictToSelf, function(req, res){</p>509-510-<pre><code> res.send('Editing user ' + req.user.name);511-</code></pre>512-513-<p> });</p>514-503 <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>504 517-<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+}512 519-<pre><code> return function(req, res, next) {520- req.authenticatedUser.role == role521- ? next()522- : next(new Error('Unauthorized'));523- }513+app.del('/user/:id', loadUser, andRestrictTo('admin'), function(req, res){514+ res.send('Deleted user ' + req.user.name);515+});516 </code></pre>517 526-<p> }</p>527-528-<p> app.del('/user/:id', loadUser, andRestrictTo('admin'), function(req, res){</p>529-530-<pre><code> res.send('Deleted user ' + req.user.name);531-</code></pre>532-533-<p> });</p>534-518 <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>519 537-<p> var a = [middleware1, middleware2]</p>520+<pre><code>var a = [middleware1, middleware2]521+ , b = [middleware3, middleware4]522+ , all = [a, b];523 539-<pre><code>, b = [middleware3, middleware4]540-, all = [a, b];524+app.get('/foo', a, function(){});525+app.get('/bar', a, function(){});526+527+app.get('/', a, middleware3, middleware4, function(){});528+app.get('/', a, b, function(){});529+app.get('/', all, function(){});530 </code></pre>531 543-<p> app.get('/foo', a, function(){});544- app.get('/bar', a, function(){});</p>545-546-<p> app.get('/', a, middleware3, middleware4, function(){});547- app.get('/', a, b, function(){});548- app.get('/', all, function(){});</p>549-532 <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>533 534 <h3 id="HTTP-Methods">HTTP Methods</h3>535 736 unmodified lines