Skip to content
Myr

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

3 files+63 added−31 removed

modified:docs/guide.1+24 added-16 removed
2 unmodified lines
3.4.TH "GUIDE" "" "July 2010" "" ""5.6.SS "Routing"7Express 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\.7Express 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.nf
6 unmodified lines
18.19.IP "" 020.21.P22Alternatively we may want to create, or \fBPOST\fR a user:23.24.IP "" 425.26.nf2728app\.post(\'/user\', function(){29 // \.\.\. logic30});31.32.fi33.34.IP "" 035.36.P22A 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 lines
81.82.fi83.84.IP "" 085.86.SS "Passing Route Control"87We 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.nf9293app\.get(\'/users/:id?\', function(req, res, params){94 if (params\.id) {95 // do something96 } else {97 next();98 }99});100101app\.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 lines
164<h3 id="Routing">Routing</h3>165166<p>Express utilizes the HTTP verbs to provide a meaningful, expressive routing API.167For example we may want to render a user's account for the path <em>/user/12</em>, this168can be done by defining the route below.</p>168can be done by defining the route below. The values associated to the named placeholders,169are passed as the <em>third</em> argument, which here we name <em>params</em>.</p>170171<pre><code>app.get('/user/:id', function(req, res, params){172 res.send('user ' + params.id);173});174</code></pre>175175<p>Alternatively we may want to create, or <strong>POST</strong> a user:</p>176177<pre><code>app.post('/user', function(){178 // ... logic179});180</code></pre>181176<p>A route is simple a string which is compiled to a <em>RegExp</em> internally. For example177when <em>/user/:id</em> is compiled, a simplified version of the regexp may look similar to:</p>178179<pre><code>\/user\/([^\/]+)\/?
35 unmodified lines
215 /products.xml216 /products217</code></pre>218219<h3 id="Passing-Route-Control">Passing Route Control</h3>220221<p>We may pass control to the next <em>matching</em> route, by calling the <em>fourth</em> parameter,222the <em>next()</em> function. When a match cannot be made, control is passed back to Connect.</p>223224<pre><code>app.get('/users/:id?', function(req, res, params){225 if (params.id) {226 // do something227 } else {228 next();229 }230});231232app.get('/users', function(req, res, params){233 // do something else234});235</code></pre>236237</div>238 </div>239 </div>240 </body>
1 unmodified line
modified:docs/guide.md+19 added-7 removed