Skip to content
Myr

Updated docs

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent 7a1525c · Browse files · 3 files+285 added−285 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+285 added−285 removed

  • other:+285 added−285 removed

3 files+285 added−285 removed

modified:docs/api.html+283 added-283 removed
335 unmodified lines
336<td class="code">337<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>);338<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>339</td>340</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">341<td class="docs">342<p>Parse mini markdown implementation.343The following conversions are supported,344primarily for the "flash" middleware:</p>345346<p> <em>foo</em> or <em>foo</em> become &lt;em&gt;foo&lt;/em&gt;347 <strong>foo</strong> or <strong>foo</strong> become &lt;strong&gt;foo&lt;/strong&gt;348 <a href="B">A</a> becomes &lt;a href="B"&gt;A&lt;/a&gt;</p>349350<h2></h2>351352<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>public</em></p></li></ul>353</td>354<td class="code">355<pre><code><span class="variable">exports</span>.<span class="variable">miniMarkdown</span> = <span class="keyword">function</span>(<span class="variable">str</span>){356 <span class="keyword">return</span> <span class="class">String</span>(<span class="variable">str</span>)357 .<span class="variable">replace</span>(<span class="regexp">/(__|\*\*)(.*?)\1/g</span>, <span class="string">'&lt;strong&gt;$2&lt;/strong&gt;'</span>)358 .<span class="variable">replace</span>(<span class="regexp">/(_|\*)(.*?)\1/g</span>, <span class="string">'&lt;em&gt;$2&lt;/em&gt;'</span>)359 .<span class="variable">replace</span>(<span class="regexp">/\[([^\]]+)\]\(([^)]+)\)/g</span>, <span class="string">'&lt;a href=&quot;$2&quot;&gt;$1&lt;/a&gt;'</span>);360};</code></pre>361</td>362</tr>363<tr class="code">364<td class="docs">365<p>Escape special characters in the given string of html.</p>366367<h2></h2>368369<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>public</em></p></li></ul>370</td>371<td class="code">372<pre><code><span class="variable">exports</span>.<span class="variable">htmlEscape</span> = <span class="keyword">function</span>(<span class="variable">html</span>) {373 <span class="keyword">return</span> <span class="class">String</span>(<span class="variable">html</span>)374 .<span class="variable">replace</span>(<span class="regexp">/&amp;/g</span>, <span class="string">'&amp;'</span>)375 .<span class="variable">replace</span>(<span class="regexp">/&quot;/g</span>, <span class="string">'&quot;'</span>)376 .<span class="variable">replace</span>(<span class="regexp">/&lt;/g</span>, <span class="string">'&lt;'</span>)377 .<span class="variable">replace</span>(<span class="regexp">/&gt;/g</span>, <span class="string">'&gt;'</span>);378};</code></pre>379</td>380</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">381<td class="docs">382<p>Module dependencies.383 </p>384</td>385<td class="code">386<pre><code><span class="keyword">var</span> <span class="variable">fs</span> = <span class="variable">require</span>(<span class="string">'fs'</span>),387 <span class="variable">http</span> = <span class="variable">require</span>(<span class="string">'http'</span>),388 <span class="variable">path</span> = <span class="variable">require</span>(<span class="string">'path'</span>),389 <span class="variable">utils</span> = <span class="variable">require</span>(<span class="string">'connect/utils'</span>),390 <span class="variable">mime</span> = <span class="variable">require</span>(<span class="string">'connect/utils'</span>).<span class="variable">mime</span>,391 <span class="class">Buffer</span> = <span class="variable">require</span>(<span class="string">'buffer'</span>).<span class="class">Buffer</span>;</code></pre>392</td>393</tr>394<tr class="code">395<td class="docs">396<p>Send a response with the given <code>body</code> and optional <code>headers</code> and <code>status</code> code.</p>397398<h2>Examples</h2>399400<pre><code>res.send(new Buffer('wahoo'));401res.send({ some: 'json' });402res.send('&lt;p&gt;some html&lt;/p&gt;');403res.send('Sorry, cant find that', 404);404res.send('text', { 'Content-Type': 'text/plain' }, 201);405res.send(404);</code></pre>406407<h2></h2>408409<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>410</td>411<td class="code">412<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>){413 <span class="comment">// Allow status as second arg</span>414 <span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">headers</span> === <span class="string">'number'</span>) {415 <span class="variable">status</span> = <span class="variable">headers</span>,416 <span class="variable">headers</span> = <span class="keyword">null</span>;417 }418419 <span class="comment">// Defaults</span>420 <span class="variable">status</span> = <span class="variable">status</span> || <span class="number integer">200</span>;421 <span class="variable">headers</span> = <span class="variable">headers</span> || {};422423 <span class="comment">// Determine content type</span>424 <span class="keyword">switch</span> (<span class="keyword">typeof</span> <span class="variable">body</span>) {425 <span class="keyword">case</span> <span class="string">'number'</span>:426 <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Type'</span>]) {427 <span class="this">this</span>.<span class="variable">contentType</span>(<span class="string">'.txt'</span>);428 }429 <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>];430 <span class="keyword">break</span>;431 <span class="keyword">case</span> <span class="string">'string'</span>:432 <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Type'</span>]) {433 <span class="this">this</span>.<span class="variable">contentType</span>(<span class="string">'.html'</span>);434 }435 <span class="keyword">break</span>;436 <span class="keyword">case</span> <span class="string">'object'</span>:437 <span class="keyword">if</span> (<span class="variable">body</span> <span class="variable">instanceof</span> <span class="class">Buffer</span>) {438 <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Type'</span>]) {439 <span class="this">this</span>.<span class="variable">contentType</span>(<span class="string">'.bin'</span>);440 }441 } <span class="keyword">else</span> {442 <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Type'</span>]) {443 <span class="this">this</span>.<span class="variable">contentType</span>(<span class="string">'.json'</span>);444 }445 <span class="variable">body</span> = <span class="class">JSON</span>.<span class="variable">stringify</span>(<span class="variable">body</span>);446 }447 <span class="keyword">break</span>;448 }449450 <span class="comment">// Populate Content-Length</span>451 <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Length'</span>]) {452 <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>453 ? <span class="variable">body</span>.<span class="variable">length</span>454 : <span class="class">Buffer</span>.<span class="variable">byteLength</span>(<span class="variable">body</span>));455 }456457 <span class="comment">// Merge headers passed</span>458 <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>);459460 <span class="comment">// Respond</span>461 <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>);462 <span class="this">this</span>.<span class="variable">end</span>(<span class="variable">body</span>);463};</code></pre>464</td>465</tr>466<tr class="code">467<td class="docs">468<p>Transfer the given <code>file</code>. Automatically sets the <em>Content-Type</em>469response header, and responds with 404 / 500 appropriately.</p>470471<h2></h2>472473<ul><li><p><strong>param</strong>: <em>String</em> file</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>474</td>475<td class="code">476<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">file</span>){477 <span class="keyword">var</span> <span class="variable">self</span> = <span class="this">this</span>;478 <span class="variable">fs</span>.<span class="variable">readFile</span>(<span class="variable">file</span>, <span class="keyword">function</span>(<span class="variable">err</span>, <span class="variable">buf</span>){479 <span class="keyword">if</span> (<span class="variable">err</span>) {480 <span class="variable">self</span>.<span class="variable">send</span>(<span class="variable">err</span>.<span class="variable">errno</span> === <span class="variable">process</span>.<span class="class">ENOENT</span>481 ? <span class="number integer">404</span>482 : <span class="number integer">500</span>);483 } <span class="keyword">else</span> {484 <span class="variable">self</span>.<span class="variable">contentType</span>(<span class="variable">file</span>);485 <span class="variable">self</span>.<span class="variable">send</span>(<span class="variable">buf</span>);486 }487 });488};</code></pre>489</td>490</tr>491<tr class="code">492<td class="docs">493<p>Set <em>Content-Type</em> response header passed through <code>mime.type()</code>.</p>494495<h2>Examples</h2>496497<pre><code>var filename = 'path/to/image.png';498res.contentType(filename);499// res.headers['Content-Type'] is now "image/png"</code></pre>500501<h2></h2>502503<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>504</td>505<td class="code">506<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>){507 <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>));508};</code></pre>509</td>510</tr>511<tr class="code">512<td class="docs">513<p>Set <em>Content-Disposition</em> header to <em>attachment</em> with optional <code>filename</code>.</p>514515<h2></h2>516517<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>518</td>519<td class="code">520<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>){521 <span class="this">this</span>.<span class="variable">header</span>(<span class="string">'Content-Disposition'</span>, <span class="variable">filename</span>522 ? <span class="string">'attachment; filename=&quot;'</span> + <span class="variable">path</span>.<span class="variable">basename</span>(<span class="variable">filename</span>) + <span class="string">'&quot;'</span>523 : <span class="string">'attachment'</span>);524 <span class="keyword">return</span> <span class="this">this</span>;525};</code></pre>526</td>527</tr>528<tr class="code">529<td class="docs">530<p>Transfer the given <code>file</code> with optional <code>filename</code> as an attachment.</p>531532<h2></h2>533534<ul><li><p><strong>param</strong>: <em>String</em> file</p></li><li><p><strong>param</strong>: <em>String</em> filename</p></li><li><p><strong>return</strong>: <em>Type</em> </p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>535</td>536<td class="code">537<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">file</span>, <span class="variable">filename</span>){538 <span class="this">this</span>.<span class="variable">attachment</span>(<span class="variable">filename</span> || <span class="variable">file</span>).<span class="variable">sendfile</span>(<span class="variable">file</span>);539};</code></pre>540</td>541</tr>542<tr class="code">543<td class="docs">544<p>Set or get response header <code>name</code> with optional <code>val</code>.</p>545546<h2></h2>547548<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>549</td>550<td class="code">551<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>){552 <span class="keyword">return</span> <span class="variable">val</span> === <span class="variable">undefined</span>553 ? <span class="this">this</span>.<span class="variable">headers</span>[<span class="variable">name</span>]554 : <span class="this">this</span>.<span class="variable">headers</span>[<span class="variable">name</span>] = <span class="variable">val</span>;555};</code></pre>556</td>557</tr>558<tr class="code">559<td class="docs">560<p>Redirect to the given <code>url</code> with optional response <code>status</code>561defauling to 302.</p>562563<p>The given <code>url</code> can also be the name of a mapped url, for564example by default express supports "back" which redirects565to the <em>Referrer</em> or <em>Referer</em> headers or the application's566"home" setting. Express also supports "home" out of the box,567which can be set via <code>app.set('home', '/blog');</code>, and defaults568to '/'.</p>569570<h2>Redirect Mapping</h2>571572<p> To extend the redirect mapping capabilities that Express provides,573 we may use the <code>app.redirect()</code> method:</p>574575<pre><code>app.redirect('google', 'http://google.com');</code></pre>576577<p> Now in a route we may call:</p>578579<pre><code>res.redirect('google');</code></pre>580581<p> We may also map dynamic redirects:</p>582583<pre><code> app.redirect('comments', function(req, res, params){584 return '/post/' + params.id + '/comments';585 });</code></pre>586587<p> So now we may do the following, and the redirect will dynamically adjust to588 the context of the request. If we called this route with <em>GET /post/12</em> our589 redirect <em>Location</em> would be <em>/post/12/comments</em>.</p>590591<pre><code> app.get('/post/:id', function(req, res){592 res.redirect('comments');593 });</code></pre>594595<h2></h2>596597<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>598</td>599<td class="code">600<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>){601 <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>;602603 <span class="comment">// Setup redirect map</span>604 <span class="keyword">var</span> <span class="variable">map</span> = {605 <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>,606 <span class="variable">home</span>: <span class="variable">basePath</span>607 };608609 <span class="comment">// Support custom redirect map</span>610 <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>;611612 <span class="comment">// Attempt mapped redirect</span>613 <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>614 ? <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>, <span class="this">this</span>.<span class="variable">req</span>.<span class="variable">params</span>.<span class="variable">path</span>)615 : <span class="variable">map</span>[<span class="variable">url</span>];616617 <span class="comment">// Perform redirect</span>618 <span class="this">this</span>.<span class="variable">writeHead</span>(<span class="variable">status</span> || <span class="number integer">302</span>, { <span class="string">'Location'</span>: <span class="variable">mapped</span> || <span class="variable">url</span> });619 <span class="this">this</span>.<span class="variable">end</span>();620};621</code></pre>622</td>340</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">341<td class="docs">342<p>Module dependencies.343 </p>
287 unmodified lines
631 <span class="keyword">return</span> <span class="variable">arguments</span>.<span class="variable">callee</span>;632})(<span class="string">'get'</span>)(<span class="string">'post'</span>)(<span class="string">'put'</span>)(<span class="string">'del'</span>);633</code></pre>634</td>635</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">636<td class="docs">637<p>Module dependencies.638 </p>639</td>640<td class="code">641<pre><code><span class="keyword">var</span> <span class="variable">fs</span> = <span class="variable">require</span>(<span class="string">'fs'</span>),642 <span class="variable">http</span> = <span class="variable">require</span>(<span class="string">'http'</span>),643 <span class="variable">path</span> = <span class="variable">require</span>(<span class="string">'path'</span>),644 <span class="variable">utils</span> = <span class="variable">require</span>(<span class="string">'connect/utils'</span>),645 <span class="variable">mime</span> = <span class="variable">require</span>(<span class="string">'connect/utils'</span>).<span class="variable">mime</span>,646 <span class="class">Buffer</span> = <span class="variable">require</span>(<span class="string">'buffer'</span>).<span class="class">Buffer</span>;</code></pre>647</td>648</tr>649<tr class="code">650<td class="docs">651<p>Send a response with the given <code>body</code> and optional <code>headers</code> and <code>status</code> code.</p>652653<h2>Examples</h2>654655<pre><code>res.send(new Buffer('wahoo'));656res.send({ some: 'json' });657res.send('&lt;p&gt;some html&lt;/p&gt;');658res.send('Sorry, cant find that', 404);659res.send('text', { 'Content-Type': 'text/plain' }, 201);660res.send(404);</code></pre>661662<h2></h2>663664<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>665</td>666<td class="code">667<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>){668 <span class="comment">// Allow status as second arg</span>669 <span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">headers</span> === <span class="string">'number'</span>) {670 <span class="variable">status</span> = <span class="variable">headers</span>,671 <span class="variable">headers</span> = <span class="keyword">null</span>;672 }673674 <span class="comment">// Defaults</span>675 <span class="variable">status</span> = <span class="variable">status</span> || <span class="number integer">200</span>;676 <span class="variable">headers</span> = <span class="variable">headers</span> || {};677678 <span class="comment">// Determine content type</span>679 <span class="keyword">switch</span> (<span class="keyword">typeof</span> <span class="variable">body</span>) {680 <span class="keyword">case</span> <span class="string">'number'</span>:681 <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Type'</span>]) {682 <span class="this">this</span>.<span class="variable">contentType</span>(<span class="string">'.txt'</span>);683 }684 <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>];685 <span class="keyword">break</span>;686 <span class="keyword">case</span> <span class="string">'string'</span>:687 <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Type'</span>]) {688 <span class="this">this</span>.<span class="variable">contentType</span>(<span class="string">'.html'</span>);689 }690 <span class="keyword">break</span>;691 <span class="keyword">case</span> <span class="string">'object'</span>:692 <span class="keyword">if</span> (<span class="variable">body</span> <span class="variable">instanceof</span> <span class="class">Buffer</span>) {693 <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Type'</span>]) {694 <span class="this">this</span>.<span class="variable">contentType</span>(<span class="string">'.bin'</span>);695 }696 } <span class="keyword">else</span> {697 <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Type'</span>]) {698 <span class="this">this</span>.<span class="variable">contentType</span>(<span class="string">'.json'</span>);699 }700 <span class="variable">body</span> = <span class="class">JSON</span>.<span class="variable">stringify</span>(<span class="variable">body</span>);701 }702 <span class="keyword">break</span>;703 }704705 <span class="comment">// Populate Content-Length</span>706 <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Length'</span>]) {707 <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>708 ? <span class="variable">body</span>.<span class="variable">length</span>709 : <span class="class">Buffer</span>.<span class="variable">byteLength</span>(<span class="variable">body</span>));710 }711712 <span class="comment">// Merge headers passed</span>713 <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>);714715 <span class="comment">// Respond</span>716 <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>);717 <span class="this">this</span>.<span class="variable">end</span>(<span class="variable">body</span>);718};</code></pre>719</td>720</tr>721<tr class="code">722<td class="docs">723<p>Transfer the given <code>file</code>. Automatically sets the <em>Content-Type</em>724response header, and responds with 404 / 500 appropriately.</p>725726<h2></h2>727728<ul><li><p><strong>param</strong>: <em>String</em> file</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>729</td>730<td class="code">731<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">file</span>){732 <span class="keyword">var</span> <span class="variable">self</span> = <span class="this">this</span>;733 <span class="variable">fs</span>.<span class="variable">readFile</span>(<span class="variable">file</span>, <span class="keyword">function</span>(<span class="variable">err</span>, <span class="variable">buf</span>){734 <span class="keyword">if</span> (<span class="variable">err</span>) {735 <span class="variable">self</span>.<span class="variable">send</span>(<span class="variable">err</span>.<span class="variable">errno</span> === <span class="variable">process</span>.<span class="class">ENOENT</span>736 ? <span class="number integer">404</span>737 : <span class="number integer">500</span>);738 } <span class="keyword">else</span> {739 <span class="variable">self</span>.<span class="variable">contentType</span>(<span class="variable">file</span>);740 <span class="variable">self</span>.<span class="variable">send</span>(<span class="variable">buf</span>);741 }742 });743};</code></pre>744</td>745</tr>746<tr class="code">747<td class="docs">748<p>Set <em>Content-Type</em> response header passed through <code>mime.type()</code>.</p>749750<h2>Examples</h2>751752<pre><code>var filename = 'path/to/image.png';753res.contentType(filename);754// res.headers['Content-Type'] is now "image/png"</code></pre>755756<h2></h2>757758<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>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">contentType</span> = <span class="keyword">function</span>(<span class="variable">type</span>){762 <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>));763};</code></pre>764</td>765</tr>766<tr class="code">767<td class="docs">768<p>Set <em>Content-Disposition</em> header to <em>attachment</em> with optional <code>filename</code>.</p>769770<h2></h2>771772<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>773</td>774<td class="code">775<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>){776 <span class="this">this</span>.<span class="variable">header</span>(<span class="string">'Content-Disposition'</span>, <span class="variable">filename</span>777 ? <span class="string">'attachment; filename=&quot;'</span> + <span class="variable">path</span>.<span class="variable">basename</span>(<span class="variable">filename</span>) + <span class="string">'&quot;'</span>778 : <span class="string">'attachment'</span>);779 <span class="keyword">return</span> <span class="this">this</span>;780};</code></pre>781</td>782</tr>783<tr class="code">784<td class="docs">785<p>Transfer the given <code>file</code> with optional <code>filename</code> as an attachment.</p>786787<h2></h2>788789<ul><li><p><strong>param</strong>: <em>String</em> file</p></li><li><p><strong>param</strong>: <em>String</em> filename</p></li><li><p><strong>return</strong>: <em>Type</em> </p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>790</td>791<td class="code">792<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">file</span>, <span class="variable">filename</span>){793 <span class="this">this</span>.<span class="variable">attachment</span>(<span class="variable">filename</span> || <span class="variable">file</span>).<span class="variable">sendfile</span>(<span class="variable">file</span>);794};</code></pre>795</td>796</tr>797<tr class="code">798<td class="docs">799<p>Set or get response header <code>name</code> with optional <code>val</code>.</p>800801<h2></h2>802803<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>804</td>805<td class="code">806<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>){807 <span class="keyword">return</span> <span class="variable">val</span> === <span class="variable">undefined</span>808 ? <span class="this">this</span>.<span class="variable">headers</span>[<span class="variable">name</span>]809 : <span class="this">this</span>.<span class="variable">headers</span>[<span class="variable">name</span>] = <span class="variable">val</span>;810};</code></pre>811</td>812</tr>813<tr class="code">814<td class="docs">815<p>Redirect to the given <code>url</code> with optional response <code>status</code>816defauling to 302.</p>817818<p>The given <code>url</code> can also be the name of a mapped url, for819example by default express supports "back" which redirects820to the <em>Referrer</em> or <em>Referer</em> headers or the application's821"home" setting. Express also supports "home" out of the box,822which can be set via <code>app.set('home', '/blog');</code>, and defaults823to '/'.</p>824825<h2>Redirect Mapping</h2>826827<p> To extend the redirect mapping capabilities that Express provides,828 we may use the <code>app.redirect()</code> method:</p>829830<pre><code>app.redirect('google', 'http://google.com');</code></pre>831832<p> Now in a route we may call:</p>833834<pre><code>res.redirect('google');</code></pre>835836<p> We may also map dynamic redirects:</p>837838<pre><code> app.redirect('comments', function(req, res, params){839 return '/post/' + params.id + '/comments';840 });</code></pre>841842<p> So now we may do the following, and the redirect will dynamically adjust to843 the context of the request. If we called this route with <em>GET /post/12</em> our844 redirect <em>Location</em> would be <em>/post/12/comments</em>.</p>845846<pre><code> app.get('/post/:id', function(req, res){847 res.redirect('comments');848 });</code></pre>849850<h2></h2>851852<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>853</td>854<td class="code">855<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>){856 <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>;857858 <span class="comment">// Setup redirect map</span>859 <span class="keyword">var</span> <span class="variable">map</span> = {860 <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>,861 <span class="variable">home</span>: <span class="variable">basePath</span>862 };863864 <span class="comment">// Support custom redirect map</span>865 <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>;866867 <span class="comment">// Attempt mapped redirect</span>868 <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>869 ? <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>, <span class="this">this</span>.<span class="variable">req</span>.<span class="variable">params</span>.<span class="variable">path</span>)870 : <span class="variable">map</span>[<span class="variable">url</span>];871872 <span class="comment">// Perform redirect</span>873 <span class="this">this</span>.<span class="variable">writeHead</span>(<span class="variable">status</span> || <span class="number integer">302</span>, { <span class="string">'Location'</span>: <span class="variable">mapped</span> || <span class="variable">url</span> });874 <span class="this">this</span>.<span class="variable">end</span>();875};876</code></pre>877</td>878</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">879<td class="docs">880<p>Parse mini markdown implementation.881The following conversions are supported,882primarily for the "flash" middleware:</p>883884<p> <em>foo</em> or <em>foo</em> become &lt;em&gt;foo&lt;/em&gt;885 <strong>foo</strong> or <strong>foo</strong> become &lt;strong&gt;foo&lt;/strong&gt;886 <a href="B">A</a> becomes &lt;a href="B"&gt;A&lt;/a&gt;</p>887888<h2></h2>889890<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>public</em></p></li></ul>891</td>892<td class="code">893<pre><code><span class="variable">exports</span>.<span class="variable">miniMarkdown</span> = <span class="keyword">function</span>(<span class="variable">str</span>){894 <span class="keyword">return</span> <span class="class">String</span>(<span class="variable">str</span>)895 .<span class="variable">replace</span>(<span class="regexp">/(__|\*\*)(.*?)\1/g</span>, <span class="string">'&lt;strong&gt;$2&lt;/strong&gt;'</span>)896 .<span class="variable">replace</span>(<span class="regexp">/(_|\*)(.*?)\1/g</span>, <span class="string">'&lt;em&gt;$2&lt;/em&gt;'</span>)897 .<span class="variable">replace</span>(<span class="regexp">/\[([^\]]+)\]\(([^)]+)\)/g</span>, <span class="string">'&lt;a href=&quot;$2&quot;&gt;$1&lt;/a&gt;'</span>);898};</code></pre>899</td>900</tr>901<tr class="code">902<td class="docs">903<p>Escape special characters in the given string of html.</p>904905<h2></h2>906907<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>public</em></p></li></ul>908</td>909<td class="code">910<pre><code><span class="variable">exports</span>.<span class="variable">htmlEscape</span> = <span class="keyword">function</span>(<span class="variable">html</span>) {911 <span class="keyword">return</span> <span class="class">String</span>(<span class="variable">html</span>)912 .<span class="variable">replace</span>(<span class="regexp">/&amp;/g</span>, <span class="string">'&amp;'</span>)913 .<span class="variable">replace</span>(<span class="regexp">/&quot;/g</span>, <span class="string">'&quot;'</span>)914 .<span class="variable">replace</span>(<span class="regexp">/&lt;/g</span>, <span class="string">'&lt;'</span>)915 .<span class="variable">replace</span>(<span class="regexp">/&gt;/g</span>, <span class="string">'&gt;'</span>);916};</code></pre>917</td>918</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">919<td class="docs">920<p>Module dependencies.921 </p>
231 unmodified lines
modified:docs/guide.1+1 added-1 removed
143 unmodified lines
144.IP "\(bu" 4145\fIview engine\fR Default view engine name for views rendered without extensions146.147.IP "\(bu" 4148\fIreload views\fR Reloads altered views, by default watches for \fImtime\fR changes with with a 5 minute interval\. Example: \fIapp\.set(\'reload views\', 60000);\fR148\fIreload views\fR Reloads altered views, by default watches for \fImtime\fR changes with with a 500 millisecond interval\.149.150.IP "" 0151.152.SS "Routing"
981 unmodified lines
modified:docs/guide.html+1 added-1 removed
319 unmodified lines
320<li><em>home</em> Application base path used for <em>res.redirect()</em> and transparently handling mounted apps.</li>321<li><em>views</em> Root views directory defaulting to <strong>CWD/views</strong></li>322<li><em>view engine</em> Default view engine name for views rendered without extensions</li>323<li><em>reload views</em> Reloads altered views, by default watches for <em>mtime</em> changes with324 with a 5 minute interval. Example: <em>app.set('reload views', 60000);</em></li>324 with a 500 millisecond interval.</li>325</ul>326327328<h3 id="Routing">Routing</h3>
648 unmodified lines