Skip to content
Myr

guide.1

This file

Path

docs/guide.1

108 lines · 1.9 KiB

Last changed by

Route passing docsTj Holowaychukbefore adoption0ace97c

History of this file

Take it

Started guideTj Holowaychukbefore adoption9e3a0c6

lines 1–6why, one step back
1.\" generated with Ronn/v0.6.6
2.\" http://github.com/rtomayko/ronn/3.4.TH "GUIDE" "" "July 2010" "" ""5.6.SS "Routing"

Route passing docsTj Holowaychukbefore adoption0ace97c

line 7why, one step back
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\.

Started guideTj Holowaychukbefore adoption9e3a0c6

lines 8–80why, one step back
8.
9.IP "" 410.11.nf1213app\.get(\'/user/:id\', function(req, res, params){14 res\.send(\'user \' + params\.id);15});16.17.fi18.19.IP "" 020.21.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.26.nf2728\\/user\\/([^\\/]+)\\/?29.30.fi31.32.IP "" 033.34.P35Literal regular expressions may also be passed for complex uses:36.37.IP "" 438.39.nf4041app\.get(/^\\/foo(bar)?$/, function(){});42.43.fi44.45.IP "" 046.47.P48Below are some route examples, and the associated paths that they may consume:49.50.IP "" 451.52.nf5354 \"/user/:id\"55 /user/125657 \"/users/:id?\"58 /users/559 /users6061 \"/files/*\"62 /files/jquery\.js63 /files/javascripts/jquery\.js6465 \"/file/*\.*\"66 /files/jquery\.js67 /files/javascripts/jquery\.js6869 \"/user/:id/:operation?\"70 /user/171 /user/1/edit7273 \"/products\.:format\"74 /products\.json75 /products\.xml7677 \"/products\.:format?\"78 /products\.json79 /products\.xml80 /products

Route passing docsTj Holowaychukbefore adoption0ace97c

lines 81–103why, one step back
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});

Started guideTj Holowaychukbefore adoption9e3a0c6

lines 104–108why, one step back
104.
105.fi106.107.IP "" 0108