guide.html
This file
Path
docs/guide.html
1,622 lines · 56.5 KiB · HTML
Last changed by
fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
History of this fileTake it
The walk stopped after 200 commits: lines older than 242efc7 point at it.
Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1–181why, one step back1<html>2 <head>3 <title>Express - node web framework</title>4 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>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;19 -webkit-font-smoothing: antialiased;20 background: url(images/bg.jpg) 50% 0 no-repeat;21 color: #8b8b8b; }2223 * {24 outline: none; }2526 em {27 color: white; }2829 a img {30 border: none !important; }3132 a {33 font-weight: bold;34 text-decoration: none;35 color: white;36 -webkit-transition-property: opacity, -webkit-transform, color, background-color, padding, -webkit-box-shadow;37 -webkit-transition-duration: 0.15s;38 -webkit-transition-timing-function: ease-out; }39 a:hover {40 opacity: 0.8; }4142 h1, h2, h3, h4 {43 margin: 45px 0 0 0;44 color: white;45 text-shadow: 1px 2px 2px rgba(0,0,0,0.6); }4647 h3 {48 font-size: 18px; }49 h4 {50 margin-left: 10px;51 font-size: 14px;52 }5354 pre {55 margin: 20px 10px;56 padding: 25px 20px;57 background: rgba(0,0,0,0.5);58 border: 1px solid #323232;59 -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);60 -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);61 -webkit-border-radius: 5px;62 -moz-border-radius: 5px; }6364 code {65 font-family: "Helvetica Neue", "Lucida Grande", "Arial"; }6667 ul {68 margin: 15px 0;69 padding: 0 0 0 35px; }70 ul li {71 margin: 0;72 padding: 2px 0;73 list-style: square; }74 ul li ul {75 margin: 0;76 padding-left: 12px;77 }7879 .man-name, #Express { display:none; }8081 .sect {82 margin-left: 40px; }83 img {84 margin-left: 20px;85 margin-bottom: 15px;86 }8788 #logo {89 display: block;90 margin-left: 30%;91 margin-bottom: 30px;92 width: 194px;93 height: 51px;94 background: url(images/logo.png) 0 0 no-repeat;95 text-indent: -99999px; }96 #logo:hover {97 opacity: 0.7; }98 #logo:active {99 opacity: 0.3; }100101 #ribbon {102 position: fixed;103 top: 0;104 right: 0;105 z-index: 2; }106107 #wrapper {108 width: 100%;109 min-height: 800px;110 background: url(images/top.png) 0 0 repeat-x; }111112 #container {113 margin: 0 auto;114 padding-top: 80px;115 width: 550px; }116117 #toc {118 position: fixed;119 top: 0;120 left: 0;121 margin: 0 0 0 15px;122 padding: 15px;123 height: 100%;124 background: rgba(0,0,0,0.2);125 overflow: auto;126 border-right: 1px solid rgba(255,255,255,0.05);127 }128 #toc li {129 padding: 0;130 list-style: none;131 }132 #toc li a {133 font-size: 11px;134 }135 #menu {136 margin-left: 75px;137 padding: 0;138 padding-bottom: 30px; }139 #menu li {140 display: inline;141 list-style: none; }142 #menu li a {143 display: block;144 float: left;145 margin: 0 2px;146 padding: 3px 15px;147 background: rgba(0,0,0,0.2);148 -webkit-border-radius: 8px;149 -moz-border-radius: 8px;150 -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);151 -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);152 -webkit-transition-property: opacity, -webkit-transform, color, background-color, -webkit-box-shadow;153 -webkit-transition-duration: 0.15s;154 -webkit-transition-timing-function: ease-out; }155 #menu li a:hover,156 #menu li a.active {157 background: rgba(0,0,0,0.5); }158 #menu li a:active {159 background: rgba(0,0,0,0.1);160 -webkit-box-shadow: 1px 1px 1px rgba(0,0,0,0.4);161 -moz-box-shadow: 1px 1px 1px rgba(0,0,0,0.4); }162 </style>163 <script>164 $(function(){165 $('.section').hide();166 $('.toggle, a.section-title').toggle(function(){167 $(this).siblings('ul').fadeIn(300);168 return false;169 }, function(){170 $(this).siblings('ul').fadeOut(300);171 return false;172 });173 });174 </script>175 </head>176 <body>177 <a href='http://github.com/visionmedia/express'> 178 <img alt='Fork me on GitHub' id='ribbon' src='http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png' /> 179 </a>180 <div id="wrapper">181 <div id="container"><ul id="toc">fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 182–199why, one step back182<li><a href="#installation">Installation</a></li>183<li><a href="#creating-a server">Creating A Server</a></li>184<li><a href="#creating-an https server">Creating An HTTPS Server</a></li>185<li><a href="#configuration">Configuration</a></li>186<li><a href="#settings">Settings</a></li>187<li><a href="#routing">Routing</a></li>188<li><a href="#passing-route control">Passing Route Control</a></li>189<li><a href="#middleware">Middleware</a></li>190<li><a href="#route-middleware">Route Middleware</a></li>191<li><a href="#http-methods">HTTP Methods</a></li>192<li><a href="#error-handling">Error Handling</a></li>193<li><a href="#route-param pre-conditions">Route Param Pre-conditions</a></li>194<li><a href="#view-rendering">View Rendering</a></li>195<li><a href="#view-partials">View Partials</a></li>196<li><a href="#view-lookup">View Lookup</a></li>197<li><a href="#template-engines">Template Engines</a></li>198<li><a href="#session-support">Session Support</a></li>199<li><a href="#migration-guide">Migration Guide</a></li>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 200why, one step back200<li><a href="#" class="toggle">+</a> <a class="section-title" href="#">Request</a><ul class="section" id="section-Request">fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 201–206why, one step back201<li><a href="#req.header()">header()</a></li>202<li><a href="#req.accepts()">accepts()</a></li>203<li><a href="#req.is()">is()</a></li>204<li><a href="#req.param()">param()</a></li>205<li><a href="#req.flash()">flash()</a></li>206<li><a href="#req.isxmlhttprequest">isXMLHttpRequest</a></li>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 207–208why, one step back207</ul></li>208<li><a href="#" class="toggle">+</a> <a class="section-title" href="#">Response</a><ul class="section" id="section-Response">fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 209–222why, one step back209<li><a href="#res.header()">header()</a></li>210<li><a href="#res.charset">charset</a></li>211<li><a href="#res.contenttype()">contentType()</a></li>212<li><a href="#res.attachment()">attachment()</a></li>213<li><a href="#res.sendfile()">sendfile()</a></li>214<li><a href="#res.download()">download()</a></li>215<li><a href="#res.send()">send()</a></li>216<li><a href="#res.redirect()">redirect()</a></li>217<li><a href="#res.cookie()">cookie()</a></li>218<li><a href="#res.clearcookie()">clearCookie()</a></li>219<li><a href="#res.render()">render()</a></li>220<li><a href="#res.partial()">partial()</a></li>221<li><a href="#res.local()">local()</a></li>222<li><a href="#res.locals()">locals()</a></li>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 223–224why, one step back223</ul></li>224<li><a href="#" class="toggle">+</a> <a class="section-title" href="#">Server</a><ul class="section" id="section-Server">fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 225–237why, one step back225<li><a href="#app.set()">set()</a></li>226<li><a href="#app.enable()">enable()</a></li>227<li><a href="#app.enabled()">enabled()</a></li>228<li><a href="#app.disable()">disable()</a></li>229<li><a href="#app.disabled()">disabled()</a></li>230<li><a href="#app.configure()">configure()</a></li>231<li><a href="#app.redirect()">redirect()</a></li>232<li><a href="#app.error()">error()</a></li>233<li><a href="#app.helpers()">helpers()</a></li>234<li><a href="#app.dynamichelpers()">dynamicHelpers()</a></li>235<li><a href="#app.mounted()">mounted()</a></li>236<li><a href="#app.register()">register()</a></li>237<li><a href="#app.listen()">listen()</a></li>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 238–250why, one step back238</ul></li>239</ul>240 <a href='http://github.com/visionmedia/express' id='logo'>Express</a> 241 <p id="tagline">242 High performance, high class web development for243 <a href="http://nodejs.org">Node.js</a>244 </p>245 <ul id="menu">246 <li><a href="index.html">Home</a></li>247 <li><a href="guide.html">Guide</a></li>248 <li><a href="screencasts.html">Screencasts</a></li>249 <li><a href="applications.html">Applications</a></li>250 </ul>fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 251why, one step back251<h3 id="installation">Installation</h3>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 252why, one step back252fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 253–254why, one step back253<pre><code>$ npm install express254</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 255why, one step back255fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 256why, one step back256<h3 id="creating-a server">Creating A Server</h3>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 257why, one step back257Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 258why, one step back258<p> To create an instance of the <em>express.HTTPServer</em>, simply invoke the <em>createServer()</em> method. With our instance <em>app</em> we can then define routes based on the HTTP verbs, in this example <em>app.get()</em>.</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 259–262why, one step back259260<pre><code>var app = require('express').createServer();261262app.get('/', function(req, res){Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 263why, one step back263 res.send('hello world');Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 264–265why, one step back264});265fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 266–267why, one step back266app.listen(3000);267</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 268why, one step back268fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 269why, one step back269<h3 id="creating-an https server">Creating An HTTPS Server</h3>Merge branch 'docs'Tj Holowaychukbefore adoption64da262
lines 270–271why, one step back270271<p> To initialize a <em>express.HTTPSServer</em> we do the same as above, however wefixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 272why, one step back272 pass an options object, accepting <em>key</em>, <em>cert</em> and the others mentioned in node’s <a href="http://nodejs.org/docs/v0.3.7/api/https.html#https.createServer">https documentation</a>.</p>Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 273why, one step back273fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 274–275why, one step back274<pre><code> var app = require('express').createServer({ key: ... });275</code></pre>Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 276why, one step back276fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 277why, one step back277<h3 id="configuration">Configuration</h3>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 278–280why, one step back278279<p>Express supports arbitrary environments, such as <em>production</em> and <em>development</em>. Developers280can use the <em>configure()</em> method to setup needs required by the current environment. Whenfixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 281why, one step back281<em>configure()</em> is called without an environment name it will be run in <em>every</em> environmentUpdated jade submoduleTj Holowaychukbefore adoption242efc7
lines 282–284why, one step back282prior to the environment specific callback.</p>283284<p>In the example below we only <em>dumpExceptions</em>, and respond with exception stack tracesMerge branch 'docs'Tj Holowaychukbefore adoption64da262
line 285why, one step back285in <em>development</em> mode, however for both environments we utilize <em>methodOverride</em> and <em>bodyParser</em>.Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 286why, one step back286Note the use of <em>app.router</em>, which can (optionally) be used to mount the application routes,Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 287why, one step back287otherwise the first call to <em>app.get()</em>, <em>app.post()</em>, etc will mount the routes.</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 288–289why, one step back288289<pre><code>app.configure(function(){fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 290–292why, one step back290 app.use(express.methodOverride());291 app.use(express.bodyParser());292 app.use(app.router);Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 293–295why, one step back293});294295app.configure('development', function(){fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 296–297why, one step back296 app.use(express.static(__dirname + '/public'));297 app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 298–300why, one step back298});299300app.configure('production', function(){Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 301why, one step back301 var oneYear = 31557600000;regenerated docsTj Holowaychukbefore adoptiondd7158a
line 302why, one step back302 app.use(express.static(__dirname + '/public', { maxAge: oneYear }));Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 303why, one step back303 app.use(express.errorHandler());fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 304–305why, one step back304});305</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 306–308why, one step back306307<p>For internal and arbitrary settings Express provides the <em>set(key[, val])</em>, <em>enable(key)</em>, <em>disable(key)</em> methods:</p>308Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 309why, one step back309<pre><code> app.configure(function(){Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 310–311why, one step back310 app.set('views', __dirname + '/views');311 app.set('views');Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 312why, one step back312 // => "/absolute/path/to/views"Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 313–318why, one step back313314 app.enable('some feature');315 // same as app.set('some feature', true);316317 app.disable('some feature');318 // same as app.set('some feature', false);Merge branch 'docs'Tj Holowaychukbefore adoption64da262
lines 319–321why, one step back319320 app.enabled('some feature')321 // => falsefixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 322–323why, one step back322 });323</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 324why, one step back324fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 325why, one step back325<p>To alter the environment we can set the <em>NODE_ENV</em> environment variable, for example:</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 326why, one step back326fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 327–328why, one step back327<pre><code>$ NODE_ENV=production node app.js328</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 329–331why, one step back329330<p>This is <em>very</em> important, as many caching mechanisms are <em>only enabled</em> when in production.</p>331fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 332why, one step back332<h3 id="settings">Settings</h3>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 333–335why, one step back333334<p>Express supports the following settings out of the box:</p>335fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 336–341why, one step back336<ul>337<li><em>home</em> Application base path used for <em>res.redirect()</em> and transparently handling mounted apps.</li>338<li><em>views</em> Root views directory defaulting to <strong>CWD/views</strong></li>339<li><em>view engine</em> Default view engine name for views rendered without extensions</li>340<li><em>view options</em> An object specifying global view options</li>341</ul>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 342–343why, one step back342343fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 344–345why, one step back344<h3 id="routing">Routing</h3>345Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 346why, one step back346<p>Express utilizes the HTTP verbs to provide a meaningful, expressive routing API.fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 347–348why, one step back347For example we may want to render a user’s account for the path <em>/user/12</em>, this348can be done by defining the route below. The values associated to the named placeholdersUpdated jade submoduleTj Holowaychukbefore adoption242efc7
lines 349–351why, one step back349are available as <code>req.params</code>.</p>350351<pre><code>app.get('/user/:id', function(req, res){fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 352–354why, one step back352 res.send('user ' + req.params.id);353});354</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 355–358why, one step back355356<p>A route is simple a string which is compiled to a <em>RegExp</em> internally. For example357when <em>/user/:id</em> is compiled, a simplified version of the regexp may look similar to:</p>358fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 359–360why, one step back359<pre><code>\/user\/([^\/]+)\/?360</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 361–362why, one step back361362<p>Regular expression literals may also be passed for complex uses. Since capturefixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 363why, one step back363groups with literal <em>RegExp</em>’s are anonymous we can access them directly <code>req.params</code>. So our first capture group would be <em>req.params[0]</em> and the second would follow as <em>req.params[1]</em>.</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 364–366why, one step back364365<pre><code>app.get(/^\/users?(?:\/(\d+)(?:\.\.(\d+))?)?/, function(req, res){366 res.send(req.params);fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 367–368why, one step back367});368</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 369–378why, one step back369370<p>Curl requests against the previously defined route:</p>371372<pre><code> $ curl http://dev:3000/user373 [null,null]374 $ curl http://dev:3000/users375 [null,null]376 $ curl http://dev:3000/users/1377 ["1",null]378 $ curl http://dev:3000/users/1..15fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 379–380why, one step back379 ["1","15"]380</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 381–414why, one step back381382<p>Below are some route examples, and the associated paths that they383may consume:</p>384385<pre><code> "/user/:id"386 /user/12387388 "/users/:id?"389 /users/5390 /users391392 "/files/*"393 /files/jquery.js394 /files/javascripts/jquery.js395396 "/file/*.*"397 /files/jquery.js398 /files/javascripts/jquery.js399400 "/user/:id/:operation?"401 /user/1402 /user/1/edit403404 "/products.:format"405 /products.json406 /products.xml407408 "/products.:format?"409 /products.json410 /products.xml411 /products412413 "/user/:id.:format?"414 /user/12fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 415–416why, one step back415 /user/12.json416</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 417why, one step back417Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 418why, one step back418<p>For example we can <strong>POST</strong> some json, and echo the json back using the <em>bodyParser</em> middleware which will parse json request bodies (as well as others), and place the result in <em>req.body</em>:</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 419–422why, one step back419420<pre><code>var express = require('express')421 , app = express.createServer();422Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 423why, one step back423app.use(express.bodyParser());Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 424–428why, one step back424425app.post('/', function(req, res){426 res.send(req.body);427});428fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 429–430why, one step back429app.listen(3000);430</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 431why, one step back431fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 432why, one step back432<p>Typically we may use a “dumb” placeholder such as “/user/:id” which has no restrictions, however say for example we are limiting a user id to digits, we may use <em>‘/user/:id(\d+)’</em> which will <em>not</em> match unless the placeholder value contains only digits.</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 433why, one step back433fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 434why, one step back434<h3 id="passing-route control">Passing Route Control</h3>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 435–437why, one step back435436<p>We may pass control to the next <em>matching</em> route, by calling the <em>third</em> argument,437the <em>next()</em> function. When a match cannot be made, control is passed back to Connect,Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 438why, one step back438and middleware continue to be invoked in the order that they are added via <em>use()</em>. The same is true for several routes which have the same path defined, they will simply be executed in order until one does <em>not</em> call <em>next()</em> and decides to respond.</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 439–440why, one step back439440<pre><code>app.get('/users/:id?', function(req, res, next){fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 441–446why, one step back441 var id = req.params.id;442 if (id) {443 // do something444 } else {445 next();446 }Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 447–449why, one step back447});448449app.get('/users', function(req, res){fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 450–452why, one step back450 // do something else451});452</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 453why, one step back453Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 454why, one step back454<p>The <em>app.all()</em> method is useful for applying the same logic for all HTTP verbs in a single call. Below we use this to load a user from our fake database, and assign it to <em>req.user</em>.</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 455–485why, one step back455456<pre><code>var express = require('express')457 , app = express.createServer();458459var users = [{ name: 'tj' }];460461app.all('/user/:id/:op?', function(req, res, next){462 req.user = users[req.params.id];463 if (req.user) {464 next();465 } else {466 next(new Error('cannot find user ' + req.params.id));467 }468});469470app.get('/user/:id', function(req, res){471 res.send('viewing ' + req.user.name);472});473474app.get('/user/:id/edit', function(req, res){475 res.send('editing ' + req.user.name);476});477478app.put('/user/:id', function(req, res){479 res.send('updating ' + req.user.name);480});481482app.get('*', function(req, res){483 res.send('what???', 404);484});485fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 486–487why, one step back486app.listen(3000); 487</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 488why, one step back488fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 489why, one step back489<h3 id="middleware">Middleware</h3>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 490–496why, one step back490491<p>Middleware via <a href="http://github.com/senchalabs/connect">Connect</a> can be492passed to <em>express.createServer()</em> as you would with a regular Connect server. For example:</p>493494<pre><code> var express = require('express');495496var app = express.createServer(fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 497–500why, one step back497 express.logger()498 , express.bodyParser()499 );500</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 501why, one step back501Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 502why, one step back502<p>Alternatively we can <em>use()</em> them which is useful when adding middleware within <em>configure()</em> blocks, in a progressive manor.</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 503why, one step back503fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 504–505why, one step back504<pre><code>app.use(express.logger({ format: ':method :uri' }));505</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 506why, one step back506fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 507why, one step back507<p>Typically with connect middleware you would <em>require(‘connect’)</em> like so:</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 508–510why, one step back508509<pre><code>var connect = require('connect');510app.use(connect.logger());fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 511–512why, one step back511app.use(connect.bodyParser());512</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 513–516why, one step back513514<p>This is somewhat annoying, so express re-exports these middleware properties, however they are <em>identical</em>:</p>515516<pre><code>app.use(express.logger());fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 517–518why, one step back517app.use(express.bodyParser());518</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 519why, one step back519fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 520why, one step back520<h3 id="route-middleware">Route Middleware</h3>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 521–523why, one step back521522<p>Routes may utilize route-specific middleware by passing one or more additional callbacks (or arrays) to the method. This feature is extremely useful for restricting access, loading data used by the route etc.</p>523fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 524why, one step back524<p>Typically async data retrieval might look similar to below, where we take the <em>:id</em> parameter, and attempt loading a user.</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 525–526why, one step back525526<pre><code>app.get('/user/:id', function(req, res, next){Merge branch 'docs'Tj Holowaychukbefore adoption64da262
lines 527–530why, one step back527 loadUser(req.params.id, function(err, user){528 if (err) return next(err);529 res.send('Viewing user ' + user.name);530 });fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 531–532why, one step back531});532</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 533why, one step back533fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 534why, one step back534<p>To keep things DRY and to increase readability we can apply this logic within a middleware. As you can see below, abstracting this logic into middleware allows us to reuse it, and clean up our route at the same time.</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 535–536why, one step back535536<pre><code>function loadUser(req, res, next) {Merge branch 'docs'Tj Holowaychukbefore adoption64da262
lines 537–544why, one step back537 // You would fetch your user from the db538 var user = users[req.params.id];539 if (user) {540 req.user = user;541 next();542 } else {543 next(new Error('Failed to load user ' + req.params.id));544 }Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 545–547why, one step back545}546547app.get('/user/:id', loadUser, function(req, res){Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 548why, one step back548 res.send('Viewing user ' + req.user.name);fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 549–550why, one step back549});550</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 551–554why, one step back551552<p>Multiple route middleware can be applied, and will be executed sequentially to apply further logic such as restricting access to a user account. In the example below only the authenticated user may edit his/her account.</p>553554<pre><code>function andRestrictToSelf(req, res, next) {Merge branch 'docs'Tj Holowaychukbefore adoption64da262
lines 555–557why, one step back555 req.authenticatedUser.id == req.user.id556 ? next()557 : next(new Error('Unauthorized'));Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 558–560why, one step back558}559560app.get('/user/:id/edit', loadUser, andRestrictToSelf, function(req, res){Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 561why, one step back561 res.send('Editing user ' + req.user.name);fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 562–563why, one step back562});563</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 564–567why, one step back564565<p>Keeping in mind that middleware are simply functions, we can define function that <em>returns</em> the middleware in order to create a more expressive and flexible solution as shown below.</p>566567<pre><code>function andRestrictTo(role) {Merge branch 'docs'Tj Holowaychukbefore adoption64da262
lines 568–572why, one step back568 return function(req, res, next) {569 req.authenticatedUser.role == role570 ? next()571 : next(new Error('Unauthorized'));572 }Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 573–575why, one step back573}574575app.del('/user/:id', loadUser, andRestrictTo('admin'), function(req, res){Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 576why, one step back576 res.send('Deleted user ' + req.user.name);fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 577–578why, one step back577});578</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 579why, one step back579fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 580why, one step back580<p>Commonly used “stacks” of middleware can be passed as an array (<em>applied recursively</em>), which can be mixed and matched to any degree.</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 581–590why, one step back581582<pre><code>var a = [middleware1, middleware2]583 , b = [middleware3, middleware4]584 , all = [a, b];585586app.get('/foo', a, function(){});587app.get('/bar', a, function(){});588589app.get('/', a, middleware3, middleware4, function(){});590app.get('/', a, b, function(){});fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 591–592why, one step back591app.get('/', all, function(){});592</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 593–595why, one step back593594<p>For this example in full, view the <a href="http://github.com/visionmedia/express/blob/master/examples/route-middleware/app.js">route middleware example</a> in the repository.</p>595fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 596why, one step back596<h3 id="http-methods">HTTP Methods</h3>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 597–599why, one step back597598<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>599fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 600why, one step back600<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>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 601–605why, one step back601602<pre><code> <form method="post" action="/">603 <input type="text" name="user[name]" />604 <input type="text" name="user[email]" />605 <input type="submit" value="Submit" />fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 606–607why, one step back606 </form>607</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 608why, one step back608fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 609why, one step back609<p>By default Express does not know what to do with this request body, so we should add the <em>bodyParser</em> middleware, which will parse <em>application/x-www-form-urlencoded</em> and <em>application/json</em> request bodies and place the variables in <em>req.body</em>. We can do this by “using” the middleware as shown below:</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 610why, one step back610fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 611–612why, one step back611<pre><code>app.use(express.bodyParser());612</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 613–616why, one step back613614<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>615616<pre><code>app.post('/', function(req, res){Merge branch 'docs'Tj Holowaychukbefore adoption64da262
lines 617–618why, one step back617 console.log(req.body.user);618 res.redirect('back');fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 619–620why, one step back619});620</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 621why, one step back621fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 622why, one step back622<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>bodyParser</em> so that it can utilize it’s <em>req.body</em> containing the form values.</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 623why, one step back623Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 624why, one step back624<pre><code>app.use(express.bodyParser());fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 625–626why, one step back625app.use(express.methodOverride());626</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 627–639why, one step back627628<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>629630<pre><code><form method="post" action="/">631 <input type="hidden" name="_method" value="put" />632 <input type="text" name="user[name]" />633 <input type="text" name="user[email]" />634 <input type="submit" value="Submit" /> 635</form>636637app.put('/', function(){638 console.log(req.body.user);639 res.redirect('back');fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 640–641why, one step back640});641</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 642why, one step back642fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 643why, one step back643<h3 id="error-handling">Error Handling</h3>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 644–649why, one step back644645<p>Express provides the <em>app.error()</em> method which receives exceptions thrown within a route,646or passed to <em>next(err)</em>. Below is an example which serves different pages based on our647ad-hoc <em>NotFound</em> exception:</p>648649<pre><code>function NotFound(msg){Merge branch 'docs'Tj Holowaychukbefore adoption64da262
lines 650–652why, one step back650 this.name = 'NotFound';651 Error.call(this, msg);652 Error.captureStackTrace(this, arguments.callee);Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 653–654why, one step back653}654fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 655why, one step back655NotFound.prototype.__proto__ = Error.prototype;Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 656–657why, one step back656657app.get('/404', function(req, res){Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 658why, one step back658 throw new NotFound;Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 659–661why, one step back659});660661app.get('/500', function(req, res){Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 662why, one step back662 throw new Error('keyboard cat!');fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 663–664why, one step back663});664</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 665–670why, one step back665666<p>We can call <em>app.error()</em> several times as shown below.667Here we check for an instanceof <em>NotFound</em> and show the668404 page, or we pass on to the next error handler.</p>669670<p>Note that these handlers can be defined anywhere, as theyfixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 671why, one step back671will be placed below the route handlers on <em>listen()</em>. ThisUpdated jade submoduleTj Holowaychukbefore adoption242efc7
lines 672–680why, one step back672allows for definition within <em>configure()</em> blocks so we can673handle exceptions in different ways based on the environment.</p>674675<pre><code>app.error(function(err, req, res, next){676 if (err instanceof NotFound) {677 res.render('404.jade');678 } else {679 next(err);680 }fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 681–682why, one step back681});682</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 683–684why, one step back683684<p>Here we assume all errors as 500 for the simplicity offixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 685why, one step back685this demo, however you can choose whatever you like. For example when node performs filesystem syscalls, you may receive an error object with the <em>error.code</em> of <em>ENOENT</em>, meaning “no such file or directory”, we can utilize this in our error handling and display a page specific to this if desired.</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 686–687why, one step back686687<pre><code>app.error(function(err, req, res){Merge branch 'docs'Tj Holowaychukbefore adoption64da262
lines 688–690why, one step back688 res.render('500.jade', {689 error: err690 });fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 691–692why, one step back691});692</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 693–694why, one step back693694<p>Our apps could also utilize the Connect <em>errorHandler</em> middlewarefixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 695–696why, one step back695to report on exceptions. For example if we wish to output exceptions696in “development” mode to <em>stderr</em> we can use:</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 697why, one step back697fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 698–699why, one step back698<pre><code>app.use(express.errorHandler({ dumpExceptions: true }));699</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 700–703why, one step back700701<p>Also during development we may want fancy html pages to show exceptions702that are passed or thrown, so we can set <em>showStack</em> to true:</p>703fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 704–705why, one step back704<pre><code>app.use(express.errorHandler({ showStack: true, dumpExceptions: true }));705</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 706–709why, one step back706707<p>The <em>errorHandler</em> middleware also responds with <em>json</em> if <em>Accept: application/json</em>708is present, which is useful for developing apps that rely heavily on client-side JavaScript.</p>709fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 710why, one step back710<h3 id="route-param pre-conditions">Route Param Pre-conditions</h3>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 711why, one step back711Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 712why, one step back712<p>Route param pre-conditions can drastically improve the readability of your application, through implicit loading of data, and validation of request urls. For example if you are constantly fetching common data for several routes, such as loading a user for <em>/user/:id</em>, we might typically do something like below:</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 713–718why, one step back713714<pre><code>app.get('/user/:userId', function(req, res, next){715 User.get(req.params.userId, function(err, user){716 if (err) return next(err);717 res.send('user ' + user.name);718 });fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 719–720why, one step back719}); 720</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 721why, one step back721Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 722why, one step back722<p>With preconditions our params can be mapped to callbacks which may perform validation, coercion, or even loading data from a database. Below we invoke <em>app.param()</em> with the parameter name we wish to map to some middleware, as you can see we receive the <em>id</em> argument which contains the placeholder value. Using this we load the user and perform error handling as usual, and simple call <em>next()</em> to pass control to the next precondition or route handler.</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 723–730why, one step back723724<pre><code>app.param('userId', function(req, res, next, id){725 User.get(id, function(err, user){726 if (err) return next(err);727 if (!user) return next(new Error('failed to find user'));728 req.user = user;729 next();730 });fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 731–732why, one step back731});732</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 733–737why, one step back733734<p>Doing so, as mentioned drastically improves our route readability, and allows us to easily share this logic throughout our application:</p>735736<pre><code>app.get('/user/:userId', function(req, res){737 res.send('user ' + req.user.name);fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 738–739why, one step back738});739</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 740–742why, one step back740741<p>For simple cases such as route placeholder validation and coercion we can simple pass a callback which has an arity of 1 (accepts one argument). Any errors thrown will be passed to <em>next(err)</em>.</p>742fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 743–744why, one step back743<pre><code>app.param('number', function(n){ return parseInt(n, 10); });744</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 745–747why, one step back745746<p>We may also apply the same callback to several placeholders, for example a route GET <em>/commits/:from-:to</em> are both numbers, so we may define them as an array:</p>747fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 748–749why, one step back748<pre><code>app.param(['from', 'to'], function(n){ return parseInt(n, 10); });749</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 750why, one step back750fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 751why, one step back751<h3 id="view-rendering">View Rendering</h3>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 752why, one step back752fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 753why, one step back753<p>View filenames take the form “<name>.<engine>”, where <engine> is the nameUpdated jade submoduleTj Holowaychukbefore adoption242efc7
line 754why, one step back754of the module that will be required. For example the view <em>layout.ejs</em> willfixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 755–756why, one step back755tell the view system to <em>require(‘ejs’)</em>, the module being loaded must export the method <em>exports.compile(str, options)</em>, and return a <em>Function</em> to comply with Express. To alter this behaviour756<em>app.register()</em> can be used to map engines to file extensions, so that for example “foo.html” can be rendered by ejs.</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 757why, one step back757Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 758why, one step back758<p>Below is an example using <a href="http://github.com/visionmedia/jade">Jade</a> to render <em>index.html</em>,fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 759why, one step back759and since we do not use <em>layout: false</em> the rendered contents of <em>index.jade</em> will be passed asMerge branch 'docs'Tj Holowaychukbefore adoption64da262
line 760why, one step back760the <em>body</em> local variable in <em>layout.jade</em>.</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 761–762why, one step back761762<pre><code>app.get('/', function(req, res){fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 763–765why, one step back763 res.render('index.jade', { title: 'My Site' });764});765</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 766–767why, one step back766767<p>The new <em>view engine</em> setting allows us to specify our default template engine,Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 768why, one step back768so for example when using jade we could set:</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 769why, one step back769fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 770–771why, one step back770<pre><code>app.set('view engine', 'jade');771</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 772–774why, one step back772773<p>Allowing us to render with:</p>774fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 775–776why, one step back775<pre><code>res.render('index');776</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 777–779why, one step back777778<p>vs:</p>779fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 780–781why, one step back780<pre><code>res.render('index.jade');781</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 782–785why, one step back782783<p>When <em>view engine</em> is set, extensions are entirely optional, however we can still784mix and match template engines:</p>785fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 786–787why, one step back786<pre><code>res.render('another-page.ejs');787</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 788–791why, one step back788789<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>790791<pre><code>app.set('view options', {Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 792why, one step back792 layout: falsefixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 793–794why, one step back793});794</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 795why, one step back795Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 796why, one step back796<p>Which can then be overridden within the <em>res.render()</em> call if need be:</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 797why, one step back797fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 798–799why, one step back798<pre><code>res.render('myview.ejs', { layout: true });799</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 800–802why, one step back800801<p>When an alternate layout is required, we may also specify a path. For example if we have <em>view engine</em> set to <em>jade</em> and a file named <em>./views/mylayout.jade</em> we can simply pass:</p>802fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 803–804why, one step back803<pre><code>res.render('page', { layout: 'mylayout' });804</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 805–807why, one step back805806<p>Otherwise we must specify the extension:</p>807fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 808–809why, one step back808<pre><code>res.render('page', { layout: 'mylayout.jade' });809</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 810–812why, one step back810811<p>These paths may also be absolute:</p>812fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 813–814why, one step back813<pre><code>res.render('page', { layout: __dirname + '/../../mylayout.jade' });814</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 815–820why, one step back815816<p>A good example of this is specifying custom <em>ejs</em> opening and closing tags:</p>817818<pre><code>app.set('view options', {819 open: '{{',820 close: '}}'fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 821–822why, one step back821});822</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 823why, one step back823fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 824why, one step back824<h3 id="view-partials">View Partials</h3>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 825why, one step back825fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 826why, one step back826<p>The Express view system has built-in support for partials and collections, which are “mini” views representing a document fragment. For example rather than iteratingregenerated docsTj Holowaychukbefore adoptiondd7158a
line 827why, one step back827in a view to display comments, we could use partial collection:</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 828why, one step back828fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 829–830why, one step back829<pre><code>partial('comment', { collection: comments });830</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 831why, one step back831regenerated docsTj Holowaychukbefore adoptiondd7158a
line 832why, one step back832<p>If no other options or local variables are desired, we can omit the object and simply pass our array, which is equivalent to above:</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 833why, one step back833fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 834–835why, one step back834<pre><code>partial('comment', comments);835</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 836why, one step back836fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 837why, one step back837<p>When using the partial collection support a few “magic” locals are providedUpdated jade submoduleTj Holowaychukbefore adoption242efc7
lines 838–839why, one step back838for free:</p>839fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 840–845why, one step back840<ul>841<li><em>firstInCollection</em> true if this is the first object</li>842<li><em>indexInCollection</em> index of the object in the collection</li>843<li><em>lastInCollection</em> true if this is the last object</li>844<li><em>collectionLength</em> length of the collection</li>845</ul>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 846why, one step back846docsTj Holowaychukbefore adoption4609984
line 847why, one step back847fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 848–849why, one step back848<p>Local variables passed (or generated) take precedence, however locals passed to the parent view are available in the child view as well. So for example if we were to render a blog post with <em>partial(‘blog/post’, post)</em> it would generate the <em>post</em> local, but the view calling this function had the local <em>user</em>, it would be available to the <em>blog/post</em> view as well.</p>849Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 850–851why, one step back850<p>For documentation on altering the object name view <a href="http://expressjs.com/guide.html#res-partial-view-options-">res.partial()</a>.</p>851view lookup docsTj Holowaychukbefore adoption8a62f8c
lines 852–853why, one step back852<p><strong>NOTE:</strong> be careful about when you use partial collections, as rendering an array with a length of 100 means we have to render 100 views. For simple collections you may inline the iteration instead of using partial collection support to decrease overhead.</p>853fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 854why, one step back854<h3 id="view-lookup">View Lookup</h3>view lookup docsTj Holowaychukbefore adoption8a62f8c
line 855why, one step back855fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 856why, one step back856<p>View lookup is performed relative to the parent view, for example if we had a page view named <em>views/user/list.jade</em>, and within that view we did <em>partial(‘edit’)</em> it would attempt to load <em>views/user/edit.jade</em>, whereas <em>partial(‘../messages’)</em> would load <em>views/messages.jade</em>.</p>view lookup docsTj Holowaychukbefore adoption8a62f8c
line 857why, one step back857fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 858why, one step back858<p>The view system also allows for index templates, allowing you to have a directory of the same name. For example within a route we may have <em>res.render(‘users’)</em> either <em>views/users.jade</em>, or <em>views/users/index.jade</em>.</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 859why, one step back859fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 860why, one step back860<p>When utilizing index views as shown above, we may reference <em>views/users/index.jade</em> from a view in the same directory by <em>partial(‘users’)</em>, and the view system will try <em>../users/index</em>, preventing us from needing to call <em>partial(‘index’)</em>.</p>more docsTj Holowaychukbefore adoption0b6a941
line 861why, one step back861fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 862why, one step back862<h3 id="template-engines">Template Engines</h3>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 863–865why, one step back863864<p>Below are a few template engines commonly used with Express:</p>865fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 866–872why, one step back866<ul>867<li><a href="http://github.com/visionmedia/haml.js">Haml</a> haml implementation</li>868<li><a href="http://jade-lang.com">Jade</a> haml.js successor</li>869<li><a href="http://github.com/visionmedia/ejs">EJS</a> Embedded JavaScript</li>870<li><a href="http://github.com/mauricemach/coffeekup">CoffeeKup</a> CoffeeScript based templating</li>871<li><a href="https://github.com/kof/node-jqtpl">jQuery Templates</a> for node</li>872</ul>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 873–874why, one step back873874fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 875why, one step back875<h3 id="session-support">Session Support</h3>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 876why, one step back876fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 877–878why, one step back877<p>Sessions support can be added by using Connect’s <em>session</em> middleware. To do so we also need the <em>cookieParser</em> middleware place above it, which will parse and populate cookie data to <em>req.cookies</em>.</p>878Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 879why, one step back879<pre><code>app.use(express.cookieParser());fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 880–881why, one step back880app.use(express.session({ secret: "keyboard cat" }));881</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 882–885why, one step back882883<p>By default the <em>session</em> middleware uses the memory store bundled with Connect, however many implementations exist. For example <a href="http://github.com/visionmedia/connect-redis">connect-redis</a> supplies a <a href="http://code.google.com/p/redis/">Redis</a> session store and can be used as shown below:</p>884885<pre><code>var RedisStore = require('connect-redis');Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 886why, one step back886app.use(express.cookieParser());fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 887–888why, one step back887app.use(express.session({ secret: "keyboard cat", store: new RedisStore }));888</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 889–892why, one step back889890<p>Now the <em>req.session</em> and <em>req.sessionStore</em> properties will be accessible to all routes and subsequent middleware. Properties on <em>req.session</em> are automatically saved on a response, so for example if we wish to shopping cart data:</p>891892<pre><code>var RedisStore = require('connect-redis');Merge branch 'docs'Tj Holowaychukbefore adoption64da262
lines 893–895why, one step back893app.use(express.bodyParser());894app.use(express.cookieParser());895app.use(express.session({ secret: "keyboard cat", store: new RedisStore }));Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 896–898why, one step back896897app.post('/add-to-cart', function(req, res){898 // Perhaps we posted several items with a formMerge branch 'docs'Tj Holowaychukbefore adoption64da262
line 899why, one step back899 // (use the bodyParser() middleware for this)Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 900–912why, one step back900 var items = req.body.items;901 req.session.items = items;902 res.redirect('back');903});904905app.get('/add-to-cart', function(req, res){906 // When redirected back to GET /add-to-cart907 // we could check req.session.items && req.session.items.length908 // to print out a message909 if (req.session.items && req.session.items.length) {910 req.flash('info', 'You have %s items in your cart', req.session.items.length);911 }912 res.render('shopping-cart');fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 913–914why, one step back913});914</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 915why, one step back915Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 916why, one step back916<p>The <em>req.session</em> object also has methods such as <em>Session#touch()</em>, <em>Session#destroy()</em>, <em>Session#regenerate()</em> among others to maintain and manipulate sessions. For more information view the <a href="http://senchalabs.github.com/connect/middleware-session.html">Connect Session</a> documentation.</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 917why, one step back917fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 918why, one step back918<h3 id="migration-guide">Migration Guide</h3>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 919why, one step back919docsTj Holowaychukbefore adoptionc1a82bf
line 920why, one step back920<p> Express 1.x developers may reference the <a href="migrate.html">Migration Guide</a> to get up to speed on how to upgrade your application to work with Express 2.x, Connect 1.x, and Node 0.4.x.</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 921why, one step back921fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 922why, one step back922<h3 id="req.header()">req.header(key[, defaultValue])</h3>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 923–927why, one step back923924<p>Get the case-insensitive request header <em>key</em>, with optional <em>defaultValue</em>:</p>925926<pre><code>req.header('Host');927req.header('host');fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 928–929why, one step back928req.header('Accept', '*/*');929</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 930why, one step back930Merge branch 'docs'Tj Holowaychukbefore adoption64da262
lines 931–938why, one step back931<p>The <em>Referrer</em> and <em>Referer</em> header fields are special-cased, either will work:</p>932933<pre><code>// sent Referrer: http://google.com934935req.header('Referer');936// => "http://google.com"937938req.header('Referrer');fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 939–940why, one step back939// => "http://google.com"940</code></pre>Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 941why, one step back941fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 942why, one step back942<h3 id="req.accepts()">req.accepts(type)</h3>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 943–947why, one step back943944<p>Check if the <em>Accept</em> header is present, and includes the given <em>type</em>.</p>945946<p>When the <em>Accept</em> header is not present <em>true</em> is returned. Otherwise947the given <em>type</em> is matched by an exact match, and then subtypes. Youfixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 948–949why, one step back948may pass the subtype such as “html” which is then converted internally949to “text/html” using the mime lookup table.</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 950–963why, one step back950951<pre><code>// Accept: text/html952req.accepts('html');953// => true954955// Accept: text/*; application/json956req.accepts('html');957req.accepts('text/html');958req.accepts('text/plain');959req.accepts('application/json');960// => true961962req.accepts('image/png');963req.accepts('png');fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 964–965why, one step back964// => false965</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 966why, one step back966fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 967why, one step back967<h3 id="req.is()">req.is(type)</h3>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 968–982why, one step back968969<p>Check if the incoming request contains the <em>Content-Type</em>970header field, and it contains the give mime <em>type</em>.</p>971972<pre><code> // With Content-Type: text/html; charset=utf-8973 req.is('html');974 req.is('text/html');975 // => true976977 // When Content-Type is application/json978 req.is('json');979 req.is('application/json');980 // => true981982 req.is('html');fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 983–984why, one step back983 // => false984</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 985–987why, one step back985986<p>Ad-hoc callbacks can also be registered with Express, to perform987assertions again the request, for example if we need an expressivefixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 988why, one step back988way to check if our incoming request is an image, we can register <em>“an image”</em>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 989–992why, one step back989callback:</p>990991<pre><code> app.is('an image', function(req){992 return 0 == req.headers['content-type'].indexOf('image');fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 993–994why, one step back993 });994</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 995–996why, one step back995996<p>Now within our route callbacks, we can use to to assert content typesfixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 997why, one step back997such as <em>“image/jpeg”</em>, <em>“image/png”</em>, etc.</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 998–1004why, one step back998999<pre><code> app.post('/image/upload', function(req, res, next){1000 if (req.is('an image')) {1001 // do something1002 } else {1003 next();1004 }fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1005–1006why, one step back1005 });1006</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1007–1010why, one step back10071008<p>Keep in mind this method is <em>not</em> limited to checking <em>Content-Type</em>, you1009can perform any request assertion you wish.</p>1010fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1011why, one step back1011<p>Wildcard matches can also be made, simplifying our example above for <em>“an image”</em>, by asserting the <em>subtype</em> only:</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1012why, one step back1012fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1013–1014why, one step back1013<pre><code>req.is('image/*');1014</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1015why, one step back1015fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1016why, one step back1016<p>We may also assert the <em>type</em> as shown below, which would return true for <em>“application/json”</em>, and <em>“text/json”</em>.</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1017why, one step back1017fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1018–1019why, one step back1018<pre><code>req.is('*/json');1019</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1020why, one step back1020fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1021why, one step back1021<h3 id="req.param()">req.param(name[, default])</h3>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1022why, one step back1022Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 1023why, one step back1023<p>Return the value of param <em>name</em> when present or <em>default</em>.</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1024why, one step back1024fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1025–1029why, one step back1025<ul>1026<li>Checks route params (<em>req.params</em>), ex: /user/:id</li>1027<li>Checks query string params (<em>req.query</em>), ex: ?id=12</li>1028<li>Checks urlencoded body params (<em>req.body</em>), ex: id=12</li>1029</ul>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1030why, one step back1030fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1031why, one step back1031Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1032–1033why, one step back1032<p>To utilize urlencoded request bodies, <em>req.body</em>1033should be an object. This can be done by usingMerge branch 'docs'Tj Holowaychukbefore adoption64da262
line 1034why, one step back1034the _express.bodyParser middleware.</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1035why, one step back1035fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1036why, one step back1036<h3 id="req.flash()">req.flash(type[, msg])</h3>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1037–1051why, one step back10371038<p>Queue flash <em>msg</em> of the given <em>type</em>.</p>10391040<pre><code>req.flash('info', 'email sent');1041req.flash('error', 'email delivery failed');1042req.flash('info', 'email re-sent');1043// => 210441045req.flash('info');1046// => ['email sent', 'email re-sent']10471048req.flash('info');1049// => []10501051req.flash();fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1052–1053why, one step back1052// => { error: ['email delivery failed'], info: [] }1053</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1054–1056why, one step back10541055<p>Flash notification message may also utilize formatters, by default only the %s string formatter is available:</p>1056fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1057–1058why, one step back1057<pre><code>req.flash('info', 'email delivery to _%s_ from _%s_ failed.', toUser, fromUser);1058</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1059why, one step back1059fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1060why, one step back1060<h3 id="req.isxmlhttprequest">req.isXMLHttpRequest</h3>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1061–1065why, one step back10611062<p>Also aliased as <em>req.xhr</em>, this getter checks the <em>X-Requested-With</em> header1063to see if it was issued by an <em>XMLHttpRequest</em>:</p>10641065<pre><code>req.xhrfixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1066–1067why, one step back1066req.isXMLHttpRequest1067</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1068why, one step back1068fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1069why, one step back1069<h3 id="res.header()">res.header(key[, val])</h3>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1070–1079why, one step back10701071<p>Get or set the response header <em>key</em>.</p>10721073<pre><code>res.header('Content-Length');1074// => undefined10751076res.header('Content-Length', 123);1077// => 12310781079res.header('Content-Length');fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1080–1081why, one step back1080// => 1231081</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1082why, one step back1082fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1083why, one step back1083<h3 id="res.charset">res.charset</h3>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1084why, one step back1084fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1085why, one step back1085<p>Sets the charset for subsequent <code>Content-Type</code> header fields. For example <code>res.send()</code> and <code>res.render()</code> default to “utf8”, so we may explicitly set the charset before rendering a template:</p>regenerated docsTj Holowaychukbefore adoptiondd7158a
lines 1086–1087why, one step back10861087<pre><code>res.charset = 'ISO-8859-1';fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1088–1089why, one step back1088res.render('users');1089</code></pre>regenerated docsTj Holowaychukbefore adoptiondd7158a
lines 1090–1093why, one step back10901091<p>or before responding with <code>res.send()</code>:</p>10921093<pre><code>res.charset = 'ISO-8859-1';fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1094–1095why, one step back1094res.send(str);1095</code></pre>regenerated docsTj Holowaychukbefore adoptiondd7158a
line 1096why, one step back1096fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1097why, one step back1097<p>or with node’s <code>res.end()</code>:</p>regenerated docsTj Holowaychukbefore adoptiondd7158a
lines 1098–1100why, one step back10981099<pre><code>res.charset = 'ISO-8859-1';1100res.header('Content-Type', 'text/plain');fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1101–1102why, one step back1101res.end(str);1102</code></pre>regenerated docsTj Holowaychukbefore adoptiondd7158a
line 1103why, one step back1103fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1104why, one step back1104<h3 id="res.contenttype()">res.contentType(type)</h3>regenerated docsTj Holowaychukbefore adoptiondd7158a
line 1105why, one step back1105Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1106–1109why, one step back1106<p>Sets the <em>Content-Type</em> response header to the given <em>type</em>.</p>11071108<pre><code> var filename = 'path/to/image.png';1109 res.contentType(filename);fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1110–1111why, one step back1110 // Content-Type is now "image/png"1111</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1112why, one step back1112regenerated docsTj Holowaychukbefore adoptiondd7158a
line 1113why, one step back1113<p>A literal <em>Content-Type</em> works as well:</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1114why, one step back1114fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1115–1116why, one step back1115<pre><code> res.contentType('application/json');1116</code></pre>regenerated docsTj Holowaychukbefore adoptiondd7158a
lines 1117–1119why, one step back11171118<p>Or simply the extension without leading <code>.</code>:</p>1119fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1120–1121why, one step back1120<pre><code> res.contentType('json');1121</code></pre>regenerated docsTj Holowaychukbefore adoptiondd7158a
line 1122why, one step back1122fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1123why, one step back1123<h3 id="res.attachment()">res.attachment([filename])</h3>regenerated docsTj Holowaychukbefore adoptiondd7158a
line 1124why, one step back1124fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1125why, one step back1125<p>Sets the <em>Content-Disposition</em> response header to “attachment”, with optional <em>filename</em>.</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1126why, one step back1126fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1127–1128why, one step back1127<pre><code> res.attachment('path/to/my/image.png');1128</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1129why, one step back1129fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1130why, one step back1130<h3 id="res.sendfile()">res.sendfile(path[, options[, callback]])</h3>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1131why, one step back1131fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1132why, one step back1132<p>Used by <code>res.download()</code> to transfer an arbitrary file.</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1133why, one step back1133fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1134–1135why, one step back1134<pre><code>res.sendfile('path/to/my.file');1135</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1136why, one step back1136Merge branch 'docs'Tj Holowaychukbefore adoption64da262
lines 1137–1138why, one step back1137<p>This method accepts an optional callback which is called when1138an error occurs, or when the transfer is complete. By default failures call <code>next(err)</code>, however when a callback is supplied you must do this explicitly, or act on the error.</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1139why, one step back1139Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 1140why, one step back1140<pre><code>res.sendfile(path, function(err){Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1141why, one step back1141 if (err) {Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 1142why, one step back1142 next(err);Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1143–1145why, one step back1143 } else {1144 console.log('transferred %s', path);1145 }fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1146–1147why, one step back1146});1147</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1148why, one step back1148Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 1149why, one step back1149<p>Options may also be passed to the internal <em>fs.createReadStream()</em> call, for example altering the <em>bufferSize</em>:</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1150why, one step back1150Merge branch 'docs'Tj Holowaychukbefore adoption64da262
lines 1151–1152why, one step back1151<pre><code>res.sendfile(path, { bufferSize: 1024 }, function(err){1152 // handlefixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1153–1154why, one step back1153});1154</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1155why, one step back1155fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1156why, one step back1156<h3 id="res.download()">res.download(file[, filename[, callback]])</h3>Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 1157why, one step back1157Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1158–1160why, one step back1158<p>Transfer the given <em>file</em> as an attachment with optional alternative <em>filename</em>.</p>11591160<pre><code>res.download('path/to/image.png');fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1161–1162why, one step back1161res.download('path/to/image.png', 'foo.png');1162</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1163–1166why, one step back11631164<p>This is equivalent to:</p>11651166<pre><code>res.attachment(file);fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1167–1168why, one step back1167res.sendfile(file);1168</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1169why, one step back1169Merge branch 'docs'Tj Holowaychukbefore adoption64da262
lines 1170–1173why, one step back1170<p>An optional callback may be supplied as either the second or third argument, which is passed to <em>res.sendfile()</em>:</p>11711172<pre><code>res.download(path, 'expenses.doc', function(err){1173 // handlefixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1174–1175why, one step back1174});1175</code></pre>Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 1176why, one step back1176fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1177why, one step back1177<h3 id="res.send()">res.send(body|status[, headers|status[, status]])</h3>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1178why, one step back1178Merge branch 'docs'Tj Holowaychukbefore adoption64da262
lines 1179–1180why, one step back1179<p>The <em>res.send()</em> method is a high level response utility allowing you to pass1180objects to respond with json, strings for html, Buffer instances, or numbers representing the status code. The following are all valid uses:</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1181–1187why, one step back11811182<pre><code> res.send(); // 2041183 res.send(new Buffer('wahoo'));1184 res.send({ some: 'json' });1185 res.send('<p>some html</p>');1186 res.send('Sorry, cant find that', 404);1187 res.send('text', { 'Content-Type': 'text/plain' }, 201);fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1188–1189why, one step back1188 res.send(404);1189</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1190–1194why, one step back11901191<p>By default the <em>Content-Type</em> response header is set, however if explicitly1192assigned through <code>res.send()</code> or previously with <code>res.header()</code> or <code>res.contentType()</code>1193it will not be set again.</p>1194fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1195why, one step back1195<p>Note that this method <em>end()</em>s the response, so you will want to use node’s <em>res.write()</em> for multiple writes or streaming.</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1196why, one step back1196fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1197why, one step back1197<h3 id="res.redirect()">res.redirect(url[, status])</h3>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1198–1204why, one step back11981199<p>Redirect to the given <em>url</em> with a default response <em>status</em> of 302.</p>12001201<pre><code>res.redirect('/', 301);1202res.redirect('/account');1203res.redirect('http://google.com');1204res.redirect('home');fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1205–1206why, one step back1205res.redirect('back');1206</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1207why, one step back1207fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1208why, one step back1208<p>Express supports “redirect mapping”, which by default provides <em>home</em>, and <em>back</em>.Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1209why, one step back1209The <em>back</em> map checks the <em>Referrer</em> and <em>Referer</em> headers, while <em>home</em> utilizesfixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1210why, one step back1210the “home” setting and defaults to “/”.</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1211why, one step back1211fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1212why, one step back1212<h3 id="res.cookie()">res.cookie(name, val[, options])</h3>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1213why, one step back1213Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 1214why, one step back1214<p>Sets the given cookie <em>name</em> to <em>val</em>, with options <em>httpOnly</em>, <em>secure</em>, <em>expires</em> etc.</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1215–1216why, one step back12151216<pre><code>// "Remember me" for 15 minutes fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1217–1218why, one step back1217res.cookie('rememberme', 'yes', { expires: new Date(Date.now() + 900000), httpOnly: true });1218</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1219why, one step back1219docsTj Holowaychukbefore adoption131970b
lines 1220–1221why, one step back1220<p>The <em>maxAge</em> property may be used to set <em>expires</em> relative to <em>Date.now()</em> in milliseconds, so our example above can now become:</p>1221fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1222–1223why, one step back1222<pre><code>res.cookie('rememberme', 'yes', { maxAge: 900000 });1223</code></pre>docsTj Holowaychukbefore adoption131970b
line 1224why, one step back1224Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1225–1226why, one step back1225<p>To parse incoming <em>Cookie</em> headers, use the <em>cookieDecoder</em> middleware, which provides the <em>req.cookies</em> object:</p>1226Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 1227why, one step back1227<pre><code>app.use(express.cookieParser());Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1228–1229why, one step back12281229app.get('/', function(req, res){Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 1230why, one step back1230 // use req.cookies.remembermefixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1231–1232why, one step back1231});1232</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1233why, one step back1233fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1234why, one step back1234<h3 id="res.clearcookie()">res.clearCookie(name)</h3>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1235why, one step back1235fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1236why, one step back1236<p>Clear cookie <em>name</em> by setting “expires” far in the past.</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1237why, one step back1237fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1238–1239why, one step back1238<pre><code>res.clearCookie('rememberme');1239</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1240why, one step back1240fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1241why, one step back1241<h3 id="res.render()">res.render(view[, options[, fn]])</h3>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1242–1246why, one step back12421243<p>Render <em>view</em> with the given <em>options</em> and optional callback <em>fn</em>.1244When a callback function is given a response will <em>not</em> be made1245automatically, however otherwise a response of <em>200</em> and <em>text/html</em> is given.</p>1246fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1247why, one step back1247<p>The <em>options</em> passed are the local variables as well, for example if we want to expose “user” to the view, and prevent a local we do so within the same object:</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1248why, one step back1248Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 1249why, one step back1249<pre><code>var user = { name: 'tj' };fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1250–1251why, one step back1250res.render('index', { layout: false, user: user });1251</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1252why, one step back1252fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1253why, one step back1253<h3 id="res.partial()">res.partial(view[, options])</h3>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1254–1257why, one step back12541255<p>Render <em>view</em> partial with the given <em>options</em>. This method is always available1256to the view as a local variable.</p>1257fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1258–1260why, one step back1258<ul>1259<li><em>object</em> the object named by <em>as</em> or derived from the view name</li>1260<li><p><em>as</em> Variable name for each <em>collection</em> or <em>object</em> value, defaults to the view name.</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1261why, one step back1261fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1262–1272why, one step back1262<ul>1263<li>as: ‘something’ will add the <em>something</em> local variable</li>1264<li>as: this will use the collection value as the template context</li>1265<li>as: global will merge the collection value’s properties with <em>locals</em></li>1266</ul>1267</li>1268<li><p><em>collection</em> Array of objects, the name is derived from the view name itself.1269For example <em>video.html</em> will have a object <em>video</em> available to it.</p></li>1270</ul>12711272Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1273–1280why, one step back1273<p>The following are equivalent, and the name of collection value when passed1274to the partial will be <em>movie</em> as derived from the name.</p>12751276<pre><code>partial('theatre/movie.jade', { collection: movies });1277partial('theatre/movie.jade', movies);1278partial('movie.jade', { collection: movies });1279partial('movie.jade', movies);1280partial('movie', movies);fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1281–1282why, one step back1281// In view: movie.director1282</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1283why, one step back1283fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1284why, one step back1284<p>To change the local from <em>movie</em> to <em>video</em> we can use the “as” option:</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1285–1286why, one step back12851286<pre><code>partial('movie', { collection: movies, as: 'video' });fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1287–1288why, one step back1287// In view: video.director1288</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1289–1293why, one step back12891290<p>Also we can make our movie the value of <em>this</em> within our view so that instead1291of <em>movie.director</em> we could use <em>this.director</em>.</p>12921293<pre><code>partial('movie', { collection: movies, as: this });fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1294–1295why, one step back1294// In view: this.director1295</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1296why, one step back1296fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1297why, one step back1297<p>Another alternative is to “expand” the properties of the collection item intoUpdated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1298–1300why, one step back1298pseudo globals (local variables) by using <em>as: global</em>, which again is syntactic sugar:</p>12991300<pre><code>partial('movie', { collection: movies, as: global });fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1301–1302why, one step back1301// In view: director1302</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1303–1315why, one step back13031304<p>This same logic applies to a single partial object usage:</p>13051306<pre><code>partial('movie', { object: movie, as: this });1307// In view: this.director13081309partial('movie', { object: movie, as: global });1310// In view: director13111312partial('movie', { object: movie, as: 'video' });1313// In view: video.director13141315partial('movie', { object: movie });fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1316–1317why, one step back1316// In view: movie.director1317</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1318why, one step back1318fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1319why, one step back1319<p>When a non-collection (does <em>not</em> have <em>.length</em>) is passed as the second argument, it is assumed to be the <em>object</em>, after which the object’s local variable name is derived from the view name:</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1320why, one step back1320regenerated docsTj Holowaychukbefore adoptiondd7158a
lines 1321–1322why, one step back1321<pre><code>var movie = new Movie('Nightmare Before Christmas', 'Tim Burton')1322partial('movie', movie)fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1323–1324why, one step back1323// => In view: movie.director1324</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1325why, one step back1325fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1326why, one step back1326<p>The exception of this, is when a “plain” object, aka “{}” or “new Object” is passed, which is considered an object with local variable. For example some may expect a “movie” local with the following, however since it is a plain object “director” and “title” are simply locals:</p>regenerated docsTj Holowaychukbefore adoptiondd7158a
lines 1327–1328why, one step back13271328<pre><code>var movie = { title: 'Nightmare Before Christmas', director: 'Tim Burton' }; fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1329–1330why, one step back1329partial('movie', movie)1330</code></pre>regenerated docsTj Holowaychukbefore adoptiondd7158a
lines 1331–1335why, one step back13311332<p>For cases like this where passing a plain object is desired, simply assign it to a key, or use the <code>object</code> key which will use the filename-derived variable name. The examples below are equivalent:</p>13331334<pre><code> partial('movie', { locals: { movie: movie }})1335 partial('movie', { movie: movie })fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1336–1337why, one step back1336 partial('movie', { object: movie })1337</code></pre>regenerated docsTj Holowaychukbefore adoptiondd7158a
line 1338why, one step back1338Merge branch 'docs'Tj Holowaychukbefore adoption64da262
lines 1339–1351why, one step back1339<p>This exact API can be utilized from within a route, to respond with a fragment via Ajax or WebSockets, for example we can render a collection of users directly from a route:</p>13401341<pre><code>app.get('/users', function(req, res){1342 if (req.xhr) {1343 // respond with the each user in the collection1344 // passed to the "user" view1345 res.partial('user', users);1346 } else {1347 // respond with layout, and users page1348 // which internally does partial('user', users)1349 // along with other UI1350 res.render('users', { users: users });1351 }fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1352–1353why, one step back1352});1353</code></pre>Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 1354why, one step back1354fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1355why, one step back1355<h3 id="res.local()">res.local(name[, val])</h3>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1356–1369why, one step back13561357<p>Get or set the given local variable <em>name</em>. The locals built up for a response are applied to those given to the view rendering methods such as <code>res.render()</code>.</p>13581359<pre><code> app.all('/movie/:id', function(req, res, next){1360 Movie.get(req.params.id, function(err, movie){1361 // Assigns res.locals.movie = movie1362 res.local('movie', movie);1363 });1364 });13651366 app.get('/movie/:id', function(req, res){1367 // movie is already a local, however we1368 // can pass more if we wish1369 res.render('movie', { displayReviews: true });fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1370–1371why, one step back1370 });1371</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1372why, one step back1372fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1373why, one step back1373<h3 id="res.locals()">res.locals(obj)</h3>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1374why, one step back1374regenerated docsTj Holowaychukbefore adoptiondd7158a
lines 1375–1379why, one step back1375<p> Assign several locals with the given <em>obj</em>. The following are equivalent:</p>13761377<pre><code> res.local('foo', bar);1378 res.local('bar', baz);1379fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1380–1381why, one step back1380 res.locals({ foo: bar, bar, baz });1381</code></pre>regenerated docsTj Holowaychukbefore adoptiondd7158a
line 1382why, one step back1382fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1383why, one step back1383<h3 id="app.set()">app.set(name[, val])</h3>regenerated docsTj Holowaychukbefore adoptiondd7158a
line 1384why, one step back1384Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1385–1389why, one step back1385<p>Apply an application level setting <em>name</em> to <em>val</em>, or1386get the value of <em>name</em> when <em>val</em> is not present:</p>13871388<pre><code>app.set('views', __dirname + '/views');1389app.set('views');fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1390–1391why, one step back1390// => ...path...1391</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1392–1395why, one step back13921393<p>Alternatively you may simply access the settings via <em>app.settings</em>:</p>13941395<pre><code>app.settings.viewsfixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1396–1397why, one step back1396// => ...path...1397</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1398why, one step back1398fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1399why, one step back1399<h3 id="app.enable()">app.enable(name)</h3>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1400–1407why, one step back14001401<p>Enable the given setting <em>name</em>:</p>14021403<pre><code>app.enable('some arbitrary setting');1404app.set('some arbitrary setting');1405// => true14061407app.enabled('some arbitrary setting');fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1408–1409why, one step back1408// => true1409</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1410why, one step back1410fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1411why, one step back1411<h3 id="app.enabled()">app.enabled(name)</h3>Merge branch 'docs'Tj Holowaychukbefore adoption64da262
lines 1412–1419why, one step back14121413<p>Check if setting <em>name</em> is enabled:</p>14141415<pre><code>app.enabled('view cache');1416// => false14171418app.enable('view cache');1419app.enabled('view cache');fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1420–1421why, one step back1420// => true1421</code></pre>Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 1422why, one step back1422fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1423why, one step back1423<h3 id="app.disable()">app.disable(name)</h3>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1424–1431why, one step back14241425<p>Disable the given setting <em>name</em>:</p>14261427<pre><code>app.disable('some setting');1428app.set('some setting');1429// => false14301431app.disabled('some setting');fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1432–1433why, one step back1432// => false1433</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1434why, one step back1434fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1435why, one step back1435<h3 id="app.disabled()">app.disabled(name)</h3>Merge branch 'docs'Tj Holowaychukbefore adoption64da262
lines 1436–1445why, one step back14361437<p>Check if setting <em>name</em> is disabled:</p>14381439<pre><code>app.enable('view cache');14401441app.disabled('view cache');1442// => false14431444app.disable('view cache');1445app.disabled('view cache');fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1446–1447why, one step back1446// => true1447</code></pre>Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 1448why, one step back1448fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1449why, one step back1449<h3 id="app.configure()">app.configure(env|function[, function])</h3>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1450–1458why, one step back14501451<p>Define a callback function for the given <em>env</em> (or all environments) with callback <em>function</em>:</p>14521453<pre><code>app.configure(function(){1454 // executed for each env1455});14561457app.configure('development', function(){1458 // executed for 'development' onlyfixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1459–1460why, one step back1459});1460</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1461why, one step back1461fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1462why, one step back1462<h3 id="app.redirect()">app.redirect(name, val)</h3>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1463why, one step back1463Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 1464why, one step back1464<p>For use with <em>res.redirect()</em> we can map redirects at the application level as shown below:</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1465why, one step back1465fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1466–1467why, one step back1466<pre><code>app.redirect('google', 'http://google.com');1467</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1468–1470why, one step back14681469<p>Now in a route we may call:</p>1470fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1471why, one step back1471<p> res.redirect(‘google’);</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1472–1475why, one step back14721473<p>We may also map dynamic redirects:</p>14741475<pre><code>app.redirect('comments', function(req, res){Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 1476why, one step back1476 return '/post/' + req.params.id + '/comments';fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1477–1478why, one step back1477});1478</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1479–1484why, one step back14791480<p>So now we may do the following, and the redirect will dynamically adjust to1481the context of the request. If we called this route with <em>GET /post/12</em> our1482redirect <em>Location</em> would be <em>/post/12/comments</em>.</p>14831484<pre><code>app.get('/post/:id', function(req, res){Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 1485why, one step back1485 res.redirect('comments');fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1486–1487why, one step back1486});1487</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1488why, one step back1488Merge branch 'docs'Tj Holowaychukbefore adoption64da262
lines 1489–1490why, one step back1489<p>When mounted, <em>res.redirect()</em> will respect the mount-point. For example if a blog app is mounted at <em>/blog</em>, the following will redirect to <em>/blog/posts</em>:</p>1490fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1491–1492why, one step back1491<pre><code>res.redirect('/posts');1492</code></pre>Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 1493why, one step back1493fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1494why, one step back1494<h3 id="app.error()">app.error(function)</h3>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1495–1500why, one step back14951496<p>Adds an error handler <em>function</em> which will receive the exception as the first parameter as shown below.1497Note that we may set several error handlers by making several calls to this method, however the handler1498should call <em>next(err)</em> if it does not wish to deal with the exception:</p>14991500<pre><code>app.error(function(err, req, res, next){Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 1501why, one step back1501 res.send(err.message, 500);fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1502–1503why, one step back1502});1503</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1504why, one step back1504fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1505why, one step back1505<h3 id="app.helpers()">app.helpers(obj)</h3>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1506–1508why, one step back15061507<p>Registers static view helpers.</p>1508fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1509why, one step back1509<p> app.helpers({</p>Merge branch 'docs'Tj Holowaychukbefore adoption64da262
line 1510why, one step back1510fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1511–1517why, one step back1511<pre><code> name: function(first, last){ return first + ', ' + last }1512, firstName: 'tj'1513, lastName: 'holowaychuk'1514</code></pre>15151516<p> });</p>1517Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1518–1520why, one step back1518<p>Our view could now utilize the <em>firstName</em> and <em>lastName</em> variables,1519as well as the <em>name()</em> function exposed.</p>1520fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1521–1522why, one step back1521<pre><code><%= name(firstName, lastName) %>1522</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1523why, one step back1523fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1524why, one step back1524<h3 id="app.dynamichelpers()">app.dynamicHelpers(obj)</h3>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1525–1531why, one step back15251526<p>Registers dynamic view helpers. Dynamic view helpers1527are simply functions which accept <em>req</em>, <em>res</em>, and are1528evaluated against the <em>Server</em> instance before a view is rendered. The <em>return value</em> of this function1529becomes the local variable it is associated with.</p>15301531<pre><code>app.dynamicHelpers({Merge branch 'docs'Tj Holowaychukbefore adoption64da262
lines 1532–1534why, one step back1532 session: function(req, res){1533 return req.session;1534 }fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1535–1536why, one step back1535});1536</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1537–1539why, one step back15371538<p>All views would now have <em>session</em> available so that session data can be accessed via <em>session.name</em> etc:</p>1539fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1540–1541why, one step back1540<pre><code><%= session.name %>1541</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1542why, one step back1542fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1543why, one step back1543<h3 id="app.mounted()">app.mounted(fn)</h3>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1544–1550why, one step back15441545<p>Assign a callback <em>fn</em> which is called when this <em>Server</em> is passed to <em>Server#use()</em>.</p>15461547<pre><code>var app = express.createServer(),1548 blog = express.createServer();15491550blog.mounted(function(parent){Merge branch 'docs'Tj Holowaychukbefore adoption64da262
lines 1551–1552why, one step back1551 // parent is app1552 // "this" is blogUpdated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1553–1554why, one step back1553});1554fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1555–1556why, one step back1555app.use(blog);1556</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1557why, one step back1557fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1558why, one step back1558<h3 id="app.register()">app.register(ext, exports)</h3>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1559–1560why, one step back15591560<p>Register the given template engine <em>exports</em>fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1561why, one step back1561as <em>ext</em>. For example we may wish to map “.html”Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1562–1563why, one step back1562files to jade:</p>1563fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1564–1565why, one step back1564<pre><code> app.register('.html', require('jade'));1565</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1566–1568why, one step back15661567<p>This is also useful for libraries that may not1568match extensions correctly. For example my haml.jsfixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1569–1570why, one step back1569library is installed from npm as “hamljs” so instead1570of layout.hamljs, we can register the engine as “.haml”:</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1571why, one step back1571fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1572–1573why, one step back1572<pre><code> app.register('.haml', require('haml-js'));1573</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1574–1575why, one step back15741575<p>For engines that do not comply with the ExpressMerge branch 'docs'Tj Holowaychukbefore adoption64da262
lines 1576–1578why, one step back1576specification, we can also wrap their api this way. Below1577we map <em>.md</em> to render markdown files, rendering the html once1578since it will not change on subsequent calls, and support local substitutionfixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1579why, one step back1579in the form of “{name}”.</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1580why, one step back1580Merge branch 'docs'Tj Holowaychukbefore adoption64da262
lines 1581–1589why, one step back1581<pre><code> app.register('.md', {1582 compile: function(str, options){1583 var html = md.toHTML(str);1584 return function(locals){1585 return html.replace(/\{([^}]+)\}/g, function(_, name){1586 return locals[name];1587 });1588 };1589 }fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1590–1591why, one step back1590 });1591</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
line 1592why, one step back1592fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1593why, one step back1593<h3 id="app.listen()">app.listen([port[, host]])</h3>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1594–1595why, one step back15941595<p>Bind the app server to the given <em>port</em>, which defaults to 3000. When <em>host</em> is omitted allfixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1596why, one step back1596connections will be accepted via <em>INADDR_ANY</em>.</p>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1597–1599why, one step back15971598<pre><code>app.listen();1599app.listen(3000);fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1600–1601why, one step back1600app.listen(3000, 'n.n.n.n');1601</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1602–1604why, one step back16021603<p>The <em>port</em> argument may also be a string representing the path to a unix domain socket:</p>1604fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1605–1606why, one step back1605<pre><code>app.listen('/tmp/express.sock');1606</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1607–1616why, one step back16071608<p>Then try it out:</p>16091610<pre><code>$ telnet /tmp/express.sock1611GET / HTTP/1.116121613HTTP/1.1 200 OK1614Content-Type: text/plain1615Content-Length: 111616fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1617–1618why, one step back1617Hello World1618</code></pre>Updated jade submoduleTj Holowaychukbefore adoption242efc7
lines 1619–1622why, one step back1619 </div>1620 </div>1621 </body>1622</html>