guide.html
This file
Path
docs/guide.html
561 lines · 16.7 KiB · HTML
Last changed by
Setting up TOCTj Holowaychukbefore adoption3e15ef1
History of this fileTake it
Started guideTj Holowaychukbefore adoption9e3a0c6
lines 1–114why, one step back1<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 font-size: 18px; }6162 pre {63 margin: 20px 10px;64 padding: 25px 20px;65 background: rgba(0,0,0,0.5);66 border: 1px solid #323232;67 -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);68 -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);69 -webkit-border-radius: 5px;70 -moz-border-radius: 5px; }7172 code {73 font-family: "Helvetica Neue", "Lucida Grande", "Arial"; }7475 ul {76 margin: 15px 0;77 padding: 0 0 0 35px; }78 ul li {79 margin: 0;80 padding: 2px 0;81 list-style: square; }8283 .sect {84 margin-left: 40px; }8586 #logo {87 display: block;88 margin-left: 30%;89 margin-bottom: 30px;90 width: 194px;91 height: 51px;92 background: url(images/logo.png) 0 0 no-repeat;93 text-indent: -99999px; }94 #logo:hover {95 opacity: 0.7; }96 #logo:active {97 opacity: 0.3; }9899 #ribbon {100 position: fixed;101 top: 0;102 right: 0;103 z-index: 2; }104105 #wrapper {106 width: 100%;107 min-height: 800px;108 background: url(images/top.png) 0 0 repeat-x; }109110 #container {111 margin: 0 auto;112 padding-top: 110px;113 width: 550px; }114Setting up TOCTj Holowaychukbefore adoption3e15ef1
lines 115–125why, one step back115 #toc {116 position: fixed;117 top: 70px;118 left: 0;119 margin: 0 0 0 15px;120 padding: 0;121 }122 #toc li {123 list-style: none;124 }125Started guideTj Holowaychukbefore adoption9e3a0c6
line 126why, one step back126 #menu {Added contrib docsTj Holowaychukbefore adoptionf0ba57b
line 127why, one step back127 margin-left: 65px;Started guideTj Holowaychukbefore adoption9e3a0c6
lines 128–142why, one step back128 padding: 0;129 padding-bottom: 30px; }130 #menu li {131 display: inline;132 list-style: none; }133 #menu li a {134 display: block;135 float: left;136 margin: 0 2px;137 padding: 3px 15px;138 background: rgba(0,0,0,0.2);139 -webkit-border-radius: 8px;140 -moz-border-radius: 8px;141 -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);142 -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);Removed annoying padding transitionTj Holowaychukbefore adoption346b2a2
line 143why, one step back143 -webkit-transition-property: opacity, -webkit-transform, color, background-color, -webkit-box-shadow;Started guideTj Holowaychukbefore adoption9e3a0c6
lines 144–160why, one step back144 -webkit-transition-duration: 0.15s;145 -webkit-transition-timing-function: ease-out; }146 #menu li a:hover,147 #menu li a.active {148 background: rgba(0,0,0,0.5); }149 #menu li a:active {150 background: rgba(0,0,0,0.1);151 -webkit-box-shadow: 1px 1px 1px rgba(0,0,0,0.4);152 -moz-box-shadow: 1px 1px 1px rgba(0,0,0,0.4); }153 </style>154 </head>155 <body>156 <a href='http://github.com/visionmedia/express'> 157 <img alt='Fork me on GitHub' id='ribbon' src='http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png' /> 158 </a>159 <div id="header"><strong>Sencha</strong> labs</div>160 <div id="wrapper">Generating TOCTj Holowaychukbefore adoption0cdc836
lines 161–168why, one step back161 <div id="container"><ul id="toc">162<li><a href="#Installation">Installation</a></li>163<li><a href="#Creating-An-Application">Creating An Application</a></li>164<li><a href="#Configuration">Configuration</a></li>165<li><a href="#Settings">Settings</a></li>166<li><a href="#Routing">Routing</a></li>167<li><a href="#Passing-Route-Control">Passing Route Control</a></li>168<li><a href="#Middleware">Middleware</a></li>Setting up TOCTj Holowaychukbefore adoption3e15ef1
lines 169–180why, one step back169<li><a href="#Request-header-key-defaultValue-">Request#header(key[, defaultValue])</a></li>170<li><a href="#Request-accepts-type-">Request#accepts(type)</a></li>171<li><a href="#Request-param-name-">Request#param(name)</a></li>172<li><a href="#Request-flash-type-msg-">Request#flash(type[, msg])</a></li>173<li><a href="#Request-isXMLHttpRequest">Request#isXMLHttpRequest</a></li>174<li><a href="#Response-header-key-val-">Response#header(key[, val])</a></li>175<li><a href="#Response-contentType-type-">Response#contentType(type)</a></li>176<li><a href="#Response-attachment-filename-">Response#attachment([filename])</a></li>177<li><a href="#Response-sendfile-path-">Response#sendfile(path)</a></li>178<li><a href="#Response-download-file-filename-">Response#download(file[, filename])</a></li>179<li><a href="#Response-send-body-status-headers-status-status-">Response#send(body|status[, headers|status[, status]])</a></li>180<li><a href="#Response-redirect-url-status-">Response#redirect(url[, status])</a></li>Generating TOCTj Holowaychukbefore adoption0cdc836
lines 181–182why, one step back181<li><a href="#Server-redirect-name-val-">Server#redirect(name, val)</a></li>182</ul>Started guideTj Holowaychukbefore adoption9e3a0c6
lines 183–190why, one step back183 <a href='http://github.com/visionmedia/express' id='logo'>Express</a> 184 <p id="tagline">185 High performance, high class web development for186 <a href="http://nodejs.org">Node.js</a>187 </p>188 <ul id="menu">189 <li><a href="index.html">Home</a></li>190 <li><a href="guide.html">Guide</a></li>Added contrib docsTj Holowaychukbefore adoptionf0ba57b
line 191why, one step back191 <li><a href="contrib.html">Contributing</a></li>Started guideTj Holowaychukbefore adoption9e3a0c6
lines 192–194why, one step back192 <li><a href="migrate.html">1.x Migration</a></li>193 </ul>194<div class='mp'>Install docsTj Holowaychukbefore adoption63d02f1
lines 195–211why, one step back195<h3 id="Installation">Installation</h3>196197<p>npm:</p>198199<pre><code>$ npm install express200</code></pre>201202<p>curl (or similar):</p>203204<pre><code>$ curl -# http://github.com/extjs/Connect/raw/master/install.sh | sh205</code></pre>206207<p>git clone or tarball for <strong>HEAD</strong>:</p>208209<pre><code>$ make install210</code></pre>211Removed annoying padding transitionTj Holowaychukbefore adoption346b2a2
line 212why, one step back212<h3 id="Creating-An-Application">Creating An Application</h3>Added configuration sectionTj Holowaychukbefore adoption83d2555
lines 213–214why, one step back213214<p>The <em>express.Server</em> now inherits from <em>http.Server</em>, howeverMoved config docs upTj Holowaychukbefore adoption6e8413f
lines 215–216why, one step back215follows the same idiom by providing <em>express.createServer()</em> as shown below. This means216that you can utilize Express server's transparently with other libraries.</p>Added configuration sectionTj Holowaychukbefore adoption83d2555
lines 217–226why, one step back217218<pre><code>var app = require('express').createServer();219220app.get('/', function(req, res){221 res.send('hello world');222});223224app.listen(3000);225</code></pre>226Moved config docs upTj Holowaychukbefore adoption6e8413f
lines 227–270why, one step back227<h3 id="Configuration">Configuration</h3>228229<p>Express supports arbitrary environments, such as <em>production</em> and <em>development</em>. Developers230can use the <em>configure()</em> method to setup needs required by the current environment. When231<em>configure()</em> is called without an environment name it will be run in <em>every</em> environment232prior to the environment specific callback.</p>233234<p>In the example below we only <em>dumpExceptions</em>, and respond with exception stack traces235in <em>development</em> mode, however for both environments we utilize <em>methodOverride</em> and <em>bodyDecoder</em>.</p>236237<pre><code>app.configure(function(){238 app.use('/', connect.methodOverride());239 app.use('/', connect.bodyDecoder());240});241242app.configure('development', function(){243 app.use('/', connect.errorHandler({ dumpExceptions: true, showStack: true }));244});245246app.configure('production', function(){247 app.use('/', connect.errorHandler());248});249</code></pre>250251<p>For internal and arbitrary settings Express provides the <em>set(key[, val])</em>, <em>enable(key)</em>, <em>disable(key)</em> methods:</p>252253<pre><code>app.configure(function(){254 app.set('views', __dirname + '/views');255 app.set('views');256 // => "... views directory ..."257258 app.enable('some feature');259 // same as app.set('some feature', true);260261 app.disable('some feature');262 // same as app.set('some feature', false);263});264</code></pre>265266<h3 id="Settings">Settings</h3>267268<p>Express supports the following settings out of the box:</p>269270<ul>Install docsTj Holowaychukbefore adoption63d02f1
line 271why, one step back271<li><em>home</em> Application base path used with <em>res.redirect()</em></li>Moved config docs upTj Holowaychukbefore adoption6e8413f
lines 272–273why, one step back272<li><em>views</em> Root views directory defaulting to <strong>CWD/views</strong></li>273<li><em>view engine</em> Default view engine name for views rendered without extensions</li>Added more setting docsTj Holowaychukbefore adoption38281e6
lines 274–275why, one step back274<li><em>reload views</em> Reloads altered views, by default watches for <em>mtime</em> changes with275 with a 5 minute interval. Example: <code>app.set('reload views', { interval: 60000 });</code></li>Moved config docs upTj Holowaychukbefore adoption6e8413f
lines 276–278why, one step back276</ul>277278Started guideTj Holowaychukbefore adoption9e3a0c6
lines 279–282why, one step back279<h3 id="Routing">Routing</h3>280281<p>Express utilizes the HTTP verbs to provide a meaningful, expressive routing API.282For example we may want to render a user's account for the path <em>/user/12</em>, thisRoute passing docsTj Holowaychukbefore adoption0ace97c
lines 283–284why, one step back283can be done by defining the route below. The values associated to the named placeholders,284are passed as the <em>third</em> argument, which here we name <em>params</em>.</p>Started guideTj Holowaychukbefore adoption9e3a0c6
lines 285–331why, one step back285286<pre><code>app.get('/user/:id', function(req, res, params){287 res.send('user ' + params.id);288});289</code></pre>290291<p>A route is simple a string which is compiled to a <em>RegExp</em> internally. For example292when <em>/user/:id</em> is compiled, a simplified version of the regexp may look similar to:</p>293294<pre><code>\/user\/([^\/]+)\/?295</code></pre>296297<p>Literal regular expressions may also be passed for complex uses:</p>298299<pre><code>app.get(/^\/foo(bar)?$/, function(){});300</code></pre>301302<p>Below are some route examples, and the associated paths that they303may consume:</p>304305<pre><code> "/user/:id"306 /user/12307308 "/users/:id?"309 /users/5310 /users311312 "/files/*"313 /files/jquery.js314 /files/javascripts/jquery.js315316 "/file/*.*"317 /files/jquery.js318 /files/javascripts/jquery.js319320 "/user/:id/:operation?"321 /user/1322 /user/1/edit323324 "/products.:format"325 /products.json326 /products.xml327328 "/products.:format?"329 /products.json330 /products.xml331 /productsRoute passing docsTj Holowaychukbefore adoption0ace97c
lines 332–348why, one step back332</code></pre>333334<h3 id="Passing-Route-Control">Passing Route Control</h3>335336<p>We may pass control to the next <em>matching</em> route, by calling the <em>fourth</em> parameter,337the <em>next()</em> function. When a match cannot be made, control is passed back to Connect.</p>338339<pre><code>app.get('/users/:id?', function(req, res, params){340 if (params.id) {341 // do something342 } else {343 next();344 }345});346347app.get('/users', function(req, res, params){348 // do something elseAdded configuration sectionTj Holowaychukbefore adoption83d2555
lines 349–363why, one step back349});350</code></pre>351352<h3 id="Middleware">Middleware</h3>353354<p>The Express <em>Plugin</em> is no more! middleware via <a href="http://github.com/extjs/Connect">Connect</a> can be355passed to <em>express.createServer()</em> as you would with a regular Connect server. For example:</p>356357<pre><code>var connect = require('connect'),358 express = require('express');359360var app = express.createServer(361 connect.logger(),362 connect.bodyDecoder()363);More docsTj Holowaychukbefore adoptionbedff32
lines 364–365why, one step back364</code></pre>365Setting up TOCTj Holowaychukbefore adoption3e15ef1
line 366why, one step back366<h3 id="Request-header-key-defaultValue-">Request#header(key[, defaultValue])</h3>More docsTj Holowaychukbefore adoptiondfecbe5
lines 367–374why, one step back367368<p>Get the case-insensitive request header <em>key</em>, with optional <em>defaultValue</em>:</p>369370<pre><code>req.header('Host');371req.header('host');372req.header('Accept', '*/*');373</code></pre>374Setting up TOCTj Holowaychukbefore adoption3e15ef1
line 375why, one step back375<h3 id="Request-accepts-type-">Request#accepts(type)</h3>More docsTj Holowaychukbefore adoptiondfecbe5
lines 376–399why, one step back376377<p>Check if the <em>Accept</em> header is present, and includes the given <em>type</em>.</p>378379<p>When the <em>Accept</em> header is not present <em>true</em> is returned. Otherwise380the given <em>type</em> is matched by an exact match, and then subtypes. You381may pass the subtype such as "html" which is then converted internally382to "text/html" using the mime lookup table.</p>383384<pre><code>// Accept: text/html385req.accepts('html');386// => true387388// Accept: text/*; application/json389req.accepts('html');390req.accepts('text/html');391req.accepts('text/plain');392req.accepts('application/json');393// => true394395req.accepts('image/png');396req.accepts('png');397// => false398</code></pre>399Setting up TOCTj Holowaychukbefore adoption3e15ef1
line 400why, one step back400<h3 id="Request-param-name-">Request#param(name)</h3>More docsTj Holowaychukbefore adoptiondfecbe5
lines 401–414why, one step back401402<p>Return the value of param <em>name</em> when present.</p>403404<ul>405<li>Checks route placeholders, ex: /user/:id</li>406<li>Checks query string params, ex: ?id=12</li>407<li>Checks urlencoded body params, ex: id=12</li>408</ul>409410411<p>To utilize urlencoded request bodies, <em>req.body</em>412should be an object. This can be done by using413the <em>connect.bodyDecoder</em> middleware.</p>414Setting up TOCTj Holowaychukbefore adoption3e15ef1
line 415why, one step back415<h3 id="Request-flash-type-msg-">Request#flash(type[, msg])</h3>More docsTj Holowaychukbefore adoptiondfecbe5
lines 416–433why, one step back416417<p>Queue flash <em>msg</em> of the given <em>type</em>.</p>418419<pre><code>req.flash('info', 'email sent');420req.flash('error', 'email delivery failed');421req.flash('info', 'email re-sent');422// => 2423424req.flash('info');425// => ['email sent', 'email re-sent']426427req.flash('info');428// => []429430req.flash();431// => { error: ['email delivery failed'], info: [] }432</code></pre>433Setting up TOCTj Holowaychukbefore adoption3e15ef1
line 434why, one step back434<h3 id="Request-isXMLHttpRequest">Request#isXMLHttpRequest</h3>More docsTj Holowaychukbefore adoptiondfecbe5
lines 435–442why, one step back435436<p>Also aliased as <em>req.xhr</em>, this getter checks the <em>X-Requested-With</em> header437to see if it was issued by an <em>XMLHttpRequest</em>:</p>438439<pre><code>req.xhr440req.isXMLHttpRequest441</code></pre>442Setting up TOCTj Holowaychukbefore adoption3e15ef1
line 443why, one step back443<h3 id="Response-header-key-val-">Response#header(key[, val])</h3>More docs :)Tj Holowaychukbefore adoption0c5fe61
lines 444–456why, one step back444445<p>Get or set the response header <em>key</em>.</p>446447<pre><code>res.header('Content-Length');448// => undefined449450res.header('Content-Length', 123);451// => 123452453res.header('Content-Length');454// => 123455</code></pre>456Setting up TOCTj Holowaychukbefore adoption3e15ef1
line 457why, one step back457<h3 id="Response-contentType-type-">Response#contentType(type)</h3>More docs :)Tj Holowaychukbefore adoption0c5fe61
lines 458–465why, one step back458459<p>Sets the <em>Content-Type</em> response header to the given <em>type</em>.</p>460461<pre><code> var filename = 'path/to/image.png';462 res.contentType(filename);463 // res.headers['Content-Type'] is now "image/png"464</code></pre>465Setting up TOCTj Holowaychukbefore adoption3e15ef1
line 466why, one step back466<h3 id="Response-attachment-filename-">Response#attachment([filename])</h3>Docs for res.attachment()Tj Holowaychukbefore adoptioneb235e1
lines 467–472why, one step back467468<p>Sets the <em>Content-Disposition</em> response header to "attachment", with optional <em>filename</em>.</p>469470<pre><code> res.attachment('path/to/my/image.png');471</code></pre>472Setting up TOCTj Holowaychukbefore adoption3e15ef1
line 473why, one step back473<h3 id="Response-sendfile-path-">Response#sendfile(path)</h3>Docs for new res methodsTj Holowaychukbefore adoptionb08dab9
lines 474–482why, one step back474475<p>Used by <code>res.download()</code> to transfer an arbitrary file.</p>476477<pre><code>res.sendfile('path/to/my.file');478</code></pre>479480<p><strong>NOTE</strong>: this is <em>not</em> a replacement for Connect's <em>staticProvider</em> middleware,481nor does it perform any security checks, use with caution when using in a dynamic manor.</p>482Setting up TOCTj Holowaychukbefore adoption3e15ef1
line 483why, one step back483<h3 id="Response-download-file-filename-">Response#download(file[, filename])</h3>Docs for new res methodsTj Holowaychukbefore adoptionb08dab9
lines 484–496why, one step back484485<p>Transfer the given <em>file</em> as an attachment with optional alternative <em>filename</em>.</p>486487<pre><code>res.download('path/to/image.png');488res.download('path/to/image.png', 'foo.png');489</code></pre>490491<p>This is equivalent to:</p>492493<pre><code>res.attachment(file);494res.sendfile(file);495</code></pre>496Setting up TOCTj Holowaychukbefore adoption3e15ef1
line 497why, one step back497<h3 id="Response-send-body-status-headers-status-status-">Response#send(body|status[, headers|status[, status]])</h3>Docs for res.send()Tj Holowaychukbefore adoption5a48248
lines 498–514why, one step back498499<p>The <code>res.send()</code> method is a high level response utility allowing you to pass500objects to respond with json, strings for html, arbitrary _Buffer_s or numbers for status501code based responses. The following are all valid uses:</p>502503<pre><code> res.send(new Buffer('wahoo'));504 res.send({ some: 'json' });505 res.send('<p>some html</p>');506 res.send('Sorry, cant find that', 404);507 res.send('text', { 'Content-Type': 'text/plain' }, 201);508 res.send(404);509</code></pre>510511<p>By default the <em>Content-Type</em> response header is set, however if explicitly512assigned through <code>res.send()</code> or previously with <code>res.header()</code> or <code>res.contentType()</code>513it will not be set again.</p>514Setting up TOCTj Holowaychukbefore adoption3e15ef1
line 515why, one step back515<h3 id="Response-redirect-url-status-">Response#redirect(url[, status])</h3>More docsTj Holowaychukbefore adoptionbedff32
lines 516–554why, one step back516517<p>Redirect to the given <em>url</em> with a default response <em>status</em> of 302.</p>518519<pre><code>res.redirect('/', 301);520res.redirect('/account');521res.redirect('http://google.com');522res.redirect('home');523res.redirect('back');524</code></pre>525526<p>Express supports "redirect mapping", which by default provides <em>home</em>, and <em>back</em>.527The <em>back</em> map checks the <em>Referrer</em> and <em>Referer</em> headers, while <em>home</em> utilizes528the "home" setting and defaults to "/".</p>529530<h3 id="Server-redirect-name-val-">Server#redirect(name, val)</h3>531532<p>For use with <code>res.redirect()</code> we can map redirects at the application level as shown below:</p>533534<pre><code>app.redirect('google', 'http://google.com');535</code></pre>536537<p>Now in a route we may call:</p>538539<p> res.redirect('google');</p>540541<p>We may also map dynamic redirects:</p>542543<pre><code>app.redirect('comments', function(req, res, params){544 return '/post/' + params.id + '/comments';545});546</code></pre>547548<p>So now we may do the following, and the redirect will dynamically adjust to549the context of the request. If we called this route with <em>GET /post/12</em> our550redirect <em>Location</em> would be <em>/post/12/comments</em>.</p>551552<pre><code>app.get('/post/:id', function(req, res){553 res.redirect('comments');554});Added configuration sectionTj Holowaychukbefore adoption83d2555
line 555why, one step back555</code></pre>Configuration docsTj Holowaychukbefore adoptioncb04364
line 556why, one step back556Started guideTj Holowaychukbefore adoption9e3a0c6
lines 557–561why, one step back557</div>558 </div>559 </div>560 </body>561</html>