api.html
This file
Path
docs/api.html
2,031 lines · 127.7 KiB · HTML
Last changed by
Added screencasts pageTj Holowaychukbefore adoption4144af3
History of this fileTake it
Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1–129why, one step back1<html>2 <head>3 <title>Express</title>4 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>5 <style>body {6 margin: 0;7 padding: 0;8 font: 14px/1.5 'Palatino Linotype', 'Book Antiqua', Palatino, FreeSerif, serif;9 color: #252519;10}11a {12 color: #252519;13}14a:hover {15 text-decoration: underline;16 color: #19469D;17}18p {19 margin: 12px 0;20}21h1, h2, h3 {22 margin: 0;23 padding: 0;24}25table#source {26 width: 100%;27 border-collapse: collapse;28}29table#source td:first-child {30 padding: 30px 40px 30px 40px;31 vertical-align: top;32}33table#source td:first-child,34table#source td:first-child pre {35 width: 450px;36}37table#source td:last-child {38 padding: 30px 0 30px 40px;39 border-left: 1px solid #E5E5EE;40 background: #F5F5FF;41}42table#source tr {43 border-bottom: 1px solid #E5E5EE;44}45table#source tr.filename {46 padding-top: 40px;47 border-top: 1px solid #E5E5EE;48}49table#source tr.filename td:first-child {50 text-transform: capitalize;51}52table#source tr.filename td:last-child {53 font-size: 12px;54}55table#source tr.filename h2 {56 margin: 0;57 padding: 0;58 cursor: pointer;59}60table#source tr.code h1,61table#source tr.code h2,62table#source tr.code h3 {63 margin-top: 30px;64 font-family: "Lucida Grande", "Helvetica Nueue", Arial, sans-serif;65 font-size: 18px;66}67table#source tr.code h2 {68 font-size: 16px;69}70table#source tr.code h3 {71 font-size: 14px;72}73table#source tr.code ul {74 margin: 15px 0 15px 35px;75 padding: 0;76}77table#source tr.code ul li {78 margin: 0;79 padding: 1px 0;80}81table#source tr.code ul li p {82 margin: 0;83 padding: 0;84}85table#source tr.code td:first-child pre {86 padding: 20px;87}88#ribbon {89 position: fixed;90 top: 0;91 right: 0;92}93code .string { color: #219161; }94code .regexp { color: #219161; }95code .keyword { color: #954121; }96code .number { color: #19469D; }97code .comment { color: #bbb; }98code .this { color: #19469D; }</style>99 <script>100 $(function(){101 $('tr.code').hide();102 $('tr.filename').toggle(function(){103 $(this).nextUntil('.filename').fadeIn();104 }, function(){105 $(this).nextUntil('.filename').fadeOut();106 });107 });108 </script>109 </head>110 <body>111<table id="source"><tbody><tr><td><h1>Express</h1><p>High performance web framework for <a href="http://nodejs.org">node</a>.</p></td><td></td></tr><tr class="filename"><td><h2 id="lib/express/index.js"><a href="#">index</a></h2></td><td>lib/express/index.js</td></tr><tr class="code">112<td class="docs">113<p>Re-export connect auto-loaders.</p>114115<p>This prevents the need to <code>require('connect')</code> in order116to access core middleware, so for example <code>express.logger()</code> instead117of <code>require('connect').logger()</code>.118 </p>119</td>120<td class="code">121<pre><code><span class="keyword">var</span> <span class="variable">exports</span> = <span class="variable">module</span>.<span class="variable">exports</span> = <span class="variable">require</span>(<span class="string">'connect'</span>).<span class="variable">middleware</span>;</code></pre>122</td>123</tr>124<tr class="code">125<td class="docs">126<p>Framework version.127 </p>128</td>129<td class="code">Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 130–138why, one step back130<pre><code><span class="variable">exports</span>.<span class="variable">version</span> = <span class="string">'2.0.0-pre'</span>;</code></pre>131</td>132</tr>133<tr class="code">134<td class="docs">135<p>Module dependencies.136 </p>137</td>138<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 139why, one step back139<pre><code><span class="keyword">var</span> <span class="class">Server</span> = <span class="variable">exports</span>.<span class="class">Server</span> = <span class="variable">require</span>(<span class="string">'./server'</span>);</code></pre>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 140–143why, one step back140</td>141</tr>142<tr class="code">143<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 144why, one step back144<p>Shortcut for <code>new Server(...)</code>.</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 145–147why, one step back145146<h2></h2>147Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 148why, one step back148<ul><li><p><strong>param</strong>: <em>Function</em> ...</p></li><li><p><strong>return</strong>: <em>Server</em> </p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 149–150why, one step back149</td>150<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 151–152why, one step back151<pre><code><span class="variable">exports</span>.<span class="variable">createServer</span> = <span class="keyword">function</span>(){152 <span class="keyword">return</span> <span class="keyword">new</span> <span class="class">Server</span>(<span class="class">Array</span>.<span class="variable">prototype</span>.<span class="variable">slice</span>.<span class="variable">call</span>(<span class="variable">arguments</span>));Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 153–157why, one step back153};</code></pre>154</td>155</tr>156<tr class="code">157<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 158why, one step back158<p>View extensions.Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 159–161why, one step back159 </p>160</td>161<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 162why, one step back162<pre><code><span class="variable">require</span>(<span class="string">'./view'</span>);</code></pre>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 163–166why, one step back163</td>164</tr>165<tr class="code">166<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 167why, one step back167<p>Response extensions.Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 168–170why, one step back168 </p>169</td>170<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 171why, one step back171<pre><code><span class="variable">require</span>(<span class="string">'./response'</span>);</code></pre>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 172–175why, one step back172</td>173</tr>174<tr class="code">175<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 176why, one step back176<p>Request extensions.Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 177–179why, one step back177 </p>178</td>179<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 180why, one step back180<pre><code><span class="variable">require</span>(<span class="string">'./request'</span>);Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 181–182why, one step back181</code></pre>182</td>Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 183why, one step back183</tr><tr class="filename"><td><h2 id="lib/express/request.js"><a href="#">request</a></h2></td><td>lib/express/request.js</td></tr><tr class="code">Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 184–188why, one step back184<td class="docs">185<p>Module dependencies.186 </p>187</td>188<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 189–190why, one step back189<pre><code><span class="keyword">var</span> <span class="variable">http</span> = <span class="variable">require</span>(<span class="string">'http'</span>)190 , <span class="variable">utils</span> = <span class="variable">require</span>(<span class="string">'./utils'</span>)Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 191–195why, one step back191 , <span class="variable">mime</span> = <span class="variable">require</span>(<span class="string">'connect'</span>).<span class="variable">utils</span>.<span class="variable">mime</span>;</code></pre>192</td>193</tr>194<tr class="code">195<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 196why, one step back196<p>Default flash formatters.</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 197–200why, one step back197198<ul><li><p><strong>type</strong>: <em>Object</em></p></li></ul>199</td>200<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 201–203why, one step back201<pre><code><span class="keyword">var</span> <span class="variable">flashFormatters</span> = <span class="variable">exports</span>.<span class="variable">flashFormatters</span> = {202 <span class="variable">s</span>: <span class="keyword">function</span>(<span class="variable">val</span>){203 <span class="keyword">return</span> <span class="class">String</span>(<span class="variable">val</span>);Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 204–209why, one step back204 }205};</code></pre>206</td>207</tr>208<tr class="code">209<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 210why, one step back210<p>Return request header or optional default.</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 211–213why, one step back211212<h2>Examples</h2>213Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 214why, one step back214<pre><code>req.header('Content-Type');Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 215–216why, one step back215// => "text/plain"216Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 217why, one step back217req.header('content-type');Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 218–219why, one step back218// => "text/plain"219Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 220–221why, one step back220req.header('Accept');221// => undefinedAdded screencasts pageTj Holowaychukbefore adoption4144af3
line 222why, one step back222Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 223–224why, one step back223req.header('Accept', 'text/html');224// => "text/html"</code></pre>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 225–227why, one step back225226<h2></h2>227Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 228why, one step back228<ul><li><p><strong>param</strong>: <em>String</em> name</p></li><li><p><strong>param</strong>: <em>String</em> defaultValue</p></li><li><p><strong>return</strong>: <em>String</em> </p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 229–230why, one step back229</td>230<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 231–232why, one step back231<pre><code><span class="variable">http</span>.<span class="class">IncomingMessage</span>.<span class="variable">prototype</span>.<span class="variable">header</span> = <span class="keyword">function</span>(<span class="variable">name</span>, <span class="variable">defaultValue</span>){232 <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">headers</span>[<span class="variable">name</span>.<span class="variable">toLowerCase</span>()] || <span class="variable">defaultValue</span>;Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 233–237why, one step back233};</code></pre>234</td>235</tr>236<tr class="code">237<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 238why, one step back238<p>Check if the <em>Accept</em> header is present, and includes the given <code>type</code>.</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
line 239why, one step back239Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 240–243why, one step back240<p>When the <em>Accept</em> header is not present <code>true</code> is returned. Otherwise241the given <code>type</code> is matched by an exact match, and then subtypes. You242may pass the subtype such as "html" which is then converted internally243to "text/html" using the mime lookup table.</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 244–246why, one step back244245<h2>Examples</h2>246Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 247why, one step back247<pre><code>// Accept: text/htmlAdded screencasts pageTj Holowaychukbefore adoption4144af3
lines 248–250why, one step back248req.accepts('html');249// => true250Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 251why, one step back251// Accept: text/*; application/jsonAdded screencasts pageTj Holowaychukbefore adoption4144af3
line 252why, one step back252req.accepts('html');Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 253–255why, one step back253req.accepts('text/html');254req.accepts('text/plain');255req.accepts('application/json');Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 256–257why, one step back256// => true257Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 258–260why, one step back258req.accepts('image/png');259req.accepts('png');260// => false</code></pre>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 261–266why, one step back261262<h2></h2>263264<ul><li><p><strong>param</strong>: <em>String</em> type</p></li><li><p><strong>return</strong>: <em>Boolean</em> </p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>265</td>266<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 267–268why, one step back267<pre><code><span class="variable">http</span>.<span class="class">IncomingMessage</span>.<span class="variable">prototype</span>.<span class="variable">accepts</span> = <span class="keyword">function</span>(<span class="variable">type</span>){268 <span class="keyword">var</span> <span class="variable">accept</span> = <span class="this">this</span>.<span class="variable">header</span>(<span class="string">'Accept'</span>);Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 269–301why, one step back269270 <span class="comment">// Normalize extensions ".json" -> "json"</span>271 <span class="keyword">if</span> (<span class="variable">type</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="string">'.'</span> == <span class="variable">type</span>[<span class="number integer">0</span>]) <span class="variable">type</span> = <span class="variable">type</span>.<span class="variable">substr</span>(<span class="number integer">1</span>);272273 <span class="comment">// When Accept does not exist, or is '*</span></code></pre>274</td>275</tr>276<tr class="code">277<td class="docs">278<p>' return true 279 if (!accept || '</p>280</td>281<td class="code">282<pre><code>*' == <span class="variable">accept</span>) {283 <span class="keyword">return</span> <span class="variable">true</span>;284 } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="variable">type</span>) {285 <span class="comment">// Allow "html" vs "text/html" etc</span>286 <span class="keyword">if</span> (<span class="variable">type</span>.<span class="variable">indexOf</span>(<span class="string">'/'</span>) &<span class="variable">lt</span>; <span class="number integer">0</span>) {287 <span class="variable">type</span> = <span class="variable">mime</span>.<span class="variable">types</span>[<span class="string">'.'</span> + <span class="variable">type</span>];288 }289290 <span class="comment">// Check if we have a direct match</span>291 <span class="keyword">if</span> (~<span class="variable">accept</span>.<span class="variable">indexOf</span>(<span class="variable">type</span>)) <span class="keyword">return</span> <span class="variable">true</span>;292293 <span class="comment">// Check if we have type</span></code></pre>294</td>295</tr>296<tr class="code">297<td class="docs">298<pre><code>type = type.split('/')[0] + '/*';299return accept.indexOf(type) >= 0;</code></pre>300301<p> } else {Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 302why, one step back302 return false;Added screencasts pageTj Holowaychukbefore adoption4144af3
line 303why, one step back303 }Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 304why, one step back304};</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
line 305why, one step back305Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 306–307why, one step back306<p>/**307Return the value of param <code>name</code> when present.</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
line 308why, one step back308Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 309why, one step back309<ul><li>Checks route placeholders, ex: <em>/user/:id</em></li><li>Checks query string params, ex: ?id=12</li><li>Checks urlencoded body params, ex: id=12</li></ul>Added screencasts pageTj Holowaychukbefore adoption4144af3
line 310why, one step back310Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 311–313why, one step back311<p>To utilize urlencoded request bodies, <code>req.body</code>312should be an object. This can be done by using313the <code>connect.bodyDecoder</code> middleware.</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 314–316why, one step back314315<h2></h2>316Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 317why, one step back317<ul><li><p><strong>param</strong>: <em>String</em> name</p></li><li><p><strong>return</strong>: <em>String</em> </p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 318–319why, one step back318</td>319<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 320–323why, one step back320<pre><code><span class="variable">http</span>.<span class="class">IncomingMessage</span>.<span class="variable">prototype</span>.<span class="variable">param</span> = <span class="keyword">function</span>(<span class="variable">name</span>){321 <span class="comment">// Route params like /user/:id</span>322 <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">params</span>[<span class="variable">name</span>] !== <span class="variable">undefined</span>) {323 <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">params</span>[<span class="variable">name</span>]; Added screencasts pageTj Holowaychukbefore adoption4144af3
line 324why, one step back324 }Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 325–327why, one step back325 <span class="comment">// Query string params</span>326 <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">query</span>[<span class="variable">name</span>] !== <span class="variable">undefined</span>) {327 <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">query</span>[<span class="variable">name</span>]; Added screencasts pageTj Holowaychukbefore adoption4144af3
line 328why, one step back328 }Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 329–331why, one step back329 <span class="comment">// Request body params via connect.bodyDecoder</span>330 <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">body</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="this">this</span>.<span class="variable">body</span>[<span class="variable">name</span>] !== <span class="variable">undefined</span>) {331 <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">body</span>[<span class="variable">name</span>];Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 332–337why, one step back332 }333};</code></pre>334</td>335</tr>336<tr class="code">337<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 338why, one step back338<p>Queue flash <code>msg</code> of the given <code>type</code>.</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 339–341why, one step back339340<h2>Examples</h2>341Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 342–345why, one step back342<pre><code> req.flash('info', 'email sent');343 req.flash('error', 'email delivery failed');344 req.flash('info', 'email re-sent');345 // => 2Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 346–347why, one step back346347 req.flash('info');Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 348why, one step back348 // => ['email sent', 'email re-sent']Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 349–350why, one step back349350 req.flash('info');Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 351why, one step back351 // => []Added screencasts pageTj Holowaychukbefore adoption4144af3
line 352why, one step back352Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 353–354why, one step back353 req.flash();354 // => { error: ['email delivery failed'], info: [] }</code></pre>Added screencasts pageTj Holowaychukbefore adoption4144af3
line 355why, one step back355Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 356why, one step back356<h2>Formatting</h2>Added screencasts pageTj Holowaychukbefore adoption4144af3
line 357why, one step back357Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 358–360why, one step back358<p>Flash notifications also support arbitrary formatting support.359For example you may pass variable arguments to <code>req.flash()</code>360and use the %s specifier to be replaced by the associated argument:</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
line 361why, one step back361Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 362why, one step back362<pre><code>req.flash('info', 'email has been sent to %s.', userName);</code></pre>Added screencasts pageTj Holowaychukbefore adoption4144af3
line 363why, one step back363Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 364why, one step back364<p>To add custom formatters use the <code>exports.flashFormatters</code> object.</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 365–367why, one step back365366<h2></h2>367Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 368why, one step back368<ul><li><p><strong>param</strong>: <em>String</em> type</p></li><li><p><strong>param</strong>: <em>String</em> msg</p></li><li><p><strong>return</strong>: <em>Array | Object | Number</em> </p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 369–370why, one step back369</td>370<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 371–381why, one step back371<pre><code><span class="variable">http</span>.<span class="class">IncomingMessage</span>.<span class="variable">prototype</span>.<span class="variable">flash</span> = <span class="keyword">function</span>(<span class="variable">type</span>, <span class="variable">msg</span>){372 <span class="keyword">var</span> <span class="variable">msgs</span> = <span class="this">this</span>.<span class="variable">session</span>.<span class="variable">flash</span> = <span class="this">this</span>.<span class="variable">session</span>.<span class="variable">flash</span> || {};373 <span class="keyword">if</span> (<span class="variable">type</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">msg</span>) {374 <span class="keyword">var</span> <span class="variable">i</span> = <span class="number integer">2</span>375 , <span class="variable">args</span> = <span class="variable">arguments</span>376 , <span class="variable">formatters</span> = <span class="this">this</span>.<span class="variable">app</span>.<span class="variable">flashFormatters</span> || {};377 <span class="variable">formatters</span>.<span class="variable">__proto__</span> = <span class="variable">flashFormatters</span>;378 <span class="variable">msg</span> = <span class="variable">utils</span>.<span class="variable">miniMarkdown</span>(<span class="variable">utils</span>.<span class="variable">htmlEscape</span>(<span class="variable">msg</span>));379 <span class="variable">msg</span> = <span class="variable">msg</span>.<span class="variable">replace</span>(<span class="regexp">/%([a-zA-Z])/g</span>, <span class="keyword">function</span>(<span class="variable">_</span>, <span class="variable">format</span>){380 <span class="keyword">var</span> <span class="variable">formatter</span> = <span class="variable">formatters</span>[<span class="variable">format</span>];381 <span class="keyword">if</span> (<span class="variable">formatter</span>) <span class="keyword">return</span> <span class="variable">formatter</span>(<span class="variable">args</span>[<span class="variable">i</span>++]);Added screencasts pageTj Holowaychukbefore adoption4144af3
line 382why, one step back382 });Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 383why, one step back383 <span class="keyword">return</span> (<span class="variable">msgs</span>[<span class="variable">type</span>] = <span class="variable">msgs</span>[<span class="variable">type</span>] || []).<span class="variable">push</span>(<span class="variable">msg</span>);Added screencasts pageTj Holowaychukbefore adoption4144af3
line 384why, one step back384 } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="variable">type</span>) {Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 385–387why, one step back385 <span class="keyword">var</span> <span class="variable">arr</span> = <span class="variable">msgs</span>[<span class="variable">type</span>];386 <span class="keyword">delete</span> <span class="variable">msgs</span>[<span class="variable">type</span>];387 <span class="keyword">return</span> <span class="variable">arr</span> || [];Added screencasts pageTj Holowaychukbefore adoption4144af3
line 388why, one step back388 } <span class="keyword">else</span> {Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 389–390why, one step back389 <span class="this">this</span>.<span class="variable">session</span>.<span class="variable">flash</span> = {};390 <span class="keyword">return</span> <span class="variable">msgs</span>;Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 391–396why, one step back391 }392};</code></pre>393</td>394</tr>395<tr class="code">396<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 397–398why, one step back397<p>Check if the incoming request contains the "Content-Type" 398header field, and it contains the give mime <code>type</code>.</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 399–401why, one step back399400<h2>Examples</h2>401Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 402why, one step back402<pre><code> // With Content-Type: text/html; charset=utf-8Added screencasts pageTj Holowaychukbefore adoption4144af3
line 403why, one step back403 req.is('html');Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 404why, one step back404 req.is('text/html');Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 405–406why, one step back405 // => true406Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 407–409why, one step back407 // When Content-Type is application/json408 req.is('json');409 req.is('application/json');Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 410–412why, one step back410 // => true411412 req.is('html');Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 413why, one step back413 // => false</code></pre>Added screencasts pageTj Holowaychukbefore adoption4144af3
line 414why, one step back414Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 415–418why, one step back415<p>Ad-hoc callbacks can also be registered with Express, to perform416assertions again the request, for example if we need an expressive417way to check if our incoming request is an image, we can register "an image"418callback:</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
line 419why, one step back419Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 420–422why, one step back420<pre><code> app.is('an image', function(req){421 return 0 == req.headers['content-type'].indexOf('image');422 });</code></pre>Added screencasts pageTj Holowaychukbefore adoption4144af3
line 423why, one step back423Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 424–425why, one step back424<p> Now within our route callbacks, we can use to to assert content types425 such as "image/jpeg", "image/png", etc.</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
line 426why, one step back426Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 427–432why, one step back427<pre><code> app.post('/image/upload', function(req, res, next){428 if (req.is('an image')) {429 // do something430 } else {431 next();432 }Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 433–439why, one step back433 });</code></pre>434435<h2></h2>436437<ul><li><p><strong>param</strong>: <em>String</em> type</p></li><li><p><strong>return</strong>: <em>Boolean</em> </p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>438</td>439<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 440–452why, one step back440<pre><code><span class="variable">http</span>.<span class="class">IncomingMessage</span>.<span class="variable">prototype</span>.<span class="variable">is</span> = <span class="keyword">function</span>(<span class="variable">type</span>){441 <span class="keyword">var</span> <span class="variable">fn</span> = <span class="this">this</span>.<span class="variable">app</span>.<span class="variable">is</span>(<span class="variable">type</span>);442 <span class="keyword">if</span> (<span class="variable">fn</span>) <span class="keyword">return</span> <span class="variable">fn</span>(<span class="this">this</span>);443 <span class="keyword">var</span> <span class="variable">contentType</span> = <span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'content-type'</span>];444 <span class="keyword">if</span> (!<span class="variable">contentType</span>) <span class="keyword">return</span>;445 <span class="keyword">if</span> (!~<span class="variable">type</span>.<span class="variable">indexOf</span>(<span class="string">'/'</span>)) <span class="variable">type</span> = <span class="variable">mime</span>.<span class="variable">type</span>(<span class="string">'.'</span> + <span class="variable">type</span>);446 <span class="comment">// TODO: remove when connect no longer appends charset...</span>447 <span class="keyword">if</span> (~<span class="variable">type</span>.<span class="variable">indexOf</span>(<span class="string">';'</span>)) <span class="variable">type</span> = <span class="variable">type</span>.<span class="variable">split</span>(<span class="string">';'</span>)[<span class="number integer">0</span>];448 <span class="keyword">if</span> (~<span class="variable">type</span>.<span class="variable">indexOf</span>(<span class="string">'*'</span>)) {449 <span class="variable">type</span> = <span class="variable">type</span>.<span class="variable">split</span>(<span class="string">'/'</span>)450 <span class="variable">contentType</span> = <span class="variable">contentType</span>.<span class="variable">split</span>(<span class="string">'/'</span>);451 <span class="keyword">if</span> (<span class="string">'*'</span> == <span class="variable">type</span>[<span class="number integer">0</span>] &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">type</span>[<span class="number integer">1</span>] == <span class="variable">contentType</span>[<span class="number integer">1</span>]) <span class="keyword">return</span> <span class="variable">true</span>;452 <span class="keyword">if</span> (<span class="string">'*'</span> == <span class="variable">type</span>[<span class="number integer">1</span>] &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">type</span>[<span class="number integer">0</span>] == <span class="variable">contentType</span>[<span class="number integer">0</span>]) <span class="keyword">return</span> <span class="variable">true</span>;Added screencasts pageTj Holowaychukbefore adoption4144af3
line 453why, one step back453 }Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 454why, one step back454 <span class="keyword">return</span> ~<span class="variable">contentType</span>.<span class="variable">indexOf</span>(<span class="variable">type</span>);Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 455–456why, one step back455};456Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 457why, one step back457<span class="comment">// Callback for isXMLHttpRequest / xhr</span>Added screencasts pageTj Holowaychukbefore adoption4144af3
line 458why, one step back458Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 459–460why, one step back459<span class="keyword">function</span> <span class="variable">isxhr</span>() {460 <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">header</span>(<span class="string">'X-Requested-With'</span>, <span class="string">''</span>).<span class="variable">toLowerCase</span>() === <span class="string">'xmlhttprequest'</span>;Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 461–465why, one step back461}</code></pre>462</td>463</tr>464<tr class="code">465<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 466why, one step back466<p>Check if the request was an <em>XMLHttpRequest</em>.</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 467–472why, one step back467468<h2></h2>469470<ul><li><p><strong>return</strong>: <em>Boolean</em> </p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>471</td>472<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 473–474why, one step back473<pre><code><span class="variable">http</span>.<span class="class">IncomingMessage</span>.<span class="variable">prototype</span>.<span class="variable">__defineGetter__</span>(<span class="string">'isXMLHttpRequest'</span>, <span class="variable">isxhr</span>);474<span class="variable">http</span>.<span class="class">IncomingMessage</span>.<span class="variable">prototype</span>.<span class="variable">__defineGetter__</span>(<span class="string">'xhr'</span>, <span class="variable">isxhr</span>);Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 475–476why, one step back475</code></pre>476</td>Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 477why, one step back477</tr><tr class="filename"><td><h2 id="lib/express/server.js"><a href="#">server</a></h2></td><td>lib/express/server.js</td></tr><tr class="code">Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 478–482why, one step back478<td class="docs">479<p>Module dependencies.480 </p>481</td>482<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 483–487why, one step back483<pre><code><span class="keyword">var</span> <span class="variable">url</span> = <span class="variable">require</span>(<span class="string">'url'</span>)484 , <span class="variable">view</span> = <span class="variable">require</span>(<span class="string">'./view'</span>)485 , <span class="variable">connect</span> = <span class="variable">require</span>(<span class="string">'connect'</span>)486 , <span class="variable">utils</span> = <span class="variable">connect</span>.<span class="variable">utils</span>487 , <span class="variable">queryString</span> = <span class="variable">require</span>(<span class="string">'querystring'</span>)Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 488–492why, one step back488 , <span class="variable">router</span> = <span class="variable">require</span>(<span class="string">'connect'</span>).<span class="variable">router</span>;</code></pre>489</td>490</tr>491<tr class="code">492<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 493why, one step back493<p>Initialize a new <code>Server</code> with optional <code>middleware</code>.</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 494–496why, one step back494495<h2></h2>496Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 497why, one step back497<ul><li><p><strong>param</strong>: <em>Array</em> middleware</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 498–499why, one step back498</td>499<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 500why, one step back500<pre><code><span class="keyword">var</span> <span class="class">Server</span> = <span class="variable">exports</span> = <span class="variable">module</span>.<span class="variable">exports</span> = <span class="keyword">function</span> <span class="class">Server</span>(<span class="variable">middleware</span>){Added screencasts pageTj Holowaychukbefore adoption4144af3
line 501why, one step back501 <span class="keyword">var</span> <span class="variable">self</span> = <span class="this">this</span>;Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 502–509why, one step back502 <span class="this">this</span>.<span class="variable">config</span> = {};503 <span class="this">this</span>.<span class="variable">settings</span> = {};504 <span class="this">this</span>.<span class="variable">redirects</span> = {};505 <span class="this">this</span>.<span class="variable">isCallbacks</span> = {};506 <span class="this">this</span>.<span class="variable">viewHelpers</span> = {};507 <span class="this">this</span>.<span class="variable">dynamicViewHelpers</span> = {};508 <span class="this">this</span>.<span class="variable">errorHandlers</span> = [];509 <span class="variable">connect</span>.<span class="class">Server</span>.<span class="variable">call</span>(<span class="this">this</span>, <span class="variable">middleware</span> || []);Added screencasts pageTj Holowaychukbefore adoption4144af3
line 510why, one step back510Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 511–512why, one step back511 <span class="comment">// Default "home" to / </span>512 <span class="this">this</span>.<span class="variable">set</span>(<span class="string">'home'</span>, <span class="string">'/'</span>);Added screencasts pageTj Holowaychukbefore adoption4144af3
line 513why, one step back513Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 514–515why, one step back514 <span class="comment">// TODO: remove when Connect removes "Server" ...</span>515 <span class="this">this</span>.<span class="variable">showVersion</span> = <span class="variable">false</span>;Added screencasts pageTj Holowaychukbefore adoption4144af3
line 516why, one step back516Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 517–518why, one step back517 <span class="comment">// Set "env" to NODE_ENV, defaulting to "development"</span>518 <span class="this">this</span>.<span class="variable">set</span>(<span class="string">'env'</span>, <span class="variable">process</span>.<span class="variable">env</span>.<span class="class">NODE_ENV</span> || <span class="string">'development'</span>);Added screencasts pageTj Holowaychukbefore adoption4144af3
line 519why, one step back519Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 520–531why, one step back520 <span class="comment">// Expose objects to each other</span>521 <span class="this">this</span>.<span class="variable">use</span>(<span class="keyword">function</span>(<span class="variable">req</span>, <span class="variable">res</span>, <span class="variable">next</span>){522 <span class="variable">req</span>.<span class="variable">query</span> = {};523 <span class="variable">res</span>.<span class="variable">headers</span> = { <span class="string">'X-Powered-By'</span>: <span class="string">'Express'</span> };524 <span class="variable">req</span>.<span class="variable">app</span> = <span class="variable">res</span>.<span class="variable">app</span> = <span class="variable">self</span>;525 <span class="variable">req</span>.<span class="variable">res</span> = <span class="variable">res</span>;526 <span class="variable">res</span>.<span class="variable">req</span> = <span class="variable">req</span>;527 <span class="variable">req</span>.<span class="variable">next</span> = <span class="variable">next</span>;528 <span class="comment">// Assign req.params.get</span>529 <span class="keyword">if</span> (<span class="variable">req</span>.<span class="variable">url</span>.<span class="variable">indexOf</span>(<span class="string">'?'</span>) &<span class="variable">gt</span>; <span class="number integer">0</span>) {530 <span class="keyword">var</span> <span class="variable">query</span> = <span class="variable">url</span>.<span class="variable">parse</span>(<span class="variable">req</span>.<span class="variable">url</span>).<span class="variable">query</span>;531 <span class="variable">req</span>.<span class="variable">query</span> = <span class="variable">exports</span>.<span class="variable">parseQueryString</span>(<span class="variable">query</span>);Added screencasts pageTj Holowaychukbefore adoption4144af3
line 532why, one step back532 }Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 533why, one step back533 <span class="variable">next</span>();Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 534–535why, one step back534 });535Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 536–540why, one step back536 <span class="comment">// Use router, expose as app.get(), etc</span>537 <span class="keyword">var</span> <span class="variable">fn</span> = <span class="variable">router</span>(<span class="keyword">function</span>(<span class="variable">app</span>){ <span class="variable">self</span>.<span class="variable">routes</span> = <span class="variable">app</span>; });538 <span class="this">this</span>.<span class="variable">__defineGetter__</span>(<span class="string">'router'</span>, <span class="keyword">function</span>(){539 <span class="this">this</span>.<span class="variable">__usedRouter</span> = <span class="variable">true</span>;540 <span class="keyword">return</span> <span class="variable">fn</span>;Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 541–551why, one step back541 });542543 <span class="comment">// Default production configuration</span>544 <span class="this">this</span>.<span class="variable">configure</span>(<span class="string">'production'</span>, <span class="keyword">function</span>(){545 <span class="this">this</span>.<span class="variable">enable</span>(<span class="string">'cache views'</span>);546 });547};</code></pre>548</td>549</tr>550<tr class="code">551<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 552why, one step back552<p>Inherit from <code>connect.Server</code>.Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 553–555why, one step back553 </p>554</td>555<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 556why, one step back556<pre><code><span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">__proto__</span> = <span class="variable">connect</span>.<span class="class">Server</span>.<span class="variable">prototype</span>;</code></pre>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 557–560why, one step back557</td>558</tr>559<tr class="code">560<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 561why, one step back561<p>Support swappable querystring parsers.Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 562–564why, one step back562 </p>563</td>564<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 565why, one step back565<pre><code><span class="variable">exports</span>.<span class="variable">parseQueryString</span> = <span class="variable">queryString</span>.<span class="variable">parse</span>;</code></pre>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 566–573why, one step back566</td>567</tr>568<tr class="code">569<td class="docs">570<p>Proxy in order to register error handlers.571 </p>572</td>573<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 574why, one step back574<pre><code><span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">listen</span> = <span class="keyword">function</span>(){Added screencasts pageTj Holowaychukbefore adoption4144af3
line 575why, one step back575 <span class="this">this</span>.<span class="variable">registerErrorHandlers</span>();Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 576why, one step back576 <span class="variable">connect</span>.<span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">listen</span>.<span class="variable">apply</span>(<span class="this">this</span>, <span class="variable">arguments</span>);Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 577–585why, one step back577};</code></pre>578</td>579</tr>580<tr class="code">581<td class="docs">582<p>Proxy in order to register error handlers.583 </p>584</td>585<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 586why, one step back586<pre><code><span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">listenFD</span> = <span class="keyword">function</span>(){Added screencasts pageTj Holowaychukbefore adoption4144af3
line 587why, one step back587 <span class="this">this</span>.<span class="variable">registerErrorHandlers</span>();Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 588why, one step back588 <span class="variable">connect</span>.<span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">listenFD</span>.<span class="variable">apply</span>(<span class="this">this</span>, <span class="variable">arguments</span>);Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 589–593why, one step back589};</code></pre>590</td>591</tr>592<tr class="code">593<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 594–595why, one step back594<p>Register error handlers. This is automatically595called from within <code>Server#listen()</code> and <code>Server#listenFD()</code>.</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 596–601why, one step back596597<h2></h2>598599<ul><li><p><strong>return</strong>: <em>Server</em> for chaining</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>600</td>601<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 602–605why, one step back602<pre><code><span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">registerErrorHandlers</span> = <span class="keyword">function</span>(){603 <span class="this">this</span>.<span class="variable">errorHandlers</span>.<span class="variable">forEach</span>(<span class="keyword">function</span>(<span class="variable">fn</span>){604 <span class="this">this</span>.<span class="variable">use</span>(<span class="keyword">function</span>(<span class="variable">err</span>, <span class="variable">req</span>, <span class="variable">res</span>, <span class="variable">next</span>){605 <span class="variable">fn</span>.<span class="variable">apply</span>(<span class="this">this</span>, <span class="variable">arguments</span>);Added screencasts pageTj Holowaychukbefore adoption4144af3
line 606why, one step back606 });Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 607why, one step back607 }, <span class="this">this</span>);Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 608–613why, one step back608 <span class="keyword">return</span> <span class="this">this</span>;609};</code></pre>610</td>611</tr>612<tr class="code">613<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 614–615why, one step back614<p>Proxy <code>connect.Server#use()</code> to apply settings to615mounted applications.</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 616–618why, one step back616617<h2></h2>618Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 619why, one step back619<ul><li><p><strong>param</strong>: <em>String | Function | Server</em> route</p></li><li><p><strong>param</strong>: <em>Function | Server</em> middleware</p></li><li><p><strong>return</strong>: <em>Server</em> for chaining</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 620–621why, one step back620</td>621<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 622–624why, one step back622<pre><code><span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">use</span> = <span class="keyword">function</span>(<span class="variable">route</span>, <span class="variable">middleware</span>){623 <span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">route</span> !== <span class="string">'string'</span>) {624 <span class="variable">middleware</span> = <span class="variable">route</span>, <span class="variable">route</span> = <span class="string">'/'</span>;Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 625–626why, one step back625 }626Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 627why, one step back627 <span class="variable">connect</span>.<span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">use</span>.<span class="variable">call</span>(<span class="this">this</span>, <span class="variable">route</span>, <span class="variable">middleware</span>);Added screencasts pageTj Holowaychukbefore adoption4144af3
line 628why, one step back628Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 629–638why, one step back629 <span class="comment">// Mounted an app</span>630 <span class="keyword">if</span> (<span class="variable">middleware</span> <span class="variable">instanceof</span> <span class="class">Server</span>) {631 <span class="comment">// Home is /:route/:home</span>632 <span class="keyword">var</span> <span class="variable">app</span> = <span class="variable">middleware</span>633 , <span class="variable">home</span> = <span class="variable">app</span>.<span class="variable">set</span>(<span class="string">'home'</span>);634 <span class="keyword">if</span> (<span class="variable">home</span> === <span class="string">'/'</span>) <span class="variable">home</span> = <span class="string">''</span>;635 <span class="variable">app</span>.<span class="variable">set</span>(<span class="string">'home'</span>, (<span class="variable">app</span>.<span class="variable">route</span> || <span class="string">''</span>) + <span class="variable">home</span>);636 <span class="variable">app</span>.<span class="variable">parent</span> = <span class="this">this</span>;637 <span class="comment">// Mounted hook</span>638 <span class="keyword">if</span> (<span class="variable">app</span>.<span class="variable">__mounted</span>) <span class="variable">app</span>.<span class="variable">__mounted</span>.<span class="variable">call</span>(<span class="variable">app</span>, <span class="this">this</span>);Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 639–646why, one step back639 }640641 <span class="keyword">return</span> <span class="this">this</span>;642};</code></pre>643</td>644</tr>645<tr class="code">646<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 647–648why, one step back647<p>Assign a callback <code>fn</code> which is called648when this <code>Server</code> is passed to <code>Server#use()</code>.</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 649–651why, one step back649650<h2>Examples</h2>651Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 652–653why, one step back652<p> var app = express.createServer(),653 blog = express.createServer();</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
line 654why, one step back654Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 655–657why, one step back655<p> blog.mounted(function(parent){656 // parent is app657 // "this" is blogAdded screencasts pageTj Holowaychukbefore adoption4144af3
lines 658–659why, one step back658 });</p>659Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 660why, one step back660<p> app.use(blog);</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 661–666why, one step back661662<h2></h2>663664<ul><li><p><strong>param</strong>: <em>Function</em> fn</p></li><li><p><strong>return</strong>: <em>Server</em> for chaining</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>665</td>666<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 667–668why, one step back667<pre><code><span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">mounted</span> = <span class="keyword">function</span>(<span class="variable">fn</span>){668 <span class="this">this</span>.<span class="variable">__mounted</span> = <span class="variable">fn</span>;Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 669–674why, one step back669 <span class="keyword">return</span> <span class="this">this</span>;670};</code></pre>671</td>672</tr>673<tr class="code">674<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 675why, one step back675<h2>See view.register.</h2>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 676–681why, one step back676677<h2></h2>678679<ul><li><p><strong>return</strong>: <em>Server</em> for chaining</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>680</td>681<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 682–683why, one step back682<pre><code><span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">register</span> = <span class="keyword">function</span>(){683 <span class="variable">view</span>.<span class="variable">register</span>.<span class="variable">apply</span>(<span class="this">this</span>, <span class="variable">arguments</span>);Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 684–689why, one step back684 <span class="keyword">return</span> <span class="this">this</span>;685};</code></pre>686</td>687</tr>688<tr class="code">689<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 690why, one step back690<p>Register the given view helpers <code>obj</code>. This methodAdded screencasts pageTj Holowaychukbefore adoption4144af3
lines 691–699why, one step back691can be called several times to apply additional helpers.</p>692693<h2></h2>694695<ul><li><p><strong>param</strong>: <em>Object</em> obj</p></li><li><p><strong>return</strong>: <em>Server</em> for chaining</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>696</td>697<td class="code">698<pre><code><span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">helpers</span> = 699<span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">locals</span> = <span class="keyword">function</span>(<span class="variable">obj</span>){Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 700why, one step back700 <span class="variable">utils</span>.<span class="variable">merge</span>(<span class="this">this</span>.<span class="variable">viewHelpers</span>, <span class="variable">obj</span>);Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 701–706why, one step back701 <span class="keyword">return</span> <span class="this">this</span>;702};</code></pre>703</td>704</tr>705<tr class="code">706<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 707why, one step back707<p>Register the given dynamic view helpers <code>obj</code>. This methodAdded screencasts pageTj Holowaychukbefore adoption4144af3
lines 708–714why, one step back708can be called several times to apply additional helpers.</p>709710<h2></h2>711712<ul><li><p><strong>param</strong>: <em>Object</em> obj</p></li><li><p><strong>return</strong>: <em>Server</em> for chaining</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>713</td>714<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 715–716why, one step back715<pre><code><span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">dynamicHelpers</span> = <span class="keyword">function</span>(<span class="variable">obj</span>){716 <span class="variable">utils</span>.<span class="variable">merge</span>(<span class="this">this</span>.<span class="variable">dynamicViewHelpers</span>, <span class="variable">obj</span>);Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 717–778why, one step back717 <span class="keyword">return</span> <span class="this">this</span>;718};</code></pre>719</td>720</tr>721<tr class="code">722<td class="docs">723<p>Map the given param placeholder <code>name</code>(s) to the given callback <code>fn</code>.</p>724725<p>Param mapping is used to provide pre-conditions to routes726which us normalized placeholders. For example ":user_id" may727attempt to load the user from the database, where as ":num" may 728pass the value through <code>parseInt(num, 10)</code>.</p>729730<p>When the callback function accepts only a single argument, the731value of placeholder is passed:</p>732733<p> app.param('page', function(n){ return parseInt(n, 10); });</p>734735<p>After which "/users/:page" would automatically provide us with736an integer for <code>req.params.page</code>. If desired we could use the callback 737signature shown below, and immediately <code>next(new Error('invalid page'))</code>738when <code>parseInt</code> fails.</p>739740<p>Alternatively the callback may accept the request, response, next, and741the value, acting like middlware:</p>742743<p> app.param('userId', function(req, res, next, id){744 User.find(id, function(err, user){745 if (err) {746 next(err);747 } else if (user) {748 req.user = user;749 next();750 } else {751 next(new Error('failed to load user'));752 }753 });754 });</p>755756<p>Now every time ":userId" is present, the associated user object757will be loaded and assigned before the route handler is invoked.</p>758759<h2></h2>760761<ul><li><p><strong>param</strong>: <em>String | Array</em> name</p></li><li><p><strong>param</strong>: <em>Function</em> fn</p></li><li><p><strong>return</strong>: <em>Server</em> for chaining</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>762</td>763<td class="code">764<pre><code><span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">param</span> = <span class="keyword">function</span>(<span class="variable">name</span>, <span class="variable">fn</span>){765 <span class="keyword">if</span> (<span class="class">Array</span>.<span class="variable">isArray</span>(<span class="variable">name</span>)) {766 <span class="variable">name</span>.<span class="variable">forEach</span>(<span class="keyword">function</span>(<span class="variable">name</span>){767 <span class="this">this</span>.<span class="variable">param</span>(<span class="variable">name</span>, <span class="variable">fn</span>);768 }, <span class="this">this</span>);769 } <span class="keyword">else</span> {770 <span class="keyword">if</span> (<span class="string">':'</span> == <span class="variable">name</span>[<span class="number integer">0</span>]) <span class="variable">name</span> = <span class="variable">name</span>.<span class="variable">substr</span>(<span class="number integer">1</span>);771 <span class="this">this</span>.<span class="variable">routes</span>.<span class="variable">param</span>(<span class="variable">name</span>, <span class="variable">fn</span>);772 }773 <span class="keyword">return</span> <span class="this">this</span>;774};</code></pre>775</td>776</tr>777<tr class="code">778<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 779–780why, one step back779<p>Assign a custom exception handler callback <code>fn</code>.780These handlers are always <em>last</em> in the middleware stack.</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 781–786why, one step back781782<h2></h2>783784<ul><li><p><strong>param</strong>: <em>Function</em> fn</p></li><li><p><strong>return</strong>: <em>Server</em> for chaining</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>785</td>786<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 787–788why, one step back787<pre><code><span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">error</span> = <span class="keyword">function</span>(<span class="variable">fn</span>){788 <span class="this">this</span>.<span class="variable">errorHandlers</span>.<span class="variable">push</span>(<span class="variable">fn</span>);Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 789–794why, one step back789 <span class="keyword">return</span> <span class="this">this</span>;790};</code></pre>791</td>792</tr>793<tr class="code">794<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 795why, one step back795<p>Register the given callback <code>fn</code> for the given <code>type</code>.</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 796–798why, one step back796797<h2></h2>798Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 799why, one step back799<ul><li><p><strong>param</strong>: <em>String</em> type</p></li><li><p><strong>param</strong>: <em>Function</em> fn</p></li><li><p><strong>return</strong>: <em>Server</em> for chaining</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 800–801why, one step back800</td>801<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 802–804why, one step back802<pre><code><span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">is</span> = <span class="keyword">function</span>(<span class="variable">type</span>, <span class="variable">fn</span>){803 <span class="keyword">if</span> (!<span class="variable">fn</span>) <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">isCallbacks</span>[<span class="variable">type</span>];804 <span class="this">this</span>.<span class="variable">isCallbacks</span>[<span class="variable">type</span>] = <span class="variable">fn</span>;Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 805–810why, one step back805 <span class="keyword">return</span> <span class="this">this</span>;806};</code></pre>807</td>808</tr>809<tr class="code">810<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 811–812why, one step back811<p>Assign <code>setting</code> to <code>val</code>, or return <code>setting</code>'s value.812Mounted servers inherit their parent server's settings.</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 813–815why, one step back813814<h2></h2>815Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 816why, one step back816<ul><li><p><strong>param</strong>: <em>String</em> setting</p></li><li><p><strong>param</strong>: <em>String</em> val</p></li><li><p><strong>return</strong>: <em>Server | Mixed</em> for chaining, or the setting value</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 817–818why, one step back817</td>818<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 819why, one step back819<pre><code><span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">set</span> = <span class="keyword">function</span>(<span class="variable">setting</span>, <span class="variable">val</span>){Added screencasts pageTj Holowaychukbefore adoption4144af3
line 820why, one step back820 <span class="keyword">if</span> (<span class="variable">val</span> === <span class="variable">undefined</span>) {Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 821–824why, one step back821 <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">settings</span>.<span class="variable">hasOwnProperty</span>(<span class="variable">setting</span>)) {822 <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">settings</span>[<span class="variable">setting</span>];823 } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">parent</span>) {824 <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">parent</span>.<span class="variable">set</span>(<span class="variable">setting</span>);Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 825–826why, one step back825 }826 } <span class="keyword">else</span> {Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 827why, one step back827 <span class="this">this</span>.<span class="variable">settings</span>[<span class="variable">setting</span>] = <span class="variable">val</span>;Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 828–834why, one step back828 <span class="keyword">return</span> <span class="this">this</span>;829 }830};</code></pre>831</td>832</tr>833<tr class="code">834<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 835why, one step back835<p>Enable <code>setting</code>.</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 836–841why, one step back836837<h2></h2>838839<ul><li><p><strong>param</strong>: <em>String</em> setting</p></li><li><p><strong>return</strong>: <em>Server</em> for chaining</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>840</td>841<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 842–843why, one step back842<pre><code><span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">enable</span> = <span class="keyword">function</span>(<span class="variable">setting</span>){843 <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">set</span>(<span class="variable">setting</span>, <span class="variable">true</span>);Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 844–848why, one step back844};</code></pre>845</td>846</tr>847<tr class="code">848<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 849why, one step back849<p>Disable <code>setting</code>.</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 850–855why, one step back850851<h2></h2>852853<ul><li><p><strong>param</strong>: <em>String</em> setting</p></li><li><p><strong>return</strong>: <em>Server</em> for chaining</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>854</td>855<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 856–857why, one step back856<pre><code><span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">disable</span> = <span class="keyword">function</span>(<span class="variable">setting</span>){857 <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">set</span>(<span class="variable">setting</span>, <span class="variable">false</span>);Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 858–862why, one step back858};</code></pre>859</td>860</tr>861<tr class="code">862<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 863why, one step back863<p>Redirect <code>key</code> to <code>url</code>.</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 864–866why, one step back864865<h2></h2>866Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 867why, one step back867<ul><li><p><strong>param</strong>: <em>String</em> key</p></li><li><p><strong>param</strong>: <em>String</em> url</p></li><li><p><strong>return</strong>: <em>Server</em> for chaining</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 868–869why, one step back868</td>869<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 870–871why, one step back870<pre><code><span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">redirect</span> = <span class="keyword">function</span>(<span class="variable">key</span>, <span class="variable">url</span>){871 <span class="this">this</span>.<span class="variable">redirects</span>[<span class="variable">key</span>] = <span class="variable">url</span>;Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 872–877why, one step back872 <span class="keyword">return</span> <span class="this">this</span>;873};</code></pre>874</td>875</tr>876<tr class="code">877<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 878why, one step back878<p>Configure callback for the given <code>env</code>.</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 879–881why, one step back879880<h2></h2>881Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 882why, one step back882<ul><li><p><strong>param</strong>: <em>String</em> env</p></li><li><p><strong>param</strong>: <em>Function</em> fn</p></li><li><p><strong>return</strong>: <em>Server</em> for chaining</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 883–884why, one step back883</td>884<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 885–887why, one step back885<pre><code><span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">configure</span> = <span class="keyword">function</span>(<span class="variable">env</span>, <span class="variable">fn</span>){886 <span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">env</span> === <span class="string">'function'</span>) {887 <span class="variable">fn</span> = <span class="variable">env</span>, <span class="variable">env</span> = <span class="string">'all'</span>;Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 888–889why, one step back888 }889 <span class="keyword">if</span> (<span class="string">'all'</span> == <span class="variable">env</span> || <span class="variable">env</span> == <span class="this">this</span>.<span class="variable">settings</span>.<span class="variable">env</span>) {Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 890why, one step back890 <span class="variable">fn</span>.<span class="variable">call</span>(<span class="this">this</span>);Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 891–894why, one step back891 }892 <span class="keyword">return</span> <span class="this">this</span>;893};894Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 895why, one step back895<span class="comment">// Generate routing methods</span>Added screencasts pageTj Holowaychukbefore adoption4144af3
line 896why, one step back896Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 897–899why, one step back897(<span class="keyword">function</span>(<span class="variable">method</span>){898 <span class="class">Server</span>.<span class="variable">prototype</span>[<span class="variable">method</span>] = <span class="keyword">function</span>(<span class="variable">path</span>, <span class="variable">fn</span>){899 <span class="keyword">var</span> <span class="variable">self</span> = <span class="this">this</span>;Added screencasts pageTj Holowaychukbefore adoption4144af3
line 900why, one step back900Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 901–903why, one step back901 <span class="comment">// Ensure router is mounted</span>902 <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">__usedRouter</span>) {903 <span class="this">this</span>.<span class="variable">use</span>(<span class="this">this</span>.<span class="variable">router</span>);Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 904–905why, one step back904 }905Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 906–918why, one step back906 <span class="comment">// Route specific middleware support</span>907 <span class="keyword">if</span> (<span class="variable">arguments</span>.<span class="variable">length</span> &<span class="variable">gt</span>; <span class="number integer">2</span>) {908 <span class="keyword">var</span> <span class="variable">args</span> = <span class="class">Array</span>.<span class="variable">prototype</span>.<span class="variable">slice</span>.<span class="variable">call</span>(<span class="variable">arguments</span>, <span class="number integer">1</span>);909 <span class="variable">fn</span> = <span class="variable">args</span>.<span class="variable">pop</span>();910 (<span class="keyword">function</span> <span class="variable">stack</span>(<span class="variable">middleware</span>){911 <span class="variable">middleware</span>.<span class="variable">forEach</span>(<span class="keyword">function</span>(<span class="variable">fn</span>){912 <span class="keyword">if</span> (<span class="class">Array</span>.<span class="variable">isArray</span>(<span class="variable">fn</span>)) {913 <span class="variable">stack</span>(<span class="variable">fn</span>);914 } <span class="keyword">else</span> {915 <span class="variable">self</span>[<span class="variable">method</span>](<span class="variable">path</span>, <span class="variable">fn</span>);916 }917 });918 })(<span class="variable">args</span>);Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 919–920why, one step back919 }920Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 921–922why, one step back921 <span class="comment">// Generate the route</span>922 <span class="this">this</span>.<span class="variable">routes</span>[<span class="variable">method</span>](<span class="variable">path</span>, <span class="variable">fn</span>);Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 923–924why, one step back923 <span class="keyword">return</span> <span class="this">this</span>;924 };Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 925–926why, one step back925 <span class="keyword">return</span> <span class="variable">arguments</span>.<span class="variable">callee</span>;926})(<span class="string">'get'</span>)(<span class="string">'post'</span>)(<span class="string">'put'</span>)(<span class="string">'delete'</span>)(<span class="string">'all'</span>);Added screencasts pageTj Holowaychukbefore adoption4144af3
line 927why, one step back927Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 928why, one step back928<span class="comment">// Alias delete as "del"</span>Added screencasts pageTj Holowaychukbefore adoption4144af3
line 929why, one step back929Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 930why, one step back930<span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">del</span> = <span class="class">Server</span>.<span class="variable">prototype</span>.<span class="keyword">delete</span>;Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 931–1391why, one step back931</code></pre>932</td>933</tr><tr class="filename"><td><h2 id="lib/express/response.js"><a href="#">response</a></h2></td><td>lib/express/response.js</td></tr><tr class="code">934<td class="docs">935<p>Module dependencies.936 </p>937</td>938<td class="code">939<pre><code><span class="keyword">var</span> <span class="variable">fs</span> = <span class="variable">require</span>(<span class="string">'fs'</span>)940 , <span class="variable">http</span> = <span class="variable">require</span>(<span class="string">'http'</span>)941 , <span class="variable">path</span> = <span class="variable">require</span>(<span class="string">'path'</span>)942 , <span class="variable">pump</span> = <span class="variable">require</span>(<span class="string">'sys'</span>).<span class="variable">pump</span>943 , <span class="variable">utils</span> = <span class="variable">require</span>(<span class="string">'connect'</span>).<span class="variable">utils</span>944 , <span class="variable">parseRange</span> = <span class="variable">require</span>(<span class="string">'./utils'</span>).<span class="variable">parseRange</span>945 , <span class="variable">mime</span> = <span class="variable">utils</span>.<span class="variable">mime</span>;</code></pre>946</td>947</tr>948<tr class="code">949<td class="docs">950<p>Header fields supporting multiple values.</p>951952<ul><li><p><strong>type</strong>: <em>Array</em></p></li></ul>953</td>954<td class="code">955<pre><code><span class="keyword">var</span> <span class="variable">multiple</span> = [<span class="string">'Set-Cookie'</span>];</code></pre>956</td>957</tr>958<tr class="code">959<td class="docs">960<p>Send a response with the given <code>body</code> and optional <code>headers</code> and <code>status</code> code.</p>961962<h2>Examples</h2>963964<pre><code>res.send();965res.send(new Buffer('wahoo'));966res.send({ some: 'json' });967res.send('<p>some html</p>');968res.send('Sorry, cant find that', 404);969res.send('text', { 'Content-Type': 'text/plain' }, 201);970res.send(404);</code></pre>971972<h2></h2>973974<ul><li><p><strong>param</strong>: <em>String | Object | Number | Buffer</em> body or status</p></li><li><p><strong>param</strong>: <em>Object | Number</em> headers or status</p></li><li><p><strong>param</strong>: <em>Number</em> status</p></li><li><p><strong>return</strong>: <em>ServerResponse</em> </p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>975</td>976<td class="code">977<pre><code><span class="variable">http</span>.<span class="class">ServerResponse</span>.<span class="variable">prototype</span>.<span class="variable">send</span> = <span class="keyword">function</span>(<span class="variable">body</span>, <span class="variable">headers</span>, <span class="variable">status</span>){978 <span class="comment">// Allow status as second arg</span>979 <span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">headers</span> === <span class="string">'number'</span>) {980 <span class="variable">status</span> = <span class="variable">headers</span>,981 <span class="variable">headers</span> = <span class="keyword">null</span>;982 }983984 <span class="comment">// Defaults</span>985 <span class="variable">status</span> = <span class="variable">status</span> || <span class="number integer">200</span>;986987 <span class="comment">// Allow 0 args as 204</span>988 <span class="keyword">if</span> (!<span class="variable">arguments</span>.<span class="variable">length</span>) {989 <span class="variable">body</span> = <span class="variable">status</span> = <span class="number integer">204</span>;990 }991992 <span class="comment">// Determine content type</span>993 <span class="keyword">switch</span> (<span class="keyword">typeof</span> <span class="variable">body</span>) {994 <span class="keyword">case</span> <span class="string">'number'</span>:995 <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Type'</span>]) {996 <span class="this">this</span>.<span class="variable">contentType</span>(<span class="string">'.txt'</span>);997 }998 <span class="variable">body</span> = <span class="variable">http</span>.<span class="class">STATUS_CODES</span>[<span class="variable">status</span> = <span class="variable">body</span>];999 <span class="keyword">break</span>;1000 <span class="keyword">case</span> <span class="string">'string'</span>:1001 <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Type'</span>]) {1002 <span class="this">this</span>.<span class="variable">contentType</span>(<span class="string">'.html'</span>);1003 }1004 <span class="keyword">break</span>;1005 <span class="keyword">case</span> <span class="string">'object'</span>:1006 <span class="keyword">if</span> (<span class="variable">body</span> <span class="variable">instanceof</span> <span class="class">Buffer</span>) {1007 <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Type'</span>]) {1008 <span class="this">this</span>.<span class="variable">contentType</span>(<span class="string">'.bin'</span>);1009 }1010 } <span class="keyword">else</span> {1011 <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Type'</span>]) {1012 <span class="this">this</span>.<span class="variable">contentType</span>(<span class="string">'.json'</span>);1013 }1014 <span class="variable">body</span> = <span class="class">JSON</span>.<span class="variable">stringify</span>(<span class="variable">body</span>);1015 <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">req</span>.<span class="variable">query</span>.<span class="variable">callback</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="this">this</span>.<span class="variable">app</span>.<span class="variable">settings</span>[<span class="string">'jsonp callback'</span>]) {1016 <span class="this">this</span>.<span class="variable">header</span>(<span class="string">'Content-Type'</span>, <span class="string">'text/javascript'</span>);1017 <span class="variable">body</span> = <span class="this">this</span>.<span class="variable">req</span>.<span class="variable">query</span>.<span class="variable">callback</span> + <span class="string">'('</span> + <span class="variable">body</span> + <span class="string">');'</span>;1018 }1019 }1020 <span class="keyword">break</span>;1021 }10221023 <span class="comment">// Populate Content-Length</span>1024 <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Length'</span>]) {1025 <span class="this">this</span>.<span class="variable">header</span>(<span class="string">'Content-Length'</span>, <span class="variable">body</span> <span class="variable">instanceof</span> <span class="class">Buffer</span>1026 ? <span class="variable">body</span>.<span class="variable">length</span>1027 : <span class="class">Buffer</span>.<span class="variable">byteLength</span>(<span class="variable">body</span>));1028 }10291030 <span class="comment">// Merge headers passed</span>1031 <span class="keyword">if</span> (<span class="variable">headers</span>) {1032 <span class="keyword">var</span> <span class="variable">fields</span> = <span class="class">Object</span>.<span class="variable">keys</span>(<span class="variable">headers</span>);1033 <span class="keyword">for</span> (<span class="keyword">var</span> <span class="variable">i</span> = <span class="number integer">0</span>, <span class="variable">len</span> = <span class="variable">fields</span>.<span class="variable">length</span>; <span class="variable">i</span> &<span class="variable">lt</span>; <span class="variable">len</span>; ++<span class="variable">i</span>) {1034 <span class="keyword">var</span> <span class="variable">field</span> = <span class="variable">fields</span>[<span class="variable">i</span>];1035 <span class="this">this</span>.<span class="variable">header</span>(<span class="variable">field</span>, <span class="variable">headers</span>[<span class="variable">field</span>]);1036 }1037 }10381039 <span class="comment">// Strip irrelevant headers</span>1040 <span class="keyword">if</span> (<span class="number integer">204</span> === <span class="variable">status</span>) {1041 <span class="keyword">delete</span> <span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Type'</span>];1042 <span class="keyword">delete</span> <span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Length'</span>];1043 }10441045 <span class="comment">// Respond</span>1046 <span class="this">this</span>.<span class="variable">writeHead</span>(<span class="variable">status</span>, <span class="this">this</span>.<span class="variable">headers</span>);1047 <span class="this">this</span>.<span class="variable">end</span>(<span class="string">'HEAD'</span> == <span class="this">this</span>.<span class="variable">req</span>.<span class="variable">method</span> ? <span class="variable">undefined</span> : <span class="variable">body</span>);1048};</code></pre>1049</td>1050</tr>1051<tr class="code">1052<td class="docs">1053<p>Transfer the file at the given <code>path</code>. Automatically sets 1054the <em>Content-Type</em> response header via <code>res.contentType()</code>. </p>10551056<p>The given callback <code>fn</code> is invoked when an error occurs,1057passing it as the first argument, or when the file is transferred,1058passing the path as the second argument.</p>10591060<p>When the filesize is >= "stream threshold" (defaulting to 32k), the1061file will be streamed using an <code>fs.ReadStream</code> and <code>sys.pump()</code>.</p>10621063<h2></h2>10641065<ul><li><p><strong>param</strong>: <em>String</em> path</p></li><li><p><strong>param</strong>: <em>Function</em> fn</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>1066</td>1067<td class="code">1068<pre><code><span class="variable">http</span>.<span class="class">ServerResponse</span>.<span class="variable">prototype</span>.<span class="variable">sendfile</span> = <span class="keyword">function</span>(<span class="variable">path</span>, <span class="variable">fn</span>){1069 <span class="keyword">var</span> <span class="variable">self</span> = <span class="this">this</span>1070 , <span class="variable">streamThreshold</span> = <span class="this">this</span>.<span class="variable">app</span>.<span class="variable">set</span>(<span class="string">'stream threshold'</span>) || <span class="number integer">32</span> * <span class="number integer">1024</span>1071 , <span class="variable">ranges</span> = <span class="variable">self</span>.<span class="variable">req</span>.<span class="variable">headers</span>.<span class="variable">range</span>;10721073 <span class="keyword">if</span> (~<span class="variable">path</span>.<span class="variable">indexOf</span>(<span class="string">'..'</span>)) <span class="this">this</span>.<span class="variable">send</span>(<span class="number integer">403</span>);10741075 <span class="keyword">function</span> <span class="variable">error</span>(<span class="variable">err</span>) {1076 <span class="keyword">delete</span> <span class="variable">self</span>.<span class="variable">headers</span>[<span class="string">'Content-Disposition'</span>];1077 <span class="keyword">if</span> (<span class="variable">fn</span>) {1078 <span class="variable">fn</span>(<span class="variable">err</span>, <span class="variable">path</span>);1079 } <span class="keyword">else</span> {1080 <span class="variable">self</span>.<span class="variable">req</span>.<span class="variable">next</span>(<span class="variable">err</span>);1081 }1082 }10831084 <span class="variable">fs</span>.<span class="variable">stat</span>(<span class="variable">path</span>, <span class="keyword">function</span>(<span class="variable">err</span>, <span class="variable">stat</span>){1085 <span class="keyword">if</span> (<span class="variable">err</span>) <span class="keyword">return</span> <span class="variable">error</span>(<span class="variable">err</span>);1086 <span class="keyword">if</span> (<span class="variable">stat</span>.<span class="variable">size</span> &<span class="variable">gt</span>;= <span class="variable">streamThreshold</span>) {1087 <span class="keyword">var</span> <span class="variable">status</span> = <span class="number integer">200</span>;1088 <span class="keyword">if</span> (<span class="variable">ranges</span>) <span class="variable">ranges</span> = <span class="variable">parseRange</span>(<span class="variable">stat</span>.<span class="variable">size</span>, <span class="variable">ranges</span>);1089 <span class="keyword">if</span> (<span class="variable">ranges</span>) {1090 <span class="keyword">var</span> <span class="variable">stream</span> = <span class="variable">fs</span>.<span class="variable">createReadStream</span>(<span class="variable">path</span>, <span class="variable">ranges</span>[<span class="number integer">0</span>])1091 , <span class="variable">start</span> = <span class="variable">ranges</span>[<span class="number integer">0</span>].<span class="variable">start</span>1092 , <span class="variable">end</span> = <span class="variable">ranges</span>[<span class="number integer">0</span>].<span class="variable">end</span>;1093 <span class="variable">status</span> = <span class="number integer">206</span>;1094 <span class="variable">self</span>.<span class="variable">header</span>(<span class="string">'Content-Range'</span>, <span class="string">'bytes '</span>1095 + <span class="variable">start</span>1096 + <span class="string">'-'</span>1097 + <span class="variable">end</span>1098 + <span class="string">'/'</span>1099 + <span class="variable">stat</span>.<span class="variable">size</span>);1100 } <span class="keyword">else</span> {1101 <span class="keyword">var</span> <span class="variable">stream</span> = <span class="variable">fs</span>.<span class="variable">createReadStream</span>(<span class="variable">path</span>);1102 }1103 <span class="variable">self</span>.<span class="variable">contentType</span>(<span class="variable">path</span>);1104 <span class="variable">self</span>.<span class="variable">header</span>(<span class="string">'Accept-Ranges'</span>, <span class="string">'bytes'</span>);1105 <span class="variable">self</span>.<span class="variable">writeHead</span>(<span class="variable">status</span>, <span class="variable">self</span>.<span class="variable">headers</span>);1106 <span class="variable">pump</span>(<span class="variable">stream</span>, <span class="variable">self</span>, <span class="keyword">function</span>(<span class="variable">err</span>){1107 <span class="variable">fn</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">fn</span>(<span class="variable">err</span>, <span class="variable">path</span>, <span class="variable">true</span>);1108 });1109 } <span class="keyword">else</span> {1110 <span class="variable">fs</span>.<span class="variable">readFile</span>(<span class="variable">path</span>, <span class="keyword">function</span>(<span class="variable">err</span>, <span class="variable">buf</span>){1111 <span class="keyword">if</span> (<span class="variable">err</span>) <span class="keyword">return</span> <span class="variable">error</span>(<span class="variable">err</span>);1112 <span class="variable">self</span>.<span class="variable">contentType</span>(<span class="variable">path</span>);1113 <span class="variable">self</span>.<span class="variable">send</span>(<span class="variable">buf</span>);1114 <span class="variable">fn</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">fn</span>(<span class="keyword">null</span>, <span class="variable">path</span>);1115 });1116 }1117 });1118};</code></pre>1119</td>1120</tr>1121<tr class="code">1122<td class="docs">1123<p>Set <em>Content-Type</em> response header passed through <code>mime.type()</code>.</p>11241125<h2>Examples</h2>11261127<pre><code>var filename = 'path/to/image.png';1128res.contentType(filename);1129// res.headers['Content-Type'] is now "image/png"11301131res.contentType('.html');1132res.contentType('html');1133res.contentType('json');1134res.contentType('png');</code></pre>11351136<h2></h2>11371138<ul><li><p><strong>param</strong>: <em>String</em> type</p></li><li><p><strong>return</strong>: <em>String</em> the resolved mime type</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>1139</td>1140<td class="code">1141<pre><code><span class="variable">http</span>.<span class="class">ServerResponse</span>.<span class="variable">prototype</span>.<span class="variable">contentType</span> = <span class="keyword">function</span>(<span class="variable">type</span>){1142 <span class="keyword">if</span> (!~<span class="variable">type</span>.<span class="variable">indexOf</span>(<span class="string">'.'</span>)) <span class="variable">type</span> = <span class="string">'.'</span> + <span class="variable">type</span>;1143 <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">header</span>(<span class="string">'Content-Type'</span>, <span class="variable">mime</span>.<span class="variable">type</span>(<span class="variable">type</span>));1144};</code></pre>1145</td>1146</tr>1147<tr class="code">1148<td class="docs">1149<p>Set <em>Content-Disposition</em> header to <em>attachment</em> with optional <code>filename</code>.</p>11501151<h2></h2>11521153<ul><li><p><strong>param</strong>: <em>String</em> filename</p></li><li><p><strong>return</strong>: <em>ServerResponse</em> </p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>1154</td>1155<td class="code">1156<pre><code><span class="variable">http</span>.<span class="class">ServerResponse</span>.<span class="variable">prototype</span>.<span class="variable">attachment</span> = <span class="keyword">function</span>(<span class="variable">filename</span>){1157 <span class="this">this</span>.<span class="variable">header</span>(<span class="string">'Content-Disposition'</span>, <span class="variable">filename</span>1158 ? <span class="string">'attachment; filename="'</span> + <span class="variable">path</span>.<span class="variable">basename</span>(<span class="variable">filename</span>) + <span class="string">'"'</span>1159 : <span class="string">'attachment'</span>);1160 <span class="keyword">return</span> <span class="this">this</span>;1161};</code></pre>1162</td>1163</tr>1164<tr class="code">1165<td class="docs">1166<p>Transfer the file at the given <code>path</code>, with optional 1167<code>filename</code> as an attachment. Once transferred, or if an1168error occurs <code>fn</code> is called with the error and path.</p>11691170<h2></h2>11711172<ul><li><p><strong>param</strong>: <em>String</em> path</p></li><li><p><strong>param</strong>: <em>String</em> filename</p></li><li><p><strong>param</strong>: <em>Function</em> fn</p></li><li><p><strong>return</strong>: <em>Type</em> </p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>1173</td>1174<td class="code">1175<pre><code><span class="variable">http</span>.<span class="class">ServerResponse</span>.<span class="variable">prototype</span>.<span class="variable">download</span> = <span class="keyword">function</span>(<span class="variable">path</span>, <span class="variable">filename</span>, <span class="variable">fn</span>){1176 <span class="this">this</span>.<span class="variable">attachment</span>(<span class="variable">filename</span> || <span class="variable">path</span>).<span class="variable">sendfile</span>(<span class="variable">path</span>, <span class="variable">fn</span>);1177};</code></pre>1178</td>1179</tr>1180<tr class="code">1181<td class="docs">1182<p>Set or get response header <code>name</code> with optional <code>val</code>.</p>11831184<p>Headers that may be set multiple times (as indicated by the <code>multiple</code> array)1185can be called with a value several times, for example:</p>11861187<p> res.header('Set-Cookie', '...');1188 res.header('Set-Cookie', '...');</p>11891190<h2></h2>11911192<ul><li><p><strong>param</strong>: <em>String</em> name</p></li><li><p><strong>param</strong>: <em>String</em> val</p></li><li><p><strong>return</strong>: <em>String</em> </p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>1193</td>1194<td class="code">1195<pre><code><span class="variable">http</span>.<span class="class">ServerResponse</span>.<span class="variable">prototype</span>.<span class="variable">header</span> = <span class="keyword">function</span>(<span class="variable">name</span>, <span class="variable">val</span>){1196 <span class="keyword">if</span> (<span class="variable">val</span> === <span class="variable">undefined</span>) {1197 <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">headers</span>[<span class="variable">name</span>];1198 } <span class="keyword">else</span> {1199 <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">headers</span>[<span class="variable">name</span>] &<span class="variable">amp</span>;&<span class="variable">amp</span>; ~<span class="variable">multiple</span>.<span class="variable">indexOf</span>(<span class="variable">name</span>)) {1200 <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">headers</span>[<span class="variable">name</span>] += <span class="string">'\r\n'</span> + <span class="variable">name</span> + <span class="string">': '</span> + <span class="variable">val</span>;1201 } <span class="keyword">else</span> {1202 <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">headers</span>[<span class="variable">name</span>] = <span class="variable">val</span>;1203 }1204 }1205};</code></pre>1206</td>1207</tr>1208<tr class="code">1209<td class="docs">1210<p>Clear cookie <code>name</code>.</p>12111212<h2></h2>12131214<ul><li><p><strong>param</strong>: <em>String</em> name</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>1215</td>1216<td class="code">1217<pre><code><span class="variable">http</span>.<span class="class">ServerResponse</span>.<span class="variable">prototype</span>.<span class="variable">clearCookie</span> = <span class="keyword">function</span>(<span class="variable">name</span>){1218 <span class="this">this</span>.<span class="variable">cookie</span>(<span class="variable">name</span>, <span class="string">''</span>, { <span class="variable">expires</span>: <span class="keyword">new</span> <span class="class">Date</span>(<span class="number integer">1</span>) });1219};</code></pre>1220</td>1221</tr>1222<tr class="code">1223<td class="docs">1224<p>Set cookie <code>name</code> to <code>val</code>.</p>12251226<h2>Examples</h2>12271228<p> // "Remember Me" for 15 minutes1229 res.cookie('rememberme', '1', { expires: new Date(Date.now() + 900000), httpOnly: true });</p>12301231<h2></h2>12321233<ul><li><p><strong>param</strong>: <em>String</em> name</p></li><li><p><strong>param</strong>: <em>String</em> val</p></li><li><p><strong>param</strong>: <em>Options</em> options</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>1234</td>1235<td class="code">1236<pre><code><span class="variable">http</span>.<span class="class">ServerResponse</span>.<span class="variable">prototype</span>.<span class="variable">cookie</span> = <span class="keyword">function</span>(<span class="variable">name</span>, <span class="variable">val</span>, <span class="variable">options</span>){1237 <span class="keyword">var</span> <span class="variable">cookie</span> = <span class="variable">utils</span>.<span class="variable">serializeCookie</span>(<span class="variable">name</span>, <span class="variable">val</span>, <span class="variable">options</span>);1238 <span class="this">this</span>.<span class="variable">header</span>(<span class="string">'Set-Cookie'</span>, <span class="variable">cookie</span>);1239};</code></pre>1240</td>1241</tr>1242<tr class="code">1243<td class="docs">1244<p>Redirect to the given <code>url</code> with optional response <code>status</code>1245defauling to 302.</p>12461247<p>The given <code>url</code> can also be the name of a mapped url, for1248example by default express supports "back" which redirects1249to the <em>Referrer</em> or <em>Referer</em> headers or the application's1250"home" setting. Express also supports "home" out of the box,1251which can be set via <code>app.set('home', '/blog');</code>, and defaults1252to '/'.</p>12531254<h2>Redirect Mapping</h2>12551256<p> To extend the redirect mapping capabilities that Express provides,1257 we may use the <code>app.redirect()</code> method:</p>12581259<pre><code>app.redirect('google', 'http://google.com');</code></pre>12601261<p> Now in a route we may call:</p>12621263<pre><code>res.redirect('google');</code></pre>12641265<p> We may also map dynamic redirects:</p>12661267<pre><code> app.redirect('comments', function(req, res){1268 return '/post/' + req.params.id + '/comments';1269 });</code></pre>12701271<p> So now we may do the following, and the redirect will dynamically adjust to1272 the context of the request. If we called this route with <em>GET /post/12</em> our1273 redirect <em>Location</em> would be <em>/post/12/comments</em>.</p>12741275<pre><code> app.get('/post/:id', function(req, res){1276 res.redirect('comments');1277 });</code></pre>12781279<h2></h2>12801281<ul><li><p><strong>param</strong>: <em>String</em> url</p></li><li><p><strong>param</strong>: <em>Number</em> code</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>1282</td>1283<td class="code">1284<pre><code><span class="variable">http</span>.<span class="class">ServerResponse</span>.<span class="variable">prototype</span>.<span class="variable">redirect</span> = <span class="keyword">function</span>(<span class="variable">url</span>, <span class="variable">status</span>){1285 <span class="keyword">var</span> <span class="variable">basePath</span> = <span class="this">this</span>.<span class="variable">app</span>.<span class="variable">set</span>(<span class="string">'home'</span>) || <span class="string">'/'</span>1286 , <span class="variable">status</span> = <span class="variable">status</span> || <span class="number integer">302</span>1287 , <span class="variable">body</span>;12881289 <span class="comment">// Setup redirect map</span>1290 <span class="keyword">var</span> <span class="variable">map</span> = {1291 <span class="variable">back</span>: <span class="this">this</span>.<span class="variable">req</span>.<span class="variable">headers</span>.<span class="variable">referrer</span> || <span class="this">this</span>.<span class="variable">req</span>.<span class="variable">headers</span>.<span class="variable">referer</span> || <span class="variable">basePath</span>,1292 <span class="variable">home</span>: <span class="variable">basePath</span>1293 };12941295 <span class="comment">// Support custom redirect map</span>1296 <span class="variable">map</span>.<span class="variable">__proto__</span> = <span class="this">this</span>.<span class="variable">app</span>.<span class="variable">redirects</span>;12971298 <span class="comment">// Attempt mapped redirect</span>1299 <span class="keyword">var</span> <span class="variable">mapped</span> = <span class="keyword">typeof</span> <span class="variable">map</span>[<span class="variable">url</span>] === <span class="string">'function'</span>1300 ? <span class="variable">map</span>[<span class="variable">url</span>](<span class="this">this</span>.<span class="variable">req</span>, <span class="this">this</span>)1301 : <span class="variable">map</span>[<span class="variable">url</span>];13021303 <span class="comment">// Perform redirect</span>1304 <span class="variable">url</span> = <span class="variable">mapped</span> || <span class="variable">url</span>;13051306 <span class="comment">// Support text/{plain,html} by default</span>1307 <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">req</span>.<span class="variable">accepts</span>(<span class="string">'html'</span>)) {1308 <span class="variable">body</span> = <span class="string">'<p>'</span> + <span class="variable">http</span>.<span class="class">STATUS_CODES</span>[<span class="variable">status</span>] + <span class="string">'. Redirecting to <a href="'</span> + <span class="variable">url</span> + <span class="string">'">'</span> + <span class="variable">url</span> + <span class="string">'</a></p>'</span>;1309 <span class="this">this</span>.<span class="variable">header</span>(<span class="string">'Content-Type'</span>, <span class="string">'text/html'</span>);1310 } <span class="keyword">else</span> {1311 <span class="variable">body</span> = <span class="variable">http</span>.<span class="class">STATUS_CODES</span>[<span class="variable">status</span>] + <span class="string">'. Redirecting to '</span> + <span class="variable">url</span>;1312 <span class="this">this</span>.<span class="variable">header</span>(<span class="string">'Content-Type'</span>, <span class="string">'text/plain'</span>);1313 }13141315 <span class="comment">// Respond</span>1316 <span class="this">this</span>.<span class="variable">send</span>(<span class="variable">body</span>, { <span class="class">Location</span>: <span class="variable">url</span> }, <span class="variable">status</span>);1317};1318</code></pre>1319</td>1320</tr><tr class="filename"><td><h2 id="lib/express/view/partial.js"><a href="#">partial</a></h2></td><td>lib/express/view/partial.js</td></tr><tr class="code">1321<td class="docs">1322<p>Module dependencies.1323 </p>1324</td>1325<td class="code">1326<pre><code><span class="keyword">var</span> <span class="class">View</span> = <span class="variable">require</span>(<span class="string">'./view'</span>)1327 , <span class="variable">basename</span> = <span class="variable">require</span>(<span class="string">'path'</span>).<span class="variable">basename</span>;</code></pre>1328</td>1329</tr>1330<tr class="code">1331<td class="docs">1332<p>Memory cache.1333 </p>1334</td>1335<td class="code">1336<pre><code><span class="keyword">var</span> <span class="variable">cache</span> = {};</code></pre>1337</td>1338</tr>1339<tr class="code">1340<td class="docs">1341<p>Initialize a new <code>Partial</code> for the given <code>view</code> and <code>options</code>.</p>13421343<h2></h2>13441345<ul><li><p><strong>param</strong>: <em>String</em> view</p></li><li><p><strong>param</strong>: <em>Object</em> options</p></li><li><p><strong>api</strong>: <em>private</em></p></li></ul>1346</td>1347<td class="code">1348<pre><code><span class="keyword">var</span> <span class="class">Partial</span> = <span class="variable">exports</span> = <span class="variable">module</span>.<span class="variable">exports</span> = <span class="keyword">function</span> <span class="class">Partial</span>(<span class="variable">view</span>, <span class="variable">options</span>) {1349 <span class="variable">options</span> = <span class="variable">options</span> || {};1350 <span class="class">View</span>.<span class="variable">call</span>(<span class="this">this</span>, <span class="variable">view</span>, <span class="variable">options</span>);1351};</code></pre>1352</td>1353</tr>1354<tr class="code">1355<td class="docs">1356<p>Inherit from <code>View.prototype</code>.1357 </p>1358</td>1359<td class="code">1360<pre><code><span class="class">Partial</span>.<span class="variable">prototype</span>.<span class="variable">__proto__</span> = <span class="class">View</span>.<span class="variable">prototype</span>;</code></pre>1361</td>1362</tr>1363<tr class="code">1364<td class="docs">1365<p>Resolve partial object name from the view path.</p>13661367<h2>Examples</h2>13681369<p> "user.ejs" becomes "user"1370 "forum thread.ejs" becomes "forumThread"1371 "forum/thread/post.ejs" becomes "post"1372 "blog-post.ejs" becomes "blogPost"</p>13731374<h2></h2>13751376<ul><li><p><strong>return</strong>: <em>String</em> </p></li><li><p><strong>api</strong>: <em>private</em></p></li></ul>1377</td>1378<td class="code">1379<pre><code><span class="variable">exports</span>.<span class="variable">resolveObjectName</span> = <span class="keyword">function</span>(<span class="variable">view</span>){1380 <span class="keyword">return</span> <span class="variable">cache</span>[<span class="variable">view</span>] || (<span class="variable">cache</span>[<span class="variable">view</span>] = <span class="variable">view</span>1381 .<span class="variable">split</span>(<span class="string">'/'</span>)1382 .<span class="variable">slice</span>(-<span class="number integer">1</span>)[<span class="number integer">0</span>]1383 .<span class="variable">split</span>(<span class="string">'.'</span>)[<span class="number integer">0</span>]1384 .<span class="variable">replace</span>(<span class="regexp">/[^a-zA-Z0-9 ]+/g</span>, <span class="string">' '</span>)1385 .<span class="variable">split</span>(<span class="regexp">/ +/</span>).<span class="variable">map</span>(<span class="keyword">function</span>(<span class="variable">word</span>, <span class="variable">i</span>){1386 <span class="keyword">return</span> <span class="variable">i</span>1387 ? <span class="variable">word</span>[<span class="number integer">0</span>].<span class="variable">toUpperCase</span>() + <span class="variable">word</span>.<span class="variable">substr</span>(<span class="number integer">1</span>)1388 : <span class="variable">word</span>;1389 }).<span class="variable">join</span>(<span class="string">''</span>));1390};</code></pre>1391</td>Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1392why, one step back1392</tr><tr class="filename"><td><h2 id="lib/express/utils.js"><a href="#">utils</a></h2></td><td>lib/express/utils.js</td></tr><tr class="code">Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1393–1463why, one step back1393<td class="docs">1394<p>Module dependencies.1395 </p>1396</td>1397<td class="code">1398<pre><code><span class="keyword">var</span> <span class="variable">path</span> = <span class="variable">require</span>(<span class="string">'path'</span>)1399 , <span class="variable">basename</span> = <span class="variable">path</span>.<span class="variable">basename</span>1400 , <span class="variable">dirname</span> = <span class="variable">path</span>.<span class="variable">dirname</span>1401 , <span class="variable">extname</span> = <span class="variable">path</span>.<span class="variable">extname</span>;</code></pre>1402</td>1403</tr>1404<tr class="code">1405<td class="docs">1406<p>Memory cache.1407 </p>1408</td>1409<td class="code">1410<pre><code><span class="keyword">var</span> <span class="variable">cache</span> = {1411 <span class="variable">basename</span>: {}1412 , <span class="variable">dirname</span>: {}1413 , <span class="variable">extname</span>: {}1414};</code></pre>1415</td>1416</tr>1417<tr class="code">1418<td class="docs">1419<p>Cached basename.</p>14201421<h2></h2>14221423<ul><li><p><strong>param</strong>: <em>String</em> path</p></li><li><p><strong>return</strong>: <em>String</em> </p></li><li><p><strong>api</strong>: <em>private</em></p></li></ul>1424</td>1425<td class="code">1426<pre><code><span class="variable">exports</span>.<span class="variable">basename</span> = <span class="keyword">function</span>(<span class="variable">path</span>){1427 <span class="keyword">return</span> <span class="variable">cache</span>.<span class="variable">basename</span>[<span class="variable">path</span>]1428 || (<span class="variable">cache</span>.<span class="variable">basename</span>[<span class="variable">path</span>] = <span class="variable">basename</span>(<span class="variable">path</span>));1429};</code></pre>1430</td>1431</tr>1432<tr class="code">1433<td class="docs">1434<p>Cached dirname.</p>14351436<h2></h2>14371438<ul><li><p><strong>param</strong>: <em>String</em> path</p></li><li><p><strong>return</strong>: <em>String</em> </p></li><li><p><strong>api</strong>: <em>private</em></p></li></ul>1439</td>1440<td class="code">1441<pre><code><span class="variable">exports</span>.<span class="variable">dirname</span> = <span class="keyword">function</span>(<span class="variable">path</span>){1442 <span class="keyword">return</span> <span class="variable">cache</span>.<span class="variable">dirname</span>[<span class="variable">path</span>]1443 || (<span class="variable">cache</span>.<span class="variable">dirname</span>[<span class="variable">path</span>] = <span class="variable">dirname</span>(<span class="variable">path</span>));1444};</code></pre>1445</td>1446</tr>1447<tr class="code">1448<td class="docs">1449<p>Cached extname.</p>14501451<h2></h2>14521453<ul><li><p><strong>param</strong>: <em>String</em> path</p></li><li><p><strong>return</strong>: <em>String</em> </p></li><li><p><strong>api</strong>: <em>private</em></p></li></ul>1454</td>1455<td class="code">1456<pre><code><span class="variable">exports</span>.<span class="variable">extname</span> = <span class="keyword">function</span>(<span class="variable">path</span>){1457 <span class="keyword">return</span> <span class="variable">cache</span>.<span class="variable">extname</span>[<span class="variable">path</span>]1458 || (<span class="variable">cache</span>.<span class="variable">extname</span>[<span class="variable">path</span>] = <span class="variable">extname</span>(<span class="variable">path</span>));1459};</code></pre>1460</td>1461</tr>1462<tr class="code">1463<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1464–1466why, one step back1464<p>Parse mini markdown implementation.1465The following conversions are supported,1466primarily for the "flash" middleware:</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
line 1467why, one step back1467Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1468–1470why, one step back1468<p> <em>foo</em> or <em>foo</em> become <em>foo</em>1469 <strong>foo</strong> or <strong>foo</strong> become <strong>foo</strong>1470 <a href="B">A</a> becomes <a href="B">A</a></p>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1471–1473why, one step back14711472<h2></h2>1473Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1474why, one step back1474<ul><li><p><strong>param</strong>: <em>String</em> str</p></li><li><p><strong>return</strong>: <em>String</em> </p></li><li><p><strong>api</strong>: <em>private</em></p></li></ul>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1475–1476why, one step back1475</td>1476<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1477–1481why, one step back1477<pre><code><span class="variable">exports</span>.<span class="variable">miniMarkdown</span> = <span class="keyword">function</span>(<span class="variable">str</span>){1478 <span class="keyword">return</span> <span class="class">String</span>(<span class="variable">str</span>)1479 .<span class="variable">replace</span>(<span class="regexp">/(__|\*\*)(.*?)\1/g</span>, <span class="string">'<strong>$2</strong>'</span>)1480 .<span class="variable">replace</span>(<span class="regexp">/(_|\*)(.*?)\1/g</span>, <span class="string">'<em>$2</em>'</span>)1481 .<span class="variable">replace</span>(<span class="regexp">/\[([^\]]+)\]\(([^)]+)\)/g</span>, <span class="string">'<a href="$2">$1</a>'</span>);Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1482–1486why, one step back1482};</code></pre>1483</td>1484</tr>1485<tr class="code">1486<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1487why, one step back1487<p>Escape special characters in the given string of html.</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1488–1490why, one step back14881489<h2></h2>1490Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1491why, one step back1491<ul><li><p><strong>param</strong>: <em>String</em> html</p></li><li><p><strong>return</strong>: <em>String</em> </p></li><li><p><strong>api</strong>: <em>private</em></p></li></ul>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1492–1493why, one step back1492</td>1493<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1494–1499why, one step back1494<pre><code><span class="variable">exports</span>.<span class="variable">htmlEscape</span> = <span class="keyword">function</span>(<span class="variable">html</span>) {1495 <span class="keyword">return</span> <span class="class">String</span>(<span class="variable">html</span>)1496 .<span class="variable">replace</span>(<span class="regexp">/&/g</span>, <span class="string">'&'</span>)1497 .<span class="variable">replace</span>(<span class="regexp">/"/g</span>, <span class="string">'"'</span>)1498 .<span class="variable">replace</span>(<span class="regexp">/</g</span>, <span class="string">'<'</span>)1499 .<span class="variable">replace</span>(<span class="regexp">/>/g</span>, <span class="string">'>'</span>);Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1500–1522why, one step back1500};</code></pre>1501</td>1502</tr>1503<tr class="code">1504<td class="docs">1505<p>Clone <code>obj</code>.</p>15061507<h2></h2>15081509<ul><li><p><strong>param</strong>: <em>Object</em> obj</p></li><li><p><strong>return</strong>: <em>Object</em> </p></li><li><p><strong>api</strong>: <em>private</em></p></li></ul>1510</td>1511<td class="code">1512<pre><code><span class="variable">exports</span>.<span class="variable">clone</span> = <span class="keyword">function</span>(<span class="variable">obj</span>){1513 <span class="keyword">var</span> <span class="variable">ret</span> = {};1514 <span class="keyword">for</span> (<span class="keyword">var</span> <span class="variable">key</span> <span class="keyword">in</span> <span class="variable">obj</span>) {1515 <span class="variable">ret</span>[<span class="variable">key</span>] = <span class="variable">obj</span>[<span class="variable">key</span>];1516 }1517 <span class="keyword">return</span> <span class="variable">ret</span>;1518};</code></pre>1519</td>1520</tr>1521<tr class="code">1522<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1523why, one step back1523<p>Parse "Range" header <code>str</code> relative to the given file <code>size</code>.</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1524–1526why, one step back15241525<h2></h2>1526Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1527why, one step back1527<ul><li><p><strong>param</strong>: <em>Number</em> size</p></li><li><p><strong>param</strong>: <em>String</em> str</p></li><li><p><strong>return</strong>: <em>Array</em> </p></li><li><p><strong>api</strong>: <em>private</em></p></li></ul>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1528–1529why, one step back1528</td>1529<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1530–1535why, one step back1530<pre><code><span class="variable">exports</span>.<span class="variable">parseRange</span> = <span class="keyword">function</span>(<span class="variable">size</span>, <span class="variable">str</span>){1531 <span class="keyword">var</span> <span class="variable">valid</span> = <span class="variable">true</span>;1532 <span class="keyword">var</span> <span class="variable">arr</span> = <span class="variable">str</span>.<span class="variable">substr</span>(<span class="number integer">6</span>).<span class="variable">split</span>(<span class="string">','</span>).<span class="variable">map</span>(<span class="keyword">function</span>(<span class="variable">range</span>){1533 <span class="keyword">var</span> <span class="variable">range</span> = <span class="variable">range</span>.<span class="variable">split</span>(<span class="string">'-'</span>)1534 , <span class="variable">start</span> = <span class="variable">parseInt</span>(<span class="variable">range</span>[<span class="number integer">0</span>], <span class="number integer">10</span>)1535 , <span class="variable">end</span> = <span class="variable">parseInt</span>(<span class="variable">range</span>[<span class="number integer">1</span>], <span class="number integer">10</span>);Added screencasts pageTj Holowaychukbefore adoption4144af3
line 1536why, one step back1536Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1537–1539why, one step back1537 <span class="comment">// -500</span>1538 <span class="keyword">if</span> (<span class="variable">isNaN</span>(<span class="variable">start</span>)) {1539 <span class="variable">start</span> = <span class="variable">size</span> - <span class="variable">end</span>;Added screencasts pageTj Holowaychukbefore adoption4144af3
line 1540why, one step back1540 <span class="variable">end</span> = <span class="variable">size</span> - <span class="number integer">1</span>;Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1541–1542why, one step back1541 <span class="comment">// 500-</span>1542 } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="variable">isNaN</span>(<span class="variable">end</span>)) {Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1543–1545why, one step back1543 <span class="variable">end</span> = <span class="variable">size</span> - <span class="number integer">1</span>;1544 }1545Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1546–1547why, one step back1546 <span class="comment">// Invalid</span>1547 <span class="keyword">if</span> (<span class="variable">isNaN</span>(<span class="variable">start</span>) || <span class="variable">isNaN</span>(<span class="variable">end</span>) || <span class="variable">start</span> &<span class="variable">gt</span>; <span class="variable">end</span>) <span class="variable">valid</span> = <span class="variable">false</span>;Added screencasts pageTj Holowaychukbefore adoption4144af3
line 1548why, one step back1548Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1549why, one step back1549 <span class="keyword">return</span> { <span class="variable">start</span>: <span class="variable">start</span>, <span class="variable">end</span>: <span class="variable">end</span> };Added screencasts pageTj Holowaychukbefore adoption4144af3
line 1550why, one step back1550 });Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1551why, one step back1551 <span class="keyword">return</span> <span class="variable">valid</span> ? <span class="variable">arr</span> : <span class="variable">undefined</span>;Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1552–1564why, one step back1552};1553</code></pre>1554</td>1555</tr><tr class="filename"><td><h2 id="lib/express/view/view.js"><a href="#">view</a></h2></td><td>lib/express/view/view.js</td></tr><tr class="code">1556<td class="docs">1557<p>Module dependencies.1558 </p>1559</td>1560<td class="code">1561<pre><code><span class="keyword">var</span> <span class="variable">utils</span> = <span class="variable">require</span>(<span class="string">'../utils'</span>)1562 , <span class="variable">extname</span> = <span class="variable">utils</span>.<span class="variable">extname</span>1563 , <span class="variable">dirname</span> = <span class="variable">utils</span>.<span class="variable">dirname</span>1564 , <span class="variable">basename</span> = <span class="variable">utils</span>.<span class="variable">basename</span>Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1565why, one step back1565 , <span class="variable">fs</span> = <span class="variable">require</span>(<span class="string">'fs'</span>)Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1566–1727why, one step back1566 , <span class="variable">stat</span> = <span class="variable">fs</span>.<span class="variable">statSync</span>;</code></pre>1567</td>1568</tr>1569<tr class="code">1570<td class="docs">1571<p>Memory cache.1572 </p>1573</td>1574<td class="code">1575<pre><code><span class="keyword">var</span> <span class="variable">cache</span> = {};</code></pre>1576</td>1577</tr>1578<tr class="code">1579<td class="docs">1580<p>Existance cache.1581 </p>1582</td>1583<td class="code">1584<pre><code><span class="keyword">var</span> <span class="variable">exists</span> = {};</code></pre>1585</td>1586</tr>1587<tr class="code">1588<td class="docs">1589<p>Initialize a new <code>View</code> with the given <code>view</code> path and <code>options</code>.</p>15901591<h2></h2>15921593<ul><li><p><strong>param</strong>: <em>String</em> view</p></li><li><p><strong>param</strong>: <em>Object</em> options</p></li><li><p><strong>api</strong>: <em>private</em></p></li></ul>1594</td>1595<td class="code">1596<pre><code><span class="keyword">var</span> <span class="class">View</span> = <span class="variable">exports</span> = <span class="variable">module</span>.<span class="variable">exports</span> = <span class="keyword">function</span> <span class="class">View</span>(<span class="variable">view</span>, <span class="variable">options</span>) {1597 <span class="variable">options</span> = <span class="variable">options</span> || {};1598 <span class="comment">// TODO: more caching</span>1599 <span class="this">this</span>.<span class="variable">view</span> = <span class="variable">view</span>;1600 <span class="this">this</span>.<span class="variable">root</span> = <span class="variable">options</span>.<span class="variable">root</span>;1601 <span class="this">this</span>.<span class="variable">relative</span> = <span class="variable">false</span> !== <span class="variable">options</span>.<span class="variable">relative</span>;1602 <span class="this">this</span>.<span class="variable">defaultEngine</span> = <span class="variable">options</span>.<span class="variable">defaultEngine</span>;1603 <span class="this">this</span>.<span class="variable">parent</span> = <span class="variable">options</span>.<span class="variable">parentView</span>;1604 <span class="this">this</span>.<span class="variable">basename</span> = <span class="variable">basename</span>(<span class="variable">view</span>);1605 <span class="this">this</span>.<span class="variable">engine</span> = <span class="this">this</span>.<span class="variable">resolveEngine</span>();1606 <span class="this">this</span>.<span class="variable">extension</span> = <span class="string">'.'</span> + <span class="this">this</span>.<span class="variable">engine</span>;1607 <span class="this">this</span>.<span class="variable">path</span> = <span class="this">this</span>.<span class="variable">resolvePath</span>();1608 <span class="this">this</span>.<span class="variable">dirname</span> = <span class="variable">dirname</span>(<span class="this">this</span>.<span class="variable">path</span>);1609};</code></pre>1610</td>1611</tr>1612<tr class="code">1613<td class="docs">1614<p>Check if the view path exists.</p>16151616<h2></h2>16171618<ul><li><p><strong>return</strong>: <em>Boolean</em> </p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>1619</td>1620<td class="code">1621<pre><code><span class="class">View</span>.<span class="variable">prototype</span>.<span class="variable">__defineGetter__</span>(<span class="string">'exists'</span>, <span class="keyword">function</span>(){1622 <span class="keyword">var</span> <span class="variable">path</span> = <span class="this">this</span>.<span class="variable">path</span>;1623 <span class="keyword">if</span> (<span class="keyword">null</span> != <span class="variable">exists</span>[<span class="variable">path</span>]) {1624 <span class="keyword">return</span> <span class="variable">exists</span>[<span class="variable">path</span>];1625 } <span class="keyword">else</span> {1626 <span class="keyword">try</span> {1627 <span class="variable">stat</span>(<span class="variable">path</span>);1628 <span class="keyword">return</span> <span class="variable">exists</span>[<span class="variable">path</span>] = <span class="variable">true</span>;1629 } <span class="keyword">catch</span> (<span class="variable">err</span>) {1630 <span class="keyword">return</span> <span class="variable">exists</span>[<span class="variable">path</span>] = <span class="variable">false</span>;1631 }1632 }1633});</code></pre>1634</td>1635</tr>1636<tr class="code">1637<td class="docs">1638<p>Resolve view engine.</p>16391640<h2></h2>16411642<ul><li><p><strong>return</strong>: <em>String</em> </p></li><li><p><strong>api</strong>: <em>private</em></p></li></ul>1643</td>1644<td class="code">1645<pre><code><span class="class">View</span>.<span class="variable">prototype</span>.<span class="variable">resolveEngine</span> = <span class="keyword">function</span>(){1646 <span class="comment">// Explicit</span>1647 <span class="keyword">if</span> (~<span class="this">this</span>.<span class="variable">basename</span>.<span class="variable">indexOf</span>(<span class="string">'.'</span>)) <span class="keyword">return</span> <span class="variable">extname</span>(<span class="this">this</span>.<span class="variable">basename</span>).<span class="variable">substr</span>(<span class="number integer">1</span>);1648 <span class="comment">// Inherit from parent</span>1649 <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">parent</span>) <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">parent</span>.<span class="variable">engine</span>;1650 <span class="comment">// Default</span>1651 <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">defaultEngine</span>;1652};</code></pre>1653</td>1654</tr>1655<tr class="code">1656<td class="docs">1657<p>Resolve view path.</p>16581659<h2></h2>16601661<ul><li><p><strong>return</strong>: <em>String</em> </p></li><li><p><strong>api</strong>: <em>private</em></p></li></ul>1662</td>1663<td class="code">1664<pre><code><span class="class">View</span>.<span class="variable">prototype</span>.<span class="variable">resolvePath</span> = <span class="keyword">function</span>(){1665 <span class="keyword">var</span> <span class="variable">path</span> = <span class="this">this</span>.<span class="variable">view</span>;1666 <span class="comment">// Implicit engine</span>1667 <span class="keyword">if</span> (!~<span class="this">this</span>.<span class="variable">basename</span>.<span class="variable">indexOf</span>(<span class="string">'.'</span>)) <span class="variable">path</span> += <span class="this">this</span>.<span class="variable">extension</span>;1668 <span class="comment">// Absolute</span>1669 <span class="keyword">if</span> (<span class="string">'/'</span> == <span class="variable">path</span>[<span class="number integer">0</span>]) <span class="keyword">return</span> <span class="variable">path</span>;1670 <span class="comment">// Relative to parent</span>1671 <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">relative</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="this">this</span>.<span class="variable">parent</span>) <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">parent</span>.<span class="variable">dirname</span> + <span class="string">'/'</span> + <span class="variable">path</span>;1672 <span class="comment">// Relative to root</span>1673 <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">root</span>1674 ? <span class="this">this</span>.<span class="variable">root</span> + <span class="string">'/'</span> + <span class="variable">path</span>1675 : <span class="variable">path</span>;1676};</code></pre>1677</td>1678</tr>1679<tr class="code">1680<td class="docs">1681<p>Get view contents. This is a one-time hit, so we1682can afford to be sync.</p>16831684<h2></h2>16851686<ul><li><p><strong>return</strong>: <em>String</em> </p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>1687</td>1688<td class="code">1689<pre><code><span class="class">View</span>.<span class="variable">prototype</span>.<span class="variable">__defineGetter__</span>(<span class="string">'contents'</span>, <span class="keyword">function</span>(){1690 <span class="keyword">return</span> <span class="variable">fs</span>.<span class="variable">readFileSync</span>(<span class="this">this</span>.<span class="variable">path</span>, <span class="string">'utf8'</span>);1691});</code></pre>1692</td>1693</tr>1694<tr class="code">1695<td class="docs">1696<p>Get template engine api, cache exports to reduce1697require() calls.</p>16981699<h2></h2>17001701<ul><li><p><strong>return</strong>: <em>Object</em> </p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>1702</td>1703<td class="code">1704<pre><code><span class="class">View</span>.<span class="variable">prototype</span>.<span class="variable">__defineGetter__</span>(<span class="string">'templateEngine'</span>, <span class="keyword">function</span>(){1705 <span class="keyword">var</span> <span class="variable">ext</span> = <span class="this">this</span>.<span class="variable">extension</span>;1706 <span class="keyword">return</span> <span class="variable">cache</span>[<span class="variable">ext</span>] || (<span class="variable">cache</span>[<span class="variable">ext</span>] = <span class="variable">require</span>(<span class="this">this</span>.<span class="variable">engine</span>));1707});</code></pre>1708</td>1709</tr>1710<tr class="code">1711<td class="docs">1712<p>Return index path alternative.</p>17131714<h2></h2>17151716<ul><li><p><strong>return</strong>: <em>String</em> </p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>1717</td>1718<td class="code">1719<pre><code><span class="class">View</span>.<span class="variable">prototype</span>.<span class="variable">__defineGetter__</span>(<span class="string">'indexPath'</span>, <span class="keyword">function</span>(){1720 <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">dirname</span>1721 + <span class="string">'/'</span> + <span class="this">this</span>.<span class="variable">basename</span>.<span class="variable">replace</span>(<span class="this">this</span>.<span class="variable">extension</span>, <span class="string">''</span>)1722 + <span class="string">'/index'</span> + <span class="this">this</span>.<span class="variable">extension</span>;1723});</code></pre>1724</td>1725</tr>1726<tr class="code">1727<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1728–1730why, one step back1728<p>Register the given template engine <code>exports</code>1729as <code>ext</code>. For example we may wish to map ".html"1730files to jade:</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
line 1731why, one step back1731Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1732why, one step back1732<p> app.register('.html', require('jade'));</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1733–1737why, one step back17331734<p>or</p>17351736<p> app.register('html', require('jade'));</p>1737Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1738–1741why, one step back1738<p>This is also useful for libraries that may not1739match extensions correctly. For example my haml.js1740library is installed from npm as "hamljs" so instead1741of layout.hamljs, we can register the engine as ".haml":</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
line 1742why, one step back1742Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1743why, one step back1743<p> app.register('.haml', require('haml-js'));</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
line 1744why, one step back1744Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1745–1746why, one step back1745<p>For engines that do not comply with the Express1746specification, we can also wrap their api this way.</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
line 1747why, one step back1747Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1748–1751why, one step back1748<p> app.register('.foo', {1749 render: function(str, options) {1750 // perhaps their api is1751 // return foo.toHTML(str, options);Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1752–1756why, one step back1752 }1753 });</p>17541755<h2></h2>1756Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1757why, one step back1757<ul><li><p><strong>param</strong>: <em>String</em> ext</p></li><li><p><strong>param</strong>: <em>Object</em> obj</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1758–1759why, one step back1758</td>1759<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1760why, one step back1760<pre><code><span class="variable">exports</span>.<span class="variable">register</span> = <span class="keyword">function</span>(<span class="variable">ext</span>, <span class="variable">exports</span>) {Added screencasts pageTj Holowaychukbefore adoption4144af3
line 1761why, one step back1761 <span class="keyword">if</span> (<span class="string">'.'</span> != <span class="variable">ext</span>[<span class="number integer">0</span>]) <span class="variable">ext</span> = <span class="string">'.'</span> + <span class="variable">ext</span>;Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1762why, one step back1762 <span class="variable">cache</span>[<span class="variable">ext</span>] = <span class="variable">exports</span>;Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1763–1815why, one step back1763};1764</code></pre>1765</td>1766</tr><tr class="filename"><td><h2 id="lib/express/view.js"><a href="#">view</a></h2></td><td>lib/express/view.js</td></tr><tr class="code">1767<td class="docs">1768<p>Module dependencies.1769 </p>1770</td>1771<td class="code">1772<pre><code><span class="keyword">var</span> <span class="variable">path</span> = <span class="variable">require</span>(<span class="string">'path'</span>)1773 , <span class="variable">extname</span> = <span class="variable">path</span>.<span class="variable">extname</span>1774 , <span class="variable">dirname</span> = <span class="variable">path</span>.<span class="variable">dirname</span>1775 , <span class="variable">basename</span> = <span class="variable">path</span>.<span class="variable">basename</span>1776 , <span class="variable">utils</span> = <span class="variable">require</span>(<span class="string">'connect'</span>).<span class="variable">utils</span>1777 , <span class="variable">clone</span> = <span class="variable">require</span>(<span class="string">'./utils'</span>).<span class="variable">clone</span>1778 , <span class="class">View</span> = <span class="variable">require</span>(<span class="string">'./view/view'</span>)1779 , <span class="class">Partial</span> = <span class="variable">require</span>(<span class="string">'./view/partial'</span>)1780 , <span class="variable">merge</span> = <span class="variable">utils</span>.<span class="variable">merge</span>1781 , <span class="variable">http</span> = <span class="variable">require</span>(<span class="string">'http'</span>)1782 , <span class="variable">mime</span> = <span class="variable">utils</span>.<span class="variable">mime</span>;</code></pre>1783</td>1784</tr>1785<tr class="code">1786<td class="docs">1787<p>Memory cache.</p>17881789<ul><li><p><strong>type</strong>: <em>Object</em></p></li></ul>1790</td>1791<td class="code">1792<pre><code><span class="keyword">var</span> <span class="variable">cache</span> = {};</code></pre>1793</td>1794</tr>1795<tr class="code">1796<td class="docs">1797<p>Expose constructors.1798 </p>1799</td>1800<td class="code">1801<pre><code><span class="variable">exports</span> = <span class="variable">module</span>.<span class="variable">exports</span> = <span class="class">View</span>;1802<span class="variable">exports</span>.<span class="class">Partial</span> = <span class="class">Partial</span>;</code></pre>1803</td>1804</tr>1805<tr class="code">1806<td class="docs">1807<p>Export template engine registrar.1808 </p>1809</td>1810<td class="code">1811<pre><code><span class="variable">exports</span>.<span class="variable">register</span> = <span class="class">View</span>.<span class="variable">register</span>;</code></pre>1812</td>1813</tr>1814<tr class="code">1815<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1816why, one step back1816<p>Render <code>view</code> partial with the given <code>options</code>.</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1817–1819why, one step back18171818<h2>Options</h2>1819Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1820–1821why, one step back1820<ul><li><p><code>object</code> Single object with name derived from the view (unless <code>as</code> is present) </p></li><li><p><code>as</code> Variable name for each <code>collection</code> value, defaults to the view name.</p><ul><li>as: 'something' will add the <code>something</code> local variable</li><li>as: this will use the collection value as the template context</li><li><p>as: global will merge the collection value's properties with <code>locals</code></p></li></ul></li><li><p><code>collection</code> Array of objects, the name is derived from the view name itself. 1821For example <em>video.html</em> will have a object <em>video</em> available to it.</p></li></ul>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1822–1824why, one step back18221823<h2></h2>1824Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1825why, one step back1825<ul><li><p><strong>param</strong>: <em>String</em> view</p></li><li><p><strong>param</strong>: <em>Object | Array</em> options, collection, or object</p></li><li><p><strong>return</strong>: <em>String</em> </p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1826–1830why, one step back1826</td>1827<td class="code">1828<pre><code><span class="variable">http</span>.<span class="class">ServerResponse</span>.<span class="variable">prototype</span>.<span class="variable">partial</span> = <span class="keyword">function</span>(<span class="variable">view</span>, <span class="variable">options</span>, <span class="variable">locals</span>, <span class="variable">parent</span>){1829 <span class="keyword">var</span> <span class="variable">self</span> = <span class="this">this</span>;1830Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1831why, one step back1831 <span class="comment">// Inherit parent view extension when not present</span>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1832–1835why, one step back1832 <span class="keyword">if</span> (<span class="variable">parent</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; !~<span class="variable">view</span>.<span class="variable">indexOf</span>(<span class="string">'.'</span>)) {1833 <span class="variable">view</span> += <span class="variable">parent</span>.<span class="variable">extension</span>;1834 }1835Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1836–1841why, one step back1836 <span class="comment">// Allow collection to be passed as second param</span>1837 <span class="keyword">if</span> (<span class="variable">options</span>) {1838 <span class="keyword">if</span> (<span class="string">'length'</span> <span class="keyword">in</span> <span class="variable">options</span>) {1839 <span class="variable">options</span> = { <span class="variable">collection</span>: <span class="variable">options</span> };1840 } <span class="keyword">else</span> <span class="keyword">if</span> (!<span class="variable">options</span>.<span class="variable">collection</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; !<span class="variable">options</span>.<span class="variable">locals</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; !<span class="variable">options</span>.<span class="variable">object</span>) {1841 <span class="variable">options</span> = { <span class="variable">object</span>: <span class="variable">options</span> };Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1842–1843why, one step back1842 }1843 } <span class="keyword">else</span> {Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1844why, one step back1844 <span class="variable">options</span> = {};Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1845–1846why, one step back1845 }1846Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1847why, one step back1847 <span class="comment">// Inherit locals from parent</span>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1848–1854why, one step back1848 <span class="variable">merge</span>(<span class="variable">options</span>, <span class="variable">locals</span>);18491850 <span class="comment">// Merge locals</span>1851 <span class="keyword">if</span> (<span class="variable">options</span>.<span class="variable">locals</span>) {1852 <span class="variable">merge</span>(<span class="variable">options</span>, <span class="variable">options</span>.<span class="variable">locals</span>);1853 }1854Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1855why, one step back1855 <span class="comment">// Partials dont need layouts</span>Added screencasts pageTj Holowaychukbefore adoption4144af3
line 1856why, one step back1856 <span class="variable">options</span>.<span class="variable">renderPartial</span> = <span class="variable">true</span>;Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1857why, one step back1857 <span class="variable">options</span>.<span class="variable">layout</span> = <span class="variable">false</span>;Added screencasts pageTj Holowaychukbefore adoption4144af3
line 1858why, one step back1858Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1859why, one step back1859 <span class="comment">// Deduce name from view path</span>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1860–1863why, one step back1860 <span class="keyword">var</span> <span class="variable">name</span> = <span class="variable">options</span>.<span class="variable">as</span> || <span class="class">Partial</span>.<span class="variable">resolveObjectName</span>(<span class="variable">view</span>);18611862 <span class="comment">// Render partial</span>1863 <span class="keyword">function</span> <span class="variable">render</span>(){Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1864–1865why, one step back1864 <span class="keyword">if</span> (<span class="variable">options</span>.<span class="variable">object</span>) {1865 <span class="keyword">if</span> (<span class="string">'string'</span> == <span class="keyword">typeof</span> <span class="variable">name</span>) {Added screencasts pageTj Holowaychukbefore adoption4144af3
line 1866why, one step back1866 <span class="variable">options</span>[<span class="variable">name</span>] = <span class="variable">options</span>.<span class="variable">object</span>;Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1867why, one step back1867 } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="variable">name</span> === <span class="variable">global</span>) {Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1868–1869why, one step back1868 <span class="variable">merge</span>(<span class="variable">options</span>, <span class="variable">options</span>.<span class="variable">object</span>);1869 } <span class="keyword">else</span> {Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1870why, one step back1870 <span class="variable">options</span>.<span class="variable">scope</span> = <span class="variable">options</span>.<span class="variable">object</span>;Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1871–1899why, one step back1871 }1872 }1873 <span class="keyword">return</span> <span class="variable">self</span>.<span class="variable">render</span>(<span class="variable">view</span>, <span class="variable">options</span>, <span class="keyword">null</span>, <span class="variable">parent</span>);1874 }18751876 <span class="comment">// Collection support</span>1877 <span class="keyword">var</span> <span class="variable">collection</span> = <span class="variable">options</span>.<span class="variable">collection</span>;1878 <span class="keyword">if</span> (<span class="variable">collection</span>) {1879 <span class="keyword">var</span> <span class="variable">len</span> = <span class="variable">collection</span>.<span class="variable">length</span>1880 , <span class="variable">buf</span> = <span class="string">''</span>;1881 <span class="keyword">delete</span> <span class="variable">options</span>.<span class="variable">collection</span>;1882 <span class="variable">options</span>.<span class="variable">collectionLength</span> = <span class="variable">len</span>;1883 <span class="keyword">for</span> (<span class="keyword">var</span> <span class="variable">i</span> = <span class="number integer">0</span>; <span class="variable">i</span> &<span class="variable">lt</span>; <span class="variable">len</span>; ++<span class="variable">i</span>) {1884 <span class="keyword">var</span> <span class="variable">val</span> = <span class="variable">collection</span>[<span class="variable">i</span>];1885 <span class="variable">options</span>.<span class="variable">firstInCollection</span> = <span class="variable">i</span> === <span class="number integer">0</span>;1886 <span class="variable">options</span>.<span class="variable">indexInCollection</span> = <span class="variable">i</span>;1887 <span class="variable">options</span>.<span class="variable">lastInCollection</span> = <span class="variable">i</span> === <span class="variable">len</span> - <span class="number integer">1</span>;1888 <span class="variable">options</span>.<span class="variable">object</span> = <span class="variable">val</span>;1889 <span class="variable">buf</span> += <span class="variable">render</span>();1890 }1891 <span class="keyword">return</span> <span class="variable">buf</span>;1892 } <span class="keyword">else</span> {1893 <span class="keyword">return</span> <span class="variable">render</span>();1894 }1895};</code></pre>1896</td>1897</tr>1898<tr class="code">1899<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1900–1902why, one step back1900<p>Render <code>view</code> with the given <code>options</code> and optional callback <code>fn</code>.1901When a callback function is given a response will <em>not</em> be made1902automatically, however otherwise a response of <em>200</em> and <em>text/html</em> is given.</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1903–1905why, one step back19031904<h2>Options</h2>1905Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1906–1907why, one step back1906<p> Most engines accept one or more of the following options,1907 both <em>haml</em> and <em>jade</em> accept all:</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1908–1912why, one step back19081909<ul><li><code>scope</code> Template evaluation context (the value of <code>this</code>)</li><li><code>debug</code> Output debugging information</li><li><code>status</code> Response status code, defaults to 200</li><li><code>headers</code> Response headers object</li></ul>19101911<h2></h2>1912Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1913why, one step back1913<ul><li><p><strong>param</strong>: <em>String</em> view</p></li><li><p><strong>param</strong>: <em>Object | Function</em> options or callback function</p></li><li><p><strong>param</strong>: <em>Function</em> fn</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1914–1916why, one step back1914</td>1915<td class="code">1916<pre><code><span class="variable">http</span>.<span class="class">ServerResponse</span>.<span class="variable">prototype</span>.<span class="variable">render</span> = <span class="keyword">function</span>(<span class="variable">view</span>, <span class="variable">options</span>, <span class="variable">fn</span>, <span class="variable">parent</span>){Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1917–1919why, one step back1917 <span class="comment">// Support callback function as second arg</span>1918 <span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">options</span> === <span class="string">'function'</span>) {1919 <span class="variable">fn</span> = <span class="variable">options</span>, <span class="variable">options</span> = {};Added screencasts pageTj Holowaychukbefore adoption4144af3
line 1920why, one step back1920 }Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1921why, one step back1921 Added screencasts pageTj Holowaychukbefore adoption4144af3
line 1922why, one step back1922 <span class="keyword">var</span> <span class="variable">self</span> = <span class="this">this</span>Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1923why, one step back1923 , <span class="variable">app</span> = <span class="this">this</span>.<span class="variable">app</span>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1924–1926why, one step back1924 , <span class="variable">options</span> = <span class="variable">options</span> || {}1925 , <span class="variable">helpers</span> = <span class="variable">app</span>.<span class="variable">viewHelpers</span>1926 , <span class="variable">dynamicHelpers</span> = <span class="variable">app</span>.<span class="variable">dynamicViewHelpers</span>Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1927why, one step back1927 , <span class="variable">viewOptions</span> = <span class="variable">app</span>.<span class="variable">settings</span>[<span class="string">'view options'</span>]Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1928–1929why, one step back1928 , <span class="variable">cacheTemplates</span> = <span class="variable">app</span>.<span class="variable">settings</span>[<span class="string">'cache views'</span>];1929Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1930why, one step back1930 <span class="comment">// Mixin "view options"</span>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1931–1943why, one step back1931 <span class="keyword">if</span> (<span class="variable">viewOptions</span>) <span class="variable">options</span> = <span class="variable">merge</span>(<span class="variable">clone</span>(<span class="variable">viewOptions</span>), <span class="variable">options</span>);19321933 <span class="comment">// Defaults</span>1934 <span class="keyword">var</span> <span class="variable">self</span> = <span class="this">this</span>1935 , <span class="variable">root</span> = <span class="variable">app</span>.<span class="variable">settings</span>.<span class="variable">views</span> || <span class="variable">process</span>.<span class="variable">cwd</span>() + <span class="string">'/views'</span>1936 , <span class="variable">partial</span> = <span class="variable">options</span>.<span class="variable">renderPartial</span>1937 , <span class="variable">layout</span> = <span class="variable">options</span>.<span class="variable">layout</span>;19381939 <span class="comment">// Layout support</span>1940 <span class="keyword">if</span> (<span class="variable">true</span> === <span class="variable">layout</span> || <span class="variable">undefined</span> === <span class="variable">layout</span>) {1941 <span class="variable">layout</span> = <span class="string">'layout'</span>;1942 }1943Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1944–1945why, one step back1944 <span class="comment">// Default execution scope to the response</span>1945 <span class="variable">options</span>.<span class="variable">scope</span> = <span class="variable">options</span>.<span class="variable">scope</span> || <span class="this">this</span>.<span class="variable">req</span>;Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1946–1968why, one step back19461947 <span class="comment">// Populate view</span>1948 <span class="variable">options</span>.<span class="variable">parentView</span> = <span class="variable">parent</span>;19491950 <span class="comment">// "views" setting</span>1951 <span class="variable">options</span>.<span class="variable">root</span> = <span class="variable">root</span>;19521953 <span class="comment">// "view engine" setting</span>1954 <span class="variable">options</span>.<span class="variable">defaultEngine</span> = <span class="variable">app</span>.<span class="variable">settings</span>[<span class="string">'view engine'</span>];19551956 <span class="comment">// Populate view</span>1957 <span class="comment">// TODO: cache</span>1958 <span class="variable">view</span> = <span class="variable">partial</span>1959 ? <span class="keyword">new</span> <span class="class">Partial</span>(<span class="variable">view</span>, <span class="variable">options</span>)1960 : <span class="keyword">new</span> <span class="class">View</span>(<span class="variable">view</span>, <span class="variable">options</span>);19611962 <span class="comment">// Ensure view exists, otherwise try index</span>1963 <span class="keyword">if</span> (!<span class="variable">view</span>.<span class="variable">exists</span>) {1964 <span class="variable">view</span> = <span class="variable">partial</span>1965 ? <span class="keyword">new</span> <span class="class">Partial</span>(<span class="variable">view</span>.<span class="variable">indexPath</span>, <span class="variable">options</span>)1966 : <span class="keyword">new</span> <span class="class">View</span>(<span class="variable">view</span>.<span class="variable">indexPath</span>, <span class="variable">options</span>);1967 }1968Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1969–1973why, one step back1969 <span class="comment">// Dynamic helper support</span>1970 <span class="keyword">if</span> (<span class="variable">false</span> !== <span class="variable">options</span>.<span class="variable">dynamicHelpers</span>) {1971 <span class="comment">// cache</span>1972 <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">__dynamicHelpers</span>) {1973 <span class="this">this</span>.<span class="variable">__dynamicHelpers</span> = {};Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1974–1981why, one step back1974 <span class="keyword">for</span> (<span class="keyword">var</span> <span class="variable">key</span> <span class="keyword">in</span> <span class="variable">dynamicHelpers</span>) {1975 <span class="this">this</span>.<span class="variable">__dynamicHelpers</span>[<span class="variable">key</span>] = <span class="variable">dynamicHelpers</span>[<span class="variable">key</span>].<span class="variable">call</span>(1976 <span class="this">this</span>.<span class="variable">app</span>1977 , <span class="this">this</span>.<span class="variable">req</span>1978 , <span class="this">this</span>);1979 }1980 }1981Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1982why, one step back1982 <span class="comment">// apply</span>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1983–1985why, one step back1983 <span class="variable">merge</span>(<span class="variable">options</span>, <span class="this">this</span>.<span class="variable">__dynamicHelpers</span>);1984 }1985Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1986why, one step back1986 <span class="comment">// Merge view helpers</span>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1987–1988why, one step back1987 <span class="variable">options</span> = <span class="variable">merge</span>(<span class="variable">clone</span>(<span class="variable">helpers</span>), <span class="variable">options</span>);1988Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1989why, one step back1989 <span class="comment">// Always expose partial() as a local</span>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1990–1995why, one step back1990 <span class="variable">options</span>.<span class="variable">partial</span> = <span class="keyword">function</span>(<span class="variable">path</span>, <span class="variable">opts</span>){1991 <span class="keyword">return</span> <span class="variable">self</span>.<span class="variable">partial</span>(<span class="variable">path</span>, <span class="variable">opts</span>, <span class="variable">options</span>, <span class="variable">view</span>);1992 };19931994 <span class="keyword">function</span> <span class="variable">error</span>(<span class="variable">err</span>) {1995 <span class="keyword">if</span> (<span class="variable">fn</span>) {Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1996why, one step back1996 <span class="variable">fn</span>(<span class="variable">err</span>);Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1997–2001why, one step back1997 } <span class="keyword">else</span> {1998 <span class="variable">self</span>.<span class="variable">req</span>.<span class="variable">next</span>(<span class="variable">err</span>);1999 }2000 }The first 2,000 of 2,031 lines.