api.html
This file
Path
docs/api.html
2,031 lines · 127.7 KiB · HTML
Last changed by
Regenerated docsTj Holowaychukbefore adoptiond3abcaf
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>Regenerated docsTj Holowaychukbefore adoptiond3abcaf
lines 477–863why, one step back477</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">478<td class="docs">479<p>Module dependencies.480 </p>481</td>482<td class="code">483<pre><code><span class="keyword">var</span> <span class="variable">fs</span> = <span class="variable">require</span>(<span class="string">'fs'</span>)484 , <span class="variable">http</span> = <span class="variable">require</span>(<span class="string">'http'</span>)485 , <span class="variable">path</span> = <span class="variable">require</span>(<span class="string">'path'</span>)486 , <span class="variable">pump</span> = <span class="variable">require</span>(<span class="string">'sys'</span>).<span class="variable">pump</span>487 , <span class="variable">utils</span> = <span class="variable">require</span>(<span class="string">'connect'</span>).<span class="variable">utils</span>488 , <span class="variable">parseRange</span> = <span class="variable">require</span>(<span class="string">'./utils'</span>).<span class="variable">parseRange</span>489 , <span class="variable">mime</span> = <span class="variable">utils</span>.<span class="variable">mime</span>;</code></pre>490</td>491</tr>492<tr class="code">493<td class="docs">494<p>Header fields supporting multiple values.</p>495496<ul><li><p><strong>type</strong>: <em>Array</em></p></li></ul>497</td>498<td class="code">499<pre><code><span class="keyword">var</span> <span class="variable">multiple</span> = [<span class="string">'Set-Cookie'</span>];</code></pre>500</td>501</tr>502<tr class="code">503<td class="docs">504<p>Send a response with the given <code>body</code> and optional <code>headers</code> and <code>status</code> code.</p>505506<h2>Examples</h2>507508<pre><code>res.send();509res.send(new Buffer('wahoo'));510res.send({ some: 'json' });511res.send('<p>some html</p>');512res.send('Sorry, cant find that', 404);513res.send('text', { 'Content-Type': 'text/plain' }, 201);514res.send(404);</code></pre>515516<h2></h2>517518<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>519</td>520<td class="code">521<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>){522 <span class="comment">// Allow status as second arg</span>523 <span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">headers</span> === <span class="string">'number'</span>) {524 <span class="variable">status</span> = <span class="variable">headers</span>,525 <span class="variable">headers</span> = <span class="keyword">null</span>;526 }527528 <span class="comment">// Defaults</span>529 <span class="variable">status</span> = <span class="variable">status</span> || <span class="number integer">200</span>;530531 <span class="comment">// Allow 0 args as 204</span>532 <span class="keyword">if</span> (!<span class="variable">arguments</span>.<span class="variable">length</span>) {533 <span class="variable">body</span> = <span class="variable">status</span> = <span class="number integer">204</span>;534 }535536 <span class="comment">// Determine content type</span>537 <span class="keyword">switch</span> (<span class="keyword">typeof</span> <span class="variable">body</span>) {538 <span class="keyword">case</span> <span class="string">'number'</span>:539 <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Type'</span>]) {540 <span class="this">this</span>.<span class="variable">contentType</span>(<span class="string">'.txt'</span>);541 }542 <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>];543 <span class="keyword">break</span>;544 <span class="keyword">case</span> <span class="string">'string'</span>:545 <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Type'</span>]) {546 <span class="this">this</span>.<span class="variable">contentType</span>(<span class="string">'.html'</span>);547 }548 <span class="keyword">break</span>;549 <span class="keyword">case</span> <span class="string">'object'</span>:550 <span class="keyword">if</span> (<span class="variable">body</span> <span class="variable">instanceof</span> <span class="class">Buffer</span>) {551 <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Type'</span>]) {552 <span class="this">this</span>.<span class="variable">contentType</span>(<span class="string">'.bin'</span>);553 }554 } <span class="keyword">else</span> {555 <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Type'</span>]) {556 <span class="this">this</span>.<span class="variable">contentType</span>(<span class="string">'.json'</span>);557 }558 <span class="variable">body</span> = <span class="class">JSON</span>.<span class="variable">stringify</span>(<span class="variable">body</span>);559 <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>]) {560 <span class="this">this</span>.<span class="variable">header</span>(<span class="string">'Content-Type'</span>, <span class="string">'text/javascript'</span>);561 <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>;562 }563 }564 <span class="keyword">break</span>;565 }566567 <span class="comment">// Populate Content-Length</span>568 <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Length'</span>]) {569 <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>570 ? <span class="variable">body</span>.<span class="variable">length</span>571 : <span class="class">Buffer</span>.<span class="variable">byteLength</span>(<span class="variable">body</span>));572 }573574 <span class="comment">// Merge headers passed</span>575 <span class="keyword">if</span> (<span class="variable">headers</span>) {576 <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>);577 <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>) {578 <span class="keyword">var</span> <span class="variable">field</span> = <span class="variable">fields</span>[<span class="variable">i</span>];579 <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>]);580 }581 }582583 <span class="comment">// Strip irrelevant headers</span>584 <span class="keyword">if</span> (<span class="number integer">204</span> === <span class="variable">status</span>) {585 <span class="keyword">delete</span> <span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Type'</span>];586 <span class="keyword">delete</span> <span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Length'</span>];587 }588589 <span class="comment">// Respond</span>590 <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>);591 <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>);592};</code></pre>593</td>594</tr>595<tr class="code">596<td class="docs">597<p>Transfer the file at the given <code>path</code>. Automatically sets 598the <em>Content-Type</em> response header via <code>res.contentType()</code>. </p>599600<p>The given callback <code>fn</code> is invoked when an error occurs,601passing it as the first argument, or when the file is transferred,602passing the path as the second argument.</p>603604<p>When the filesize is >= "stream threshold" (defaulting to 32k), the605file will be streamed using an <code>fs.ReadStream</code> and <code>sys.pump()</code>.</p>606607<h2></h2>608609<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>610</td>611<td class="code">612<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>){613 <span class="keyword">var</span> <span class="variable">self</span> = <span class="this">this</span>614 , <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>615 , <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>;616617 <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>);618619 <span class="keyword">function</span> <span class="variable">error</span>(<span class="variable">err</span>) {620 <span class="keyword">delete</span> <span class="variable">self</span>.<span class="variable">headers</span>[<span class="string">'Content-Disposition'</span>];621 <span class="keyword">if</span> (<span class="variable">fn</span>) {622 <span class="variable">fn</span>(<span class="variable">err</span>, <span class="variable">path</span>);623 } <span class="keyword">else</span> {624 <span class="variable">self</span>.<span class="variable">req</span>.<span class="variable">next</span>(<span class="variable">err</span>);625 }626 }627628 <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>){629 <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>);630 <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>) {631 <span class="keyword">var</span> <span class="variable">status</span> = <span class="number integer">200</span>;632 <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>);633 <span class="keyword">if</span> (<span class="variable">ranges</span>) {634 <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>])635 , <span class="variable">start</span> = <span class="variable">ranges</span>[<span class="number integer">0</span>].<span class="variable">start</span>636 , <span class="variable">end</span> = <span class="variable">ranges</span>[<span class="number integer">0</span>].<span class="variable">end</span>;637 <span class="variable">status</span> = <span class="number integer">206</span>;638 <span class="variable">self</span>.<span class="variable">header</span>(<span class="string">'Content-Range'</span>, <span class="string">'bytes '</span>639 + <span class="variable">start</span>640 + <span class="string">'-'</span>641 + <span class="variable">end</span>642 + <span class="string">'/'</span>643 + <span class="variable">stat</span>.<span class="variable">size</span>);644 } <span class="keyword">else</span> {645 <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>);646 }647 <span class="variable">self</span>.<span class="variable">contentType</span>(<span class="variable">path</span>);648 <span class="variable">self</span>.<span class="variable">header</span>(<span class="string">'Accept-Ranges'</span>, <span class="string">'bytes'</span>);649 <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>);650 <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>){651 <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>);652 });653 } <span class="keyword">else</span> {654 <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>){655 <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>);656 <span class="variable">self</span>.<span class="variable">contentType</span>(<span class="variable">path</span>);657 <span class="variable">self</span>.<span class="variable">send</span>(<span class="variable">buf</span>);658 <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>);659 });660 }661 });662};</code></pre>663</td>664</tr>665<tr class="code">666<td class="docs">667<p>Set <em>Content-Type</em> response header passed through <code>mime.type()</code>.</p>668669<h2>Examples</h2>670671<pre><code>var filename = 'path/to/image.png';672res.contentType(filename);673// res.headers['Content-Type'] is now "image/png"674675res.contentType('.html');676res.contentType('html');677res.contentType('json');678res.contentType('png');</code></pre>679680<h2></h2>681682<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>683</td>684<td class="code">685<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>){686 <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>;687 <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>));688};</code></pre>689</td>690</tr>691<tr class="code">692<td class="docs">693<p>Set <em>Content-Disposition</em> header to <em>attachment</em> with optional <code>filename</code>.</p>694695<h2></h2>696697<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>698</td>699<td class="code">700<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>){701 <span class="this">this</span>.<span class="variable">header</span>(<span class="string">'Content-Disposition'</span>, <span class="variable">filename</span>702 ? <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>703 : <span class="string">'attachment'</span>);704 <span class="keyword">return</span> <span class="this">this</span>;705};</code></pre>706</td>707</tr>708<tr class="code">709<td class="docs">710<p>Transfer the file at the given <code>path</code>, with optional 711<code>filename</code> as an attachment. Once transferred, or if an712error occurs <code>fn</code> is called with the error and path.</p>713714<h2></h2>715716<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>717</td>718<td class="code">719<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>){720 <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>);721};</code></pre>722</td>723</tr>724<tr class="code">725<td class="docs">726<p>Set or get response header <code>name</code> with optional <code>val</code>.</p>727728<p>Headers that may be set multiple times (as indicated by the <code>multiple</code> array)729can be called with a value several times, for example:</p>730731<p> res.header('Set-Cookie', '...');732 res.header('Set-Cookie', '...');</p>733734<h2></h2>735736<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>737</td>738<td class="code">739<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>){740 <span class="keyword">if</span> (<span class="variable">val</span> === <span class="variable">undefined</span>) {741 <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">headers</span>[<span class="variable">name</span>];742 } <span class="keyword">else</span> {743 <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>)) {744 <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>;745 } <span class="keyword">else</span> {746 <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>;747 }748 }749};</code></pre>750</td>751</tr>752<tr class="code">753<td class="docs">754<p>Clear cookie <code>name</code>.</p>755756<h2></h2>757758<ul><li><p><strong>param</strong>: <em>String</em> name</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>759</td>760<td class="code">761<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>){762 <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>) });763};</code></pre>764</td>765</tr>766<tr class="code">767<td class="docs">768<p>Set cookie <code>name</code> to <code>val</code>.</p>769770<h2>Examples</h2>771772<p> // "Remember Me" for 15 minutes773 res.cookie('rememberme', '1', { expires: new Date(Date.now() + 900000), httpOnly: true });</p>774775<h2></h2>776777<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>778</td>779<td class="code">780<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>){781 <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>);782 <span class="this">this</span>.<span class="variable">header</span>(<span class="string">'Set-Cookie'</span>, <span class="variable">cookie</span>);783};</code></pre>784</td>785</tr>786<tr class="code">787<td class="docs">788<p>Redirect to the given <code>url</code> with optional response <code>status</code>789defauling to 302.</p>790791<p>The given <code>url</code> can also be the name of a mapped url, for792example by default express supports "back" which redirects793to the <em>Referrer</em> or <em>Referer</em> headers or the application's794"home" setting. Express also supports "home" out of the box,795which can be set via <code>app.set('home', '/blog');</code>, and defaults796to '/'.</p>797798<h2>Redirect Mapping</h2>799800<p> To extend the redirect mapping capabilities that Express provides,801 we may use the <code>app.redirect()</code> method:</p>802803<pre><code>app.redirect('google', 'http://google.com');</code></pre>804805<p> Now in a route we may call:</p>806807<pre><code>res.redirect('google');</code></pre>808809<p> We may also map dynamic redirects:</p>810811<pre><code> app.redirect('comments', function(req, res){812 return '/post/' + req.params.id + '/comments';813 });</code></pre>814815<p> So now we may do the following, and the redirect will dynamically adjust to816 the context of the request. If we called this route with <em>GET /post/12</em> our817 redirect <em>Location</em> would be <em>/post/12/comments</em>.</p>818819<pre><code> app.get('/post/:id', function(req, res){820 res.redirect('comments');821 });</code></pre>822823<h2></h2>824825<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>826</td>827<td class="code">828<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>){829 <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>830 , <span class="variable">status</span> = <span class="variable">status</span> || <span class="number integer">302</span>831 , <span class="variable">body</span>;832833 <span class="comment">// Setup redirect map</span>834 <span class="keyword">var</span> <span class="variable">map</span> = {835 <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>,836 <span class="variable">home</span>: <span class="variable">basePath</span>837 };838839 <span class="comment">// Support custom redirect map</span>840 <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>;841842 <span class="comment">// Attempt mapped redirect</span>843 <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>844 ? <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>)845 : <span class="variable">map</span>[<span class="variable">url</span>];846847 <span class="comment">// Perform redirect</span>848 <span class="variable">url</span> = <span class="variable">mapped</span> || <span class="variable">url</span>;849850 <span class="comment">// Support text/{plain,html} by default</span>851 <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>)) {852 <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>;853 <span class="this">this</span>.<span class="variable">header</span>(<span class="string">'Content-Type'</span>, <span class="string">'text/html'</span>);854 } <span class="keyword">else</span> {855 <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>;856 <span class="this">this</span>.<span class="variable">header</span>(<span class="string">'Content-Type'</span>, <span class="string">'text/plain'</span>);857 }858859 <span class="comment">// Respond</span>860 <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>);861};862</code></pre>863</td>Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 864why, one step back864</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 865–869why, one step back865<td class="docs">866<p>Module dependencies.867 </p>868</td>869<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 870–874why, one step back870<pre><code><span class="keyword">var</span> <span class="variable">url</span> = <span class="variable">require</span>(<span class="string">'url'</span>)871 , <span class="variable">view</span> = <span class="variable">require</span>(<span class="string">'./view'</span>)872 , <span class="variable">connect</span> = <span class="variable">require</span>(<span class="string">'connect'</span>)873 , <span class="variable">utils</span> = <span class="variable">connect</span>.<span class="variable">utils</span>874 , <span class="variable">queryString</span> = <span class="variable">require</span>(<span class="string">'querystring'</span>)Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 875–879why, one step back875 , <span class="variable">router</span> = <span class="variable">require</span>(<span class="string">'connect'</span>).<span class="variable">router</span>;</code></pre>876</td>877</tr>878<tr class="code">879<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 880why, one step back880<p>Initialize a new <code>Server</code> with optional <code>middleware</code>.</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 881–883why, one step back881882<h2></h2>883Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 884why, one step back884<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 885–886why, one step back885</td>886<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 887why, one step back887<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 888why, one step back888 <span class="keyword">var</span> <span class="variable">self</span> = <span class="this">this</span>;Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 889–896why, one step back889 <span class="this">this</span>.<span class="variable">config</span> = {};890 <span class="this">this</span>.<span class="variable">settings</span> = {};891 <span class="this">this</span>.<span class="variable">redirects</span> = {};892 <span class="this">this</span>.<span class="variable">isCallbacks</span> = {};893 <span class="this">this</span>.<span class="variable">viewHelpers</span> = {};894 <span class="this">this</span>.<span class="variable">dynamicViewHelpers</span> = {};895 <span class="this">this</span>.<span class="variable">errorHandlers</span> = [];896 <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 897why, one step back897Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 898–899why, one step back898 <span class="comment">// Default "home" to / </span>899 <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 900why, one step back900Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 901–902why, one step back901 <span class="comment">// TODO: remove when Connect removes "Server" ...</span>902 <span class="this">this</span>.<span class="variable">showVersion</span> = <span class="variable">false</span>;Added screencasts pageTj Holowaychukbefore adoption4144af3
line 903why, one step back903Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 904–905why, one step back904 <span class="comment">// Set "env" to NODE_ENV, defaulting to "development"</span>905 <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 906why, one step back906Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 907–918why, one step back907 <span class="comment">// Expose objects to each other</span>908 <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>){909 <span class="variable">req</span>.<span class="variable">query</span> = {};910 <span class="variable">res</span>.<span class="variable">headers</span> = { <span class="string">'X-Powered-By'</span>: <span class="string">'Express'</span> };911 <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>;912 <span class="variable">req</span>.<span class="variable">res</span> = <span class="variable">res</span>;913 <span class="variable">res</span>.<span class="variable">req</span> = <span class="variable">req</span>;914 <span class="variable">req</span>.<span class="variable">next</span> = <span class="variable">next</span>;915 <span class="comment">// Assign req.params.get</span>916 <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>) {917 <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>;918 <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 919why, one step back919 }Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 920why, one step back920 <span class="variable">next</span>();Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 921–922why, one step back921 });922Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 923–927why, one step back923 <span class="comment">// Use router, expose as app.get(), etc</span>924 <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>; });925 <span class="this">this</span>.<span class="variable">__defineGetter__</span>(<span class="string">'router'</span>, <span class="keyword">function</span>(){926 <span class="this">this</span>.<span class="variable">__usedRouter</span> = <span class="variable">true</span>;927 <span class="keyword">return</span> <span class="variable">fn</span>;Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 928–938why, one step back928 });929930 <span class="comment">// Default production configuration</span>931 <span class="this">this</span>.<span class="variable">configure</span>(<span class="string">'production'</span>, <span class="keyword">function</span>(){932 <span class="this">this</span>.<span class="variable">enable</span>(<span class="string">'cache views'</span>);933 });934};</code></pre>935</td>936</tr>937<tr class="code">938<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 939why, one step back939<p>Inherit from <code>connect.Server</code>.Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 940–942why, one step back940 </p>941</td>942<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 943why, one step back943<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 944–947why, one step back944</td>945</tr>946<tr class="code">947<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 948why, one step back948<p>Support swappable querystring parsers.Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 949–951why, one step back949 </p>950</td>951<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 952why, one step back952<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 953–960why, one step back953</td>954</tr>955<tr class="code">956<td class="docs">957<p>Proxy in order to register error handlers.958 </p>959</td>960<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 961why, one step back961<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 962why, one step back962 <span class="this">this</span>.<span class="variable">registerErrorHandlers</span>();Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 963why, one step back963 <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 964–972why, one step back964};</code></pre>965</td>966</tr>967<tr class="code">968<td class="docs">969<p>Proxy in order to register error handlers.970 </p>971</td>972<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 973why, one step back973<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 974why, one step back974 <span class="this">this</span>.<span class="variable">registerErrorHandlers</span>();Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 975why, one step back975 <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 976–980why, one step back976};</code></pre>977</td>978</tr>979<tr class="code">980<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 981–982why, one step back981<p>Register error handlers. This is automatically982called from within <code>Server#listen()</code> and <code>Server#listenFD()</code>.</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 983–988why, one step back983984<h2></h2>985986<ul><li><p><strong>return</strong>: <em>Server</em> for chaining</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>987</td>988<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 989–992why, one step back989<pre><code><span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">registerErrorHandlers</span> = <span class="keyword">function</span>(){990 <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>){991 <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>){992 <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 993why, one step back993 });Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 994why, one step back994 }, <span class="this">this</span>);Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 995–1000why, one step back995 <span class="keyword">return</span> <span class="this">this</span>;996};</code></pre>997</td>998</tr>999<tr class="code">1000<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1001–1002why, one step back1001<p>Proxy <code>connect.Server#use()</code> to apply settings to1002mounted applications.</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1003–1005why, one step back10031004<h2></h2>1005Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1006why, one step back1006<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 1007–1008why, one step back1007</td>1008<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1009–1011why, one step back1009<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>){1010 <span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">route</span> !== <span class="string">'string'</span>) {1011 <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 1012–1013why, one step back1012 }1013Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1014why, one step back1014 <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 1015why, one step back1015Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1016–1025why, one step back1016 <span class="comment">// Mounted an app</span>1017 <span class="keyword">if</span> (<span class="variable">middleware</span> <span class="variable">instanceof</span> <span class="class">Server</span>) {1018 <span class="comment">// Home is /:route/:home</span>1019 <span class="keyword">var</span> <span class="variable">app</span> = <span class="variable">middleware</span>1020 , <span class="variable">home</span> = <span class="variable">app</span>.<span class="variable">set</span>(<span class="string">'home'</span>);1021 <span class="keyword">if</span> (<span class="variable">home</span> === <span class="string">'/'</span>) <span class="variable">home</span> = <span class="string">''</span>;1022 <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>);1023 <span class="variable">app</span>.<span class="variable">parent</span> = <span class="this">this</span>;1024 <span class="comment">// Mounted hook</span>1025 <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 1026–1033why, one step back1026 }10271028 <span class="keyword">return</span> <span class="this">this</span>;1029};</code></pre>1030</td>1031</tr>1032<tr class="code">1033<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1034–1035why, one step back1034<p>Assign a callback <code>fn</code> which is called1035when this <code>Server</code> is passed to <code>Server#use()</code>.</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1036–1038why, one step back10361037<h2>Examples</h2>1038Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1039–1040why, one step back1039<p> var app = express.createServer(),1040 blog = express.createServer();</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
line 1041why, one step back1041Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1042–1044why, one step back1042<p> blog.mounted(function(parent){1043 // parent is app1044 // "this" is blogAdded screencasts pageTj Holowaychukbefore adoption4144af3
lines 1045–1046why, one step back1045 });</p>1046Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1047why, one step back1047<p> app.use(blog);</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1048–1053why, one step back10481049<h2></h2>10501051<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>1052</td>1053<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1054–1055why, one step back1054<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>){1055 <span class="this">this</span>.<span class="variable">__mounted</span> = <span class="variable">fn</span>;Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1056–1061why, one step back1056 <span class="keyword">return</span> <span class="this">this</span>;1057};</code></pre>1058</td>1059</tr>1060<tr class="code">1061<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1062why, one step back1062<h2>See view.register.</h2>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1063–1068why, one step back10631064<h2></h2>10651066<ul><li><p><strong>return</strong>: <em>Server</em> for chaining</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>1067</td>1068<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1069–1070why, one step back1069<pre><code><span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">register</span> = <span class="keyword">function</span>(){1070 <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 1071–1076why, one step back1071 <span class="keyword">return</span> <span class="this">this</span>;1072};</code></pre>1073</td>1074</tr>1075<tr class="code">1076<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1077why, one step back1077<p>Register the given view helpers <code>obj</code>. This methodAdded screencasts pageTj Holowaychukbefore adoption4144af3
lines 1078–1086why, one step back1078can be called several times to apply additional helpers.</p>10791080<h2></h2>10811082<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>1083</td>1084<td class="code">1085<pre><code><span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">helpers</span> = 1086<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 1087why, one step back1087 <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 1088–1093why, one step back1088 <span class="keyword">return</span> <span class="this">this</span>;1089};</code></pre>1090</td>1091</tr>1092<tr class="code">1093<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1094why, one step back1094<p>Register the given dynamic view helpers <code>obj</code>. This methodAdded screencasts pageTj Holowaychukbefore adoption4144af3
lines 1095–1101why, one step back1095can be called several times to apply additional helpers.</p>10961097<h2></h2>10981099<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>1100</td>1101<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1102–1103why, one step back1102<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>){1103 <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 1104–1165why, one step back1104 <span class="keyword">return</span> <span class="this">this</span>;1105};</code></pre>1106</td>1107</tr>1108<tr class="code">1109<td class="docs">1110<p>Map the given param placeholder <code>name</code>(s) to the given callback <code>fn</code>.</p>11111112<p>Param mapping is used to provide pre-conditions to routes1113which us normalized placeholders. For example ":user_id" may1114attempt to load the user from the database, where as ":num" may 1115pass the value through <code>parseInt(num, 10)</code>.</p>11161117<p>When the callback function accepts only a single argument, the1118value of placeholder is passed:</p>11191120<p> app.param('page', function(n){ return parseInt(n, 10); });</p>11211122<p>After which "/users/:page" would automatically provide us with1123an integer for <code>req.params.page</code>. If desired we could use the callback 1124signature shown below, and immediately <code>next(new Error('invalid page'))</code>1125when <code>parseInt</code> fails.</p>11261127<p>Alternatively the callback may accept the request, response, next, and1128the value, acting like middlware:</p>11291130<p> app.param('userId', function(req, res, next, id){1131 User.find(id, function(err, user){1132 if (err) {1133 next(err);1134 } else if (user) {1135 req.user = user;1136 next();1137 } else {1138 next(new Error('failed to load user'));1139 }1140 });1141 });</p>11421143<p>Now every time ":userId" is present, the associated user object1144will be loaded and assigned before the route handler is invoked.</p>11451146<h2></h2>11471148<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>1149</td>1150<td class="code">1151<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>){1152 <span class="keyword">if</span> (<span class="class">Array</span>.<span class="variable">isArray</span>(<span class="variable">name</span>)) {1153 <span class="variable">name</span>.<span class="variable">forEach</span>(<span class="keyword">function</span>(<span class="variable">name</span>){1154 <span class="this">this</span>.<span class="variable">param</span>(<span class="variable">name</span>, <span class="variable">fn</span>);1155 }, <span class="this">this</span>);1156 } <span class="keyword">else</span> {1157 <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>);1158 <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>);1159 }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">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1166–1167why, one step back1166<p>Assign a custom exception handler callback <code>fn</code>.1167These handlers are always <em>last</em> in the middleware stack.</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1168–1173why, one step back11681169<h2></h2>11701171<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>1172</td>1173<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1174–1175why, one step back1174<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>){1175 <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 1176–1181why, one step back1176 <span class="keyword">return</span> <span class="this">this</span>;1177};</code></pre>1178</td>1179</tr>1180<tr class="code">1181<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1182why, one step back1182<p>Register the given callback <code>fn</code> for the given <code>type</code>.</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1183–1185why, one step back11831184<h2></h2>1185Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1186why, one step back1186<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 1187–1188why, one step back1187</td>1188<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1189–1191why, one step back1189<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>){1190 <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>];1191 <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 1192–1197why, one step back1192 <span class="keyword">return</span> <span class="this">this</span>;1193};</code></pre>1194</td>1195</tr>1196<tr class="code">1197<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1198–1199why, one step back1198<p>Assign <code>setting</code> to <code>val</code>, or return <code>setting</code>'s value.1199Mounted servers inherit their parent server's settings.</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1200–1202why, one step back12001201<h2></h2>1202Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1203why, one step back1203<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 1204–1205why, one step back1204</td>1205<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1206why, one step back1206<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 1207why, one step back1207 <span class="keyword">if</span> (<span class="variable">val</span> === <span class="variable">undefined</span>) {Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1208–1211why, one step back1208 <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>)) {1209 <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">settings</span>[<span class="variable">setting</span>];1210 } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">parent</span>) {1211 <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 1212–1213why, one step back1212 }1213 } <span class="keyword">else</span> {Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1214why, one step back1214 <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 1215–1221why, one step back1215 <span class="keyword">return</span> <span class="this">this</span>;1216 }1217};</code></pre>1218</td>1219</tr>1220<tr class="code">1221<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1222why, one step back1222<p>Enable <code>setting</code>.</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1223–1228why, one step back12231224<h2></h2>12251226<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>1227</td>1228<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1229–1230why, one step back1229<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>){1230 <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 1231–1235why, one step back1231};</code></pre>1232</td>1233</tr>1234<tr class="code">1235<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1236why, one step back1236<p>Disable <code>setting</code>.</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1237–1242why, one step back12371238<h2></h2>12391240<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>1241</td>1242<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1243–1244why, one step back1243<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>){1244 <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 1245–1249why, one step back1245};</code></pre>1246</td>1247</tr>1248<tr class="code">1249<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1250why, one step back1250<p>Redirect <code>key</code> to <code>url</code>.</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1251–1253why, one step back12511252<h2></h2>1253Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1254why, one step back1254<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 1255–1256why, one step back1255</td>1256<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1257–1258why, one step back1257<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>){1258 <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 1259–1264why, one step back1259 <span class="keyword">return</span> <span class="this">this</span>;1260};</code></pre>1261</td>1262</tr>1263<tr class="code">1264<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1265why, one step back1265<p>Configure callback for the given <code>env</code>.</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1266–1268why, one step back12661267<h2></h2>1268Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1269why, one step back1269<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 1270–1271why, one step back1270</td>1271<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1272–1274why, one step back1272<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>){1273 <span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">env</span> === <span class="string">'function'</span>) {1274 <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 1275–1276why, one step back1275 }1276 <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 1277why, one step back1277 <span class="variable">fn</span>.<span class="variable">call</span>(<span class="this">this</span>);Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1278–1281why, one step back1278 }1279 <span class="keyword">return</span> <span class="this">this</span>;1280};1281Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1282why, one step back1282<span class="comment">// Generate routing methods</span>Added screencasts pageTj Holowaychukbefore adoption4144af3
line 1283why, one step back1283Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1284–1286why, one step back1284(<span class="keyword">function</span>(<span class="variable">method</span>){1285 <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>){1286 <span class="keyword">var</span> <span class="variable">self</span> = <span class="this">this</span>;Added screencasts pageTj Holowaychukbefore adoption4144af3
line 1287why, one step back1287Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1288–1290why, one step back1288 <span class="comment">// Ensure router is mounted</span>1289 <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">__usedRouter</span>) {1290 <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 1291–1292why, one step back1291 }1292Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1293–1305why, one step back1293 <span class="comment">// Route specific middleware support</span>1294 <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>) {1295 <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>);1296 <span class="variable">fn</span> = <span class="variable">args</span>.<span class="variable">pop</span>();1297 (<span class="keyword">function</span> <span class="variable">stack</span>(<span class="variable">middleware</span>){1298 <span class="variable">middleware</span>.<span class="variable">forEach</span>(<span class="keyword">function</span>(<span class="variable">fn</span>){1299 <span class="keyword">if</span> (<span class="class">Array</span>.<span class="variable">isArray</span>(<span class="variable">fn</span>)) {1300 <span class="variable">stack</span>(<span class="variable">fn</span>);1301 } <span class="keyword">else</span> {1302 <span class="variable">self</span>[<span class="variable">method</span>](<span class="variable">path</span>, <span class="variable">fn</span>);1303 }1304 });1305 })(<span class="variable">args</span>);Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1306–1307why, one step back1306 }1307Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1308–1309why, one step back1308 <span class="comment">// Generate the route</span>1309 <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 1310–1311why, one step back1310 <span class="keyword">return</span> <span class="this">this</span>;1311 };Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1312–1313why, one step back1312 <span class="keyword">return</span> <span class="variable">arguments</span>.<span class="variable">callee</span>;1313})(<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 1314why, one step back1314Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1315why, one step back1315<span class="comment">// Alias delete as "del"</span>Added screencasts pageTj Holowaychukbefore adoption4144af3
line 1316why, one step back1316Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1317why, one step back1317<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 1318–1319why, one step back1318</code></pre>1319</td>Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1320why, one step back1320</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 1321–1391why, one step back1321<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="variable">path</span> = <span class="variable">require</span>(<span class="string">'path'</span>)1327 , <span class="variable">basename</span> = <span class="variable">path</span>.<span class="variable">basename</span>1328 , <span class="variable">dirname</span> = <span class="variable">path</span>.<span class="variable">dirname</span>1329 , <span class="variable">extname</span> = <span class="variable">path</span>.<span class="variable">extname</span>;</code></pre>1330</td>1331</tr>1332<tr class="code">1333<td class="docs">1334<p>Memory cache.1335 </p>1336</td>1337<td class="code">1338<pre><code><span class="keyword">var</span> <span class="variable">cache</span> = {1339 <span class="variable">basename</span>: {}1340 , <span class="variable">dirname</span>: {}1341 , <span class="variable">extname</span>: {}1342};</code></pre>1343</td>1344</tr>1345<tr class="code">1346<td class="docs">1347<p>Cached basename.</p>13481349<h2></h2>13501351<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>1352</td>1353<td class="code">1354<pre><code><span class="variable">exports</span>.<span class="variable">basename</span> = <span class="keyword">function</span>(<span class="variable">path</span>){1355 <span class="keyword">return</span> <span class="variable">cache</span>.<span class="variable">basename</span>[<span class="variable">path</span>]1356 || (<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>));1357};</code></pre>1358</td>1359</tr>1360<tr class="code">1361<td class="docs">1362<p>Cached dirname.</p>13631364<h2></h2>13651366<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>1367</td>1368<td class="code">1369<pre><code><span class="variable">exports</span>.<span class="variable">dirname</span> = <span class="keyword">function</span>(<span class="variable">path</span>){1370 <span class="keyword">return</span> <span class="variable">cache</span>.<span class="variable">dirname</span>[<span class="variable">path</span>]1371 || (<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>));1372};</code></pre>1373</td>1374</tr>1375<tr class="code">1376<td class="docs">1377<p>Cached extname.</p>13781379<h2></h2>13801381<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>1382</td>1383<td class="code">1384<pre><code><span class="variable">exports</span>.<span class="variable">extname</span> = <span class="keyword">function</span>(<span class="variable">path</span>){1385 <span class="keyword">return</span> <span class="variable">cache</span>.<span class="variable">extname</span>[<span class="variable">path</span>]1386 || (<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>));1387};</code></pre>1388</td>1389</tr>1390<tr class="code">1391<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1392–1394why, one step back1392<p>Parse mini markdown implementation.1393The following conversions are supported,1394primarily for the "flash" middleware:</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
line 1395why, one step back1395Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1396–1398why, one step back1396<p> <em>foo</em> or <em>foo</em> become <em>foo</em>1397 <strong>foo</strong> or <strong>foo</strong> become <strong>foo</strong>1398 <a href="B">A</a> becomes <a href="B">A</a></p>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1399–1401why, one step back13991400<h2></h2>1401Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1402why, one step back1402<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 1403–1404why, one step back1403</td>1404<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1405–1409why, one step back1405<pre><code><span class="variable">exports</span>.<span class="variable">miniMarkdown</span> = <span class="keyword">function</span>(<span class="variable">str</span>){1406 <span class="keyword">return</span> <span class="class">String</span>(<span class="variable">str</span>)1407 .<span class="variable">replace</span>(<span class="regexp">/(__|\*\*)(.*?)\1/g</span>, <span class="string">'<strong>$2</strong>'</span>)1408 .<span class="variable">replace</span>(<span class="regexp">/(_|\*)(.*?)\1/g</span>, <span class="string">'<em>$2</em>'</span>)1409 .<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 1410–1414why, one step back1410};</code></pre>1411</td>1412</tr>1413<tr class="code">1414<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1415why, one step back1415<p>Escape special characters in the given string of html.</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1416–1418why, one step back14161417<h2></h2>1418Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1419why, one step back1419<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 1420–1421why, one step back1420</td>1421<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1422–1427why, one step back1422<pre><code><span class="variable">exports</span>.<span class="variable">htmlEscape</span> = <span class="keyword">function</span>(<span class="variable">html</span>) {1423 <span class="keyword">return</span> <span class="class">String</span>(<span class="variable">html</span>)1424 .<span class="variable">replace</span>(<span class="regexp">/&/g</span>, <span class="string">'&'</span>)1425 .<span class="variable">replace</span>(<span class="regexp">/"/g</span>, <span class="string">'"'</span>)1426 .<span class="variable">replace</span>(<span class="regexp">/</g</span>, <span class="string">'<'</span>)1427 .<span class="variable">replace</span>(<span class="regexp">/>/g</span>, <span class="string">'>'</span>);Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1428–1450why, one step back1428};</code></pre>1429</td>1430</tr>1431<tr class="code">1432<td class="docs">1433<p>Clone <code>obj</code>.</p>14341435<h2></h2>14361437<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>1438</td>1439<td class="code">1440<pre><code><span class="variable">exports</span>.<span class="variable">clone</span> = <span class="keyword">function</span>(<span class="variable">obj</span>){1441 <span class="keyword">var</span> <span class="variable">ret</span> = {};1442 <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>) {1443 <span class="variable">ret</span>[<span class="variable">key</span>] = <span class="variable">obj</span>[<span class="variable">key</span>];1444 }1445 <span class="keyword">return</span> <span class="variable">ret</span>;1446};</code></pre>1447</td>1448</tr>1449<tr class="code">1450<td class="docs">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1451why, one step back1451<p>Parse "Range" header <code>str</code> relative to the given file <code>size</code>.</p>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1452–1454why, one step back14521453<h2></h2>1454Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1455why, one step back1455<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 1456–1457why, one step back1456</td>1457<td class="code">Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1458–1463why, one step back1458<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>){1459 <span class="keyword">var</span> <span class="variable">valid</span> = <span class="variable">true</span>;1460 <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>){1461 <span class="keyword">var</span> <span class="variable">range</span> = <span class="variable">range</span>.<span class="variable">split</span>(<span class="string">'-'</span>)1462 , <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>)1463 , <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 1464why, one step back1464Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1465–1467why, one step back1465 <span class="comment">// -500</span>1466 <span class="keyword">if</span> (<span class="variable">isNaN</span>(<span class="variable">start</span>)) {1467 <span class="variable">start</span> = <span class="variable">size</span> - <span class="variable">end</span>;Added screencasts pageTj Holowaychukbefore adoption4144af3
line 1468why, one step back1468 <span class="variable">end</span> = <span class="variable">size</span> - <span class="number integer">1</span>;Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1469–1470why, one step back1469 <span class="comment">// 500-</span>1470 } <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 1471–1473why, one step back1471 <span class="variable">end</span> = <span class="variable">size</span> - <span class="number integer">1</span>;1472 }1473Rebuilt docsTj Holowaychukbefore adoptiond017c3e
lines 1474–1475why, one step back1474 <span class="comment">// Invalid</span>1475 <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 1476why, one step back1476Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1477why, one step back1477 <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 1478why, one step back1478 });Rebuilt docsTj Holowaychukbefore adoptiond017c3e
line 1479why, one step back1479 <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 1480–1481why, one step back1480};1481</code></pre>Regenerated docsTj Holowaychukbefore adoptiond3abcaf
lines 1482–1553why, one step back1482</td>1483</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">1484<td class="docs">1485<p>Module dependencies.1486 </p>1487</td>1488<td class="code">1489<pre><code><span class="keyword">var</span> <span class="class">View</span> = <span class="variable">require</span>(<span class="string">'./view'</span>)1490 , <span class="variable">basename</span> = <span class="variable">require</span>(<span class="string">'path'</span>).<span class="variable">basename</span>;</code></pre>1491</td>1492</tr>1493<tr class="code">1494<td class="docs">1495<p>Memory cache.1496 </p>1497</td>1498<td class="code">1499<pre><code><span class="keyword">var</span> <span class="variable">cache</span> = {};</code></pre>1500</td>1501</tr>1502<tr class="code">1503<td class="docs">1504<p>Initialize a new <code>Partial</code> for the given <code>view</code> and <code>options</code>.</p>15051506<h2></h2>15071508<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>1509</td>1510<td class="code">1511<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>) {1512 <span class="variable">options</span> = <span class="variable">options</span> || {};1513 <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>);1514};</code></pre>1515</td>1516</tr>1517<tr class="code">1518<td class="docs">1519<p>Inherit from <code>View.prototype</code>.1520 </p>1521</td>1522<td class="code">1523<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>1524</td>1525</tr>1526<tr class="code">1527<td class="docs">1528<p>Resolve partial object name from the view path.</p>15291530<h2>Examples</h2>15311532<p> "user.ejs" becomes "user"1533 "forum thread.ejs" becomes "forumThread"1534 "forum/thread/post.ejs" becomes "post"1535 "blog-post.ejs" becomes "blogPost"</p>15361537<h2></h2>15381539<ul><li><p><strong>return</strong>: <em>String</em> </p></li><li><p><strong>api</strong>: <em>private</em></p></li></ul>1540</td>1541<td class="code">1542<pre><code><span class="variable">exports</span>.<span class="variable">resolveObjectName</span> = <span class="keyword">function</span>(<span class="variable">view</span>){1543 <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>1544 .<span class="variable">split</span>(<span class="string">'/'</span>)1545 .<span class="variable">slice</span>(-<span class="number integer">1</span>)[<span class="number integer">0</span>]1546 .<span class="variable">split</span>(<span class="string">'.'</span>)[<span class="number integer">0</span>]1547 .<span class="variable">replace</span>(<span class="regexp">/[^a-zA-Z0-9 ]+/g</span>, <span class="string">' '</span>)1548 .<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>){1549 <span class="keyword">return</span> <span class="variable">i</span>1550 ? <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>)1551 : <span class="variable">word</span>;1552 }).<span class="variable">join</span>(<span class="string">''</span>));1553};</code></pre>Added screencasts pageTj Holowaychukbefore adoption4144af3
lines 1554–1564why, one step back1554</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.