guide.html
This file
Path
docs/guide.html
241 lines · 6.1 KiB · HTML
Last changed by
Route passing docsTj Holowaychukbefore adoption0ace97c
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242<html> <head> <title>Express - node web framework</title> <style> #header { position: absolute; top: 10px; left: 0; padding: 12px 0; text-indent: 40px; width: 100%; border-top: 1px solid rgba(0,0,0,0.7); border-bottom: 1px solid rgba(0,0,0,0.7); background: rgba(255,255,255,0.1) url(http://www.sencha.com/favicon.ico) no-repeat 15px 50%; text-align: left; color: #fff; } #tagline { margin-left: 75px; margin-bottom: 30px; color: rgba(255,255,255,0.7); } html { background: #1c1c1c url(images/bg.tile.jpg); }
body { margin: 0; padding-bottom: 30px; font: 14px/1.4 "Helvetica Neue", "Lucida Grande", "Arial"; font-size: 14px; line-height: 1.5; -webkit-text-stroke: 1px rgba(0, 0, 0, 0.1); -moz-text-stroke: 1px rgba(0, 0, 0, 0.1); background: url(images/bg.jpg) 50% 0 no-repeat; color: #8b8b8b; }
* { outline: none; }
em { color: white; }
a img { border: none !important; }
a { font-weight: bold; color: white; -webkit-transition-property: opacity, -webkit-transform, color, background-color, padding, -webkit-box-shadow; -webkit-transition-duration: 0.15s; -webkit-transition-timing-function: ease-out; } a:hover { opacity: 0.8; }
h1, h2, h3 { margin: 45px 0 0 0; color: white; text-shadow: 1px 2px 2px rgba(0,0,0,0.6); }
h3 { margin-left: 20px; font-size: 18px; }
pre { margin: 20px 10px; padding: 25px 20px; background: rgba(0,0,0,0.5); border: 1px solid #323232; -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6); -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6); -webkit-border-radius: 5px; -moz-border-radius: 5px; }
code { font-family: "Helvetica Neue", "Lucida Grande", "Arial"; }
ul { margin: 15px 0; padding: 0 0 0 35px; } ul li { margin: 0; padding: 2px 0; list-style: square; }
.sect { margin-left: 40px; }
#logo { display: block; margin-left: 30%; margin-bottom: 30px; width: 194px; height: 51px; background: url(images/logo.png) 0 0 no-repeat; text-indent: -99999px; } #logo:hover { opacity: 0.7; } #logo:active { opacity: 0.3; }
#ribbon { position: fixed; top: 0; right: 0; z-index: 2; }
#wrapper { width: 100%; min-height: 800px; background: url(images/top.png) 0 0 repeat-x; }
#container { margin: 0 auto; padding-top: 110px; width: 550px; }
#menu { margin-left: 130px; padding: 0; padding-bottom: 30px; } #menu li { display: inline; list-style: none; } #menu li a { display: block; float: left; margin: 0 2px; padding: 3px 15px; background: rgba(0,0,0,0.2); -webkit-border-radius: 8px; -moz-border-radius: 8px; -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6); -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6); -webkit-transition-property: opacity, -webkit-transform, color, background-color, padding, -webkit-box-shadow; -webkit-transition-duration: 0.15s; -webkit-transition-timing-function: ease-out; } #menu li a:hover, #menu li a.active { padding: 3px 20px; background: rgba(0,0,0,0.5); } #menu li a:active { background: rgba(0,0,0,0.1); -webkit-box-shadow: 1px 1px 1px rgba(0,0,0,0.4); -moz-box-shadow: 1px 1px 1px rgba(0,0,0,0.4); } </style> </head> <body> <a href='http://github.com/visionmedia/express'> <img alt='Fork me on GitHub' id='ribbon' src='http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png' /> </a> <div id="header"><strong>Sencha</strong> labs</div> <div id="wrapper"> <div id="container"> <a href='http://github.com/visionmedia/express' id='logo'>Express</a> <p id="tagline"> High performance, high class web development for <a href="http://nodejs.org">Node.js</a> </p> <ul id="menu"> <li><a href="index.html">Home</a></li> <li><a href="guide.html">Guide</a></li> <li><a href="migrate.html">1.x Migration</a></li> </ul><div class='mp'><h3 id="Routing">Routing</h3>
<p>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 <em>/user/12</em>, thiscan be done by defining the route below. The values associated to the named placeholders,are passed as the <em>third</em> argument, which here we name <em>params</em>.</p>
<pre><code>app.get('/user/:id', function(req, res, params){ res.send('user ' + params.id);});</code></pre>
<p>A route is simple a string which is compiled to a <em>RegExp</em> internally. For examplewhen <em>/user/:id</em> is compiled, a simplified version of the regexp may look similar to:</p>
<pre><code>\/user\/([^\/]+)\/?</code></pre>
<p>Literal regular expressions may also be passed for complex uses:</p>
<pre><code>app.get(/^\/foo(bar)?$/, function(){});</code></pre>
<p>Below are some route examples, and the associated paths that theymay consume:</p>
<pre><code> "/user/:id" /user/12
"/users/:id?" /users/5 /users
"/files/*" /files/jquery.js /files/javascripts/jquery.js
"/file/*.*" /files/jquery.js /files/javascripts/jquery.js
"/user/:id/:operation?" /user/1 /user/1/edit
"/products.:format" /products.json /products.xml
"/products.:format?" /products.json /products.xml /products</code></pre>
<h3 id="Passing-Route-Control">Passing Route Control</h3>
<p>We may pass control to the next <em>matching</em> route, by calling the <em>fourth</em> parameter,the <em>next()</em> function. When a match cannot be made, control is passed back to Connect.</p>
<pre><code>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});</code></pre>
</div> </div> </div> </body></html>