api.html
This file
Path
docs/api.html
1,089 lines · 56.6 KiB · HTML
Last changed by
Added configuration sectionTj Holowaychukbefore adoption83d2555
History of this fileTake it
Generated docsTj Holowaychukbefore adoptionbafa300
lines 1–138why, one step back1<html>2 <head>3 <title>Express</title>4 <style>body {5 margin: 0;6 padding: 0;7 font: 14px/1.5 'Palatino Linotype', 'Book Antiqua', Palatino, FreeSerif, serif;8 color: #252519;9}10a {11 color: #252519;12}13a:hover {14 text-decoration: underline;15 color: #19469D;16}17p {18 margin: 12px 0;19}20h1, h2, h3 {21 margin: 0;22 padding: 0;23}24ul#menu {25 opacity: 0;26 position: fixed;27 top: 0;28 right: 0;29 list-style: none;30 margin: 0;31 padding: 10px 2px;32 text-align: right;33 background: rgba(255,255,255,0.7);34 -webkit-box-shadow: -1px 2px 2px rgba(0,0,0,0.3);35 -moz-box-shadow: -1px 2px 2px rgba(0,0,0,0.3);36 -webkit-border-bottom-left-radius: 5px;37 -moz-border-bottom-left-radius: 5px;38 -webkit-transition-properties: opacity;39 -webkit-transition-duration: 0.6s;40}41ul#menu:hover {42 opacity: 1;43}44ul#menu li {45 padding: 2px 10px;46 border-bottom: 1px solid #eee;47}48ul#menu li:hover {49 background: rgba(0,0,0,0.03);50}51ul#menu li:last-child {52 border-bottom: none;53}54ul#menu li a {55 text-decoration: none;56 font-size: 12px;57 color: rgba(0,0,0,0.5);58}59ul#menu li a strong {60 font-weight: normal;61 color: #252519;62}63table#source {64 width: 100%;65 border-collapse: collapse;66}67table#source td:first-child {68 padding: 30px 40px 30px 40px;69 vertical-align: top;70}71table#source td:first-child,72table#source td:first-child pre {73 width: 450px;74}75table#source td:last-child {76 padding: 30px 0 30px 40px;77 border-left: 1px solid #E5E5EE;78 background: #F5F5FF;79}80table#source tr {81 border-bottom: 1px solid #E5E5EE;82}83table#source tr.filename {84 padding-top: 40px;85 border-top: 1px solid #E5E5EE;86}87table#source tr.filename td:first-child {88 text-transform: capitalize;89}90table#source tr.filename td:last-child {91 font-size: 12px;92}93table#source tr.filename h2 {94 margin: 0;95 padding: 0;96}97table#source tr.code h1,98table#source tr.code h2,99table#source tr.code h3 {100 margin-top: 30px;101 font-family: "Lucida Grande", "Helvetica Nueue", Arial, sans-serif;102 font-size: 18px;103}104table#source tr.code h2 {105 font-size: 16px;106}107table#source tr.code h3 {108 font-size: 14px;109}110table#source tr.code ul {111 margin: 15px 0 15px 35px;112 padding: 0;113}114table#source tr.code ul li {115 margin: 0;116 padding: 1px 0;117}118table#source tr.code ul li p {119 margin: 0;120 padding: 0;121}122table#source tr.code td:first-child pre {123 padding: 20px;124}125#ribbon {126 position: absolute;127 top: 0;128 right: 0;129}130code .string { color: #219161; }131code .regexp { color: #219161; }132code .keyword { color: #954121; }133code .number { color: #19469D; }134code .comment { color: #bbb; }135code .this { color: #19469D; }</style>136 </head>137 <body>138<ul id="menu"><li><a href="#lib/express/index.js">lib/express/<strong>index</strong></a></li>Updated docsTj Holowaychukbefore adoption09dabef
line 139why, one step back139<li><a href="#lib/express/request.js">lib/express/<strong>request</strong></a></li>Generated docsTj Holowaychukbefore adoptionbafa300
lines 140–141why, one step back140<li><a href="#lib/express/response.js">lib/express/<strong>response</strong></a></li>141<li><a href="#lib/express/server.js">lib/express/<strong>server</strong></a></li>Started migration docsTj Holowaychukbefore adoption9e68a5b
line 142why, one step back142<li><a href="#lib/express/utils.js">lib/express/<strong>utils</strong></a></li>Updated docsTj Holowaychukbefore adoption09dabef
line 143why, one step back143<li><a href="#lib/express/view.js">lib/express/<strong>view</strong></a></li></ul><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">index</h2></td><td>lib/express/index.js</td></tr><tr class="code">Generated docsTj Holowaychukbefore adoptionbafa300
line 144why, one step back144<td class="docs">Updated docsTj Holowaychukbefore adoption09dabef
lines 145–152why, one step back145<p>Framework version.</p>146</td>147<td class="code">148<pre><code><span class="variable">exports</span>.<span class="variable">version</span> = <span class="string">'0.14.0'</span>;</code></pre>149</td>150</tr>151<tr class="code">152<td class="docs">Generated docsTj Holowaychukbefore adoptionbafa300
lines 153–155why, one step back153<p>Module dependencies.</p>154</td>155<td class="code">Updated docsTj Holowaychukbefore adoption09dabef
lines 156–187why, one step back156<pre><code><span class="keyword">var</span> <span class="class">Server</span> = <span class="variable">require</span>(<span class="string">'./server'</span>);</code></pre>157</td>158</tr>159<tr class="code">160<td class="docs">161<p>Shortcut for <code>new Server(...)</code>.</p>162163<h2></h2>164165<ul>166<li><p><strong>param</strong>: <em>Function</em> …</p></li>167<li><p><strong>return</strong>: <em>Server</em></p></li>168<li><p><strong>api</strong>: <em>public</em></p></li>169</ul>170171</td>172<td class="code">173<pre><code><span class="variable">exports</span>.<span class="variable">createServer</span> = <span class="keyword">function</span>(){174 <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>));175};</code></pre>176</td>177</tr>178<tr class="code">179<td class="docs">180<p>View extensions.</p>181</td>182<td class="code">183<pre><code><span class="variable">require</span>(<span class="string">'./view'</span>);184<span class="variable">require</span>(<span class="string">'./request'</span>);185<span class="variable">require</span>(<span class="string">'./response'</span>);186</code></pre>187</td>Fixed bannerTj Holowaychukbefore adoption2fda5c7
lines 188–344why, one step back188</tr><tr class="filename"><td><h2 id="lib/express/response.js">response</h2></td><td>lib/express/response.js</td></tr><tr class="code">189<td class="docs">190<p>Module dependencies.</p>191</td>192<td class="code">193<pre><code><span class="keyword">var</span> <span class="variable">http</span> = <span class="variable">require</span>(<span class="string">'http'</span>),194 <span class="variable">path</span> = <span class="variable">require</span>(<span class="string">'path'</span>),195 <span class="variable">utils</span> = <span class="variable">require</span>(<span class="string">'connect/utils'</span>),196 <span class="variable">mime</span> = <span class="variable">require</span>(<span class="string">'connect/utils'</span>).<span class="variable">mime</span>,197 <span class="class">Buffer</span> = <span class="variable">require</span>(<span class="string">'buffer'</span>).<span class="class">Buffer</span>;</code></pre>198</td>199</tr>200<tr class="code">201<td class="docs">202<p>Send a response with the given <code>body</code> and optional <code>headers</code> and <code>status</code> code.</p>203204<h2>Examples</h2>205206<pre><code>res.send(new Buffer('wahoo'));207res.send({ some: 'json' });208res.send('<p>some html</p>');209res.send('Sorry, cant find that', 404);210res.send('text', { 'Content-Type': 'text/plain' }, 201);211res.send(404);212</code></pre>213214<h2></h2>215216<ul>217<li><p><strong>param</strong>: <em>String | Object | Number | Buffer</em> body or status</p></li>218<li><p><strong>param</strong>: <em>Object | Number</em> headers or status</p></li>219<li><p><strong>param</strong>: <em>Number</em> status</p></li>220<li><p><strong>return</strong>: <em>ServerResponse</em></p></li>221<li><p><strong>api</strong>: <em>public</em></p></li>222</ul>223224</td>225<td class="code">226<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>){227 <span class="comment">// Allow status as second arg</span>228 <span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">headers</span> === <span class="string">'number'</span>) {229 <span class="variable">status</span> = <span class="variable">headers</span>,230 <span class="variable">headers</span> = <span class="keyword">null</span>;231 }232233 <span class="comment">// Defaults</span>234 <span class="variable">status</span> = <span class="variable">status</span> || <span class="number integer">200</span>;235 <span class="variable">headers</span> = <span class="variable">headers</span> || {};236237 <span class="comment">// Determine content type</span>238 <span class="keyword">switch</span> (<span class="keyword">typeof</span> <span class="variable">body</span>) {239 <span class="keyword">case</span> <span class="string">'number'</span>:240 <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Type'</span>]) {241 <span class="this">this</span>.<span class="variable">contentType</span>(<span class="string">'.txt'</span>);242 }243 <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>];244 <span class="keyword">break</span>;245 <span class="keyword">case</span> <span class="string">'string'</span>:246 <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Type'</span>]) {247 <span class="this">this</span>.<span class="variable">contentType</span>(<span class="string">'.html'</span>);248 }249 <span class="keyword">break</span>;250 <span class="keyword">case</span> <span class="string">'object'</span>:251 <span class="keyword">if</span> (!(<span class="variable">body</span> <span class="variable">instanceof</span> <span class="class">Buffer</span>)) {252 <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Type'</span>]) {253 <span class="this">this</span>.<span class="variable">contentType</span>(<span class="string">'.json'</span>);254 }255 <span class="variable">body</span> = <span class="class">JSON</span>.<span class="variable">stringify</span>(<span class="variable">body</span>);256 }257 <span class="keyword">break</span>;258 }259260 <span class="comment">// Populate Content-Length</span>261 <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Length'</span>]) {262 <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>263 ? <span class="variable">body</span>.<span class="variable">length</span>264 : <span class="class">Buffer</span>.<span class="variable">byteLength</span>(<span class="variable">body</span>));265 }266267 <span class="comment">// Merge headers passed</span>268 <span class="variable">utils</span>.<span class="variable">merge</span>(<span class="this">this</span>.<span class="variable">headers</span>, <span class="variable">headers</span>);269270 <span class="comment">// Respond</span>271 <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>);272 <span class="this">this</span>.<span class="variable">end</span>(<span class="variable">body</span>);273};</code></pre>274</td>275</tr>276<tr class="code">277<td class="docs">278<p>Set <em>Content-Type</em> response header passed through <code>mime.type()</code>.</p>279280<h2>Examples</h2>281282<pre><code>var filename = 'path/to/image.png';283res.contentType(filename);284// res.headers['Content-Type'] is now "image/png"285</code></pre>286287<h2></h2>288289<ul>290<li><p><strong>param</strong>: <em>String</em> type</p></li>291<li><p><strong>return</strong>: <em>String</em> the resolved mime type</p></li>292<li><p><strong>api</strong>: <em>public</em></p></li>293</ul>294295</td>296<td class="code">297<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>){298 <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>));299};</code></pre>300</td>301</tr>302<tr class="code">303<td class="docs">304<p>Set <em>Content-Disposition</em> header to <em>attachment</em> with optional <code>filename</code>.</p>305306<h2></h2>307308<ul>309<li><p><strong>param</strong>: <em>String</em> filename</p></li>310<li><p><strong>return</strong>: <em>ServerResponse</em></p></li>311<li><p><strong>api</strong>: <em>public</em></p></li>312</ul>313314</td>315<td class="code">316<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>){317 <span class="this">this</span>.<span class="variable">header</span>(<span class="string">'Content-Disposition'</span>, <span class="variable">filename</span>318 ? <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>319 : <span class="string">'attachment'</span>);320 <span class="keyword">return</span> <span class="this">this</span>;321};</code></pre>322</td>323</tr>324<tr class="code">325<td class="docs">326<p>Set or get response header <code>name</code> with optional <code>val</code>.</p>327328<h2></h2>329330<ul>331<li><p><strong>param</strong>: <em>String</em> name</p></li>332<li><p><strong>param</strong>: <em>String</em> val</p></li>333<li><p><strong>return</strong>: <em>String</em></p></li>334<li><p><strong>api</strong>: <em>public</em></p></li>335</ul>336337</td>338<td class="code">339<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>){340 <span class="keyword">return</span> <span class="variable">val</span> === <span class="variable">undefined</span>341 ? <span class="this">this</span>.<span class="variable">headers</span>[<span class="variable">name</span>]342 : <span class="this">this</span>.<span class="variable">headers</span>[<span class="variable">name</span>] = <span class="variable">val</span>;343};</code></pre>344</td>Updated docsTj Holowaychukbefore adoption09dabef
lines 345–349why, one step back345</tr><tr class="filename"><td><h2 id="lib/express/request.js">request</h2></td><td>lib/express/request.js</td></tr><tr class="code">346<td class="docs">347<p>Module dependencies.</p>348</td>349<td class="code">Generated docsTj Holowaychukbefore adoptionbafa300
line 350why, one step back350<pre><code><span class="keyword">var</span> <span class="variable">http</span> = <span class="variable">require</span>(<span class="string">'http'</span>),Started migration docsTj Holowaychukbefore adoption9e68a5b
line 351why, one step back351 <span class="variable">utils</span> = <span class="variable">require</span>(<span class="string">'./utils'</span>),Updated docsTj Holowaychukbefore adoption09dabef
lines 352–477why, one step back352 <span class="variable">mime</span> = <span class="variable">require</span>(<span class="string">'connect/utils'</span>).<span class="variable">mime</span>;</code></pre>353</td>354</tr>355<tr class="code">356<td class="docs">357<p>Return request header or optional default.</p>358359<h2>Examples</h2>360361<pre><code>req.header('Content-Type');362// => "text/plain"363364req.header('content-type');365// => "text/plain"366367req.header('Accept');368// => undefined369370req.header('Accept', 'text/html');371// => "text/html"372</code></pre>373374<h2></h2>375376<ul>377<li><p><strong>param</strong>: <em>String</em> name</p></li>378<li><p><strong>param</strong>: <em>String</em> defaultValue</p></li>379<li><p><strong>return</strong>: <em>String</em></p></li>380<li><p><strong>api</strong>: <em>public</em></p></li>381</ul>382383</td>384<td class="code">385<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>){386 <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>;387};</code></pre>388</td>389</tr>390<tr class="code">391<td class="docs">392<p>Check if the <em>Accept</em> header is present, and includes the given <code>type</code>.</p>393394<p>When the <em>Accept</em> header is not present <code>true</code> is returned. Otherwise395the given <code>type</code> is matched by an exact match, and then subtypes. You396may pass the subtype such as “html” which is then converted internally397to “text/html” using the mime lookup table.</p>398399<h2>Examples</h2>400401<pre><code>// Accept: text/html402req.accepts('html');403// => true404405// Accept: text/*; application/json406req.accepts('html');407req.accepts('text/html');408req.accepts('text/plain');409req.accepts('application/json');410// => true411412req.accepts('image/png');413req.accepts('png');414// => false415</code></pre>416417<h2></h2>418419<ul>420<li><p><strong>param</strong>: <em>String</em> type</p></li>421<li><p><strong>return</strong>: <em>Boolean</em></p></li>422<li><p><strong>api</strong>: <em>public</em></p></li>423</ul>424425</td>426<td class="code">427<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>){428 <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>);429 <span class="keyword">if</span> (!<span class="variable">accept</span> || <span class="variable">accept</span> === '*</code></pre>430</td>431</tr>432<tr class="code">433<td class="docs">434<p>‘) {</p>435436<pre><code> return true;437} else if (type) {438 // Allow "html" vs "text/html" etc439 if (type.indexOf('/') < 0) {440 type = mime.types['.' + type];441 }442 // Check if we have a direct match443 if (accept.indexOf(type) >= 0) {444 return true;445 // Check if we have type/*446 } else {447 type = type.split('/')[0] + '/*';448 return accept.indexOf(type) >= 0;449 }450} else {451 return false;452}453</code></pre>454455<p>};</p>456457<p>/**458Return the value of param <code>name</code> when present.</p>459460<ul>461<li>Checks route placeholders, ex: <em>/user/:id</em></li>462<li>Checks query string params, ex: ?id=12</li>463<li>Checks urlencoded body params, ex: id=12</li>464</ul>465466467<p>To utilize urlencoded request bodies, <code>req.body</code>468should be an object. This can be done by using469the <code>connect.bodyDecoder</code> middleware.</p>470471<h2></h2>472473<ul>474<li><p><strong>param</strong>: <em>String</em> name</p></li>475<li><p><strong>return</strong>: <em>String</em></p></li>476<li><p><strong>api</strong>: <em>public</em></p></li>477</ul>Added menu to docsTj Holowaychukbefore adoptionac560be
line 478why, one step back478Updated docsTj Holowaychukbefore adoption09dabef
lines 479–493why, one step back479</td>480<td class="code">481<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>){482 <span class="comment">// Route params like /user/:id</span>483 <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">params</span>.<span class="variable">path</span>[<span class="variable">name</span>] !== <span class="variable">undefined</span>) {484 <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">params</span>.<span class="variable">path</span>[<span class="variable">name</span>]; 485 }486 <span class="comment">// Query string params</span>487 <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">params</span>.<span class="variable">get</span>[<span class="variable">name</span>] !== <span class="variable">undefined</span>) {488 <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">params</span>.<span class="variable">get</span>[<span class="variable">name</span>]; 489 }490 <span class="comment">// Request body params via connect.bodyDecoder</span>491 <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>) {492 <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">body</span>[<span class="variable">name</span>];493 }Started migration docsTj Holowaychukbefore adoption9e68a5b
line 494why, one step back494};</code></pre>Updated docsTj Holowaychukbefore adoption09dabef
lines 495–498why, one step back495</td>496</tr>497<tr class="code">498<td class="docs">Started migration docsTj Holowaychukbefore adoption9e68a5b
line 499why, one step back499<p>Queue flash <code>msg</code> of the given <code>type</code>.</p>Updated docsTj Holowaychukbefore adoption09dabef
line 500why, one step back500Regenerated docsTj Holowaychukbefore adoption60cfd56
lines 501–502why, one step back501<h2>Examples</h2>502Started migration docsTj Holowaychukbefore adoption9e68a5b
lines 503–506why, one step back503<pre><code> req.flash('info', 'email sent');504 req.flash('error', 'email delivery failed');505 req.flash('info', 'email re-sent');506 // => 2Regenerated docsTj Holowaychukbefore adoption60cfd56
line 507why, one step back507Started migration docsTj Holowaychukbefore adoption9e68a5b
lines 508–509why, one step back508 req.flash('info');509 // => ['email sent', 'email re-sent']Regenerated docsTj Holowaychukbefore adoption60cfd56
line 510why, one step back510Started migration docsTj Holowaychukbefore adoption9e68a5b
lines 511–512why, one step back511 req.flash('info');512 // => []Regenerated docsTj Holowaychukbefore adoption60cfd56
line 513why, one step back513Started migration docsTj Holowaychukbefore adoption9e68a5b
lines 514–515why, one step back514 req.flash();515 // => { error: ['email delivery failed'], info: [] }Regenerated docsTj Holowaychukbefore adoption60cfd56
lines 516–521why, one step back516</code></pre>517518<h2></h2>519520<ul>521<li><p><strong>param</strong>: <em>String</em> type</p></li>Started migration docsTj Holowaychukbefore adoption9e68a5b
lines 522–523why, one step back522<li><p><strong>param</strong>: <em>String</em> msg</p></li>523<li><p><strong>return</strong>: <em>Array | Object | Number</em></p></li>Regenerated docsTj Holowaychukbefore adoption60cfd56
lines 524–528why, one step back524<li><p><strong>api</strong>: <em>public</em></p></li>525</ul>526527</td>528<td class="code">Started migration docsTj Holowaychukbefore adoption9e68a5b
lines 529–542why, one step back529<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>){530 <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> || {};531 <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>) {532 <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>));533 <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>);534 } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="variable">type</span>) {535 <span class="keyword">var</span> <span class="variable">arr</span> = <span class="variable">msgs</span>[<span class="variable">type</span>];536 <span class="keyword">delete</span> <span class="variable">msgs</span>[<span class="variable">type</span>];537 <span class="keyword">return</span> <span class="variable">arr</span> || [];538 } <span class="keyword">else</span> {539 <span class="this">this</span>.<span class="variable">session</span>.<span class="variable">flash</span> = {};540 <span class="keyword">return</span> <span class="variable">msgs</span>;541 }542};Regenerated docsTj Holowaychukbefore adoption60cfd56
line 543why, one step back543Started migration docsTj Holowaychukbefore adoption9e68a5b
line 544why, one step back544<span class="comment">// Callback for isXMLHttpRequest / xhr</span>Regenerated docsTj Holowaychukbefore adoption60cfd56
line 545why, one step back545Started migration docsTj Holowaychukbefore adoption9e68a5b
lines 546–548why, one step back546<span class="keyword">function</span> <span class="variable">isxhr</span>() {547 <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>;548}</code></pre>Regenerated docsTj Holowaychukbefore adoption60cfd56
lines 549–552why, one step back549</td>550</tr>551<tr class="code">552<td class="docs">Started migration docsTj Holowaychukbefore adoption9e68a5b
line 553why, one step back553<p>Check if the request was an <em>XMLHttpRequest</em>.</p>Regenerated docsTj Holowaychukbefore adoption60cfd56
lines 554–557why, one step back554555<h2></h2>556557<ul>Started migration docsTj Holowaychukbefore adoption9e68a5b
line 558why, one step back558<li><p><strong>return</strong>: <em>Boolean</em></p></li>Regenerated docsTj Holowaychukbefore adoption60cfd56
lines 559–563why, one step back559<li><p><strong>api</strong>: <em>public</em></p></li>560</ul>561562</td>563<td class="code">Started migration docsTj Holowaychukbefore adoption9e68a5b
lines 564–565why, one step back564<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>);565<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>);</code></pre>Regenerated docsTj Holowaychukbefore adoption60cfd56
line 566why, one step back566</td>Generated docsTj Holowaychukbefore adoptionbafa300
lines 567–572why, one step back567</tr><tr class="filename"><td><h2 id="lib/express/server.js">server</h2></td><td>lib/express/server.js</td></tr><tr class="code">568<td class="docs">569<p>Module dependencies.</p>570</td>571<td class="code">572<pre><code><span class="keyword">var</span> <span class="variable">sys</span> = <span class="variable">require</span>(<span class="string">'sys'</span>),Updated docsTj Holowaychukbefore adoption09dabef
line 573why, one step back573 <span class="variable">url</span> = <span class="variable">require</span>(<span class="string">'url'</span>),Regenerated docsTj Holowaychukbefore adoption09147a2
line 574why, one step back574 <span class="variable">view</span> = <span class="variable">require</span>(<span class="string">'./view'</span>),Generated docsTj Holowaychukbefore adoptionbafa300
line 575why, one step back575 <span class="variable">connect</span> = <span class="variable">require</span>(<span class="string">'connect'</span>),Updated docsTj Holowaychukbefore adoption09dabef
line 576why, one step back576 <span class="variable">queryString</span> = <span class="variable">require</span>(<span class="string">'querystring'</span>),Generated docsTj Holowaychukbefore adoptionbafa300
lines 577–589why, one step back577 <span class="variable">router</span> = <span class="variable">require</span>(<span class="string">'connect/middleware/router'</span>);</code></pre>578</td>579</tr>580<tr class="code">581<td class="docs">582<p>Initialize a new <code>Server</code> with optional <code>middleware</code>.</p>583584<h2></h2>585586<ul>587<li><p><strong>param</strong>: <em>Array</em> middleware</p></li>588<li><p><strong>api</strong>: <em>public</em></p></li>589</ul>Absolute labs bannerTj Holowaychukbefore adoption2b2fa7e
line 590why, one step back590Generated docsTj Holowaychukbefore adoptionbafa300
lines 591–598why, one step back591</td>592<td class="code">593<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>){594 <span class="keyword">var</span> <span class="variable">self</span> = <span class="this">this</span>;595 <span class="this">this</span>.<span class="variable">config</span> = {};596 <span class="this">this</span>.<span class="variable">settings</span> = {};597 <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> || []);598Updated docsTj Holowaychukbefore adoption09dabef
line 599why, one step back599 <span class="comment">// Expose objects to each other</span>Generated docsTj Holowaychukbefore adoptionbafa300
line 600why, one step back600 <span class="this">this</span>.<span class="variable">use</span>(<span class="string">'/'</span>, <span class="keyword">function</span>(<span class="variable">req</span>, <span class="variable">res</span>, <span class="variable">next</span>){Updated docsTj Holowaychukbefore adoption09dabef
lines 601–603why, one step back601 <span class="variable">req</span>.<span class="variable">params</span> = <span class="variable">req</span>.<span class="variable">params</span> || {};602 <span class="variable">req</span>.<span class="variable">params</span>.<span class="variable">get</span> = {};603 <span class="variable">res</span>.<span class="variable">headers</span> = {};Generated docsTj Holowaychukbefore adoptionbafa300
line 604why, one step back604 <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>;Updated docsTj Holowaychukbefore adoption09dabef
lines 605–612why, one step back605 <span class="variable">req</span>.<span class="variable">res</span> = <span class="variable">res</span>;606 <span class="variable">res</span>.<span class="variable">req</span> = <span class="variable">req</span>;607 <span class="comment">// Assign req.params.get</span>608 <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>) {609 <span class="comment">// TODO: consider simple substr to increase performance</span>610 <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>;611 <span class="variable">req</span>.<span class="variable">params</span>.<span class="variable">get</span> = <span class="variable">queryString</span>.<span class="variable">parse</span>(<span class="variable">query</span>);612 }Generated docsTj Holowaychukbefore adoptionbafa300
lines 613–624why, one step back613 <span class="variable">next</span>();614 });615616 <span class="comment">// Use router, expose as app.get(), etc</span>617 <span class="this">this</span>.<span class="variable">use</span>(<span class="string">'/'</span>, <span class="variable">router</span>(<span class="keyword">function</span>(<span class="variable">app</span>){618 <span class="variable">self</span>.<span class="variable">routes</span> = <span class="variable">app</span>;619 }));620};</code></pre>621</td>622</tr>623<tr class="code">624<td class="docs">Added configuration sectionTj Holowaychukbefore adoption83d2555
line 625why, one step back625<p>Inherit from <code>connect.Server</code>.</p> Generated docsTj Holowaychukbefore adoptionbafa300
lines 626–640why, one step back626</td>627<td class="code">628<pre><code><span class="variable">sys</span>.<span class="variable">inherits</span>(<span class="class">Server</span>, <span class="variable">connect</span>.<span class="class">Server</span>);</code></pre>629</td>630</tr>631<tr class="code">632<td class="docs">633<p>Listen on the given <code>port</code> after running configuration blocks.</p>634635<h2></h2>636637<ul>638<li><p><strong>param</strong>: <em>Number</em> port</p></li>639<li><p><strong>api</strong>: <em>public</em></p></li>640</ul>Added configuration sectionTj Holowaychukbefore adoption83d2555
line 641why, one step back641Generated docsTj Holowaychukbefore adoptionbafa300
lines 642–644why, one step back642</td>643<td class="code">644<pre><code><span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">listen</span> = <span class="keyword">function</span>(<span class="variable">port</span>){Updated docsTj Holowaychukbefore adoption09dabef
lines 645–646why, one step back645 <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">EXPRESS_ENV</span> || <span class="variable">process</span>.<span class="variable">connectEnv</span>.<span class="variable">name</span>);646 <span class="this">this</span>.<span class="variable">runConfig</span>(<span class="string">'any'</span>, <span class="this">this</span>.<span class="variable">set</span>(<span class="string">'env'</span>));Regenerated docsTj Holowaychukbefore adoption09147a2
lines 647–652why, one step back647648 <span class="comment">// Setup view reloading</span>649 <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">set</span>(<span class="string">'reload views'</span>)) {650 <span class="variable">view</span>.<span class="variable">watcher</span>.<span class="variable">call</span>(<span class="this">this</span>, <span class="this">this</span>.<span class="variable">set</span>(<span class="string">'reload views'</span>));651 }652 Generated docsTj Holowaychukbefore adoptionbafa300
lines 653–720why, one step back653 <span class="variable">connect</span>.<span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">listen</span>.<span class="variable">call</span>(<span class="this">this</span>, <span class="variable">port</span>);654};</code></pre>655</td>656</tr>657<tr class="code">658<td class="docs">659<p>Run config callbacks for the given environment(s);</p>660661<h2></h2>662663<ul>664<li><p><strong>param</strong>: <em>String</em> env …</p></li>665<li><p><strong>return</strong>: <em>Server</em> for chaining</p></li>666<li><p><strong>api</strong>: <em>public</em></p></li>667</ul>668669</td>670<td class="code">671<pre><code><span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">runConfig</span> = <span class="keyword">function</span>(){672 <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">arguments</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>) {673 <span class="keyword">var</span> <span class="variable">env</span> = <span class="variable">arguments</span>[<span class="variable">i</span>];674 <span class="keyword">if</span> (<span class="variable">env</span> <span class="keyword">in</span> <span class="this">this</span>.<span class="variable">config</span>) {675 <span class="keyword">var</span> <span class="variable">config</span> = <span class="this">this</span>.<span class="variable">config</span>[<span class="variable">env</span>];676 <span class="variable">config</span>.<span class="variable">forEach</span>(<span class="keyword">function</span>(<span class="variable">fn</span>){677 <span class="variable">fn</span>.<span class="variable">call</span>(<span class="this">this</span>);678 }, <span class="this">this</span>);679 }680 }681 <span class="keyword">return</span> <span class="this">this</span>;682};</code></pre>683</td>684</tr>685<tr class="code">686<td class="docs">687<p>Assign <code>setting</code> to <code>val</code>, or return <code>setting</code>’s value.</p>688689<h2></h2>690691<ul>692<li><p><strong>param</strong>: <em>String</em> setting</p></li>693<li><p><strong>param</strong>: <em>String</em> val</p></li>694<li><p><strong>return</strong>: <em>Server | Mixed</em> for chaining, or the setting value</p></li>695<li><p><strong>api</strong>: <em>public</em></p></li>696</ul>697698</td>699<td class="code">700<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>){701 <span class="keyword">if</span> (<span class="variable">val</span> === <span class="variable">undefined</span>) {702 <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">settings</span>[<span class="variable">setting</span>];703 } <span class="keyword">else</span> {704 <span class="this">this</span>.<span class="variable">settings</span>[<span class="variable">setting</span>] = <span class="variable">val</span>;705 <span class="keyword">return</span> <span class="this">this</span>;706 }707};</code></pre>708</td>709</tr>710<tr class="code">711<td class="docs">712<p>Enable <code>setting</code>.</p>713714<h2></h2>715716<ul>717<li><p><strong>param</strong>: <em>String</em> setting</p></li>718<li><p><strong>return</strong>: <em>Server</em> for chaining</p></li>719<li><p><strong>api</strong>: <em>public</em></p></li>720</ul>Added configuration sectionTj Holowaychukbefore adoption83d2555
line 721why, one step back721UQGenerated docsTj Holowaychukbefore adoptionbafa300
lines 722–739why, one step back722</td>723<td class="code">724<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>){725 <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>);726};</code></pre>727</td>728</tr>729<tr class="code">730<td class="docs">731<p>Disable <code>setting</code>.</p>732733<h2></h2>734735<ul>736<li><p><strong>param</strong>: <em>String</em> setting</p></li>737<li><p><strong>return</strong>: <em>Server</em> for chaining</p></li>738<li><p><strong>api</strong>: <em>public</em></p></li>739</ul>Added configuration sectionTj Holowaychukbefore adoption83d2555
line 740why, one step back740QGenerated docsTj Holowaychukbefore adoptionbafa300
lines 741–758why, one step back741</td>742<td class="code">743<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>){744 <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>);745};</code></pre>746</td>747</tr>748<tr class="code">749<td class="docs">750<p>Disable <code>setting</code>.</p>751752<h2></h2>753754<ul>755<li><p><strong>param</strong>: <em>String</em> setting</p></li>756<li><p><strong>return</strong>: <em>Server</em> for chaining</p></li>757<li><p><strong>api</strong>: <em>public</em></p></li>758</ul>Added configuration sectionTj Holowaychukbefore adoption83d2555
line 759why, one step back759QGenerated docsTj Holowaychukbefore adoptionbafa300
lines 760–767why, one step back760</td>761<td class="code">762<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>){763 <span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">env</span> === <span class="string">'function'</span>) <span class="variable">fn</span> = <span class="variable">env</span>, <span class="variable">env</span> = <span class="string">'any'</span>;764 (<span class="this">this</span>.<span class="variable">config</span>[<span class="variable">env</span>] = <span class="this">this</span>.<span class="variable">config</span>[<span class="variable">env</span>] || []).<span class="variable">push</span>(<span class="variable">fn</span>);765 <span class="keyword">return</span> <span class="this">this</span>;766};767Updated docsTj Holowaychukbefore adoption09dabef
line 768why, one step back768<span class="comment">// Generate routing methods</span>Generated docsTj Holowaychukbefore adoptionbafa300
lines 769–776why, one step back769770(<span class="keyword">function</span>(<span class="variable">method</span>){771 <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>){772 <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>);773 <span class="keyword">return</span> <span class="this">this</span>;774 };775 <span class="keyword">return</span> <span class="variable">arguments</span>.<span class="variable">callee</span>;776})(<span class="string">'get'</span>)(<span class="string">'post'</span>)(<span class="string">'put'</span>)(<span class="string">'del'</span>);Added contributors back to docsTj Holowaychukbefore adoption195c8a9
lines 777–778why, one step back777</code></pre>778</td>Added configuration sectionTj Holowaychukbefore adoption83d2555
lines 779–828why, one step back779</tr><tr class="filename"><td><h2 id="lib/express/utils.js">utils</h2></td><td>lib/express/utils.js</td></tr><tr class="code">780<td class="docs">781<p>Parse mini markdown implementation.782The following conversions are supported,783primarily for the “flash” middleware:</p>784785<p> <em>foo</em> or <em>foo</em> become <em>foo</em>786 <strong>foo</strong> or <strong>foo</strong> become <strong>foo</strong>787 <a href="B">A</a> becomes <a href="B">A</a></p>788789<h2></h2>790791<ul>792<li><p><strong>param</strong>: <em>String</em> str</p></li>793<li><p><strong>return</strong>: <em>String</em></p></li>794<li><p><strong>api</strong>: <em>public</em></p></li>795</ul>796 797</td>798<td class="code">799<pre><code><span class="variable">exports</span>.<span class="variable">miniMarkdown</span> = <span class="keyword">function</span>(<span class="variable">str</span>){800 <span class="keyword">return</span> <span class="class">String</span>(<span class="variable">str</span>)801 .<span class="variable">replace</span>(<span class="regexp">/(__|\*\*)(.*?)\1/g</span>, <span class="string">'<strong>$2</strong>'</span>)802 .<span class="variable">replace</span>(<span class="regexp">/(_|\*)(.*?)\1/g</span>, <span class="string">'<em>$2</em>'</span>)803 .<span class="variable">replace</span>(<span class="regexp">/\[([^\]]+)\]\(([^)]+)\)/g</span>, <span class="string">'<a href="$2">$1</a>'</span>);804};</code></pre>805</td>806</tr>807<tr class="code">808<td class="docs">809<p>Escape special characters in the given string of html.</p>810811<h2></h2>812813<ul>814<li><p><strong>param</strong>: <em>String</em> html</p></li>815<li><p><strong>return</strong>: <em>String</em></p></li>816<li><p><strong>api</strong>: <em>public</em></p></li>817</ul>818819</td>820<td class="code">821<pre><code><span class="variable">exports</span>.<span class="variable">htmlEscape</span> = <span class="keyword">function</span>(<span class="variable">html</span>) {822 <span class="keyword">return</span> <span class="class">String</span>(<span class="variable">html</span>)823 .<span class="variable">replace</span>(<span class="regexp">/&/g</span>, <span class="string">'&'</span>)824 .<span class="variable">replace</span>(<span class="regexp">/"/g</span>, <span class="string">'"'</span>)825 .<span class="variable">replace</span>(<span class="regexp">/</g</span>, <span class="string">'<'</span>)826 .<span class="variable">replace</span>(<span class="regexp">/>/g</span>, <span class="string">'>'</span>);827};</code></pre>828</td>Added contributors back to docsTj Holowaychukbefore adoption195c8a9
lines 829–835why, one step back829</tr><tr class="filename"><td><h2 id="lib/express/view.js">view</h2></td><td>lib/express/view.js</td></tr><tr class="code">830<td class="docs">831<p>Module dependencies.</p>832</td>833<td class="code">834<pre><code><span class="keyword">var</span> <span class="variable">extname</span> = <span class="variable">require</span>(<span class="string">'path'</span>).<span class="variable">extname</span>,835 <span class="variable">mime</span> = <span class="variable">require</span>(<span class="string">'connect/utils'</span>).<span class="variable">mime</span>,Regenerated docsTj Holowaychukbefore adoption09147a2
line 836why, one step back836 <span class="variable">utils</span> = <span class="variable">require</span>(<span class="string">'connect/utils'</span>),Added contributors back to docsTj Holowaychukbefore adoption195c8a9
line 837why, one step back837 <span class="variable">http</span> = <span class="variable">require</span>(<span class="string">'http'</span>),Regenerated docsTj Holowaychukbefore adoption09147a2
line 838why, one step back838 <span class="variable">sys</span> = <span class="variable">require</span>(<span class="string">'sys'</span>),Added contributors back to docsTj Holowaychukbefore adoption195c8a9
lines 839–853why, one step back839 <span class="variable">fs</span> = <span class="variable">require</span>(<span class="string">'fs'</span>);</code></pre>840</td>841</tr>842<tr class="code">843<td class="docs">844<p>Cache supported template engine exports to845increase performance by lowering the number846of calls to <code>require()</code>.</p>847848<ul>849<li><strong>type</strong>: <em>Object</em></li>850</ul>851852</td>853<td class="code">Regenerated docsTj Holowaychukbefore adoption09147a2
lines 854–859why, one step back854<pre><code><span class="keyword">var</span> <span class="variable">cache</span> = {};</code></pre>855</td>856</tr>857<tr class="code">858<td class="docs">859<p>Cache view contents to prevent I/O hits.</p>Added contributors back to docsTj Holowaychukbefore adoption195c8a9
line 860why, one step back860Regenerated docsTj Holowaychukbefore adoption09147a2
lines 861–863why, one step back861<ul>862<li><strong>type</strong>: <em>Object</em></li>863</ul>Added configuration sectionTj Holowaychukbefore adoption83d2555
line 864why, one step back864Added contributors back to docsTj Holowaychukbefore adoption195c8a9
line 865why, one step back865</td>Regenerated docsTj Holowaychukbefore adoption09147a2
lines 866–868why, one step back866<td class="code">867<pre><code><span class="keyword">var</span> <span class="variable">viewCache</span> = {};</code></pre>868</td>Added contributors back to docsTj Holowaychukbefore adoption195c8a9
lines 869–871why, one step back869</tr>870<tr class="code">871<td class="docs">Regenerated docsTj Holowaychukbefore adoption09147a2
lines 872–876why, one step back872<p>Clear the view cache.</p>873874<ul>875<li><strong>api</strong>: <em>public</em></li>876</ul>Added configuration sectionTj Holowaychukbefore adoption83d2555
line 877why, one step back877 Regenerated docsTj Holowaychukbefore adoption09147a2
lines 878–886why, one step back878</td>879<td class="code">880<pre><code><span class="variable">exports</span>.<span class="variable">clearCache</span> = <span class="keyword">function</span>(){881 <span class="variable">viewCache</span> = {};882};</code></pre>883</td>884</tr>885<tr class="code">886<td class="docs">Regenerated docsTj Holowaychukbefore adoption60cfd56
lines 887–891why, one step back887<p>View helpers merged with each render’s locals.</p>888889<ul>890<li><strong>type</strong>: <em>Object</em></li>891</ul>Added configuration sectionTj Holowaychukbefore adoption83d2555
line 892why, one step back892 Regenerated docsTj Holowaychukbefore adoption60cfd56
lines 893–899why, one step back893</td>894<td class="code">895<pre><code><span class="keyword">var</span> <span class="variable">helpers</span> = <span class="variable">exports</span>.<span class="variable">helpers</span> = {};</code></pre>896</td>897</tr>898<tr class="code">899<td class="docs">Added contributors back to docsTj Holowaychukbefore adoption195c8a9
lines 900–904why, one step back900<p>Render <code>view</code> partial with the given <code>options</code>.</p>901902<h2>Options</h2>903904<ul>Regenerated docsTj Holowaychukbefore adoption09147a2
lines 905–910why, one step back905<li><p><code>as</code> Variable name for each <code>collection</code> value, defaults to the view name.</p>906907<ul>908<li>as: ‘something’ will add the <code>something</code> local variable</li>909<li>as: this will use the collection value as the template context</li>910<li>as: global will merge the collection value’s properties with <code>locals</code></li>Added contributors back to docsTj Holowaychukbefore adoption195c8a9
line 911why, one step back911</ul>Regenerated docsTj Holowaychukbefore adoption09147a2
lines 912–915why, one step back912</li>913<li><p><code>collection</code> Array of objects, the name is derived from the view name itself.914For example <em>video.html</em> will have a object <em>video</em> available to it.</p></li>915</ul>Added contributors back to docsTj Holowaychukbefore adoption195c8a9
lines 916–921why, one step back916917918<h2></h2>919920<ul>921<li><p><strong>param</strong>: <em>String</em> view</p></li>Regenerated docsTj Holowaychukbefore adoption09147a2
line 922why, one step back922<li><p><strong>param</strong>: <em>Object | Array</em> options or collection</p></li>Added contributors back to docsTj Holowaychukbefore adoption195c8a9
lines 923–925why, one step back923<li><p><strong>return</strong>: <em>String</em></p></li>924<li><p><strong>api</strong>: <em>public</em></p></li>925</ul>Added configuration sectionTj Holowaychukbefore adoption83d2555
line 926why, one step back926\QAdded contributors back to docsTj Holowaychukbefore adoption195c8a9
lines 927–928why, one step back927</td>928<td class="code">Regenerated docsTj Holowaychukbefore adoption09147a2
lines 929–935why, one step back929<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">ext</span>){930 <span class="keyword">if</span> (<span class="variable">ext</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>) &<span class="variable">lt</span>; <span class="number integer">0</span>) {931 <span class="variable">view</span> += <span class="variable">ext</span>;932 }933 <span class="keyword">if</span> (<span class="variable">options</span> <span class="variable">instanceof</span> <span class="class">Array</span>) {934 <span class="variable">options</span> = { <span class="variable">collection</span>: <span class="variable">options</span> };935 }Added contributors back to docsTj Holowaychukbefore adoption195c8a9
lines 936–945why, one step back936 <span class="variable">options</span> = <span class="variable">options</span> || {};937 <span class="variable">options</span>.<span class="variable">partial</span> = <span class="variable">true</span>;938 <span class="variable">options</span>.<span class="variable">layout</span> = <span class="variable">false</span>;939 <span class="keyword">var</span> <span class="variable">collection</span> = <span class="variable">options</span>.<span class="variable">collection</span>;940 <span class="keyword">if</span> (<span class="variable">collection</span>) {941 <span class="keyword">var</span> <span class="variable">name</span> = <span class="variable">options</span>.<span class="variable">as</span> || <span class="variable">view</span>.<span class="variable">split</span>(<span class="string">'.'</span>)[<span class="number integer">0</span>],942 <span class="variable">len</span> = <span class="variable">collection</span>.<span class="variable">length</span>;943 <span class="keyword">var</span> <span class="variable">locals</span> = <span class="variable">options</span>.<span class="variable">locals</span> = <span class="variable">options</span>.<span class="variable">locals</span> || {};944 <span class="variable">locals</span>.<span class="variable">collectionLength</span> = <span class="variable">len</span>;945 <span class="keyword">return</span> <span class="variable">collection</span>.<span class="variable">map</span>(<span class="keyword">function</span>(<span class="variable">val</span>, <span class="variable">i</span>){Regenerated docsTj Holowaychukbefore adoption09147a2
lines 946–952why, one step back946 <span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">name</span> === <span class="string">'string'</span>) {947 <span class="variable">locals</span>[<span class="variable">name</span>] = <span class="variable">val</span>;948 } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="variable">name</span> === <span class="variable">global</span>) {949 <span class="variable">utils</span>.<span class="variable">merge</span>(<span class="variable">locals</span>, <span class="variable">val</span>);950 } <span class="keyword">else</span> {951 <span class="variable">options</span>.<span class="variable">context</span> = <span class="variable">val</span>;952 }Added contributors back to docsTj Holowaychukbefore adoption195c8a9
lines 953–969why, one step back953 <span class="variable">locals</span>.<span class="variable">firstInCollection</span> = <span class="variable">i</span> === <span class="number integer">0</span>;954 <span class="variable">locals</span>.<span class="variable">indexInCollection</span> = <span class="variable">i</span>;955 <span class="variable">locals</span>.<span class="variable">lastInCollection</span> = <span class="variable">i</span> === <span class="variable">len</span> - <span class="number integer">1</span>;956 <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">render</span>(<span class="variable">view</span>, <span class="variable">options</span>);957 }, <span class="this">this</span>).<span class="variable">join</span>(<span class="string">''</span>);958 } <span class="keyword">else</span> {959 <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">render</span>(<span class="variable">view</span>, <span class="variable">options</span>);960 }961};</code></pre>962</td>963</tr>964<tr class="code">965<td class="docs">966<p>Render <code>view</code> with the given <code>options</code> and optional callback <code>fn</code>.967When a callback function is given a response will <em>not</em> be made968automatically, however otherwise a response of <em>200</em> and <em>text/html</em> is given.</p>969Updated docsTj Holowaychukbefore adoption09dabef
lines 970–983why, one step back970<h2>Options</h2>971972<p> Most engines accept one or more of the following options,973 both <em>haml</em> and <em>jade</em> accept all:</p>974975<ul>976<li><code>context</code> Template evaluation context (<code>this</code>)</li>977<li><code>locals</code> Object containing local variables</li>978<li><code>filename</code> Filename used for the <code>cache</code> option</li>979<li><code>cache</code> Cache intermediate JavaScript in memory</li>980<li><code>debug</code> Output debugging information</li>981</ul>982983Added contributors back to docsTj Holowaychukbefore adoption195c8a9
lines 984–991why, one step back984<h2></h2>985986<ul>987<li><p><strong>param</strong>: <em>String</em> view</p></li>988<li><p><strong>param</strong>: <em>Object</em> options</p></li>989<li><p><strong>param</strong>: <em>Function</em> fn</p></li>990<li><p><strong>api</strong>: <em>public</em></p></li>991</ul>Added configuration sectionTj Holowaychukbefore adoption83d2555
line 992why, one step back992�Added contributors back to docsTj Holowaychukbefore adoption195c8a9
lines 993–996why, one step back993</td>994<td class="code">995<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>){996 <span class="variable">options</span> = <span class="variable">options</span> || {};Regenerated docsTj Holowaychukbefore adoption09147a2
line 997why, one step back997 <span class="keyword">var</span> <span class="variable">defaultEngine</span> = <span class="this">this</span>.<span class="variable">app</span>.<span class="variable">set</span>(<span class="string">'view engine'</span>);Added contributors back to docsTj Holowaychukbefore adoption195c8a9
line 998why, one step back998Regenerated docsTj Holowaychukbefore adoption09147a2
lines 999–1003why, one step back999 <span class="comment">// Support "view engine" setting</span>1000 <span class="keyword">if</span> (<span class="variable">view</span>.<span class="variable">indexOf</span>(<span class="string">'.'</span>) &<span class="variable">lt</span>; <span class="number integer">0</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">defaultEngine</span>) {1001 <span class="variable">view</span> += <span class="string">'.'</span> + <span class="variable">defaultEngine</span>;1002 }1003Added contributors back to docsTj Holowaychukbefore adoption195c8a9
lines 1004–1005why, one step back1004 <span class="comment">// Defaults</span>1005 <span class="keyword">var</span> <span class="variable">self</span> = <span class="this">this</span>,Regenerated docsTj Holowaychukbefore adoption09147a2
line 1006why, one step back1006 <span class="variable">root</span> = <span class="variable">viewRoot</span>(<span class="this">this</span>.<span class="variable">app</span>),Added contributors back to docsTj Holowaychukbefore adoption195c8a9
lines 1007–1012why, one step back1007 <span class="variable">ext</span> = <span class="variable">extname</span>(<span class="variable">view</span>),1008 <span class="variable">partial</span> = <span class="variable">options</span>.<span class="variable">partial</span>,1009 <span class="variable">layout</span> = <span class="variable">options</span>.<span class="variable">layout</span> === <span class="variable">undefined</span> ? <span class="variable">true</span> : <span class="variable">options</span>.<span class="variable">layout</span>,1010 <span class="variable">layout</span> = <span class="variable">layout</span> === <span class="variable">true</span>1011 ? <span class="string">'layout'</span> + <span class="variable">ext</span>1012 : <span class="variable">layout</span>;Updated docsTj Holowaychukbefore adoption09dabef
lines 1013–1017why, one step back10131014 <span class="comment">// Auto-cache in production</span>1015 <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">app</span>.<span class="variable">set</span>(<span class="string">'env'</span>) === <span class="string">'production'</span>) {1016 <span class="variable">options</span>.<span class="variable">cache</span> = <span class="variable">true</span>;1017 }Added contributors back to docsTj Holowaychukbefore adoption195c8a9
lines 1018–1035why, one step back10181019 <span class="comment">// Partials support</span>1020 <span class="keyword">if</span> (<span class="variable">options</span>.<span class="variable">partial</span>) {1021 <span class="variable">root</span> += <span class="string">'/partials'</span>;1022 }10231024 <span class="comment">// View path</span>1025 <span class="keyword">var</span> <span class="variable">path</span> = <span class="variable">root</span> + <span class="string">'/'</span> + <span class="variable">view</span>;10261027 <span class="comment">// Send response</span>1028 <span class="keyword">function</span> <span class="variable">send</span>(<span class="variable">str</span>) {1029 <span class="variable">self</span>.<span class="variable">writeHead</span>(<span class="number integer">200</span>, {1030 <span class="string">'Content-Type'</span>: <span class="string">'text/html'</span>,1031 <span class="string">'Content-Length'</span>: <span class="variable">str</span>.<span class="variable">length</span>1032 });1033 <span class="variable">self</span>.<span class="variable">end</span>(<span class="variable">str</span>);1034 }1035Regenerated docsTj Holowaychukbefore adoption09147a2
lines 1036–1043why, one step back1036 <span class="comment">// Pass filename to the engine and view</span>1037 <span class="variable">options</span>.<span class="variable">locals</span> = <span class="variable">options</span>.<span class="variable">locals</span> || {};1038 <span class="variable">options</span>.<span class="variable">locals</span>.<span class="variable">__filename</span> = <span class="variable">options</span>.<span class="variable">filename</span> = <span class="variable">path</span>;10391040 <span class="comment">// Always expose partial() as a local</span>1041 <span class="variable">options</span>.<span class="variable">locals</span>.<span class="variable">partial</span> = <span class="keyword">function</span>(<span class="variable">view</span>, <span class="variable">options</span>){1042 <span class="keyword">return</span> <span class="variable">self</span>.<span class="variable">partial</span>.<span class="variable">call</span>(<span class="variable">self</span>, <span class="variable">view</span>, <span class="variable">options</span>, <span class="variable">ext</span>);1043 };Regenerated docsTj Holowaychukbefore adoption60cfd56
lines 1044–1045why, one step back1044 1045 <span class="comment">// Merge view helpers</span>Started migration docsTj Holowaychukbefore adoption9e68a5b
line 1046why, one step back1046 <span class="variable">options</span>.<span class="variable">locals</span>.<span class="variable">__proto__</span> = <span class="variable">helpers</span>;Regenerated docsTj Holowaychukbefore adoption09147a2
line 1047why, one step back1047Added contributors back to docsTj Holowaychukbefore adoption195c8a9
lines 1048–1055why, one step back1048 <span class="keyword">function</span> <span class="variable">error</span>(<span class="variable">err</span>) {1049 <span class="keyword">if</span> (<span class="variable">fn</span>) {1050 <span class="variable">fn</span>(<span class="variable">err</span>);1051 } <span class="keyword">else</span> {1052 <span class="keyword">throw</span> <span class="variable">err</span>;1053 }1054 }1055Regenerated docsTj Holowaychukbefore adoption09147a2
lines 1056–1086why, one step back1056 <span class="comment">// Cache contents</span>1057 <span class="keyword">try</span> {1058 <span class="keyword">var</span> <span class="variable">str</span> = <span class="variable">viewCache</span>[<span class="variable">path</span>] || <span class="variable">cacheView</span>(<span class="variable">path</span>);1059 } <span class="keyword">catch</span> (<span class="variable">err</span>) {1060 <span class="keyword">return</span> <span class="variable">error</span>(<span class="variable">err</span>);1061 }10621063 <span class="comment">// Cache template engine exports</span>1064 <span class="keyword">var</span> <span class="variable">engine</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="variable">ext</span>.<span class="variable">substr</span>(<span class="number integer">1</span>)));10651066 <span class="comment">// Attempt render</span>1067 <span class="keyword">try</span> {1068 <span class="keyword">var</span> <span class="variable">str</span> = <span class="variable">engine</span>.<span class="variable">render</span>(<span class="variable">str</span>, <span class="variable">options</span>);1069 } <span class="keyword">catch</span> (<span class="variable">err</span>) {1070 <span class="keyword">return</span> <span class="variable">error</span>(<span class="variable">err</span>);1071 }10721073 <span class="comment">// Layout support</span>1074 <span class="keyword">if</span> (<span class="variable">layout</span>) {1075 <span class="variable">options</span>.<span class="variable">layout</span> = <span class="variable">false</span>;1076 <span class="variable">options</span>.<span class="variable">locals</span>.<span class="variable">body</span> = <span class="variable">str</span>;1077 <span class="variable">self</span>.<span class="variable">render</span>(<span class="variable">layout</span>, <span class="variable">options</span>);1078 } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="variable">partial</span>) {1079 <span class="keyword">return</span> <span class="variable">str</span>;1080 } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="variable">fn</span>) {1081 <span class="variable">fn</span>(<span class="keyword">null</span>, <span class="variable">str</span>);1082 } <span class="keyword">else</span> {1083 <span class="variable">send</span>(<span class="variable">str</span>);1084 }1085};1086</code></pre>Generated docsTj Holowaychukbefore adoptionbafa300
lines 1087–1089why, one step back1087</td>1088</tr> </body>1089</html></tbody></table>