Skip to content
Myr

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 file

Take it

Started guideTj Holowaychukbefore adoption9e3a0c6

lines 1–167why, one step back
1<html>
2 <head>3 <title>Express - node web framework</title>4 <style>5 #header {6 position: absolute;7 top: 10px;8 left: 0;9 padding: 12px 0;10 text-indent: 40px;11 width: 100%;12 border-top: 1px solid rgba(0,0,0,0.7);13 border-bottom: 1px solid rgba(0,0,0,0.7);14 background: rgba(255,255,255,0.1) url(http://www.sencha.com/favicon.ico) no-repeat 15px 50%;15 text-align: left;16 color: #fff;17 }18 #tagline {19 margin-left: 75px;20 margin-bottom: 30px;21 color: rgba(255,255,255,0.7); }22 html {23 background: #1c1c1c url(images/bg.tile.jpg); }2425 body {26 margin: 0;27 padding-bottom: 30px;28 font: 14px/1.4 "Helvetica Neue", "Lucida Grande", "Arial";29 font-size: 14px;30 line-height: 1.5;31 -webkit-text-stroke: 1px rgba(0, 0, 0, 0.1);32 -moz-text-stroke: 1px rgba(0, 0, 0, 0.1);33 background: url(images/bg.jpg) 50% 0 no-repeat;34 color: #8b8b8b; }3536 * {37 outline: none; }3839 em {40 color: white; }4142 a img {43 border: none !important; }4445 a {46 font-weight: bold;47 color: white;48 -webkit-transition-property: opacity, -webkit-transform, color, background-color, padding, -webkit-box-shadow;49 -webkit-transition-duration: 0.15s;50 -webkit-transition-timing-function: ease-out; }51 a:hover {52 opacity: 0.8; }5354 h1, h2, h3 {55 margin: 45px 0 0 0;56 color: white;57 text-shadow: 1px 2px 2px rgba(0,0,0,0.6); }5859 h3 {60 margin-left: 20px;61 font-size: 18px; }6263 pre {64 margin: 20px 10px;65 padding: 25px 20px;66 background: rgba(0,0,0,0.5);67 border: 1px solid #323232;68 -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);69 -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);70 -webkit-border-radius: 5px;71 -moz-border-radius: 5px; }7273 code {74 font-family: "Helvetica Neue", "Lucida Grande", "Arial"; }7576 ul {77 margin: 15px 0;78 padding: 0 0 0 35px; }79 ul li {80 margin: 0;81 padding: 2px 0;82 list-style: square; }8384 .sect {85 margin-left: 40px; }8687 #logo {88 display: block;89 margin-left: 30%;90 margin-bottom: 30px;91 width: 194px;92 height: 51px;93 background: url(images/logo.png) 0 0 no-repeat;94 text-indent: -99999px; }95 #logo:hover {96 opacity: 0.7; }97 #logo:active {98 opacity: 0.3; }99100 #ribbon {101 position: fixed;102 top: 0;103 right: 0;104 z-index: 2; }105106 #wrapper {107 width: 100%;108 min-height: 800px;109 background: url(images/top.png) 0 0 repeat-x; }110111 #container {112 margin: 0 auto;113 padding-top: 110px;114 width: 550px; }115116 #menu {117 margin-left: 130px;118 padding: 0;119 padding-bottom: 30px; }120 #menu li {121 display: inline;122 list-style: none; }123 #menu li a {124 display: block;125 float: left;126 margin: 0 2px;127 padding: 3px 15px;128 background: rgba(0,0,0,0.2);129 -webkit-border-radius: 8px;130 -moz-border-radius: 8px;131 -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);132 -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);133 -webkit-transition-property: opacity, -webkit-transform, color, background-color, padding, -webkit-box-shadow;134 -webkit-transition-duration: 0.15s;135 -webkit-transition-timing-function: ease-out; }136 #menu li a:hover,137 #menu li a.active {138 padding: 3px 20px;139 background: rgba(0,0,0,0.5); }140 #menu li a:active {141 background: rgba(0,0,0,0.1);142 -webkit-box-shadow: 1px 1px 1px rgba(0,0,0,0.4);143 -moz-box-shadow: 1px 1px 1px rgba(0,0,0,0.4); }144 </style>145 </head>146 <body>147 <a href='http://github.com/visionmedia/express'> 148 <img alt='Fork me on GitHub' id='ribbon' src='http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png' /> 149 </a>150 <div id="header"><strong>Sencha</strong> labs</div>151 <div id="wrapper">152 <div id="container">153 <a href='http://github.com/visionmedia/express' id='logo'>Express</a> 154 <p id="tagline">155 High performance, high class web development for156 <a href="http://nodejs.org">Node.js</a>157 </p>158 <ul id="menu">159 <li><a href="index.html">Home</a></li>160 <li><a href="guide.html">Guide</a></li>161 <li><a href="migrate.html">1.x Migration</a></li>162 </ul>163<div class='mp'>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>, this

Route passing docsTj Holowaychukbefore adoption0ace97c

lines 168–169why, one step back
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>

Started guideTj Holowaychukbefore adoption9e3a0c6

lines 170–216why, one step back
170
171<pre><code>app.get('/user/:id', function(req, res, params){172 res.send('user ' + params.id);173});174</code></pre>175176<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\/([^\/]+)\/?180</code></pre>181182<p>Literal regular expressions may also be passed for complex uses:</p>183184<pre><code>app.get(/^\/foo(bar)?$/, function(){});185</code></pre>186187<p>Below are some route examples, and the associated paths that they188may consume:</p>189190<pre><code> "/user/:id"191 /user/12192193 "/users/:id?"194 /users/5195 /users196197 "/files/*"198 /files/jquery.js199 /files/javascripts/jquery.js200201 "/file/*.*"202 /files/jquery.js203 /files/javascripts/jquery.js204205 "/user/:id/:operation?"206 /user/1207 /user/1/edit208209 "/products.:format"210 /products.json211 /products.xml212213 "/products.:format?"214 /products.json215 /products.xml216 /products

Route passing docsTj Holowaychukbefore adoption0ace97c

lines 217–234why, one step back
217</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});

Started guideTj Holowaychukbefore adoption9e3a0c6

lines 235–241why, one step back
235</code></pre>
236237</div>238 </div>239 </div>240 </body>241</html>