Route passing 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 9e3a0c6 · Browse files · 3 files·+63 added−31 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+63 added−31 removed
- other:+63 added−31 removed
Provenance
- Commit
0ace97c- Tree
58a6136- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
9e3a0c6
3 files changed+63 added−31 removed
modified:docs/guide.1+24 added-16 removed
2 unmodified lines3 .4 .TH "GUIDE" "" "July 2010" "" ""5 .6 .SS "Routing"7-Express utilizes the HTTP verbs to provide a meaningful, expressive routing API\. For example we may want to render a user\'s account for the path \fI/user/12\fR, this can be done by defining the route below\.7+Express utilizes the HTTP verbs to provide a meaningful, expressive routing API\. For example we may want to render a user\'s account for the path \fI/user/12\fR, this can be done by defining the route below\. The values associated to the named placeholders, are passed as the \fIthird\fR argument, which here we name \fIparams\fR\.8 .9 .IP "" 410 .11 .nf6 unmodified lines18 .19 .IP "" 020 .21 .P22-Alternatively we may want to create, or \fBPOST\fR a user:23-.24-.IP "" 425-.26-.nf27-28-app\.post(\'/user\', function(){29- // \.\.\. logic30-});31-.32-.fi33-.34-.IP "" 035-.36-.P22 A route is simple a string which is compiled to a \fIRegExp\fR internally\. For example when \fI/user/:id\fR is compiled, a simplified version of the regexp may look similar to:23 .24 .IP "" 425 .55 unmodified lines81 .82 .fi83 .84 .IP "" 085+.86+.SS "Passing Route Control"87+We may pass control to the next \fImatching\fR route, by calling the \fIfourth\fR parameter, the \fInext()\fR function\. When a match cannot be made, control is passed back to Connect\.88+.89+.IP "" 490+.91+.nf92 93+app\.get(\'/users/:id?\', function(req, res, params){94+ if (params\.id) {95+ // do something96+ } else {97+ next();98+ }99+});100+101+app\.get(\'/users\', function(req, res, params){102+ // do something else103+});104+.105+.fi106+.107+.IP "" 0108+modified:docs/guide.html+20 added-8 removed
163 unmodified lines164 <h3 id="Routing">Routing</h3>165 166 <p>Express utilizes the HTTP verbs to provide a meaningful, expressive routing API.167 For example we may want to render a user's account for the path <em>/user/12</em>, this168-can be done by defining the route below.</p>168+can be done by defining the route below. The values associated to the named placeholders,169+are passed as the <em>third</em> argument, which here we name <em>params</em>.</p>170 171 <pre><code>app.get('/user/:id', function(req, res, params){172 res.send('user ' + params.id);173 });174 </code></pre>175 175-<p>Alternatively we may want to create, or <strong>POST</strong> a user:</p>176-177-<pre><code>app.post('/user', function(){178- // ... logic179-});180-</code></pre>181-176 <p>A route is simple a string which is compiled to a <em>RegExp</em> internally. For example177 when <em>/user/:id</em> is compiled, a simplified version of the regexp may look similar to:</p>178 179 <pre><code>\/user\/([^\/]+)\/?35 unmodified lines215 /products.xml216 /products217 </code></pre>218 219+<h3 id="Passing-Route-Control">Passing Route Control</h3>220+221+<p>We may pass control to the next <em>matching</em> route, by calling the <em>fourth</em> parameter,222+the <em>next()</em> function. When a match cannot be made, control is passed back to Connect.</p>223+224+<pre><code>app.get('/users/:id?', function(req, res, params){225+ if (params.id) {226+ // do something227+ } else {228+ next();229+ }230+});231+232+app.get('/users', function(req, res, params){233+ // do something else234+});235+</code></pre>236+237 </div>238 </div>239 </div>240 </body>1 unmodified linemodified:docs/guide.md+19 added-7 removed
12345667891011121213141516171314151631 unmodified lines484950515253545556575859606162636465666768
### Routing
Express utilizes the HTTP verbs to provide a meaningful, expressive routing API.For example we may want to render a user's account for the path _/user/12_, thiscan be done by defining the route below. can be done by defining the route below. The values associated to the named placeholders,are passed as the _third_ argument, which here we name _params_.
app.get('/user/:id', function(req, res, params){ res.send('user ' + params.id); });
Alternatively we may want to create, or **POST** a user:
app.post('/user', function(){ // ... logic });
A route is simple a string which is compiled to a _RegExp_ internally. For examplewhen _/user/:id_ is compiled, a simplified version of the regexp may look similar to:
\/user\/([^\/]+)\/?31 unmodified lines "/products.:format?" /products.json /products.xml /products
### Passing Route Control
We may pass control to the next _matching_ route, by calling the _fourth_ parameter,the _next()_ function. When a match cannot be made, control is passed back to Connect.
app.get('/users/:id?', function(req, res, params){ if (params.id) { // do something } else { next(); } }); app.get('/users', function(req, res, params){ // do something else });No newline at end of file