Skip to content
Myr

guide.html

This file

Path

docs/guide.html

905 lines · 28.0 KiB · HTML

Last changed by

Updated curl install docsTj Holowaychukbefore adoption3ceb0de

History of this file

Take it

Started guideTj Holowaychukbefore adoption9e3a0c6

lines 1–46why, 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;

Merge branch 'docs' into connectTj Holowaychukbefore adoption492f70f

line 47why, one step back
47 text-decoration: none;

Started guideTj Holowaychukbefore adoption9e3a0c6

lines 48–115why, one step back
48 color: white;
49 -webkit-transition-property: opacity, -webkit-transform, color, background-color, padding, -webkit-box-shadow;50 -webkit-transition-duration: 0.15s;51 -webkit-transition-timing-function: ease-out; }52 a:hover {53 opacity: 0.8; }5455 h1, h2, h3 {56 margin: 45px 0 0 0;57 color: white;58 text-shadow: 1px 2px 2px rgba(0,0,0,0.6); }5960 h3 {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; }115

Merge branch 'docs' into connectTj Holowaychukbefore adoption492f70f

lines 116–126why, one step back
116 #toc {
117 position: fixed;118 top: 60px;119 left: 0;120 margin: 0 0 0 15px;121 padding: 15px;122 height: 100%;123 background: rgba(0,0,0,0.2);124 border-right: 1px solid rgba(255,255,255,0.05);125 }126 #toc li {

More view docsTj Holowaychukbefore adoption1c8ff48

line 127why, one step back
127 padding: 0;

Merge branch 'docs' into connectTj Holowaychukbefore adoption492f70f

lines 128–132why, one step back
128 list-style: none;
129 }130 #toc li a {131 font-size: 11px;132 }

Started guideTj Holowaychukbefore adoption9e3a0c6

line 133why, one step back
133 #menu {

Added contrib docsTj Holowaychukbefore adoptionf0ba57b

line 134why, one step back
134 margin-left: 65px;

Started guideTj Holowaychukbefore adoption9e3a0c6

lines 135–149why, one step back
135 padding: 0;
136 padding-bottom: 30px; }137 #menu li {138 display: inline;139 list-style: none; }140 #menu li a {141 display: block;142 float: left;143 margin: 0 2px;144 padding: 3px 15px;145 background: rgba(0,0,0,0.2);146 -webkit-border-radius: 8px;147 -moz-border-radius: 8px;148 -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);149 -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);

Removed annoying padding transitionTj Holowaychukbefore adoption346b2a2

line 150why, one step back
150 -webkit-transition-property: opacity, -webkit-transform, color, background-color, -webkit-box-shadow;

Started guideTj Holowaychukbefore adoption9e3a0c6

lines 151–167why, one step back
151 -webkit-transition-duration: 0.15s;
152 -webkit-transition-timing-function: ease-out; }153 #menu li a:hover,154 #menu li a.active {155 background: rgba(0,0,0,0.5); }156 #menu li a:active {157 background: rgba(0,0,0,0.1);158 -webkit-box-shadow: 1px 1px 1px rgba(0,0,0,0.4);159 -moz-box-shadow: 1px 1px 1px rgba(0,0,0,0.4); }160 </style>161 </head>162 <body>163 <a href='http://github.com/visionmedia/express'> 164 <img alt='Fork me on GitHub' id='ribbon' src='http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png' /> 165 </a>166 <div id="header"><strong>Sencha</strong> labs</div>167 <div id="wrapper">

Merge branch 'docs' into connectTj Holowaychukbefore adoption492f70f

lines 168–175why, one step back
168 <div id="container"><ul id="toc">
169<li><a href="#Installation">Installation</a></li>170<li><a href="#Creating-An-Application">Creating An Application</a></li>171<li><a href="#Configuration">Configuration</a></li>172<li><a href="#Settings">Settings</a></li>173<li><a href="#Routing">Routing</a></li>174<li><a href="#Passing-Route-Control">Passing Route Control</a></li>175<li><a href="#Middleware">Middleware</a></li>

Error handling docsTj Holowaychukbefore adoption9fbb9fa

line 176why, one step back
176<li><a href="#Error-Handling">Error Handling</a></li>

Added more view docsTj Holowaychukbefore adoption2f96506

lines 177–178why, one step back
177<li><a href="#View-Rendering">View Rendering</a></li>
178<li><a href="#View-Partials">View Partials</a></li>

Added view helper docs. Closes #344Tj Holowaychukbefore adoptionc4b5208

line 179why, one step back
179<li><a href="#View-Helpers">View Helpers</a></li>

Added more view docsTj Holowaychukbefore adoption2f96506

line 180why, one step back
180<li><a href="#Template-Engines">Template Engines</a></li>

More docsTj Holowaychukbefore adoptionde81ee7

lines 181–192why, one step back
181<li><a href="#req-header-key-defaultValue-">req.header()</a></li>
182<li><a href="#req-accepts-type-">req.accepts()</a></li>183<li><a href="#req-param-name-">req.param()</a></li>184<li><a href="#req-flash-type-msg-">req.flash()</a></li>185<li><a href="#req-isXMLHttpRequest">req.isXMLHttpRequest</a></li>186<li><a href="#res-header-key-val-">res.header()</a></li>187<li><a href="#res-contentType-type-">res.contentType()</a></li>188<li><a href="#res-attachment-filename-">res.attachment()</a></li>189<li><a href="#res-sendfile-path-">res.sendfile()</a></li>190<li><a href="#res-download-file-filename-">res.download()</a></li>191<li><a href="#res-send-body-status-headers-status-status-">res.send()</a></li>192<li><a href="#res-redirect-url-status-">res.redirect()</a></li>

More view docsTj Holowaychukbefore adoption1c8ff48

line 193why, one step back
193<li><a href="#res-render-view-options-fn-">res.render()</a></li>

In depth partial docsTj Holowaychukbefore adoption793943f

line 194why, one step back
194<li><a href="#res-partial-view-options-">res.partial()</a></li>

More docsTj Holowaychukbefore adoptionde81ee7

lines 195–199why, one step back
195<li><a href="#app-set-name-val-">app.set()</a></li>
196<li><a href="#app-enable-name-">app.enable()</a></li>197<li><a href="#app-disable-name-">app.disable()</a></li>198<li><a href="#app-configure-env-function-function-">app.configure()</a></li>199<li><a href="#app-redirect-name-val-">app.redirect()</a></li>

Error handling docsTj Holowaychukbefore adoption9fbb9fa

line 200why, one step back
200<li><a href="#app-error-function-">app.error()</a></li>

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

line 201why, one step back
201<li><a href="#app-listen-port-host-">app.listen()</a></li>

Merge branch 'docs' into connectTj Holowaychukbefore adoption492f70f

line 202why, one step back
202</ul>

Started guideTj Holowaychukbefore adoption9e3a0c6

lines 203–210why, one step back
203 <a href='http://github.com/visionmedia/express' id='logo'>Express</a>
204 <p id="tagline">205 High performance, high class web development for206 <a href="http://nodejs.org">Node.js</a>207 </p>208 <ul id="menu">209 <li><a href="index.html">Home</a></li>210 <li><a href="guide.html">Guide</a></li>

Added contrib docsTj Holowaychukbefore adoptionf0ba57b

line 211why, one step back
211 <li><a href="contrib.html">Contributing</a></li>

Started guideTj Holowaychukbefore adoption9e3a0c6

lines 212–214why, one step back
212 <li><a href="migrate.html">1.x Migration</a></li>
213 </ul>214<div class='mp'>

Install docsTj Holowaychukbefore adoption63d02f1

lines 215–216why, one step back
215<h3 id="Installation">Installation</h3>
216

Updated curl install docsTj Holowaychukbefore adoption3ceb0de

line 217why, one step back
217<p>curl (or similar):</p>

Install docsTj Holowaychukbefore adoption63d02f1

line 218why, one step back
218

Updated curl install docsTj Holowaychukbefore adoption3ceb0de

line 219why, one step back
219<pre><code>$ curl -# http://expressjs.com/install.sh | sh

Install docsTj Holowaychukbefore adoption63d02f1

lines 220–221why, one step back
220</code></pre>
221

Updated curl install docsTj Holowaychukbefore adoption3ceb0de

line 222why, one step back
222<p>npm:</p>

Install docsTj Holowaychukbefore adoption63d02f1

line 223why, one step back
223

Updated curl install docsTj Holowaychukbefore adoption3ceb0de

lines 224–225why, one step back
224<pre><code>$ npm install connect
225$ npm install express

Install docsTj Holowaychukbefore adoption63d02f1

lines 226–227why, one step back
226</code></pre>
227

Updated install docsTj Holowaychukbefore adoption193c4a9

line 228why, one step back
228<p>git clone, first update the submodules:</p>

Install docsTj Holowaychukbefore adoption63d02f1

line 229why, one step back
229

Updated install docsTj Holowaychukbefore adoption193c4a9

lines 230–232why, one step back
230<pre><code>$ git submodule update --init
231$ make install232$ make install-support

Install docsTj Holowaychukbefore adoption63d02f1

lines 233–234why, one step back
233</code></pre>
234

Removed annoying padding transitionTj Holowaychukbefore adoption346b2a2

line 235why, one step back
235<h3 id="Creating-An-Application">Creating An Application</h3>

Added configuration sectionTj Holowaychukbefore adoption83d2555

lines 236–237why, one step back
236
237<p>The <em>express.Server</em> now inherits from <em>http.Server</em>, however

Moved config docs upTj Holowaychukbefore adoption6e8413f

lines 238–239why, one step back
238follows the same idiom by providing <em>express.createServer()</em> as shown below. This means
239that you can utilize Express server's transparently with other libraries.</p>

Added configuration sectionTj Holowaychukbefore adoption83d2555

lines 240–249why, one step back
240
241<pre><code>var app = require('express').createServer();242243app.get('/', function(req, res){244 res.send('hello world');245});246247app.listen(3000);248</code></pre>249

Moved config docs upTj Holowaychukbefore adoption6e8413f

lines 250–260why, one step back
250<h3 id="Configuration">Configuration</h3>
251252<p>Express supports arbitrary environments, such as <em>production</em> and <em>development</em>. Developers253can use the <em>configure()</em> method to setup needs required by the current environment. When254<em>configure()</em> is called without an environment name it will be run in <em>every</em> environment255prior to the environment specific callback.</p>256257<p>In the example below we only <em>dumpExceptions</em>, and respond with exception stack traces258in <em>development</em> mode, however for both environments we utilize <em>methodOverride</em> and <em>bodyDecoder</em>.</p>259260<pre><code>app.configure(function(){

Updated docsTj Holowaychukbefore adoptioncacada8

lines 261–262why, one step back
261 app.use(connect.methodOverride());
262 app.use(connect.bodyDecoder());

Moved config docs upTj Holowaychukbefore adoption6e8413f

lines 263–265why, one step back
263});
264265app.configure('development', function(){

Updated docsTj Holowaychukbefore adoptioncacada8

line 266why, one step back
266 app.use(connect.errorHandler({ dumpExceptions: true, showStack: true }));

Moved config docs upTj Holowaychukbefore adoption6e8413f

lines 267–269why, one step back
267});
268269app.configure('production', function(){

Updated docsTj Holowaychukbefore adoptioncacada8

line 270why, one step back
270 app.use(connect.errorHandler());

Moved config docs upTj Holowaychukbefore adoption6e8413f

lines 271–288why, one step back
271});
272</code></pre>273274<p>For internal and arbitrary settings Express provides the <em>set(key[, val])</em>, <em>enable(key)</em>, <em>disable(key)</em> methods:</p>275276<pre><code>app.configure(function(){277 app.set('views', __dirname + '/views');278 app.set('views');279 // =&gt; "... views directory ..."280281 app.enable('some feature');282 // same as app.set('some feature', true);283284 app.disable('some feature');285 // same as app.set('some feature', false);286});287</code></pre>288

More docsTj Holowaychukbefore adoptionde81ee7

lines 289–294why, one step back
289<p>To alter the environment we can set the <em>CONNECT_ENV</em> environment variable,
290or more specifically <em>EXPRESS_ENV</em>, for example:</p>291292<pre><code>$ EXPRESS_ENV=production node app.js293</code></pre>294

Moved config docs upTj Holowaychukbefore adoption6e8413f

lines 295–299why, one step back
295<h3 id="Settings">Settings</h3>
296297<p>Express supports the following settings out of the box:</p>298299<ul>

More docsTj Holowaychukbefore adoptionde81ee7

line 300why, one step back
300<li><em>env</em> Application environment set internally, use <em>app.set('env')</em> to utilize</li>

Install docsTj Holowaychukbefore adoption63d02f1

line 301why, one step back
301<li><em>home</em> Application base path used with <em>res.redirect()</em></li>

Moved config docs upTj Holowaychukbefore adoption6e8413f

lines 302–303why, one step back
302<li><em>views</em> Root views directory defaulting to <strong>CWD/views</strong></li>
303<li><em>view engine</em> Default view engine name for views rendered without extensions</li>

Added more setting docsTj Holowaychukbefore adoption38281e6

line 304why, one step back
304<li><em>reload views</em> Reloads altered views, by default watches for <em>mtime</em> changes with

Fixed reload views exampleTj Holowaychukbefore adoption5fcf971

line 305why, one step back
305 with a 5 minute interval. Example: <em>app.set('reload views', 60000);</em></li>

Moved config docs upTj Holowaychukbefore adoption6e8413f

lines 306–308why, one step back
306</ul>
307308

Started guideTj Holowaychukbefore adoption9e3a0c6

lines 309–312why, one step back
309<h3 id="Routing">Routing</h3>
310311<p>Express utilizes the HTTP verbs to provide a meaningful, expressive routing API.312For example we may want to render a user's account for the path <em>/user/12</em>, this

Route passing docsTj Holowaychukbefore adoption0ace97c

lines 313–314why, one step back
313can be done by defining the route below. The values associated to the named placeholders,
314are passed as the <em>third</em> argument, which here we name <em>params</em>.</p>

Started guideTj Holowaychukbefore adoption9e3a0c6

lines 315–361why, one step back
315
316<pre><code>app.get('/user/:id', function(req, res, params){317 res.send('user ' + params.id);318});319</code></pre>320321<p>A route is simple a string which is compiled to a <em>RegExp</em> internally. For example322when <em>/user/:id</em> is compiled, a simplified version of the regexp may look similar to:</p>323324<pre><code>\/user\/([^\/]+)\/?325</code></pre>326327<p>Literal regular expressions may also be passed for complex uses:</p>328329<pre><code>app.get(/^\/foo(bar)?$/, function(){});330</code></pre>331332<p>Below are some route examples, and the associated paths that they333may consume:</p>334335<pre><code> "/user/:id"336 /user/12337338 "/users/:id?"339 /users/5340 /users341342 "/files/*"343 /files/jquery.js344 /files/javascripts/jquery.js345346 "/file/*.*"347 /files/jquery.js348 /files/javascripts/jquery.js349350 "/user/:id/:operation?"351 /user/1352 /user/1/edit353354 "/products.:format"355 /products.json356 /products.xml357358 "/products.:format?"359 /products.json360 /products.xml361 /products

Route passing docsTj Holowaychukbefore adoption0ace97c

lines 362–378why, one step back
362</code></pre>
363364<h3 id="Passing-Route-Control">Passing Route Control</h3>365366<p>We may pass control to the next <em>matching</em> route, by calling the <em>fourth</em> parameter,367the <em>next()</em> function. When a match cannot be made, control is passed back to Connect.</p>368369<pre><code>app.get('/users/:id?', function(req, res, params){370 if (params.id) {371 // do something372 } else {373 next();374 }375});376377app.get('/users', function(req, res, params){378 // do something else

Added configuration sectionTj Holowaychukbefore adoption83d2555

lines 379–393why, one step back
379});
380</code></pre>381382<h3 id="Middleware">Middleware</h3>383384<p>The Express <em>Plugin</em> is no more! middleware via <a href="http://github.com/extjs/Connect">Connect</a> can be385passed to <em>express.createServer()</em> as you would with a regular Connect server. For example:</p>386387<pre><code>var connect = require('connect'),388 express = require('express');389390var app = express.createServer(391 connect.logger(),392 connect.bodyDecoder()393);

More docsTj Holowaychukbefore adoptionbedff32

lines 394–395why, one step back
394</code></pre>
395

Error handling docsTj Holowaychukbefore adoption9fbb9fa

lines 396–397why, one step back
396<p>Alternatively we can <em>use()</em> them which is useful when adding middleware within <em>configure()</em> blocks:</p>
397

Updated docsTj Holowaychukbefore adoptioncacada8

line 398why, one step back
398<pre><code>app.use(connect.logger({ format: ':method :uri' }));

Error handling docsTj Holowaychukbefore adoption9fbb9fa

lines 399–447why, one step back
399</code></pre>
400401<h3 id="Error-Handling">Error Handling</h3>402403<p>Express provides the <em>app.error()</em> method which receives exceptions thrown within a route,404or passed to <em>next(err)</em>. Below is an example which serves different pages based on our405ad-hoc <em>NotFound</em> exception:</p>406407<pre><code>function NotFound(msg){408 this.name = 'NotFound';409 Error.call(this, msg);410 Error.captureStackTrace(this, arguments.callee);411}412413sys.inherits(NotFound, Error);414415app.get('/404', function(req, res){416 throw new NotFound;417});418419app.get('/500', function(req, res){420 throw new Error('keyboard cat!');421});422</code></pre>423424<p>We can call <em>app.error()</em> several times as shown below.425Here we check for an instanceof <em>NotFound</em> and show the426404 page, or we pass on to the next error handler.</p>427428<pre><code>app.error(function(err, req, res, next){429 if (err instanceof NotFound) {430 res.render('404.jade');431 } else {432 next(err);433 }434});435</code></pre>436437<p>Here we assume all errors as 500 for the simplicity of438this demo, however you can choose whatever you like</p>439440<pre><code>app.error(function(err, req, res){441 res.render('500.jade', {442 locals: {443 error: err444 } 445 });446});447</code></pre>

Added more view docsTj Holowaychukbefore adoption2f96506

line 448why, one step back
448

More error handling docsTj Holowaychukbefore adoptioncea401c

lines 449–452why, one step back
449<p>Our apps could also utilize the Connect <em>errorHandler</em> middleware
450to report on exceptions. For example if we wish to output exceptions451in "development" mode to <em>stderr</em> we can use:</p>452

Updated docsTj Holowaychukbefore adoptioncacada8

line 453why, one step back
453<pre><code>app.use(connect.errorHandler({ dumpExceptions: true }));

More error handling docsTj Holowaychukbefore adoptioncea401c

lines 454–458why, one step back
454</code></pre>
455456<p>Also during development we may want fancy html pages to show exceptions457that are passed or thrown, so we can set <em>showStack</em> to true:</p>458

Updated docsTj Holowaychukbefore adoptioncacada8

line 459why, one step back
459<pre><code>app.use(connect.errorHandler({ showStack: true, dumpExceptions: true }));

More error handling docsTj Holowaychukbefore adoptioncea401c

lines 460–464why, one step back
460</code></pre>
461462<p>The <em>errorHandler</em> middleware also responds with <em>json</em> if <em>Accept: application/json</em>463is present, which is useful for developing apps that rely heavily on client-side JavaScript.</p>464

Added more view docsTj Holowaychukbefore adoption2f96506

lines 465–483why, one step back
465<h3 id="View-Rendering">View Rendering</h3>
466467<p>View filenames take the form <em>Express</em>.<em>ENGINE</em>, where <em>ENGINE</em> is the name468of the module that will be required. For example the view <em>layout.ejs</em> will469tell the view system to <em>require('ejs')</em>, the module being loaded must (currently)470export the method <em>exports.render(str, options)</em> to comply with Express, however471with will likely be extensible in the future.</p>472473<p>Below is an example using <a href="http://github.com/visionmedia/haml.js">Haml.js</a> to render <em>index.html</em>,474and since we do not use <em>layout: false</em> the rendered contents of <em>index.html</em> will be passed as475the <em>body</em> local variable in <em>layout.haml</em>.</p>476477<pre><code>app.get('/', function(req, res){478 res.render('index.haml', {479 locals: { title: 'My Site' }480 });481});482</code></pre>483

Docs for "view engine" settingTj Holowaychukbefore adoption1810c9b

lines 484–505why, one step back
484<p>The new <em>view engine</em> setting allows us to specify our default template engine,
485so for example when using <a href="http://github.com/visionmedia/jade">Jade</a> we could set:</p>486487<pre><code>app.set('view engine', 'jade');488</code></pre>489490<p>Allowing us to render with:</p>491492<pre><code>res.render('index');493</code></pre>494495<p>vs:</p>496497<pre><code>res.render('index.jade');498</code></pre>499500<p>When <em>view engine</em> is set, extensions are entirely optional, however we can still501mix and match template engines:</p>502503<pre><code>res.render('another-page.ejs');504</code></pre>505

Added more view docsTj Holowaychukbefore adoption2f96506

lines 506–526why, one step back
506<h3 id="View-Partials">View Partials</h3>
507508<p>The Express view system has built-in support for partials and collections, which are509sort of "mini" views representing a document fragment. For example rather than iterating510in a view to display comments, we would use a partial with collection support:</p>511512<pre><code>partial('comment.haml', { collection: comments });513</code></pre>514515<p>To make things even less verbose we can assume the extension as <em>.haml</em> when omitted,516however if we wished we could use an ejs partial, within a haml view for example.</p>517518<pre><code>partial('comment', { collection: comments });519</code></pre>520521<p>And once again even further, when rendering a collection we can simply pass522an array, if no other options are desired:</p>523524<pre><code>partial('comments', comments);525</code></pre>526

Added partial magic var docs. Closes #342Tj Holowaychukbefore adoptionbe85701

lines 527–535why, one step back
527<p>When using the partial collection support a few "magic" variables are provided
528for free:</p>529530<ul>531<li><em>firstInCollection</em> True if this is the first object</li>532<li><em>indexInCollection</em> Index of the object in the collection</li>533<li>_lastInCollection _ True if this is the last object</li>534</ul>535

Added view helper docs. Closes #344Tj Holowaychukbefore adoptionc4b5208

lines 536–551why, one step back
536
537<h3 id="View-Helpers">View Helpers</h3>538539<p>View helpers are merged with all template local variables, exposing them540as pseudo globals. Below is an example of exposing <em>foo()</em> to all views:</p>541542<pre><code>var view = require('express/view');543view.helpers.foo = function(){544 return 'bar';545};546</code></pre>547548<p>Which we could then utilize in a template as shown below:</p>549550<pre><code>&lt;%= foo() %>551</code></pre>

Added partial magic var docs. Closes #342Tj Holowaychukbefore adoptionbe85701

line 552why, one step back
552

Added more view docsTj Holowaychukbefore adoption2f96506

lines 553–562why, one step back
553<h3 id="Template-Engines">Template Engines</h3>
554555<p>Below are a few template engines commonly used with Express:</p>556557<ul>558<li><a href="http://github.com/visionmedia/jade">Jade</a> haml.js successor</li>559<li><a href="http://github.com/visionmedia/haml.js">Haml</a> indented templates</li>560<li><a href="http://github.com/visionmedia/ejs">EJS</a> Embedded JavaScript</li>561</ul>562

Error handling docsTj Holowaychukbefore adoption9fbb9fa

line 563why, one step back
563

More docsTj Holowaychukbefore adoptionde81ee7

line 564why, one step back
564<h3 id="req-header-key-defaultValue-">req.header(key[, defaultValue])</h3>

More docsTj Holowaychukbefore adoptiondfecbe5

lines 565–572why, one step back
565
566<p>Get the case-insensitive request header <em>key</em>, with optional <em>defaultValue</em>:</p>567568<pre><code>req.header('Host');569req.header('host');570req.header('Accept', '*/*');571</code></pre>572

More docsTj Holowaychukbefore adoptionde81ee7

line 573why, one step back
573<h3 id="req-accepts-type-">req.accepts(type)</h3>

More docsTj Holowaychukbefore adoptiondfecbe5

lines 574–597why, one step back
574
575<p>Check if the <em>Accept</em> header is present, and includes the given <em>type</em>.</p>576577<p>When the <em>Accept</em> header is not present <em>true</em> is returned. Otherwise578the given <em>type</em> is matched by an exact match, and then subtypes. You579may pass the subtype such as "html" which is then converted internally580to "text/html" using the mime lookup table.</p>581582<pre><code>// Accept: text/html583req.accepts('html');584// =&gt; true585586// Accept: text/*; application/json587req.accepts('html');588req.accepts('text/html');589req.accepts('text/plain');590req.accepts('application/json');591// =&gt; true592593req.accepts('image/png');594req.accepts('png');595// =&gt; false596</code></pre>597

More docsTj Holowaychukbefore adoptionde81ee7

line 598why, one step back
598<h3 id="req-param-name-">req.param(name)</h3>

More docsTj Holowaychukbefore adoptiondfecbe5

lines 599–612why, one step back
599
600<p>Return the value of param <em>name</em> when present.</p>601602<ul>603<li>Checks route placeholders, ex: /user/:id</li>604<li>Checks query string params, ex: ?id=12</li>605<li>Checks urlencoded body params, ex: id=12</li>606</ul>607608609<p>To utilize urlencoded request bodies, <em>req.body</em>610should be an object. This can be done by using611the <em>connect.bodyDecoder</em> middleware.</p>612

More docsTj Holowaychukbefore adoptionde81ee7

line 613why, one step back
613<h3 id="req-flash-type-msg-">req.flash(type[, msg])</h3>

More docsTj Holowaychukbefore adoptiondfecbe5

lines 614–631why, one step back
614
615<p>Queue flash <em>msg</em> of the given <em>type</em>.</p>616617<pre><code>req.flash('info', 'email sent');618req.flash('error', 'email delivery failed');619req.flash('info', 'email re-sent');620// =&gt; 2621622req.flash('info');623// =&gt; ['email sent', 'email re-sent']624625req.flash('info');626// =&gt; []627628req.flash();629// =&gt; { error: ['email delivery failed'], info: [] }630</code></pre>631

More docsTj Holowaychukbefore adoptionde81ee7

line 632why, one step back
632<h3 id="req-isXMLHttpRequest">req.isXMLHttpRequest</h3>

More docsTj Holowaychukbefore adoptiondfecbe5

lines 633–640why, one step back
633
634<p>Also aliased as <em>req.xhr</em>, this getter checks the <em>X-Requested-With</em> header635to see if it was issued by an <em>XMLHttpRequest</em>:</p>636637<pre><code>req.xhr638req.isXMLHttpRequest639</code></pre>640

More docsTj Holowaychukbefore adoptionde81ee7

line 641why, one step back
641<h3 id="res-header-key-val-">res.header(key[, val])</h3>

More docs :)Tj Holowaychukbefore adoption0c5fe61

lines 642–654why, one step back
642
643<p>Get or set the response header <em>key</em>.</p>644645<pre><code>res.header('Content-Length');646// =&gt; undefined647648res.header('Content-Length', 123);649// =&gt; 123650651res.header('Content-Length');652// =&gt; 123653</code></pre>654

More docsTj Holowaychukbefore adoptionde81ee7

line 655why, one step back
655<h3 id="res-contentType-type-">res.contentType(type)</h3>

More docs :)Tj Holowaychukbefore adoption0c5fe61

lines 656–663why, one step back
656
657<p>Sets the <em>Content-Type</em> response header to the given <em>type</em>.</p>658659<pre><code> var filename = 'path/to/image.png';660 res.contentType(filename);661 // res.headers['Content-Type'] is now "image/png"662</code></pre>663

More docsTj Holowaychukbefore adoptionde81ee7

line 664why, one step back
664<h3 id="res-attachment-filename-">res.attachment([filename])</h3>

Docs for res.attachment()Tj Holowaychukbefore adoptioneb235e1

lines 665–670why, one step back
665
666<p>Sets the <em>Content-Disposition</em> response header to "attachment", with optional <em>filename</em>.</p>667668<pre><code> res.attachment('path/to/my/image.png');669</code></pre>670

More docsTj Holowaychukbefore adoptionde81ee7

line 671why, one step back
671<h3 id="res-sendfile-path-">res.sendfile(path)</h3>

Docs for new res methodsTj Holowaychukbefore adoptionb08dab9

lines 672–677why, one step back
672
673<p>Used by <code>res.download()</code> to transfer an arbitrary file.</p>674675<pre><code>res.sendfile('path/to/my.file');676</code></pre>677

Better docs for res.sendfile()Tj Holowaychukbefore adoption8a337f1

lines 678–681why, one step back
678<p>This is <em>not</em> a substitution for Connect's <em>staticProvider</em> middleware, it does not
679support HTTP caching, and does not perform any security checks. This method is utilized680by <em>res.download()</em> to transfer static files, and allows you do to so from outside of681the public directory, so suitable security checks should be applied.</p>

Docs for new res methodsTj Holowaychukbefore adoptionb08dab9

line 682why, one step back
682

More docsTj Holowaychukbefore adoptionde81ee7

line 683why, one step back
683<h3 id="res-download-file-filename-">res.download(file[, filename])</h3>

Docs for new res methodsTj Holowaychukbefore adoptionb08dab9

lines 684–696why, one step back
684
685<p>Transfer the given <em>file</em> as an attachment with optional alternative <em>filename</em>.</p>686687<pre><code>res.download('path/to/image.png');688res.download('path/to/image.png', 'foo.png');689</code></pre>690691<p>This is equivalent to:</p>692693<pre><code>res.attachment(file);694res.sendfile(file);695</code></pre>696

More docsTj Holowaychukbefore adoptionde81ee7

line 697why, one step back
697<h3 id="res-send-body-status-headers-status-status-">res.send(body|status[, headers|status[, status]])</h3>

Docs for res.send()Tj Holowaychukbefore adoption5a48248

lines 698–714why, one step back
698
699<p>The <code>res.send()</code> method is a high level response utility allowing you to pass700objects to respond with json, strings for html, arbitrary _Buffer_s or numbers for status701code based responses. The following are all valid uses:</p>702703<pre><code> res.send(new Buffer('wahoo'));704 res.send({ some: 'json' });705 res.send('&lt;p&gt;some html&lt;/p&gt;');706 res.send('Sorry, cant find that', 404);707 res.send('text', { 'Content-Type': 'text/plain' }, 201);708 res.send(404);709</code></pre>710711<p>By default the <em>Content-Type</em> response header is set, however if explicitly712assigned through <code>res.send()</code> or previously with <code>res.header()</code> or <code>res.contentType()</code>713it will not be set again.</p>714

More docsTj Holowaychukbefore adoptionde81ee7

line 715why, one step back
715<h3 id="res-redirect-url-status-">res.redirect(url[, status])</h3>

More docsTj Holowaychukbefore adoptionbedff32

lines 716–728why, one step back
716
717<p>Redirect to the given <em>url</em> with a default response <em>status</em> of 302.</p>718719<pre><code>res.redirect('/', 301);720res.redirect('/account');721res.redirect('http://google.com');722res.redirect('home');723res.redirect('back');724</code></pre>725726<p>Express supports "redirect mapping", which by default provides <em>home</em>, and <em>back</em>.727The <em>back</em> map checks the <em>Referrer</em> and <em>Referer</em> headers, while <em>home</em> utilizes728the "home" setting and defaults to "/".</p>

More view docsTj Holowaychukbefore adoption1c8ff48

lines 729–741why, one step back
729
730<h3 id="res-render-view-options-fn-">res.render(view[, options[, fn]])</h3>731732<p>Render <em>view</em> with the given <em>options</em> and optional callback <em>fn</em>.733When a callback function is given a response will <em>not</em> be made734automatically, however otherwise a response of <em>200</em> and <em>text/html</em> is given.</p>735736<p> Most engines accept one or more of the following options,737 both <a href="http://github.com/visionmedia/haml.js">haml</a> and <a href="http://github.com/visionmedia/jade">jade</a> accept all:</p>738739<ul>740<li><em>context|scope</em> Template evaluation context (<em>this</em>)</li>741<li><em>locals</em> Object containing local variables</li>

Cleaning up view docsTj Holowaychukbefore adoption25c8883

line 742why, one step back
742<li><em>cache</em> Cache intermediate JavaScript in memory (the default in <em>production</em> mode)</li>

More view docsTj Holowaychukbefore adoption1c8ff48

lines 743–745why, one step back
743<li><em>debug</em> Output debugging information</li>
744</ul>745

In depth partial docsTj Holowaychukbefore adoption793943f

lines 746–793why, one step back
746
747<h3 id="res-partial-view-options-">res.partial(view[, options])</h3>748749<p>Render <em>view</em> partial with the given <em>options</em>. This method is always available750to the view as a local variable.</p>751752<ul>753<li><p><em>as</em> Variable name for each <em>collection</em> value, defaults to the view name.</p>754755<ul>756<li>as: 'something' will add the <em>something</em> local variable</li>757<li>as: this will use the collection value as the template context</li>758<li>as: global will merge the collection value's properties with <em>locals</em></li>759</ul>760</li>761<li><p><em>collection</em> Array of objects, the name is derived from the view name itself.762For example <em>video.html</em> will have a object <em>video</em> available to it.</p></li>763</ul>764765766<p>The following are equivalent, and the name of collection value when passed767to the partial will be <em>movie</em> as derived from the name.</p>768769<pre><code>partial('movie.jade', { collection: movies });770partial('movie.jade', movies);771partial('movie', movies);772// In view: movie.director773</code></pre>774775<p>To change the local from <em>movie</em> to <em>video</em> we can use the "as" option:</p>776777<pre><code>partial('movie', { collection: movies, as: 'video' });778// In view: video.director779</code></pre>780781<p>Also we can make our movie the value of <em>this</em> within our view so that instead782of <em>movie.director</em> we could use <em>this.director</em>.</p>783784<pre><code>partial('movie', { collection: movies, as: this });785// In view: this.director786</code></pre>787788<p>Another alternative is to "explode" the properties of the collection item into789pseudo globals (local variables) by using <em>as: global</em>, which again is syntactic sugar:</p>790791<pre><code>partials('movie', { collection: movies, as: global });792// In view: director793</code></pre>

More docsTj Holowaychukbefore adoptionbedff32

line 794why, one step back
794

More docsTj Holowaychukbefore adoptionde81ee7

lines 795–836why, one step back
795<h3 id="app-set-name-val-">app.set(name[, val])</h3>
796797<p>Apply an application level setting <em>name</em> to <em>val</em>, or798get the value of <em>name</em> when <em>val</em> is not present:</p>799800<pre><code>app.set('reload views', 200);801app.set('reload views');802// =&gt; 200803</code></pre>804805<h3 id="app-enable-name-">app.enable(name)</h3>806807<p>Enable the given setting <em>name</em>:</p>808809<pre><code>app.enable('some arbitrary setting');810app.set('some arbitrary setting');811// =&gt; true812</code></pre>813814<h3 id="app-disable-name-">app.disable(name)</h3>815816<p>Disable the given setting <em>name</em>:</p>817818<pre><code>app.disable('some setting');819app.set('some setting');820// =&gt; false821</code></pre>822823<h3 id="app-configure-env-function-function-">app.configure(env|function[, function])</h3>824825<p>Define a callback function for the given <em>env</em> (or all environments) with callback <em>function</em>:</p>826827<pre><code>app.configure(function(){828 // executed for each env829});830831app.configure('development', function(){832 // executed for 'development' only833});834</code></pre>835836<h3 id="app-redirect-name-val-">app.redirect(name, val)</h3>

More docsTj Holowaychukbefore adoptionbedff32

lines 837–859why, one step back
837
838<p>For use with <code>res.redirect()</code> we can map redirects at the application level as shown below:</p>839840<pre><code>app.redirect('google', 'http://google.com');841</code></pre>842843<p>Now in a route we may call:</p>844845<p> res.redirect('google');</p>846847<p>We may also map dynamic redirects:</p>848849<pre><code>app.redirect('comments', function(req, res, params){850 return '/post/' + params.id + '/comments';851});852</code></pre>853854<p>So now we may do the following, and the redirect will dynamically adjust to855the context of the request. If we called this route with <em>GET /post/12</em> our856redirect <em>Location</em> would be <em>/post/12/comments</em>.</p>857858<pre><code>app.get('/post/:id', function(req, res){859 res.redirect('comments');

Error handling docsTj Holowaychukbefore adoption9fbb9fa

lines 860–864why, one step back
860});
861</code></pre>862863<h3 id="app-error-function-">app.error(function)</h3>864

More error docsTj Holowaychukbefore adoption6227f5c

lines 865–867why, one step back
865<p>Adds an error handler <em>function</em> which will receive the exception as the first parameter as shown below.
866Note that we may set several error handlers by making several calls to this method, however the handler867should call <em>next(err)</em> if it does not wish to deal with the exception:</p>

Error handling docsTj Holowaychukbefore adoption9fbb9fa

lines 868–870why, one step back
868
869<pre><code>app.error(function(err, req, res, next){870 res.send(err.message, 500);

More docsTj Holowaychukbefore adoptionbedff32

line 871why, one step back
871});

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 872–881why, one step back
872</code></pre>
873874<h3 id="app-listen-port-host-">app.listen([port[, host]])</h3>875876<p>Bind the app server to the given <em>port</em>, which defaults to 3000. When <em>host</em> is omitted all877connections will be accepted via <em>INADDR_ANY</em>.</p>878879<pre><code>app.listen();880app.listen(3000);881app.listen(3000, 'n.n.n.n');

Added configuration sectionTj Holowaychukbefore adoption83d2555

line 882why, one step back
882</code></pre>

Configuration docsTj Holowaychukbefore adoptioncb04364

line 883why, one step back
883

Unix domain socket docsTj Holowaychukbefore adoption2fa7907

lines 884–900why, one step back
884<p>The <em>port</em> argument may also be a string representing the path to a unix domain socket:</p>
885886<pre><code>app.listen('/tmp/express.sock');887</code></pre>888889<p>Then try it out:</p>890891<pre><code>$ telnet /tmp/express.sock892GET / HTTP/1.1893894HTTP/1.1 200 OK895Content-Type: text/plain896Content-Length: 11897898Hello World899</code></pre>900

Started guideTj Holowaychukbefore adoption9e3a0c6

lines 901–905why, one step back
901</div>
902 </div>903 </div>904 </body>905</html>