Skip to content
Myr

guide.html

This file

Path

docs/guide.html

975 lines · 30.1 KiB · HTML

Last changed by

Added "Apps using express" section, PM me to add to the listTj Holowaychukbefore adoption7328842

History of this file

Take it

The walk stopped after 200 commits: lines older than 9a1f62f point at it.

Added multipart exampleTj Holowaychukbefore adoption9a1f62f

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>

Added multipart exampleTj Holowaychukbefore adoption9a1f62f

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;

Added multipart exampleTj Holowaychukbefore adoption9a1f62f

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 }

Added multipart exampleTj Holowaychukbefore adoption9a1f62f

line 146why, one step back
146 #menu {

Added contrib docsTj Holowaychukbefore adoptionf0ba57b

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

Added multipart exampleTj Holowaychukbefore adoption9a1f62f

lines 148–180why, 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);163 -webkit-transition-property: opacity, -webkit-transform, color, background-color, -webkit-box-shadow;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>

Rebuilt docsTj Holowaychukbefore adoptionba88e48

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

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

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

Merge branch 'docs' into connectTj Holowaychukbefore adoption492f70f

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

Added multipart exampleTj Holowaychukbefore adoption9a1f62f

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

Added contrib docsTj Holowaychukbefore adoptionf0ba57b

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

Added multipart exampleTj Holowaychukbefore adoption9a1f62f

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

Install docsTj Holowaychukbefore adoption63d02f1

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

No "or similar" for curl installTj Holowaychukbefore adoption56a1dd9

line 232why, one step back
232<p>curl:</p>

Install docsTj Holowaychukbefore adoption63d02f1

line 233why, one step back
233

Updated curl install docsTj Holowaychukbefore adoption3ceb0de

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

Install docsTj Holowaychukbefore adoption63d02f1

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

Updated curl install docsTj Holowaychukbefore adoption3ceb0de

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

Install docsTj Holowaychukbefore adoption63d02f1

line 238why, one step back
238

Added "Apps using express" section, PM me to add to the listTj Holowaychukbefore adoption7328842

line 239why, one step back
239<pre><code>$ 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

Added multipart exampleTj Holowaychukbefore adoption9a1f62f

lines 249–271why, one step back
249<h3 id="Creating-An-Application">Creating An Application</h3>
250251<p>The <em>express.Server</em> now inherits from <em>http.Server</em>, however252follows the same idiom by providing <em>express.createServer()</em> as shown below. This means253that you can utilize Express server's transparently with other libraries.</p>254255<pre><code>var app = require('express').createServer();256257app.get('/', function(req, res){258 res.send('hello world');259});260261app.listen(3000);262</code></pre>263264<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 traces

Updated configuration docsTj Holowaychukbefore adoptionf8ccaa3

lines 272–274why, one step back
272in <em>development</em> mode, however for both environments we utilize <em>methodOverride</em> and <em>bodyDecoder</em>.
273Note the use of <em>app.router</em>, which can (optionally) be used to mount the application routes,274otherwise the first call to <em>app.{get,put,del,post}()</em> will mount the routes.</p>

Added multipart exampleTj Holowaychukbefore adoption9a1f62f

lines 275–276why, one step back
275
276<pre><code>app.configure(function(){

Updated docsTj Holowaychukbefore adoptioncacada8

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

Updated configuration docsTj Holowaychukbefore adoptionf8ccaa3

lines 279–280why, one step back
279 app.use(app.router);
280 app.use(connect.staticProvider(__dirname + '/public'));

Added multipart exampleTj Holowaychukbefore adoption9a1f62f

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

Updated docsTj Holowaychukbefore adoptioncacada8

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

Added multipart exampleTj Holowaychukbefore adoption9a1f62f

lines 285–287why, one step back
285});
286287app.configure('production', function(){

Updated docsTj Holowaychukbefore adoptioncacada8

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

Added multipart exampleTj Holowaychukbefore adoption9a1f62f

lines 289–306why, one step back
289});
290</code></pre>291292<p>For internal and arbitrary settings Express provides the <em>set(key[, val])</em>, <em>enable(key)</em>, <em>disable(key)</em> methods:</p>293294<pre><code>app.configure(function(){295 app.set('views', __dirname + '/views');296 app.set('views');297 // =&gt; "... views directory ..."298299 app.enable('some feature');300 // same as app.set('some feature', true);301302 app.disable('some feature');303 // same as app.set('some feature', false);304});305</code></pre>306

More docsTj Holowaychukbefore adoptionde81ee7

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

Added multipart exampleTj Holowaychukbefore adoption9a1f62f

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

Updated docsTj Holowaychukbefore adoption4d32762

line 318why, one step back
318<li><em>env</em> Application environment set internally, use <em>app.set('env')</em> on <em>Server#listen()</em></li>

New "home" docsTj Holowaychukbefore adoption1e7a12f

line 319why, one step back
319<li><em>home</em> Application base path used for <em>res.redirect()</em> and transparently handling mounted apps.</li>

Added multipart exampleTj Holowaychukbefore adoption9a1f62f

lines 320–322why, one step back
320<li><em>views</em> Root views directory defaulting to <strong>CWD/views</strong></li>
321<li><em>view engine</em> Default view engine name for views rendered without extensions</li>322<li><em>reload views</em> Reloads altered views, by default watches for <em>mtime</em> changes with

Updated docsTj Holowaychukbefore adoption6234268

line 323why, one step back
323 with a 500 millisecond interval.</li>

Added multipart exampleTj Holowaychukbefore adoption9a1f62f

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

Updated docsTj Holowaychukbefore adoptiond691ecb

lines 331–332why, one step back
331can be done by defining the route below. The values associated to the named placeholders
332are available as <code>req.params</code>.</p>

Added multipart exampleTj Holowaychukbefore adoption9a1f62f

line 333why, one step back
333

Updated docsTj Holowaychukbefore adoptiond691ecb

lines 334–335why, one step back
334<pre><code>app.get('/user/:id', function(req, res){
335 res.send('user ' + req.params.id);

Added multipart exampleTj Holowaychukbefore adoption9a1f62f

lines 336–344why, one step back
336});
337</code></pre>338339<p>A route is simple a string which is compiled to a <em>RegExp</em> internally. For example340when <em>/user/:id</em> is compiled, a simplified version of the regexp may look similar to:</p>341342<pre><code>\/user\/([^\/]+)\/?343</code></pre>344

Docs for params.captures. Closes #358Tj Holowaychukbefore adoptiond03821b

line 345why, one step back
345<p>Regular expression literals may also be passed for complex uses. Since capture

Updated docsTj Holowaychukbefore adoptiond691ecb

line 346why, one step back
346groups with literal <em>RegExp</em>'s are anonymous we can access them directly <code>req.params</code>.</p>

Added multipart exampleTj Holowaychukbefore adoption9a1f62f

line 347why, one step back
347

Updated docsTj Holowaychukbefore adoptiond691ecb

lines 348–349why, one step back
348<pre><code>app.get(/^\/users?(?:\/(\d+)(?:\.\.(\d+))?)?/, function(req, res){
349 res.send(req.params);

Docs for params.captures. Closes #358Tj Holowaychukbefore adoptiond03821b

lines 350–362why, one step back
350});
351</code></pre>352353<p>Curl requests against the previously defined route:</p>354355<pre><code> $ curl http://dev:3000/user356 [null,null]357 $ curl http://dev:3000/users358 [null,null]359 $ curl http://dev:3000/users/1360 ["1",null]361 $ curl http://dev:3000/users/1..15362 ["1","15"]

Added multipart exampleTj Holowaychukbefore adoption9a1f62f

lines 363–398why, one step back
363</code></pre>
364365<p>Below are some route examples, and the associated paths that they366may consume:</p>367368<pre><code> "/user/:id"369 /user/12370371 "/users/:id?"372 /users/5373 /users374375 "/files/*"376 /files/jquery.js377 /files/javascripts/jquery.js378379 "/file/*.*"380 /files/jquery.js381 /files/javascripts/jquery.js382383 "/user/:id/:operation?"384 /user/1385 /user/1/edit386387 "/products.:format"388 /products.json389 /products.xml390391 "/products.:format?"392 /products.json393 /products.xml394 /products395</code></pre>396397<h3 id="Passing-Route-Control">Passing Route Control</h3>398

Updated docsTj Holowaychukbefore adoptiond691ecb

lines 399–401why, one step back
399<p>We may pass control to the next <em>matching</em> route, by calling the <em>third</em> argument,
400the <em>next()</em> function. When a match cannot be made, control is passed back to Connect,401and middleware continue to be invoked.</p>

Added multipart exampleTj Holowaychukbefore adoption9a1f62f

line 402why, one step back
402

Updated docsTj Holowaychukbefore adoptiond691ecb

lines 403–405why, one step back
403<pre><code>app.get('/users/:id?', function(req, res){
404 var id = req.params.id;405 if (id) {

Added multipart exampleTj Holowaychukbefore adoption9a1f62f

lines 406–411why, one step back
406 // do something
407 } else {408 next();409 }410});411

Updated docsTj Holowaychukbefore adoptiond691ecb

line 412why, one step back
412app.get('/users', function(req, res){

Added multipart exampleTj Holowaychukbefore adoption9a1f62f

lines 413–428why, one step back
413 // do something else
414});415</code></pre>416417<h3 id="Middleware">Middleware</h3>418419<p>The Express <em>Plugin</em> is no more! middleware via <a href="http://github.com/extjs/Connect">Connect</a> can be420passed to <em>express.createServer()</em> as you would with a regular Connect server. For example:</p>421422<pre><code>var connect = require('connect'),423 express = require('express');424425var app = express.createServer(426 connect.logger(),427 connect.bodyDecoder()428);

More docsTj Holowaychukbefore adoptionbedff32

lines 429–430why, one step back
429</code></pre>
430

Error handling docsTj Holowaychukbefore adoption9fbb9fa

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

Updated docsTj Holowaychukbefore adoptioncacada8

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

Error handling docsTj Holowaychukbefore adoption9fbb9fa

lines 434–482why, one step back
434</code></pre>
435436<h3 id="Error-Handling">Error Handling</h3>437438<p>Express provides the <em>app.error()</em> method which receives exceptions thrown within a route,439or passed to <em>next(err)</em>. Below is an example which serves different pages based on our440ad-hoc <em>NotFound</em> exception:</p>441442<pre><code>function NotFound(msg){443 this.name = 'NotFound';444 Error.call(this, msg);445 Error.captureStackTrace(this, arguments.callee);446}447448sys.inherits(NotFound, Error);449450app.get('/404', function(req, res){451 throw new NotFound;452});453454app.get('/500', function(req, res){455 throw new Error('keyboard cat!');456});457</code></pre>458459<p>We can call <em>app.error()</em> several times as shown below.460Here we check for an instanceof <em>NotFound</em> and show the461404 page, or we pass on to the next error handler.</p>462463<pre><code>app.error(function(err, req, res, next){464 if (err instanceof NotFound) {465 res.render('404.jade');466 } else {467 next(err);468 }469});470</code></pre>471472<p>Here we assume all errors as 500 for the simplicity of473this demo, however you can choose whatever you like</p>474475<pre><code>app.error(function(err, req, res){476 res.render('500.jade', {477 locals: {478 error: err479 } 480 });481});482</code></pre>

Added more view docsTj Holowaychukbefore adoption2f96506

line 483why, one step back
483

More error handling docsTj Holowaychukbefore adoptioncea401c

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

Updated docsTj Holowaychukbefore adoptioncacada8

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

More error handling docsTj Holowaychukbefore adoptioncea401c

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

Updated docsTj Holowaychukbefore adoptioncacada8

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

More error handling docsTj Holowaychukbefore adoptioncea401c

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

Added more view docsTj Holowaychukbefore adoption2f96506

lines 500–518why, one step back
500<h3 id="View-Rendering">View Rendering</h3>
501502<p>View filenames take the form <em>Express</em>.<em>ENGINE</em>, where <em>ENGINE</em> is the name503of the module that will be required. For example the view <em>layout.ejs</em> will504tell the view system to <em>require('ejs')</em>, the module being loaded must (currently)505export the method <em>exports.render(str, options)</em> to comply with Express, however506with will likely be extensible in the future.</p>507508<p>Below is an example using <a href="http://github.com/visionmedia/haml.js">Haml.js</a> to render <em>index.html</em>,509and since we do not use <em>layout: false</em> the rendered contents of <em>index.html</em> will be passed as510the <em>body</em> local variable in <em>layout.haml</em>.</p>511512<pre><code>app.get('/', function(req, res){513 res.render('index.haml', {514 locals: { title: 'My Site' }515 });516});517</code></pre>518

Docs for "view engine" settingTj Holowaychukbefore adoption1810c9b

lines 519–540why, one step back
519<p>The new <em>view engine</em> setting allows us to specify our default template engine,
520so for example when using <a href="http://github.com/visionmedia/jade">Jade</a> we could set:</p>521522<pre><code>app.set('view engine', 'jade');523</code></pre>524525<p>Allowing us to render with:</p>526527<pre><code>res.render('index');528</code></pre>529530<p>vs:</p>531532<pre><code>res.render('index.jade');533</code></pre>534535<p>When <em>view engine</em> is set, extensions are entirely optional, however we can still536mix and match template engines:</p>537538<pre><code>res.render('another-page.ejs');539</code></pre>540

Added more view docsTj Holowaychukbefore adoption2f96506

lines 541–561why, one step back
541<h3 id="View-Partials">View Partials</h3>
542543<p>The Express view system has built-in support for partials and collections, which are544sort of "mini" views representing a document fragment. For example rather than iterating545in a view to display comments, we would use a partial with collection support:</p>546547<pre><code>partial('comment.haml', { collection: comments });548</code></pre>549550<p>To make things even less verbose we can assume the extension as <em>.haml</em> when omitted,551however if we wished we could use an ejs partial, within a haml view for example.</p>552553<pre><code>partial('comment', { collection: comments });554</code></pre>555556<p>And once again even further, when rendering a collection we can simply pass557an array, if no other options are desired:</p>558559<pre><code>partial('comments', comments);560</code></pre>561

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

lines 562–570why, one step back
562<p>When using the partial collection support a few "magic" variables are provided
563for free:</p>564565<ul>566<li><em>firstInCollection</em> True if this is the first object</li>567<li><em>indexInCollection</em> Index of the object in the collection</li>568<li>_lastInCollection _ True if this is the last object</li>569</ul>570

Added view helper docs. Closes #344Tj Holowaychukbefore adoptionc4b5208

line 571why, one step back
571

Added more view docsTj Holowaychukbefore adoption2f96506

lines 572–581why, one step back
572<h3 id="Template-Engines">Template Engines</h3>
573574<p>Below are a few template engines commonly used with Express:</p>575576<ul>577<li><a href="http://github.com/visionmedia/jade">Jade</a> haml.js successor</li>578<li><a href="http://github.com/visionmedia/haml.js">Haml</a> indented templates</li>579<li><a href="http://github.com/visionmedia/ejs">EJS</a> Embedded JavaScript</li>580</ul>581

Error handling docsTj Holowaychukbefore adoption9fbb9fa

line 582why, one step back
582

More docsTj Holowaychukbefore adoptionde81ee7

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

More docsTj Holowaychukbefore adoptiondfecbe5

lines 584–591why, one step back
584
585<p>Get the case-insensitive request header <em>key</em>, with optional <em>defaultValue</em>:</p>586587<pre><code>req.header('Host');588req.header('host');589req.header('Accept', '*/*');590</code></pre>591

More docsTj Holowaychukbefore adoptionde81ee7

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

More docsTj Holowaychukbefore adoptiondfecbe5

lines 593–616why, one step back
593
594<p>Check if the <em>Accept</em> header is present, and includes the given <em>type</em>.</p>595596<p>When the <em>Accept</em> header is not present <em>true</em> is returned. Otherwise597the given <em>type</em> is matched by an exact match, and then subtypes. You598may pass the subtype such as "html" which is then converted internally599to "text/html" using the mime lookup table.</p>600601<pre><code>// Accept: text/html602req.accepts('html');603// =&gt; true604605// Accept: text/*; application/json606req.accepts('html');607req.accepts('text/html');608req.accepts('text/plain');609req.accepts('application/json');610// =&gt; true611612req.accepts('image/png');613req.accepts('png');614// =&gt; false615</code></pre>616

More docsTj Holowaychukbefore adoptionde81ee7

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

More docsTj Holowaychukbefore adoptiondfecbe5

lines 618–621why, one step back
618
619<p>Return the value of param <em>name</em> when present.</p>620621<ul>

Updated docsTj Holowaychukbefore adoptiond691ecb

lines 622–624why, one step back
622<li>Checks route placeholders (<em>req.params</em>), ex: /user/:id</li>
623<li>Checks query string params (<em>req.query</em>), ex: ?id=12</li>624<li>Checks urlencoded body params (<em>req.body</em>), ex: id=12</li>

More docsTj Holowaychukbefore adoptiondfecbe5

lines 625–631why, one step back
625</ul>
626627628<p>To utilize urlencoded request bodies, <em>req.body</em>629should be an object. This can be done by using630the <em>connect.bodyDecoder</em> middleware.</p>631

More docsTj Holowaychukbefore adoptionde81ee7

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

More docsTj Holowaychukbefore adoptiondfecbe5

lines 633–650why, one step back
633
634<p>Queue flash <em>msg</em> of the given <em>type</em>.</p>635636<pre><code>req.flash('info', 'email sent');637req.flash('error', 'email delivery failed');638req.flash('info', 'email re-sent');639// =&gt; 2640641req.flash('info');642// =&gt; ['email sent', 'email re-sent']643644req.flash('info');645// =&gt; []646647req.flash();648// =&gt; { error: ['email delivery failed'], info: [] }649</code></pre>650

More docsTj Holowaychukbefore adoptionde81ee7

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

More docsTj Holowaychukbefore adoptiondfecbe5

lines 652–659why, one step back
652
653<p>Also aliased as <em>req.xhr</em>, this getter checks the <em>X-Requested-With</em> header654to see if it was issued by an <em>XMLHttpRequest</em>:</p>655656<pre><code>req.xhr657req.isXMLHttpRequest658</code></pre>659

More docsTj Holowaychukbefore adoptionde81ee7

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

More docs :)Tj Holowaychukbefore adoption0c5fe61

lines 661–673why, one step back
661
662<p>Get or set the response header <em>key</em>.</p>663664<pre><code>res.header('Content-Length');665// =&gt; undefined666667res.header('Content-Length', 123);668// =&gt; 123669670res.header('Content-Length');671// =&gt; 123672</code></pre>673

More docsTj Holowaychukbefore adoptionde81ee7

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

More docs :)Tj Holowaychukbefore adoption0c5fe61

lines 675–682why, one step back
675
676<p>Sets the <em>Content-Type</em> response header to the given <em>type</em>.</p>677678<pre><code> var filename = 'path/to/image.png';679 res.contentType(filename);680 // res.headers['Content-Type'] is now "image/png"681</code></pre>682

More docsTj Holowaychukbefore adoptionde81ee7

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

Docs for res.attachment()Tj Holowaychukbefore adoptioneb235e1

lines 684–689why, one step back
684
685<p>Sets the <em>Content-Disposition</em> response header to "attachment", with optional <em>filename</em>.</p>686687<pre><code> res.attachment('path/to/my/image.png');688</code></pre>689

More docsTj Holowaychukbefore adoptionde81ee7

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

Docs for new res methodsTj Holowaychukbefore adoptionb08dab9

lines 691–696why, one step back
691
692<p>Used by <code>res.download()</code> to transfer an arbitrary file.</p>693694<pre><code>res.sendfile('path/to/my.file');695</code></pre>696

Better docs for res.sendfile()Tj Holowaychukbefore adoption8a337f1

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

Docs for new res methodsTj Holowaychukbefore adoptionb08dab9

line 701why, one step back
701

More docsTj Holowaychukbefore adoptionde81ee7

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

Docs for new res methodsTj Holowaychukbefore adoptionb08dab9

lines 703–715why, one step back
703
704<p>Transfer the given <em>file</em> as an attachment with optional alternative <em>filename</em>.</p>705706<pre><code>res.download('path/to/image.png');707res.download('path/to/image.png', 'foo.png');708</code></pre>709710<p>This is equivalent to:</p>711712<pre><code>res.attachment(file);713res.sendfile(file);714</code></pre>715

More docsTj Holowaychukbefore adoptionde81ee7

line 716why, one step back
716<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 717–733why, one step back
717
718<p>The <code>res.send()</code> method is a high level response utility allowing you to pass719objects to respond with json, strings for html, arbitrary _Buffer_s or numbers for status720code based responses. The following are all valid uses:</p>721722<pre><code> res.send(new Buffer('wahoo'));723 res.send({ some: 'json' });724 res.send('&lt;p&gt;some html&lt;/p&gt;');725 res.send('Sorry, cant find that', 404);726 res.send('text', { 'Content-Type': 'text/plain' }, 201);727 res.send(404);728</code></pre>729730<p>By default the <em>Content-Type</em> response header is set, however if explicitly731assigned through <code>res.send()</code> or previously with <code>res.header()</code> or <code>res.contentType()</code>732it will not be set again.</p>733

More docsTj Holowaychukbefore adoptionde81ee7

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

More docsTj Holowaychukbefore adoptionbedff32

lines 735–747why, one step back
735
736<p>Redirect to the given <em>url</em> with a default response <em>status</em> of 302.</p>737738<pre><code>res.redirect('/', 301);739res.redirect('/account');740res.redirect('http://google.com');741res.redirect('home');742res.redirect('back');743</code></pre>744745<p>Express supports "redirect mapping", which by default provides <em>home</em>, and <em>back</em>.746The <em>back</em> map checks the <em>Referrer</em> and <em>Referer</em> headers, while <em>home</em> utilizes747the "home" setting and defaults to "/".</p>

More view docsTj Holowaychukbefore adoption1c8ff48

lines 748–760why, one step back
748
749<h3 id="res-render-view-options-fn-">res.render(view[, options[, fn]])</h3>750751<p>Render <em>view</em> with the given <em>options</em> and optional callback <em>fn</em>.752When a callback function is given a response will <em>not</em> be made753automatically, however otherwise a response of <em>200</em> and <em>text/html</em> is given.</p>754755<p> Most engines accept one or more of the following options,756 both <a href="http://github.com/visionmedia/haml.js">haml</a> and <a href="http://github.com/visionmedia/jade">jade</a> accept all:</p>757758<ul>759<li><em>context|scope</em> Template evaluation context (<em>this</em>)</li>760<li><em>locals</em> Object containing local variables</li>

Cleaning up view docsTj Holowaychukbefore adoption25c8883

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

More view docsTj Holowaychukbefore adoption1c8ff48

lines 762–764why, one step back
762<li><em>debug</em> Output debugging information</li>
763</ul>764

In depth partial docsTj Holowaychukbefore adoption793943f

lines 765–812why, one step back
765
766<h3 id="res-partial-view-options-">res.partial(view[, options])</h3>767768<p>Render <em>view</em> partial with the given <em>options</em>. This method is always available769to the view as a local variable.</p>770771<ul>772<li><p><em>as</em> Variable name for each <em>collection</em> value, defaults to the view name.</p>773774<ul>775<li>as: 'something' will add the <em>something</em> local variable</li>776<li>as: this will use the collection value as the template context</li>777<li>as: global will merge the collection value's properties with <em>locals</em></li>778</ul>779</li>780<li><p><em>collection</em> Array of objects, the name is derived from the view name itself.781For example <em>video.html</em> will have a object <em>video</em> available to it.</p></li>782</ul>783784785<p>The following are equivalent, and the name of collection value when passed786to the partial will be <em>movie</em> as derived from the name.</p>787788<pre><code>partial('movie.jade', { collection: movies });789partial('movie.jade', movies);790partial('movie', movies);791// In view: movie.director792</code></pre>793794<p>To change the local from <em>movie</em> to <em>video</em> we can use the "as" option:</p>795796<pre><code>partial('movie', { collection: movies, as: 'video' });797// In view: video.director798</code></pre>799800<p>Also we can make our movie the value of <em>this</em> within our view so that instead801of <em>movie.director</em> we could use <em>this.director</em>.</p>802803<pre><code>partial('movie', { collection: movies, as: this });804// In view: this.director805</code></pre>806807<p>Another alternative is to "explode" the properties of the collection item into808pseudo globals (local variables) by using <em>as: global</em>, which again is syntactic sugar:</p>809810<pre><code>partials('movie', { collection: movies, as: global });811// In view: director812</code></pre>

More docsTj Holowaychukbefore adoptionbedff32

line 813why, one step back
813

More docsTj Holowaychukbefore adoptionde81ee7

lines 814–855why, one step back
814<h3 id="app-set-name-val-">app.set(name[, val])</h3>
815816<p>Apply an application level setting <em>name</em> to <em>val</em>, or817get the value of <em>name</em> when <em>val</em> is not present:</p>818819<pre><code>app.set('reload views', 200);820app.set('reload views');821// =&gt; 200822</code></pre>823824<h3 id="app-enable-name-">app.enable(name)</h3>825826<p>Enable the given setting <em>name</em>:</p>827828<pre><code>app.enable('some arbitrary setting');829app.set('some arbitrary setting');830// =&gt; true831</code></pre>832833<h3 id="app-disable-name-">app.disable(name)</h3>834835<p>Disable the given setting <em>name</em>:</p>836837<pre><code>app.disable('some setting');838app.set('some setting');839// =&gt; false840</code></pre>841842<h3 id="app-configure-env-function-function-">app.configure(env|function[, function])</h3>843844<p>Define a callback function for the given <em>env</em> (or all environments) with callback <em>function</em>:</p>845846<pre><code>app.configure(function(){847 // executed for each env848});849850app.configure('development', function(){851 // executed for 'development' only852});853</code></pre>854855<h3 id="app-redirect-name-val-">app.redirect(name, val)</h3>

More docsTj Holowaychukbefore adoptionbedff32

lines 856–867why, one step back
856
857<p>For use with <code>res.redirect()</code> we can map redirects at the application level as shown below:</p>858859<pre><code>app.redirect('google', 'http://google.com');860</code></pre>861862<p>Now in a route we may call:</p>863864<p> res.redirect('google');</p>865866<p>We may also map dynamic redirects:</p>867

Updated docsTj Holowaychukbefore adoptiond691ecb

lines 868–869why, one step back
868<pre><code>app.redirect('comments', function(req, res){
869 return '/post/' + req.params.id + '/comments';

More docsTj Holowaychukbefore adoptionbedff32

lines 870–878why, one step back
870});
871</code></pre>872873<p>So now we may do the following, and the redirect will dynamically adjust to874the context of the request. If we called this route with <em>GET /post/12</em> our875redirect <em>Location</em> would be <em>/post/12/comments</em>.</p>876877<pre><code>app.get('/post/:id', function(req, res){878 res.redirect('comments');

Error handling docsTj Holowaychukbefore adoption9fbb9fa

lines 879–883why, one step back
879});
880</code></pre>881882<h3 id="app-error-function-">app.error(function)</h3>883

More error docsTj Holowaychukbefore adoption6227f5c

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

Error handling docsTj Holowaychukbefore adoption9fbb9fa

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

More docsTj Holowaychukbefore adoptionbedff32

line 890why, one step back
890});

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

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

Added app.helpers() docsTj Holowaychukbefore adoption7eb088b

lines 892–894why, one step back
892
893<h3 id="app-helpers-obj-">app.helpers(obj)</h3>894

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

lines 895–912why, one step back
895<p>Registers static view helpers.</p>
896897<pre><code>app.helpers({898 name: function(first, last){ return first + ', ' + last },899 firstName: 'tj',900 lastName: 'holowaychuk'901});902</code></pre>903904<p>Our view could now utilize the <em>firstName</em> and <em>lastName</em> variables,905as well as the <em>name()</em> function exposed.</p>906907<pre><code>&lt;%= name(firstName, lastName) %>908</code></pre>909910<h3 id="app-dynamicHelpers-obj-">app.dynamicHelpers(obj)</h3>911912<p>Registers dynamic view helpers. Dynamic view helpers

Updated docsTj Holowaychukbefore adoptiond691ecb

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

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

line 914why, one step back
914evaluated 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 915–916why, one step back
915becomes the local variable it is associated with.</p>
916

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

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

Updated docsTj Holowaychukbefore adoptiond691ecb

line 918why, one step back
918 session: function(req, res){

Added app.helpers() docsTj Holowaychukbefore adoption7eb088b

lines 919–923why, one step back
919 return req.session;
920 }921});922</code></pre>923

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

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

Rebuilt docsTj Holowaychukbefore adoptionba88e48

lines 927–941why, one step back
927</code></pre>
928929<h3 id="app-mounted-fn-">app.mounted(fn)</h3>930931<p>Assign a callback <em>fn</em> which is called when this <em>Server</em> is passed to <em>Server#use()</em>.</p>932933<pre><code>var app = express.createServer(),934 blog = express.createServer();935936blog.mounted(function(parent){937 // parent is app938 // "this" is blog939});940941app.use(blog);

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

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

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 943–951why, one step back
943
944<h3 id="app-listen-port-host-">app.listen([port[, host]])</h3>945946<p>Bind the app server to the given <em>port</em>, which defaults to 3000. When <em>host</em> is omitted all947connections will be accepted via <em>INADDR_ANY</em>.</p>948949<pre><code>app.listen();950app.listen(3000);951app.listen(3000, 'n.n.n.n');

Added multipart exampleTj Holowaychukbefore adoption9a1f62f

lines 952–953why, one step back
952</code></pre>
953

Unix domain socket docsTj Holowaychukbefore adoption2fa7907

lines 954–970why, one step back
954<p>The <em>port</em> argument may also be a string representing the path to a unix domain socket:</p>
955956<pre><code>app.listen('/tmp/express.sock');957</code></pre>958959<p>Then try it out:</p>960961<pre><code>$ telnet /tmp/express.sock962GET / HTTP/1.1963964HTTP/1.1 200 OK965Content-Type: text/plain966Content-Length: 11967968Hello World969</code></pre>970

Added multipart exampleTj Holowaychukbefore adoption9a1f62f

lines 971–975why, one step back
971</div>
972 </div>973 </div>974 </body>975</html>