Skip to content
Myr

guide.html

This file

Path

docs/guide.html

1,137 lines · 36.6 KiB · HTML

Last changed by

Merge branch 'master' into rangeTj Holowaychukbefore adoptionc3365aa

History of this file

Take it

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

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

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

line 4why, one step back
4 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 5–18why, one step back
5 <style>
6 #tagline {7 margin-left: 75px;8 margin-bottom: 30px;9 color: rgba(255,255,255,0.7); }10 html {11 background: #1c1c1c url(images/bg.tile.jpg); }1213 body {14 margin: 0;15 padding-bottom: 30px;16 font: 14px/1.4 "Helvetica Neue", "Lucida Grande", "Arial";17 font-size: 14px;18 line-height: 1.5;

Merge branch 'master' into rangeTj Holowaychukbefore adoptionc3365aa

lines 19–20why, one step back
19 -webkit-font-smoothing: antialiased;
20 -moz-font-smoothing: antialiased;

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 21–42why, one step back
21 background: url(images/bg.jpg) 50% 0 no-repeat;
22 color: #8b8b8b; }2324 * {25 outline: none; }2627 em {28 color: white; }2930 a img {31 border: none !important; }3233 a {34 font-weight: bold;35 text-decoration: none;36 color: white;37 -webkit-transition-property: opacity, -webkit-transform, color, background-color, padding, -webkit-box-shadow;38 -webkit-transition-duration: 0.15s;39 -webkit-transition-timing-function: ease-out; }40 a:hover {41 opacity: 0.8; }42

Merge branch 'master' into rangeTj Holowaychukbefore adoptionc3365aa

line 43why, one step back
43 h1, h2, h3, h4 {

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 44–49why, one step back
44 margin: 45px 0 0 0;
45 color: white;46 text-shadow: 1px 2px 2px rgba(0,0,0,0.6); }4748 h3 {49 font-size: 18px; }

Merge branch 'master' into rangeTj Holowaychukbefore adoptionc3365aa

lines 50–53why, one step back
50 h4 {
51 margin-left: 10px;52 font-size: 14px;53 }

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 54–74why, one step back
54
55 pre {56 margin: 20px 10px;57 padding: 25px 20px;58 background: rgba(0,0,0,0.5);59 border: 1px solid #323232;60 -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);61 -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);62 -webkit-border-radius: 5px;63 -moz-border-radius: 5px; }6465 code {66 font-family: "Helvetica Neue", "Lucida Grande", "Arial"; }6768 ul {69 margin: 15px 0;70 padding: 0 0 0 35px; }71 ul li {72 margin: 0;73 padding: 2px 0;74 list-style: square; }

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

lines 75–78why, one step back
75 ul li ul {
76 margin: 0;77 padding-left: 12px;78 }

Docs for session exampleTj Holowaychukbefore adoptionc53883b

line 79why, one step back
79

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

lines 80–81why, one step back
80 .man-name, #Express { display:none; }
81

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 82–110why, one step back
82 .sect {
83 margin-left: 40px; }8485 #logo {86 display: block;87 margin-left: 30%;88 margin-bottom: 30px;89 width: 194px;90 height: 51px;91 background: url(images/logo.png) 0 0 no-repeat;92 text-indent: -99999px; }93 #logo:hover {94 opacity: 0.7; }95 #logo:active {96 opacity: 0.3; }9798 #ribbon {99 position: fixed;100 top: 0;101 right: 0;102 z-index: 2; }103104 #wrapper {105 width: 100%;106 min-height: 800px;107 background: url(images/top.png) 0 0 repeat-x; }108109 #container {110 margin: 0 auto;

Regenerated docs againTj Holowaychukbefore adoption8ead901

line 111why, one step back
111 padding-top: 80px;

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 112–115why, one step back
112 width: 550px; }
113114 #toc {115 position: fixed;

Regenerated docs againTj Holowaychukbefore adoption8ead901

line 116why, one step back
116 top: 0;

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 117–121why, one step back
117 left: 0;
118 margin: 0 0 0 15px;119 padding: 15px;120 height: 100%;121 background: rgba(0,0,0,0.2);

Merge branch 'fix-toc'Tj Holowaychukbefore adoptionbb18f87

line 122why, one step back
122 overflow: auto;

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 123–159why, one step back
123 border-right: 1px solid rgba(255,255,255,0.05);
124 }125 #toc li {126 padding: 0;127 list-style: none;128 }129 #toc li a {130 font-size: 11px;131 }132 #menu {133 margin-left: 65px;134 padding: 0;135 padding-bottom: 30px; }136 #menu li {137 display: inline;138 list-style: none; }139 #menu li a {140 display: block;141 float: left;142 margin: 0 2px;143 padding: 3px 15px;144 background: rgba(0,0,0,0.2);145 -webkit-border-radius: 8px;146 -moz-border-radius: 8px;147 -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);148 -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);149 -webkit-transition-property: opacity, -webkit-transform, color, background-color, -webkit-box-shadow;150 -webkit-transition-duration: 0.15s;151 -webkit-transition-timing-function: ease-out; }152 #menu li a:hover,153 #menu li a.active {154 background: rgba(0,0,0,0.5); }155 #menu li a:active {156 background: rgba(0,0,0,0.1);157 -webkit-box-shadow: 1px 1px 1px rgba(0,0,0,0.4);158 -moz-box-shadow: 1px 1px 1px rgba(0,0,0,0.4); }159 </style>

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

lines 160–171why, one step back
160 <script>
161 $(function(){162 $('.section').hide();163 $('.toggle, a.section-title').toggle(function(){164 $(this).siblings('ul').fadeIn(300);165 return false;166 }, function(){167 $(this).siblings('ul').fadeOut(300);168 return false;169 });170 });171 </script>

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 172–184why, one step back
172 </head>
173 <body>174 <a href='http://github.com/visionmedia/express'> 175 <img alt='Fork me on GitHub' id='ribbon' src='http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png' /> 176 </a>177 <div id="wrapper">178 <div id="container"><ul id="toc">179<li><a href="#Installation">Installation</a></li>180<li><a href="#Creating-An-Application">Creating An Application</a></li>181<li><a href="#Configuration">Configuration</a></li>182<li><a href="#Settings">Settings</a></li>183<li><a href="#Routing">Routing</a></li>184<li><a href="#Passing-Route-Control">Passing Route Control</a></li>

Merge branch 'master' into rangeTj Holowaychukbefore adoptionc3365aa

line 185why, one step back
185<li><a href="#HTTP-Methods">HTTP Methods</a></li>

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 186–190why, one step back
186<li><a href="#Middleware">Middleware</a></li>
187<li><a href="#Error-Handling">Error Handling</a></li>188<li><a href="#View-Rendering">View Rendering</a></li>189<li><a href="#View-Partials">View Partials</a></li>190<li><a href="#Template-Engines">Template Engines</a></li>

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

lines 191–223why, one step back
191<li><a href="#" class="toggle">+</a> <a class="section-title" href="#">Request</a><ul class="section" id="section-Request">
192<li><a href="#req-header-key-defaultValue-">header()</a></li>193<li><a href="#req-accepts-type-">accepts()</a></li>194<li><a href="#req-param-name-">param()</a></li>195<li><a href="#req-flash-type-msg-">flash()</a></li>196<li><a href="#req-isXMLHttpRequest">isXMLHttpRequest</a></li>197</ul></li>198<li><a href="#" class="toggle">+</a> <a class="section-title" href="#">Response</a><ul class="section" id="section-Response">199<li><a href="#res-header-key-val-">header()</a></li>200<li><a href="#res-contentType-type-">contentType()</a></li>201<li><a href="#res-attachment-filename-">attachment()</a></li>202<li><a href="#res-sendfile-path-">sendfile()</a></li>203<li><a href="#res-download-file-filename-">download()</a></li>204<li><a href="#res-send-body-status-headers-status-status-">send()</a></li>205<li><a href="#res-redirect-url-status-">redirect()</a></li>206<li><a href="#res-cookie-name-val-options-">cookie()</a></li>207<li><a href="#res-clearCookie-name-">clearCookie()</a></li>208<li><a href="#res-render-view-options-fn-">render()</a></li>209<li><a href="#res-partial-view-options-">partial()</a></li>210</ul></li>211<li><a href="#" class="toggle">+</a> <a class="section-title" href="#">Server</a><ul class="section" id="section-Server">212<li><a href="#app-set-name-val-">set()</a></li>213<li><a href="#app-enable-name-">enable()</a></li>214<li><a href="#app-disable-name-">disable()</a></li>215<li><a href="#app-configure-env-function-function-">configure()</a></li>216<li><a href="#app-redirect-name-val-">redirect()</a></li>217<li><a href="#app-error-function-">error()</a></li>218<li><a href="#app-helpers-obj-">helpers()</a></li>219<li><a href="#app-dynamicHelpers-obj-">dynamicHelpers()</a></li>220<li><a href="#app-mounted-fn-">mounted()</a></li>221<li><a href="#app-register-ext-exports-">register()</a></li>222<li><a href="#app-listen-port-host-">listen()</a></li>223</ul></li>

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 224–236why, one step back
224</ul>
225 <a href='http://github.com/visionmedia/express' id='logo'>Express</a> 226 <p id="tagline">227 High performance, high class web development for228 <a href="http://nodejs.org">Node.js</a>229 </p>230 <ul id="menu">231 <li><a href="index.html">Home</a></li>232 <li><a href="guide.html">Guide</a></li>233 <li><a href="contrib.html">Contributing</a></li>234 <li><a href="migrate.html">1.x Migration</a></li>235 </ul>236<div class='mp'>

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

lines 237–240why, one step back
237<h2 id="Express">Express</h2>
238<p class="man-name">239 <code>guide</code>240</p>

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 241–242why, one step back
241<h3 id="Installation">Installation</h3>
242

No "or similar" for curl installTj Holowaychukbefore adoption56a1dd9

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

line 244why, one step back
244

Updated curl install docsTj Holowaychukbefore adoption3ceb0de

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

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

Updated curl install docsTj Holowaychukbefore adoption3ceb0de

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

line 249why, one step back
249

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

line 250why, one step back
250<pre><code>$ npm install express

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 251–282why, one step back
251</code></pre>
252253<p>git clone, first update the submodules:</p>254255<pre><code>$ git submodule update --init256$ make install257$ make install-support258</code></pre>259260<h3 id="Creating-An-Application">Creating An Application</h3>261262<p>The <em>express.Server</em> now inherits from <em>http.Server</em>, however263follows the same idiom by providing <em>express.createServer()</em> as shown below. This means264that you can utilize Express server's transparently with other libraries.</p>265266<pre><code>var app = require('express').createServer();267268app.get('/', function(req, res){269 res.send('hello world');270});271272app.listen(3000);273</code></pre>274275<h3 id="Configuration">Configuration</h3>276277<p>Express supports arbitrary environments, such as <em>production</em> and <em>development</em>. Developers278can use the <em>configure()</em> method to setup needs required by the current environment. When279<em>configure()</em> is called without an environment name it will be run in <em>every</em> environment280prior to the environment specific callback.</p>281282<p>In the example below we only <em>dumpExceptions</em>, and respond with exception stack traces

Updated configuration docsTj Holowaychukbefore adoptionf8ccaa3

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

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

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

lines 288–289why, one step back
288 app.use(express.methodOverride());
289 app.use(express.bodyDecoder());

Updated configuration docsTj Holowaychukbefore adoptionf8ccaa3

line 290why, one step back
290 app.use(app.router);

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

line 291why, one step back
291 app.use(express.staticProvider(__dirname + '/public'));

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 292–294why, one step back
292});
293294app.configure('development', function(){

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 296–298why, one step back
296});
297298app.configure('production', function(){

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

line 299why, one step back
299 app.use(express.errorHandler());

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 300–328why, one step back
300});
301</code></pre>302303<p>For internal and arbitrary settings Express provides the <em>set(key[, val])</em>, <em>enable(key)</em>, <em>disable(key)</em> methods:</p>304305<pre><code>app.configure(function(){306 app.set('views', __dirname + '/views');307 app.set('views');308 // =&gt; "... views directory ..."309310 app.enable('some feature');311 // same as app.set('some feature', true);312313 app.disable('some feature');314 // same as app.set('some feature', false);315});316</code></pre>317318<p>To alter the environment we can set the <em>CONNECT_ENV</em> environment variable,319or more specifically <em>EXPRESS_ENV</em>, for example:</p>320321<pre><code>$ EXPRESS_ENV=production node app.js322</code></pre>323324<h3 id="Settings">Settings</h3>325326<p>Express supports the following settings out of the box:</p>327328<ul>

Updated docsTj Holowaychukbefore adoption4d32762

line 329why, one step back
329<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 330why, one step back
330<li><em>home</em> Application base path used for <em>res.redirect()</em> and transparently handling mounted apps.</li>

Docs for session exampleTj Holowaychukbefore adoptionc53883b

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

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

lines 333–334why, one step back
333<li><em>view options</em> An object specifying global view options</li>
334<li><em>partials</em> Root view partials directory defaulting to <em>views</em>/partials.</li>

Updated docsTj Holowaychukbefore adoptionf0eab06

line 335why, one step back
335<li><em>stream threshold</em> Bytesize indicating when a file should be streamed for <em>res.sendfile()</em> using <em>fs.ReadStream()</em> and <em>sys.pump()</em>.</li>

Docs for session exampleTj Holowaychukbefore adoptionc53883b

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

Updated docsTj Holowaychukbefore adoptiond691ecb

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

line 345why, one step back
345

Updated docsTj Holowaychukbefore adoptiond691ecb

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 348–356why, one step back
348});
349</code></pre>350351<p>A route is simple a string which is compiled to a <em>RegExp</em> internally. For example352when <em>/user/:id</em> is compiled, a simplified version of the regexp may look similar to:</p>353354<pre><code>\/user\/([^\/]+)\/?355</code></pre>356

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

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

Updated docsTj Holowaychukbefore adoptiond691ecb

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

line 359why, one step back
359

Updated docsTj Holowaychukbefore adoptiond691ecb

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

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

lines 362–374why, one step back
362});
363</code></pre>364365<p>Curl requests against the previously defined route:</p>366367<pre><code> $ curl http://dev:3000/user368 [null,null]369 $ curl http://dev:3000/users370 [null,null]371 $ curl http://dev:3000/users/1372 ["1",null]373 $ curl http://dev:3000/users/1..15374 ["1","15"]

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 375–410why, one step back
375</code></pre>
376377<p>Below are some route examples, and the associated paths that they378may consume:</p>379380<pre><code> "/user/:id"381 /user/12382383 "/users/:id?"384 /users/5385 /users386387 "/files/*"388 /files/jquery.js389 /files/javascripts/jquery.js390391 "/file/*.*"392 /files/jquery.js393 /files/javascripts/jquery.js394395 "/user/:id/:operation?"396 /user/1397 /user/1/edit398399 "/products.:format"400 /products.json401 /products.xml402403 "/products.:format?"404 /products.json405 /products.xml406 /products407</code></pre>408409<h3 id="Passing-Route-Control">Passing Route Control</h3>410

Updated docsTj Holowaychukbefore adoptiond691ecb

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

line 414why, one step back
414

Updated docsTj Holowaychukbefore adoptionb819366

line 415why, one step back
415<pre><code>app.get('/users/:id?', function(req, res, next){

Updated docsTj Holowaychukbefore adoptiond691ecb

lines 416–417why, one step back
416 var id = req.params.id;
417 if (id) {

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 418–423why, one step back
418 // do something
419 } else {420 next();421 }422});423

Updated docsTj Holowaychukbefore adoptiond691ecb

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 425–428why, one step back
425 // do something else
426});427</code></pre>428

Merge branch 'master' into rangeTj Holowaychukbefore adoptionc3365aa

lines 429–477why, one step back
429<h3 id="HTTP-Methods">HTTP Methods</h3>
430431<p>We have seen <em>app.get()</em> a few times, however Express also exposes other familiar HTTP verbs in the same manor, such as <em>app.post()</em>, <em>app.del()</em>, etc.</p>432433<p> A common example for <em>POST</em> usage, is when "submitting" a form. Below we simply set our form method to "post" in our html, and control will be given to the route we have defined below it.</p>434435<p> <form method="post" action="/" /></p>436437<pre><code> &lt;input type="text" name="user[name]" /&gt;438 &lt;input type="text" name="user[email]" /&gt;439 &lt;input type="submit" value="Submit" /&gt;440</code></pre>441442<p> </p>443444<p>By default Express does not know what to do with this request body, so we should add the <em>bodyDecoder</em> middleware, which will parse <em>application/x-www-form-urlencoded</em> request bodies and place the variables in <em>req.body</em>. We can do this by "using" the middleware as shown below:</p>445446<pre><code>app.use(express.bodyDecoder());447</code></pre>448449<p>Our route below will now have access to the <em>req.body.user</em> object which will contain the <em>name</em> and <em>email</em> properties when defined.</p>450451<pre><code>app.post('/', function(req, res){452 console.log(req.body.user);453 res.redirect('back');454});455</code></pre>456457<p>When using methods such as <em>PUT</em> with a form, we can utilize a hidden input named <em>_method</em>, which can be used to alter the HTTP method. To do so we first need the <em>methodOverride</em> middleware, which should be placed below <em>bodyDecoder</em> so that it can utilize it's <em>req.body</em> containing the form values.</p>458459<pre><code>app.use(express.bodyDecoder());460app.use(express.methodOverride());461</code></pre>462463<p>The reason that these are not always defaults, is simply because these are not required for Express to be fully functional. Depending on the needs of your application, you may not need these at all, your methods such as <em>PUT</em> and <em>DELETE</em> can still be accessed by clients which can use them directly, although <em>methodOverride</em> provides a great solution for forms. Below shows what the usage of <em>PUT</em> might look like:</p>464465<pre><code>&lt;form method="post" action="/"&gt;466 &lt;input type="hidden" name="_method" value="put" /&gt;467 &lt;input type="text" name="user[name]" /&gt;468 &lt;input type="text" name="user[email]" /&gt;469 &lt;input type="submit" value="Submit" /&gt; 470&lt;/form&gt;471472app.put('/', function(){473 console.log(req.body.user);474 res.redirect('back');475});476</code></pre>477

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 478–479why, one step back
478<h3 id="Middleware">Middleware</h3>
479

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

line 480why, one step back
480<p>The Express <em>Plugin</em> is no more! middleware via <a href="http://github.com/senchalabs/connect">Connect</a> can be

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 481–482why, one step back
481passed to <em>express.createServer()</em> as you would with a regular Connect server. For example:</p>
482

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

line 483why, one step back
483<pre><code>var express = require('express');

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 484–485why, one step back
484
485var app = express.createServer(

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

lines 486–487why, one step back
486 express.logger(),
487 express.bodyDecoder()

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 488–492why, one step back
488);
489</code></pre>490491<p>Alternatively we can <em>use()</em> them which is useful when adding middleware within <em>configure()</em> blocks:</p>492

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 494–495why, one step back
494</code></pre>
495

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

lines 496–506why, one step back
496<p>Typically with connect middleware you would <em>require('connect')</em> like so:</p>
497498<pre><code>var connect = require('connect');499app.use(connect.logger());500</code></pre>501502<p>This is somewhat annoying, so express re-exports these middleware properties, however they are <em>identical</em>:</p>503504<pre><code>app.use(express.logger());505</code></pre>506

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 507–533why, one step back
507<h3 id="Error-Handling">Error Handling</h3>
508509<p>Express provides the <em>app.error()</em> method which receives exceptions thrown within a route,510or passed to <em>next(err)</em>. Below is an example which serves different pages based on our511ad-hoc <em>NotFound</em> exception:</p>512513<pre><code>function NotFound(msg){514 this.name = 'NotFound';515 Error.call(this, msg);516 Error.captureStackTrace(this, arguments.callee);517}518519sys.inherits(NotFound, Error);520521app.get('/404', function(req, res){522 throw new NotFound;523});524525app.get('/500', function(req, res){526 throw new Error('keyboard cat!');527});528</code></pre>529530<p>We can call <em>app.error()</em> several times as shown below.531Here we check for an instanceof <em>NotFound</em> and show the532404 page, or we pass on to the next error handler.</p>533

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

lines 534–538why, one step back
534<p>Note that these handlers can be defined anywhere, as they
535will be placed below the route handlers on <em>listen()</em>. This536allows for definition within <em>configure()</em> blocks so we can537handle exceptions in different ways based on the environment.</p>538

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 539–563why, one step back
539<pre><code>app.error(function(err, req, res, next){
540 if (err instanceof NotFound) {541 res.render('404.jade');542 } else {543 next(err);544 }545});546</code></pre>547548<p>Here we assume all errors as 500 for the simplicity of549this demo, however you can choose whatever you like</p>550551<pre><code>app.error(function(err, req, res){552 res.render('500.jade', {553 locals: {554 error: err555 } 556 });557});558</code></pre>559560<p>Our apps could also utilize the Connect <em>errorHandler</em> middleware561to report on exceptions. For example if we wish to output exceptions562in "development" mode to <em>stderr</em> we can use:</p>563

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

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

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 571–579why, one step back
571</code></pre>
572573<p>The <em>errorHandler</em> middleware also responds with <em>json</em> if <em>Accept: application/json</em>574is present, which is useful for developing apps that rely heavily on client-side JavaScript.</p>575576<h3 id="View-Rendering">View Rendering</h3>577578<p>View filenames take the form <em>Express</em>.<em>ENGINE</em>, where <em>ENGINE</em> is the name579of the module that will be required. For example the view <em>layout.ejs</em> will

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

lines 580–581why, one step back
580tell the view system to <em>require('ejs')</em>, the module being loaded must export the method <em>exports.render(str, options)</em> to comply with Express, however
581<em>app.register()</em> can be used to map engines to file extensions, so that for example "foo.html" can be rendered by jade.</p>

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 582–615why, one step back
582
583<p>Below is an example using <a href="http://github.com/visionmedia/haml.js">Haml.js</a> to render <em>index.html</em>,584and since we do not use <em>layout: false</em> the rendered contents of <em>index.html</em> will be passed as585the <em>body</em> local variable in <em>layout.haml</em>.</p>586587<pre><code>app.get('/', function(req, res){588 res.render('index.haml', {589 locals: { title: 'My Site' }590 });591});592</code></pre>593594<p>The new <em>view engine</em> setting allows us to specify our default template engine,595so for example when using <a href="http://github.com/visionmedia/jade">Jade</a> we could set:</p>596597<pre><code>app.set('view engine', 'jade');598</code></pre>599600<p>Allowing us to render with:</p>601602<pre><code>res.render('index');603</code></pre>604605<p>vs:</p>606607<pre><code>res.render('index.jade');608</code></pre>609610<p>When <em>view engine</em> is set, extensions are entirely optional, however we can still611mix and match template engines:</p>612613<pre><code>res.render('another-page.ejs');614</code></pre>615

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

lines 616–635why, one step back
616<p>Express also provides the <em>view options</em> setting, which is applied each time a view is rendered, so for example if you rarely use layouts you may set:</p>
617618<pre><code>app.set('view options', {619 layout: false620});621</code></pre>622623<p>Which can then be overridden within the <code>res.render()</code> call if need be:</p>624625<pre><code>res.render('myview.ejs', { layout: true });626</code></pre>627628<p>A good example of this is specifying custom <em>ejs</em> opening and closing tags:</p>629630<pre><code>app.set('view options', {631 open: '{{',632 close: '}}'633});634</code></pre>635

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 636–662why, one step back
636<h3 id="View-Partials">View Partials</h3>
637638<p>The Express view system has built-in support for partials and collections, which are639sort of "mini" views representing a document fragment. For example rather than iterating640in a view to display comments, we would use a partial with collection support:</p>641642<pre><code>partial('comment.haml', { collection: comments });643</code></pre>644645<p>To make things even less verbose we can assume the extension as <em>.haml</em> when omitted,646however if we wished we could use an ejs partial, within a haml view for example.</p>647648<pre><code>partial('comment', { collection: comments });649</code></pre>650651<p>And once again even further, when rendering a collection we can simply pass652an array, if no other options are desired:</p>653654<pre><code>partial('comments', comments);655</code></pre>656657<p>When using the partial collection support a few "magic" variables are provided658for free:</p>659660<ul>661<li><em>firstInCollection</em> True if this is the first object</li>662<li><em>indexInCollection</em> Index of the object in the collection</li>

Merge branch 'fix-toc'Tj Holowaychukbefore adoptionbb18f87

line 663why, one step back
663<li><em>lastInCollection</em> True if this is the last object</li>

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 664–665why, one step back
664</ul>
665

Added view helper docs. Closes #344Tj Holowaychukbefore adoptionc4b5208

line 666why, one step back
666

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 667–671why, one step back
667<h3 id="Template-Engines">Template Engines</h3>
668669<p>Below are a few template engines commonly used with Express:</p>670671<ul>

Merge branch 'fix-toc'Tj Holowaychukbefore adoptionbb18f87

lines 672–673why, one step back
672<li><a href="http://jade-lang.com">Jade</a> haml.js successor</li>
673<li><a href="http://github.com/visionmedia/haml.js">Haml</a> pythonic indented templates</li>

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 674–716why, one step back
674<li><a href="http://github.com/visionmedia/ejs">EJS</a> Embedded JavaScript</li>
675</ul>676677678<h3 id="req-header-key-defaultValue-">req.header(key[, defaultValue])</h3>679680<p>Get the case-insensitive request header <em>key</em>, with optional <em>defaultValue</em>:</p>681682<pre><code>req.header('Host');683req.header('host');684req.header('Accept', '*/*');685</code></pre>686687<h3 id="req-accepts-type-">req.accepts(type)</h3>688689<p>Check if the <em>Accept</em> header is present, and includes the given <em>type</em>.</p>690691<p>When the <em>Accept</em> header is not present <em>true</em> is returned. Otherwise692the given <em>type</em> is matched by an exact match, and then subtypes. You693may pass the subtype such as "html" which is then converted internally694to "text/html" using the mime lookup table.</p>695696<pre><code>// Accept: text/html697req.accepts('html');698// =&gt; true699700// Accept: text/*; application/json701req.accepts('html');702req.accepts('text/html');703req.accepts('text/plain');704req.accepts('application/json');705// =&gt; true706707req.accepts('image/png');708req.accepts('png');709// =&gt; false710</code></pre>711712<h3 id="req-param-name-">req.param(name)</h3>713714<p>Return the value of param <em>name</em> when present.</p>715716<ul>

Updated docsTj Holowaychukbefore adoptiond691ecb

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 720–724why, one step back
720</ul>
721722723<p>To utilize urlencoded request bodies, <em>req.body</em>724should be an object. This can be done by using

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

line 725why, one step back
725the <em>express.bodyDecoder</em> middleware.</p>

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 726–745why, one step back
726
727<h3 id="req-flash-type-msg-">req.flash(type[, msg])</h3>728729<p>Queue flash <em>msg</em> of the given <em>type</em>.</p>730731<pre><code>req.flash('info', 'email sent');732req.flash('error', 'email delivery failed');733req.flash('info', 'email re-sent');734// =&gt; 2735736req.flash('info');737// =&gt; ['email sent', 'email re-sent']738739req.flash('info');740// =&gt; []741742req.flash();743// =&gt; { error: ['email delivery failed'], info: [] }744</code></pre>745

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

lines 746–750why, one step back
746<p>Flash notification message may also utilize formatters, by default only the %s string formatter is available:</p>
747748<pre><code>req.flash('info', 'email delivery to _%s_ from _%s_ failed.', toUser, fromUser);749</code></pre>750

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 751–796why, one step back
751<h3 id="req-isXMLHttpRequest">req.isXMLHttpRequest</h3>
752753<p>Also aliased as <em>req.xhr</em>, this getter checks the <em>X-Requested-With</em> header754to see if it was issued by an <em>XMLHttpRequest</em>:</p>755756<pre><code>req.xhr757req.isXMLHttpRequest758</code></pre>759760<h3 id="res-header-key-val-">res.header(key[, val])</h3>761762<p>Get or set the response header <em>key</em>.</p>763764<pre><code>res.header('Content-Length');765// =&gt; undefined766767res.header('Content-Length', 123);768// =&gt; 123769770res.header('Content-Length');771// =&gt; 123772</code></pre>773774<h3 id="res-contentType-type-">res.contentType(type)</h3>775776<p>Sets the <em>Content-Type</em> response header to the given <em>type</em>.</p>777778<pre><code> var filename = 'path/to/image.png';779 res.contentType(filename);780 // res.headers['Content-Type'] is now "image/png"781</code></pre>782783<h3 id="res-attachment-filename-">res.attachment([filename])</h3>784785<p>Sets the <em>Content-Disposition</em> response header to "attachment", with optional <em>filename</em>.</p>786787<pre><code> res.attachment('path/to/my/image.png');788</code></pre>789790<h3 id="res-sendfile-path-">res.sendfile(path)</h3>791792<p>Used by <code>res.download()</code> to transfer an arbitrary file.</p>793794<pre><code>res.sendfile('path/to/my.file');795</code></pre>796

Updated docsTj Holowaychukbefore adoptionf0eab06

lines 797–808why, one step back
797<p>This method accepts a callback which when given will be called on an exception, as well as when the transfer has completed. When a callback is not given, and the file has <strong>not</strong> been streamed, <em>next(err)</em> will be called on an exception.</p>
798799<pre><code>res.sendfile(path, function(err, path){800 if (err) {801 // handle the error802 } else {803 console.log('transferred %s', path);804 }805});806</code></pre>807808<p>When the filesize exceeds the <em>stream threshold</em> (defaulting to 32k), the file will be streamed using <em>fs.ReadStream</em> and <em>sys.pump()</em>.</p>

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 809–829why, one step back
809
810<h3 id="res-download-file-filename-">res.download(file[, filename])</h3>811812<p>Transfer the given <em>file</em> as an attachment with optional alternative <em>filename</em>.</p>813814<pre><code>res.download('path/to/image.png');815res.download('path/to/image.png', 'foo.png');816</code></pre>817818<p>This is equivalent to:</p>819820<pre><code>res.attachment(file);821res.sendfile(file);822</code></pre>823824<h3 id="res-send-body-status-headers-status-status-">res.send(body|status[, headers|status[, status]])</h3>825826<p>The <code>res.send()</code> method is a high level response utility allowing you to pass827objects to respond with json, strings for html, arbitrary _Buffer_s or numbers for status828code based responses. The following are all valid uses:</p>829

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

lines 830–831why, one step back
830<pre><code> res.send(); // 204
831 res.send(new Buffer('wahoo'));

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 832–857why, one step back
832 res.send({ some: 'json' });
833 res.send('&lt;p&gt;some html&lt;/p&gt;');834 res.send('Sorry, cant find that', 404);835 res.send('text', { 'Content-Type': 'text/plain' }, 201);836 res.send(404);837</code></pre>838839<p>By default the <em>Content-Type</em> response header is set, however if explicitly840assigned through <code>res.send()</code> or previously with <code>res.header()</code> or <code>res.contentType()</code>841it will not be set again.</p>842843<h3 id="res-redirect-url-status-">res.redirect(url[, status])</h3>844845<p>Redirect to the given <em>url</em> with a default response <em>status</em> of 302.</p>846847<pre><code>res.redirect('/', 301);848res.redirect('/account');849res.redirect('http://google.com');850res.redirect('home');851res.redirect('back');852</code></pre>853854<p>Express supports "redirect mapping", which by default provides <em>home</em>, and <em>back</em>.855The <em>back</em> map checks the <em>Referrer</em> and <em>Referer</em> headers, while <em>home</em> utilizes856the "home" setting and defaults to "/".</p>857

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

lines 858–881why, one step back
858<h3 id="res-cookie-name-val-options-">res.cookie(name, val[, options])</h3>
859860<p>Sets the given cookie <em>name</em> to <em>val</em>, with <em>options</em> such as "httpOnly: true", "expires", "secure" etc.</p>861862<pre><code>// "Remember me" for 15 minutes 863res.cookie('rememberme', 'yes', { expires: new Date(Date.now() + 900000), httpOnly: true });864</code></pre>865866<p>To parse incoming <em>Cookie</em> headers, use the <em>cookieDecoder</em> middleware, which provides the <em>req.cookies</em> object:</p>867868<pre><code>app.use(express.cookieDecoder());869870app.get('/', function(req, res){871 // use req.cookies.rememberme872});873</code></pre>874875<h3 id="res-clearCookie-name-">res.clearCookie(name)</h3>876877<p>Clear cookie <em>name</em> by setting "expires" far in the past.</p>878879<pre><code>res.clearCookie('rememberme');880</code></pre>881

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 882–891why, one step back
882<h3 id="res-render-view-options-fn-">res.render(view[, options[, fn]])</h3>
883884<p>Render <em>view</em> with the given <em>options</em> and optional callback <em>fn</em>.885When a callback function is given a response will <em>not</em> be made886automatically, however otherwise a response of <em>200</em> and <em>text/html</em> is given.</p>887888<p> Most engines accept one or more of the following options,889 both <a href="http://github.com/visionmedia/haml.js">haml</a> and <a href="http://github.com/visionmedia/jade">jade</a> accept all:</p>890891<ul>

Merge branch 'master' into rangeTj Holowaychukbefore adoptionc3365aa

line 892why, one step back
892<li><em>scope</em> Template evaluation context (value of <em>this</em>)</li>

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 893–894why, one step back
893<li><em>locals</em> Object containing local variables</li>
894<li><em>debug</em> Output debugging information</li>

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

lines 895–896why, one step back
895<li><em>status</em> Response status code, defaults to 200</li>
896<li><em>headers</em> Response headers object</li>

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 897–952why, one step back
897</ul>
898899900<h3 id="res-partial-view-options-">res.partial(view[, options])</h3>901902<p>Render <em>view</em> partial with the given <em>options</em>. This method is always available903to the view as a local variable.</p>904905<ul>906<li><p><em>as</em> Variable name for each <em>collection</em> value, defaults to the view name.</p>907908<ul>909<li>as: 'something' will add the <em>something</em> local variable</li>910<li>as: this will use the collection value as the template context</li>911<li>as: global will merge the collection value's properties with <em>locals</em></li>912</ul>913</li>914<li><p><em>collection</em> Array of objects, the name is derived from the view name itself.915For example <em>video.html</em> will have a object <em>video</em> available to it.</p></li>916</ul>917918919<p>The following are equivalent, and the name of collection value when passed920to the partial will be <em>movie</em> as derived from the name.</p>921922<pre><code>partial('movie.jade', { collection: movies });923partial('movie.jade', movies);924partial('movie', movies);925// In view: movie.director926</code></pre>927928<p>To change the local from <em>movie</em> to <em>video</em> we can use the "as" option:</p>929930<pre><code>partial('movie', { collection: movies, as: 'video' });931// In view: video.director932</code></pre>933934<p>Also we can make our movie the value of <em>this</em> within our view so that instead935of <em>movie.director</em> we could use <em>this.director</em>.</p>936937<pre><code>partial('movie', { collection: movies, as: this });938// In view: this.director939</code></pre>940941<p>Another alternative is to "explode" the properties of the collection item into942pseudo globals (local variables) by using <em>as: global</em>, which again is syntactic sugar:</p>943944<pre><code>partials('movie', { collection: movies, as: global });945// In view: director946</code></pre>947948<h3 id="app-set-name-val-">app.set(name[, val])</h3>949950<p>Apply an application level setting <em>name</em> to <em>val</em>, or951get the value of <em>name</em> when <em>val</em> is not present:</p>952

Updated docsTj Holowaychukbefore adoption3234329

lines 953–955why, one step back
953<pre><code>app.set('views', __dirname + '/views');
954app.set('views');955// =&gt; ...path...

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 956–1001why, one step back
956</code></pre>
957958<h3 id="app-enable-name-">app.enable(name)</h3>959960<p>Enable the given setting <em>name</em>:</p>961962<pre><code>app.enable('some arbitrary setting');963app.set('some arbitrary setting');964// =&gt; true965</code></pre>966967<h3 id="app-disable-name-">app.disable(name)</h3>968969<p>Disable the given setting <em>name</em>:</p>970971<pre><code>app.disable('some setting');972app.set('some setting');973// =&gt; false974</code></pre>975976<h3 id="app-configure-env-function-function-">app.configure(env|function[, function])</h3>977978<p>Define a callback function for the given <em>env</em> (or all environments) with callback <em>function</em>:</p>979980<pre><code>app.configure(function(){981 // executed for each env982});983984app.configure('development', function(){985 // executed for 'development' only986});987</code></pre>988989<h3 id="app-redirect-name-val-">app.redirect(name, val)</h3>990991<p>For use with <code>res.redirect()</code> we can map redirects at the application level as shown below:</p>992993<pre><code>app.redirect('google', 'http://google.com');994</code></pre>995996<p>Now in a route we may call:</p>997998<p> res.redirect('google');</p>9991000<p>We may also map dynamic redirects:</p>1001

Updated docsTj Holowaychukbefore adoptiond691ecb

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 1004–1025why, one step back
1004});
1005</code></pre>10061007<p>So now we may do the following, and the redirect will dynamically adjust to1008the context of the request. If we called this route with <em>GET /post/12</em> our1009redirect <em>Location</em> would be <em>/post/12/comments</em>.</p>10101011<pre><code>app.get('/post/:id', function(req, res){1012 res.redirect('comments');1013});1014</code></pre>10151016<h3 id="app-error-function-">app.error(function)</h3>10171018<p>Adds an error handler <em>function</em> which will receive the exception as the first parameter as shown below.1019Note that we may set several error handlers by making several calls to this method, however the handler1020should call <em>next(err)</em> if it does not wish to deal with the exception:</p>10211022<pre><code>app.error(function(err, req, res, next){1023 res.send(err.message, 500);1024});1025</code></pre>

Added app.helpers() docsTj Holowaychukbefore adoption7eb088b

lines 1026–1028why, one step back
1026
1027<h3 id="app-helpers-obj-">app.helpers(obj)</h3>1028

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

lines 1029–1046why, one step back
1029<p>Registers static view helpers.</p>
10301031<pre><code>app.helpers({1032 name: function(first, last){ return first + ', ' + last },1033 firstName: 'tj',1034 lastName: 'holowaychuk'1035});1036</code></pre>10371038<p>Our view could now utilize the <em>firstName</em> and <em>lastName</em> variables,1039as well as the <em>name()</em> function exposed.</p>10401041<pre><code>&lt;%= name(firstName, lastName) %>1042</code></pre>10431044<h3 id="app-dynamicHelpers-obj-">app.dynamicHelpers(obj)</h3>10451046<p>Registers dynamic view helpers. Dynamic view helpers

Updated docsTj Holowaychukbefore adoptiond691ecb

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

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

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

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

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

Updated docsTj Holowaychukbefore adoptiond691ecb

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

Added app.helpers() docsTj Holowaychukbefore adoption7eb088b

lines 1053–1057why, one step back
1053 return req.session;
1054 }1055});1056</code></pre>1057

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

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

Rebuilt docsTj Holowaychukbefore adoptionba88e48

lines 1061–1075why, one step back
1061</code></pre>
10621063<h3 id="app-mounted-fn-">app.mounted(fn)</h3>10641065<p>Assign a callback <em>fn</em> which is called when this <em>Server</em> is passed to <em>Server#use()</em>.</p>10661067<pre><code>var app = express.createServer(),1068 blog = express.createServer();10691070blog.mounted(function(parent){1071 // parent is app1072 // "this" is blog1073});10741075app.use(blog);

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

lines 1076–1103why, one step back
1076</code></pre>
10771078<h3 id="app-register-ext-exports-">app.register(ext, exports)</h3>10791080<p>Register the given template engine <em>exports</em>1081as <em>ext</em>. For example we may wish to map ".html"1082files to jade:</p>10831084<pre><code> app.register('.html', require('jade'));1085</code></pre>10861087<p>This is also useful for libraries that may not1088match extensions correctly. For example my haml.js1089library is installed from npm as "hamljs" so instead1090of layout.hamljs, we can register the engine as ".haml":</p>10911092<pre><code> app.register('.haml', require('haml-js'));1093</code></pre>10941095<p>For engines that do not comply with the Express1096specification, we can also wrap their api this way.</p>10971098<pre><code> app.register('.foo', {1099 render: function(str, options) {1100 // perhaps their api is1101 // foo.toHTML(str, options);1102 }1103 });

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

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 1105–1137why, one step back
1105
1106<h3 id="app-listen-port-host-">app.listen([port[, host]])</h3>11071108<p>Bind the app server to the given <em>port</em>, which defaults to 3000. When <em>host</em> is omitted all1109connections will be accepted via <em>INADDR_ANY</em>.</p>11101111<pre><code>app.listen();1112app.listen(3000);1113app.listen(3000, 'n.n.n.n');1114</code></pre>11151116<p>The <em>port</em> argument may also be a string representing the path to a unix domain socket:</p>11171118<pre><code>app.listen('/tmp/express.sock');1119</code></pre>11201121<p>Then try it out:</p>11221123<pre><code>$ telnet /tmp/express.sock1124GET / HTTP/1.111251126HTTP/1.1 200 OK1127Content-Type: text/plain1128Content-Length: 1111291130Hello World1131</code></pre>11321133</div>1134 </div>1135 </div>1136 </body>1137</html>