Skip to content
Myr

guide.html

This file

Path

docs/guide.html

940 lines · 29.1 KiB · HTML

Last changed by

Updated docsTj Holowaychukbefore adoption4d32762

History of this file

Take it

Started guideTj Holowaychukbefore adoption9e3a0c6

lines 1–3why, one step back
1<html>
2 <head>3 <title>Express - node web framework</title>

Move menu up after scrolling. Closes #352Tj Holowaychukbefore adoptionce23e5f

lines 4–16why, one step back
4 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
5 <script>6 $(function(){7 var done;8 $(document).scroll(function(event){9 if (!done) {10 $('#toc').animate({11 top: 012 });13 }14 });15 });16 </script>

Started guideTj Holowaychukbefore adoption9e3a0c6

lines 17–59why, one step back
17 <style>
18 #header {19 position: absolute;20 top: 10px;21 left: 0;22 padding: 12px 0;23 text-indent: 40px;24 width: 100%;25 border-top: 1px solid rgba(0,0,0,0.7);26 border-bottom: 1px solid rgba(0,0,0,0.7);27 background: rgba(255,255,255,0.1) url(http://www.sencha.com/favicon.ico) no-repeat 15px 50%;28 text-align: left;29 color: #fff;30 }31 #tagline {32 margin-left: 75px;33 margin-bottom: 30px;34 color: rgba(255,255,255,0.7); }35 html {36 background: #1c1c1c url(images/bg.tile.jpg); }3738 body {39 margin: 0;40 padding-bottom: 30px;41 font: 14px/1.4 "Helvetica Neue", "Lucida Grande", "Arial";42 font-size: 14px;43 line-height: 1.5;44 -webkit-text-stroke: 1px rgba(0, 0, 0, 0.1);45 -moz-text-stroke: 1px rgba(0, 0, 0, 0.1);46 background: url(images/bg.jpg) 50% 0 no-repeat;47 color: #8b8b8b; }4849 * {50 outline: none; }5152 em {53 color: white; }5455 a img {56 border: none !important; }5758 a {59 font-weight: bold;

Merge branch 'docs' into connectTj Holowaychukbefore adoption492f70f

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

Started guideTj Holowaychukbefore adoption9e3a0c6

lines 61–128why, one step back
61 color: white;
62 -webkit-transition-property: opacity, -webkit-transform, color, background-color, padding, -webkit-box-shadow;63 -webkit-transition-duration: 0.15s;64 -webkit-transition-timing-function: ease-out; }65 a:hover {66 opacity: 0.8; }6768 h1, h2, h3 {69 margin: 45px 0 0 0;70 color: white;71 text-shadow: 1px 2px 2px rgba(0,0,0,0.6); }7273 h3 {74 font-size: 18px; }7576 pre {77 margin: 20px 10px;78 padding: 25px 20px;79 background: rgba(0,0,0,0.5);80 border: 1px solid #323232;81 -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);82 -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);83 -webkit-border-radius: 5px;84 -moz-border-radius: 5px; }8586 code {87 font-family: "Helvetica Neue", "Lucida Grande", "Arial"; }8889 ul {90 margin: 15px 0;91 padding: 0 0 0 35px; }92 ul li {93 margin: 0;94 padding: 2px 0;95 list-style: square; }9697 .sect {98 margin-left: 40px; }99100 #logo {101 display: block;102 margin-left: 30%;103 margin-bottom: 30px;104 width: 194px;105 height: 51px;106 background: url(images/logo.png) 0 0 no-repeat;107 text-indent: -99999px; }108 #logo:hover {109 opacity: 0.7; }110 #logo:active {111 opacity: 0.3; }112113 #ribbon {114 position: fixed;115 top: 0;116 right: 0;117 z-index: 2; }118119 #wrapper {120 width: 100%;121 min-height: 800px;122 background: url(images/top.png) 0 0 repeat-x; }123124 #container {125 margin: 0 auto;126 padding-top: 110px;127 width: 550px; }128

Merge branch 'docs' into connectTj Holowaychukbefore adoption492f70f

lines 129–139why, one step back
129 #toc {
130 position: fixed;131 top: 60px;132 left: 0;133 margin: 0 0 0 15px;134 padding: 15px;135 height: 100%;136 background: rgba(0,0,0,0.2);137 border-right: 1px solid rgba(255,255,255,0.05);138 }139 #toc li {

More view docsTj Holowaychukbefore adoption1c8ff48

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

Merge branch 'docs' into connectTj Holowaychukbefore adoption492f70f

lines 141–145why, one step back
141 list-style: none;
142 }143 #toc li a {144 font-size: 11px;145 }

Started guideTj Holowaychukbefore adoption9e3a0c6

line 146why, one step back
146 #menu {

Added contrib docsTj Holowaychukbefore adoptionf0ba57b

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

Started guideTj Holowaychukbefore adoption9e3a0c6

lines 148–162why, one step back
148 padding: 0;
149 padding-bottom: 30px; }150 #menu li {151 display: inline;152 list-style: none; }153 #menu li a {154 display: block;155 float: left;156 margin: 0 2px;157 padding: 3px 15px;158 background: rgba(0,0,0,0.2);159 -webkit-border-radius: 8px;160 -moz-border-radius: 8px;161 -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);162 -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);

Removed annoying padding transitionTj Holowaychukbefore adoption346b2a2

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

Started guideTj Holowaychukbefore adoption9e3a0c6

lines 164–180why, one step back
164 -webkit-transition-duration: 0.15s;
165 -webkit-transition-timing-function: ease-out; }166 #menu li a:hover,167 #menu li a.active {168 background: rgba(0,0,0,0.5); }169 #menu li a:active {170 background: rgba(0,0,0,0.1);171 -webkit-box-shadow: 1px 1px 1px rgba(0,0,0,0.4);172 -moz-box-shadow: 1px 1px 1px rgba(0,0,0,0.4); }173 </style>174 </head>175 <body>176 <a href='http://github.com/visionmedia/express'> 177 <img alt='Fork me on GitHub' id='ribbon' src='http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png' /> 178 </a>179 <div id="header"><strong>Sencha</strong> labs</div>180 <div id="wrapper">

Merge branch 'docs' into connectTj Holowaychukbefore adoption492f70f

lines 181–188why, one step back
181 <div id="container"><ul id="toc">
182<li><a href="#Installation">Installation</a></li>183<li><a href="#Creating-An-Application">Creating An Application</a></li>184<li><a href="#Configuration">Configuration</a></li>185<li><a href="#Settings">Settings</a></li>186<li><a href="#Routing">Routing</a></li>187<li><a href="#Passing-Route-Control">Passing Route Control</a></li>188<li><a href="#Middleware">Middleware</a></li>

Error handling docsTj Holowaychukbefore adoption9fbb9fa

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

Added more view docsTj Holowaychukbefore adoption2f96506

lines 190–192why, one step back
190<li><a href="#View-Rendering">View Rendering</a></li>
191<li><a href="#View-Partials">View Partials</a></li>192<li><a href="#Template-Engines">Template Engines</a></li>

More docsTj Holowaychukbefore adoptionde81ee7

lines 193–204why, one step back
193<li><a href="#req-header-key-defaultValue-">req.header()</a></li>
194<li><a href="#req-accepts-type-">req.accepts()</a></li>195<li><a href="#req-param-name-">req.param()</a></li>196<li><a href="#req-flash-type-msg-">req.flash()</a></li>197<li><a href="#req-isXMLHttpRequest">req.isXMLHttpRequest</a></li>198<li><a href="#res-header-key-val-">res.header()</a></li>199<li><a href="#res-contentType-type-">res.contentType()</a></li>200<li><a href="#res-attachment-filename-">res.attachment()</a></li>201<li><a href="#res-sendfile-path-">res.sendfile()</a></li>202<li><a href="#res-download-file-filename-">res.download()</a></li>203<li><a href="#res-send-body-status-headers-status-status-">res.send()</a></li>204<li><a href="#res-redirect-url-status-">res.redirect()</a></li>

More view docsTj Holowaychukbefore adoption1c8ff48

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

In depth partial docsTj Holowaychukbefore adoption793943f

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

More docsTj Holowaychukbefore adoptionde81ee7

lines 207–211why, one step back
207<li><a href="#app-set-name-val-">app.set()</a></li>
208<li><a href="#app-enable-name-">app.enable()</a></li>209<li><a href="#app-disable-name-">app.disable()</a></li>210<li><a href="#app-configure-env-function-function-">app.configure()</a></li>211<li><a href="#app-redirect-name-val-">app.redirect()</a></li>

Error handling docsTj Holowaychukbefore adoption9fbb9fa

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

Added app.helpers() docsTj Holowaychukbefore adoption7eb088b

line 213why, one step back
213<li><a href="#app-helpers-obj-">app.helpers()</a></li>

Docs for Server#{helpers,dynamicHelpers}()Tj Holowaychukbefore adoption78008d1

line 214why, one step back
214<li><a href="#app-dynamicHelpers-obj-">app.dynamicHelpers()</a></li>

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

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

Merge branch 'docs' into connectTj Holowaychukbefore adoption492f70f

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

Started guideTj Holowaychukbefore adoption9e3a0c6

lines 217–224why, one step back
217 <a href='http://github.com/visionmedia/express' id='logo'>Express</a>
218 <p id="tagline">219 High performance, high class web development for220 <a href="http://nodejs.org">Node.js</a>221 </p>222 <ul id="menu">223 <li><a href="index.html">Home</a></li>224 <li><a href="guide.html">Guide</a></li>

Added contrib docsTj Holowaychukbefore adoptionf0ba57b

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

Started guideTj Holowaychukbefore adoption9e3a0c6

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

Install docsTj Holowaychukbefore adoption63d02f1

lines 229–230why, one step back
229<h3 id="Installation">Installation</h3>
230

Updated curl install docsTj Holowaychukbefore adoption3ceb0de

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

Install docsTj Holowaychukbefore adoption63d02f1

line 232why, one step back
232

Updated curl install docsTj Holowaychukbefore adoption3ceb0de

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

Install docsTj Holowaychukbefore adoption63d02f1

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

Updated curl install docsTj Holowaychukbefore adoption3ceb0de

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

Install docsTj Holowaychukbefore adoption63d02f1

line 237why, one step back
237

Updated curl install docsTj Holowaychukbefore adoption3ceb0de

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

Install docsTj Holowaychukbefore adoption63d02f1

lines 240–241why, one step back
240</code></pre>
241

Updated install docsTj Holowaychukbefore adoption193c4a9

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

Install docsTj Holowaychukbefore adoption63d02f1

line 243why, one step back
243

Updated install docsTj Holowaychukbefore adoption193c4a9

lines 244–246why, one step back
244<pre><code>$ git submodule update --init
245$ make install246$ make install-support

Install docsTj Holowaychukbefore adoption63d02f1

lines 247–248why, one step back
247</code></pre>
248

Removed annoying padding transitionTj Holowaychukbefore adoption346b2a2

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

Added configuration sectionTj Holowaychukbefore adoption83d2555

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

Moved config docs upTj Holowaychukbefore adoption6e8413f

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

Added configuration sectionTj Holowaychukbefore adoption83d2555

lines 254–263why, one step back
254
255<pre><code>var app = require('express').createServer();256257app.get('/', function(req, res){258 res.send('hello world');259});260261app.listen(3000);262</code></pre>263

Moved config docs upTj Holowaychukbefore adoption6e8413f

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

Updated docsTj Holowaychukbefore adoptioncacada8

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

Moved config docs upTj Holowaychukbefore adoption6e8413f

lines 277–279why, one step back
277});
278279app.configure('development', function(){

Updated docsTj Holowaychukbefore adoptioncacada8

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

Moved config docs upTj Holowaychukbefore adoption6e8413f

lines 281–283why, one step back
281});
282283app.configure('production', function(){

Updated docsTj Holowaychukbefore adoptioncacada8

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

Moved config docs upTj Holowaychukbefore adoption6e8413f

lines 285–302why, one step back
285});
286</code></pre>287288<p>For internal and arbitrary settings Express provides the <em>set(key[, val])</em>, <em>enable(key)</em>, <em>disable(key)</em> methods:</p>289290<pre><code>app.configure(function(){291 app.set('views', __dirname + '/views');292 app.set('views');293 // =&gt; "... views directory ..."294295 app.enable('some feature');296 // same as app.set('some feature', true);297298 app.disable('some feature');299 // same as app.set('some feature', false);300});301</code></pre>302

More docsTj Holowaychukbefore adoptionde81ee7

lines 303–308why, one step back
303<p>To alter the environment we can set the <em>CONNECT_ENV</em> environment variable,
304or more specifically <em>EXPRESS_ENV</em>, for example:</p>305306<pre><code>$ EXPRESS_ENV=production node app.js307</code></pre>308

Moved config docs upTj Holowaychukbefore adoption6e8413f

lines 309–313why, one step back
309<h3 id="Settings">Settings</h3>
310311<p>Express supports the following settings out of the box:</p>312313<ul>

Updated docsTj Holowaychukbefore adoption4d32762

lines 314–316why, one step back
314<li><em>env</em> Application environment set internally, use <em>app.set('env')</em> on <em>Server#listen()</em></li>
315<li><em>home</em> Application base path used with <em>res.redirect()</em>, when mounted defaults316to <em>Server#route</em>, otherwise "/". Assigned on <em>Server#listen()</em>.</li>

Moved config docs upTj Holowaychukbefore adoption6e8413f

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

Added more setting docsTj Holowaychukbefore adoption38281e6

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

Fixed reload views exampleTj Holowaychukbefore adoption5fcf971

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

Moved config docs upTj Holowaychukbefore adoption6e8413f

lines 321–323why, one step back
321</ul>
322323

Started guideTj Holowaychukbefore adoption9e3a0c6

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

Route passing docsTj Holowaychukbefore adoption0ace97c

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

Started guideTj Holowaychukbefore adoption9e3a0c6

lines 330–376why, one step back
330
331<pre><code>app.get('/user/:id', function(req, res, params){332 res.send('user ' + params.id);333});334</code></pre>335336<p>A route is simple a string which is compiled to a <em>RegExp</em> internally. For example337when <em>/user/:id</em> is compiled, a simplified version of the regexp may look similar to:</p>338339<pre><code>\/user\/([^\/]+)\/?340</code></pre>341342<p>Literal regular expressions may also be passed for complex uses:</p>343344<pre><code>app.get(/^\/foo(bar)?$/, function(){});345</code></pre>346347<p>Below are some route examples, and the associated paths that they348may consume:</p>349350<pre><code> "/user/:id"351 /user/12352353 "/users/:id?"354 /users/5355 /users356357 "/files/*"358 /files/jquery.js359 /files/javascripts/jquery.js360361 "/file/*.*"362 /files/jquery.js363 /files/javascripts/jquery.js364365 "/user/:id/:operation?"366 /user/1367 /user/1/edit368369 "/products.:format"370 /products.json371 /products.xml372373 "/products.:format?"374 /products.json375 /products.xml376 /products

Route passing docsTj Holowaychukbefore adoption0ace97c

lines 377–393why, one step back
377</code></pre>
378379<h3 id="Passing-Route-Control">Passing Route Control</h3>380381<p>We may pass control to the next <em>matching</em> route, by calling the <em>fourth</em> parameter,382the <em>next()</em> function. When a match cannot be made, control is passed back to Connect.</p>383384<pre><code>app.get('/users/:id?', function(req, res, params){385 if (params.id) {386 // do something387 } else {388 next();389 }390});391392app.get('/users', function(req, res, params){393 // do something else

Added configuration sectionTj Holowaychukbefore adoption83d2555

lines 394–408why, one step back
394});
395</code></pre>396397<h3 id="Middleware">Middleware</h3>398399<p>The Express <em>Plugin</em> is no more! middleware via <a href="http://github.com/extjs/Connect">Connect</a> can be400passed to <em>express.createServer()</em> as you would with a regular Connect server. For example:</p>401402<pre><code>var connect = require('connect'),403 express = require('express');404405var app = express.createServer(406 connect.logger(),407 connect.bodyDecoder()408);

More docsTj Holowaychukbefore adoptionbedff32

lines 409–410why, one step back
409</code></pre>
410

Error handling docsTj Holowaychukbefore adoption9fbb9fa

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

Updated docsTj Holowaychukbefore adoptioncacada8

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

Error handling docsTj Holowaychukbefore adoption9fbb9fa

lines 414–462why, one step back
414</code></pre>
415416<h3 id="Error-Handling">Error Handling</h3>417418<p>Express provides the <em>app.error()</em> method which receives exceptions thrown within a route,419or passed to <em>next(err)</em>. Below is an example which serves different pages based on our420ad-hoc <em>NotFound</em> exception:</p>421422<pre><code>function NotFound(msg){423 this.name = 'NotFound';424 Error.call(this, msg);425 Error.captureStackTrace(this, arguments.callee);426}427428sys.inherits(NotFound, Error);429430app.get('/404', function(req, res){431 throw new NotFound;432});433434app.get('/500', function(req, res){435 throw new Error('keyboard cat!');436});437</code></pre>438439<p>We can call <em>app.error()</em> several times as shown below.440Here we check for an instanceof <em>NotFound</em> and show the441404 page, or we pass on to the next error handler.</p>442443<pre><code>app.error(function(err, req, res, next){444 if (err instanceof NotFound) {445 res.render('404.jade');446 } else {447 next(err);448 }449});450</code></pre>451452<p>Here we assume all errors as 500 for the simplicity of453this demo, however you can choose whatever you like</p>454455<pre><code>app.error(function(err, req, res){456 res.render('500.jade', {457 locals: {458 error: err459 } 460 });461});462</code></pre>

Added more view docsTj Holowaychukbefore adoption2f96506

line 463why, one step back
463

More error handling docsTj Holowaychukbefore adoptioncea401c

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

Updated docsTj Holowaychukbefore adoptioncacada8

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

More error handling docsTj Holowaychukbefore adoptioncea401c

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

Updated docsTj Holowaychukbefore adoptioncacada8

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

More error handling docsTj Holowaychukbefore adoptioncea401c

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

Added more view docsTj Holowaychukbefore adoption2f96506

lines 480–498why, one step back
480<h3 id="View-Rendering">View Rendering</h3>
481482<p>View filenames take the form <em>Express</em>.<em>ENGINE</em>, where <em>ENGINE</em> is the name483of the module that will be required. For example the view <em>layout.ejs</em> will484tell the view system to <em>require('ejs')</em>, the module being loaded must (currently)485export the method <em>exports.render(str, options)</em> to comply with Express, however486with will likely be extensible in the future.</p>487488<p>Below is an example using <a href="http://github.com/visionmedia/haml.js">Haml.js</a> to render <em>index.html</em>,489and since we do not use <em>layout: false</em> the rendered contents of <em>index.html</em> will be passed as490the <em>body</em> local variable in <em>layout.haml</em>.</p>491492<pre><code>app.get('/', function(req, res){493 res.render('index.haml', {494 locals: { title: 'My Site' }495 });496});497</code></pre>498

Docs for "view engine" settingTj Holowaychukbefore adoption1810c9b

lines 499–520why, one step back
499<p>The new <em>view engine</em> setting allows us to specify our default template engine,
500so for example when using <a href="http://github.com/visionmedia/jade">Jade</a> we could set:</p>501502<pre><code>app.set('view engine', 'jade');503</code></pre>504505<p>Allowing us to render with:</p>506507<pre><code>res.render('index');508</code></pre>509510<p>vs:</p>511512<pre><code>res.render('index.jade');513</code></pre>514515<p>When <em>view engine</em> is set, extensions are entirely optional, however we can still516mix and match template engines:</p>517518<pre><code>res.render('another-page.ejs');519</code></pre>520

Added more view docsTj Holowaychukbefore adoption2f96506

lines 521–541why, one step back
521<h3 id="View-Partials">View Partials</h3>
522523<p>The Express view system has built-in support for partials and collections, which are524sort of "mini" views representing a document fragment. For example rather than iterating525in a view to display comments, we would use a partial with collection support:</p>526527<pre><code>partial('comment.haml', { collection: comments });528</code></pre>529530<p>To make things even less verbose we can assume the extension as <em>.haml</em> when omitted,531however if we wished we could use an ejs partial, within a haml view for example.</p>532533<pre><code>partial('comment', { collection: comments });534</code></pre>535536<p>And once again even further, when rendering a collection we can simply pass537an array, if no other options are desired:</p>538539<pre><code>partial('comments', comments);540</code></pre>541

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

lines 542–550why, one step back
542<p>When using the partial collection support a few "magic" variables are provided
543for free:</p>544545<ul>546<li><em>firstInCollection</em> True if this is the first object</li>547<li><em>indexInCollection</em> Index of the object in the collection</li>548<li>_lastInCollection _ True if this is the last object</li>549</ul>550

Added view helper docs. Closes #344Tj Holowaychukbefore adoptionc4b5208

line 551why, one step back
551

Added more view docsTj Holowaychukbefore adoption2f96506

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

Error handling docsTj Holowaychukbefore adoption9fbb9fa

line 562why, one step back
562

More docsTj Holowaychukbefore adoptionde81ee7

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

More docsTj Holowaychukbefore adoptiondfecbe5

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

More docsTj Holowaychukbefore adoptionde81ee7

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

More docsTj Holowaychukbefore adoptiondfecbe5

lines 573–596why, one step back
573
574<p>Check if the <em>Accept</em> header is present, and includes the given <em>type</em>.</p>575576<p>When the <em>Accept</em> header is not present <em>true</em> is returned. Otherwise577the given <em>type</em> is matched by an exact match, and then subtypes. You578may pass the subtype such as "html" which is then converted internally579to "text/html" using the mime lookup table.</p>580581<pre><code>// Accept: text/html582req.accepts('html');583// =&gt; true584585// Accept: text/*; application/json586req.accepts('html');587req.accepts('text/html');588req.accepts('text/plain');589req.accepts('application/json');590// =&gt; true591592req.accepts('image/png');593req.accepts('png');594// =&gt; false595</code></pre>596

More docsTj Holowaychukbefore adoptionde81ee7

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

More docsTj Holowaychukbefore adoptiondfecbe5

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

More docsTj Holowaychukbefore adoptionde81ee7

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

More docsTj Holowaychukbefore adoptiondfecbe5

lines 613–630why, one step back
613
614<p>Queue flash <em>msg</em> of the given <em>type</em>.</p>615616<pre><code>req.flash('info', 'email sent');617req.flash('error', 'email delivery failed');618req.flash('info', 'email re-sent');619// =&gt; 2620621req.flash('info');622// =&gt; ['email sent', 'email re-sent']623624req.flash('info');625// =&gt; []626627req.flash();628// =&gt; { error: ['email delivery failed'], info: [] }629</code></pre>630

More docsTj Holowaychukbefore adoptionde81ee7

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

More docsTj Holowaychukbefore adoptiondfecbe5

lines 632–639why, one step back
632
633<p>Also aliased as <em>req.xhr</em>, this getter checks the <em>X-Requested-With</em> header634to see if it was issued by an <em>XMLHttpRequest</em>:</p>635636<pre><code>req.xhr637req.isXMLHttpRequest638</code></pre>639

More docsTj Holowaychukbefore adoptionde81ee7

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

More docs :)Tj Holowaychukbefore adoption0c5fe61

lines 641–653why, one step back
641
642<p>Get or set the response header <em>key</em>.</p>643644<pre><code>res.header('Content-Length');645// =&gt; undefined646647res.header('Content-Length', 123);648// =&gt; 123649650res.header('Content-Length');651// =&gt; 123652</code></pre>653

More docsTj Holowaychukbefore adoptionde81ee7

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

More docs :)Tj Holowaychukbefore adoption0c5fe61

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

More docsTj Holowaychukbefore adoptionde81ee7

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

Docs for res.attachment()Tj Holowaychukbefore adoptioneb235e1

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

More docsTj Holowaychukbefore adoptionde81ee7

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

Docs for new res methodsTj Holowaychukbefore adoptionb08dab9

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

Better docs for res.sendfile()Tj Holowaychukbefore adoption8a337f1

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

Docs for new res methodsTj Holowaychukbefore adoptionb08dab9

line 681why, one step back
681

More docsTj Holowaychukbefore adoptionde81ee7

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

Docs for new res methodsTj Holowaychukbefore adoptionb08dab9

lines 683–695why, one step back
683
684<p>Transfer the given <em>file</em> as an attachment with optional alternative <em>filename</em>.</p>685686<pre><code>res.download('path/to/image.png');687res.download('path/to/image.png', 'foo.png');688</code></pre>689690<p>This is equivalent to:</p>691692<pre><code>res.attachment(file);693res.sendfile(file);694</code></pre>695

More docsTj Holowaychukbefore adoptionde81ee7

line 696why, one step back
696<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 697–713why, one step back
697
698<p>The <code>res.send()</code> method is a high level response utility allowing you to pass699objects to respond with json, strings for html, arbitrary _Buffer_s or numbers for status700code based responses. The following are all valid uses:</p>701702<pre><code> res.send(new Buffer('wahoo'));703 res.send({ some: 'json' });704 res.send('&lt;p&gt;some html&lt;/p&gt;');705 res.send('Sorry, cant find that', 404);706 res.send('text', { 'Content-Type': 'text/plain' }, 201);707 res.send(404);708</code></pre>709710<p>By default the <em>Content-Type</em> response header is set, however if explicitly711assigned through <code>res.send()</code> or previously with <code>res.header()</code> or <code>res.contentType()</code>712it will not be set again.</p>713

More docsTj Holowaychukbefore adoptionde81ee7

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

More docsTj Holowaychukbefore adoptionbedff32

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

More view docsTj Holowaychukbefore adoption1c8ff48

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

Cleaning up view docsTj Holowaychukbefore adoption25c8883

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

More view docsTj Holowaychukbefore adoption1c8ff48

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

In depth partial docsTj Holowaychukbefore adoption793943f

lines 745–792why, one step back
745
746<h3 id="res-partial-view-options-">res.partial(view[, options])</h3>747748<p>Render <em>view</em> partial with the given <em>options</em>. This method is always available749to the view as a local variable.</p>750751<ul>752<li><p><em>as</em> Variable name for each <em>collection</em> value, defaults to the view name.</p>753754<ul>755<li>as: 'something' will add the <em>something</em> local variable</li>756<li>as: this will use the collection value as the template context</li>757<li>as: global will merge the collection value's properties with <em>locals</em></li>758</ul>759</li>760<li><p><em>collection</em> Array of objects, the name is derived from the view name itself.761For example <em>video.html</em> will have a object <em>video</em> available to it.</p></li>762</ul>763764765<p>The following are equivalent, and the name of collection value when passed766to the partial will be <em>movie</em> as derived from the name.</p>767768<pre><code>partial('movie.jade', { collection: movies });769partial('movie.jade', movies);770partial('movie', movies);771// In view: movie.director772</code></pre>773774<p>To change the local from <em>movie</em> to <em>video</em> we can use the "as" option:</p>775776<pre><code>partial('movie', { collection: movies, as: 'video' });777// In view: video.director778</code></pre>779780<p>Also we can make our movie the value of <em>this</em> within our view so that instead781of <em>movie.director</em> we could use <em>this.director</em>.</p>782783<pre><code>partial('movie', { collection: movies, as: this });784// In view: this.director785</code></pre>786787<p>Another alternative is to "explode" the properties of the collection item into788pseudo globals (local variables) by using <em>as: global</em>, which again is syntactic sugar:</p>789790<pre><code>partials('movie', { collection: movies, as: global });791// In view: director792</code></pre>

More docsTj Holowaychukbefore adoptionbedff32

line 793why, one step back
793

More docsTj Holowaychukbefore adoptionde81ee7

lines 794–835why, one step back
794<h3 id="app-set-name-val-">app.set(name[, val])</h3>
795796<p>Apply an application level setting <em>name</em> to <em>val</em>, or797get the value of <em>name</em> when <em>val</em> is not present:</p>798799<pre><code>app.set('reload views', 200);800app.set('reload views');801// =&gt; 200802</code></pre>803804<h3 id="app-enable-name-">app.enable(name)</h3>805806<p>Enable the given setting <em>name</em>:</p>807808<pre><code>app.enable('some arbitrary setting');809app.set('some arbitrary setting');810// =&gt; true811</code></pre>812813<h3 id="app-disable-name-">app.disable(name)</h3>814815<p>Disable the given setting <em>name</em>:</p>816817<pre><code>app.disable('some setting');818app.set('some setting');819// =&gt; false820</code></pre>821822<h3 id="app-configure-env-function-function-">app.configure(env|function[, function])</h3>823824<p>Define a callback function for the given <em>env</em> (or all environments) with callback <em>function</em>:</p>825826<pre><code>app.configure(function(){827 // executed for each env828});829830app.configure('development', function(){831 // executed for 'development' only832});833</code></pre>834835<h3 id="app-redirect-name-val-">app.redirect(name, val)</h3>

More docsTj Holowaychukbefore adoptionbedff32

lines 836–858why, one step back
836
837<p>For use with <code>res.redirect()</code> we can map redirects at the application level as shown below:</p>838839<pre><code>app.redirect('google', 'http://google.com');840</code></pre>841842<p>Now in a route we may call:</p>843844<p> res.redirect('google');</p>845846<p>We may also map dynamic redirects:</p>847848<pre><code>app.redirect('comments', function(req, res, params){849 return '/post/' + params.id + '/comments';850});851</code></pre>852853<p>So now we may do the following, and the redirect will dynamically adjust to854the context of the request. If we called this route with <em>GET /post/12</em> our855redirect <em>Location</em> would be <em>/post/12/comments</em>.</p>856857<pre><code>app.get('/post/:id', function(req, res){858 res.redirect('comments');

Error handling docsTj Holowaychukbefore adoption9fbb9fa

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

More error docsTj Holowaychukbefore adoption6227f5c

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

Error handling docsTj Holowaychukbefore adoption9fbb9fa

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

More docsTj Holowaychukbefore adoptionbedff32

line 870why, one step back
870});

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

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

Added app.helpers() docsTj Holowaychukbefore adoption7eb088b

lines 872–874why, one step back
872
873<h3 id="app-helpers-obj-">app.helpers(obj)</h3>874

Docs for Server#{helpers,dynamicHelpers}()Tj Holowaychukbefore adoption78008d1

lines 875–892why, one step back
875<p>Registers static view helpers.</p>
876877<pre><code>app.helpers({878 name: function(first, last){ return first + ', ' + last },879 firstName: 'tj',880 lastName: 'holowaychuk'881});882</code></pre>883884<p>Our view could now utilize the <em>firstName</em> and <em>lastName</em> variables,885as well as the <em>name()</em> function exposed.</p>886887<pre><code>&lt;%= name(firstName, lastName) %>888</code></pre>889890<h3 id="app-dynamicHelpers-obj-">app.dynamicHelpers(obj)</h3>891892<p>Registers dynamic view helpers. Dynamic view helpers

Added app.helpers() docsTj Holowaychukbefore adoption7eb088b

line 893why, one step back
893are simply functions which accept <em>req</em>, <em>res</em>, and <em>params</em>, and are

Docs for Server#{helpers,dynamicHelpers}()Tj Holowaychukbefore adoption78008d1

line 894why, one step back
894evaluated against the <em>Server</em> instance before a view is rendered. The <em>return value</em> of this function

Added app.helpers() docsTj Holowaychukbefore adoption7eb088b

lines 895–896why, one step back
895becomes the local variable it is associated with.</p>
896

Docs for Server#{helpers,dynamicHelpers}()Tj Holowaychukbefore adoption78008d1

line 897why, one step back
897<pre><code>app.dynamicHelpers({

Added app.helpers() docsTj Holowaychukbefore adoption7eb088b

lines 898–903why, one step back
898 session: function(req, res, params){
899 return req.session;900 }901});902</code></pre>903

Docs for Server#{helpers,dynamicHelpers}()Tj Holowaychukbefore adoption78008d1

lines 904–907why, one step back
904<p>All views would now have <em>session</em> available so that session data can be accessed via <em>session.name</em> etc:</p>
905906<pre><code>&lt;%= session.name %&gt;907</code></pre>

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 908–916why, one step back
908
909<h3 id="app-listen-port-host-">app.listen([port[, host]])</h3>910911<p>Bind the app server to the given <em>port</em>, which defaults to 3000. When <em>host</em> is omitted all912connections will be accepted via <em>INADDR_ANY</em>.</p>913914<pre><code>app.listen();915app.listen(3000);916app.listen(3000, 'n.n.n.n');

Added configuration sectionTj Holowaychukbefore adoption83d2555

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

Configuration docsTj Holowaychukbefore adoptioncb04364

line 918why, one step back
918

Unix domain socket docsTj Holowaychukbefore adoption2fa7907

lines 919–935why, one step back
919<p>The <em>port</em> argument may also be a string representing the path to a unix domain socket:</p>
920921<pre><code>app.listen('/tmp/express.sock');922</code></pre>923924<p>Then try it out:</p>925926<pre><code>$ telnet /tmp/express.sock927GET / HTTP/1.1928929HTTP/1.1 200 OK930Content-Type: text/plain931Content-Length: 11932933Hello World934</code></pre>935

Started guideTj Holowaychukbefore adoption9e3a0c6

lines 936–940why, one step back
936</div>
937 </div>938 </div>939 </body>940</html>