More 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 0bb4815 · Browse files · 4 files·+461 added−234 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+461 added−234 removed
- other:+461 added−234 removed
Provenance
- Commit
bedff32- Tree
345a779- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
0bb4815
4 files changed+461 added−234 removed
modified:docs/api.html+310 added-233 removed
316 unmodified lines317 <li><p><strong>param</strong>: <em>String</em> name</p></li>318 <li><p><strong>return</strong>: <em>String</em></p></li>319 <li><p><strong>api</strong>: <em>public</em></p></li>320 </ul>321-���:321+322 </td>323 <td class="code">324 <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>){325 <span class="comment">// Route params like /user/:id</span>80 unmodified lines406 <td class="code">407 <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>);408 <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>409 </td>410+</tr><tr class="filename"><td><h2 id="lib/express/response.js">response</h2></td><td>lib/express/response.js</td></tr><tr class="code">411+<td class="docs">412+<p>Module dependencies.</p>413+</td>414+<td class="code">415+<pre><code><span class="keyword">var</span> <span class="variable">http</span> = <span class="variable">require</span>(<span class="string">'http'</span>),416+ <span class="variable">path</span> = <span class="variable">require</span>(<span class="string">'path'</span>),417+ <span class="variable">utils</span> = <span class="variable">require</span>(<span class="string">'connect/utils'</span>),418+ <span class="variable">mime</span> = <span class="variable">require</span>(<span class="string">'connect/utils'</span>).<span class="variable">mime</span>,419+ <span class="class">Buffer</span> = <span class="variable">require</span>(<span class="string">'buffer'</span>).<span class="class">Buffer</span>;</code></pre>420+</td>421+</tr>422+<tr class="code">423+<td class="docs">424+<p>Send a response with the given <code>body</code> and optional <code>headers</code> and <code>status</code> code.</p>425+426+<h2>Examples</h2>427+428+<pre><code>res.send(new Buffer('wahoo'));429+res.send({ some: 'json' });430+res.send('<p>some html</p>');431+res.send('Sorry, cant find that', 404);432+res.send('text', { 'Content-Type': 'text/plain' }, 201);433+res.send(404);434+</code></pre>435+436+<h2></h2>437+438+<ul>439+<li><p><strong>param</strong>: <em>String | Object | Number | Buffer</em> body or status</p></li>440+<li><p><strong>param</strong>: <em>Object | Number</em> headers or status</p></li>441+<li><p><strong>param</strong>: <em>Number</em> status</p></li>442+<li><p><strong>return</strong>: <em>ServerResponse</em></p></li>443+<li><p><strong>api</strong>: <em>public</em></p></li>444+</ul>445+446+</td>447+<td class="code">448+<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>){449+ <span class="comment">// Allow status as second arg</span>450+ <span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">headers</span> === <span class="string">'number'</span>) {451+ <span class="variable">status</span> = <span class="variable">headers</span>,452+ <span class="variable">headers</span> = <span class="keyword">null</span>;453+ }454+455+ <span class="comment">// Defaults</span>456+ <span class="variable">status</span> = <span class="variable">status</span> || <span class="number integer">200</span>;457+ <span class="variable">headers</span> = <span class="variable">headers</span> || {};458+459+ <span class="comment">// Determine content type</span>460+ <span class="keyword">switch</span> (<span class="keyword">typeof</span> <span class="variable">body</span>) {461+ <span class="keyword">case</span> <span class="string">'number'</span>:462+ <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Type'</span>]) {463+ <span class="this">this</span>.<span class="variable">contentType</span>(<span class="string">'.txt'</span>);464+ }465+ <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>];466+ <span class="keyword">break</span>;467+ <span class="keyword">case</span> <span class="string">'string'</span>:468+ <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Type'</span>]) {469+ <span class="this">this</span>.<span class="variable">contentType</span>(<span class="string">'.html'</span>);470+ }471+ <span class="keyword">break</span>;472+ <span class="keyword">case</span> <span class="string">'object'</span>:473+ <span class="keyword">if</span> (!(<span class="variable">body</span> <span class="variable">instanceof</span> <span class="class">Buffer</span>)) {474+ <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Type'</span>]) {475+ <span class="this">this</span>.<span class="variable">contentType</span>(<span class="string">'.json'</span>);476+ }477+ <span class="variable">body</span> = <span class="class">JSON</span>.<span class="variable">stringify</span>(<span class="variable">body</span>);478+ }479+ <span class="keyword">break</span>;480+ }481+482+ <span class="comment">// Populate Content-Length</span>483+ <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Length'</span>]) {484+ <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>485+ ? <span class="variable">body</span>.<span class="variable">length</span>486+ : <span class="class">Buffer</span>.<span class="variable">byteLength</span>(<span class="variable">body</span>));487+ }488+489+ <span class="comment">// Merge headers passed</span>490+ <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>);491+492+ <span class="comment">// Respond</span>493+ <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>);494+ <span class="this">this</span>.<span class="variable">end</span>(<span class="variable">body</span>);495+};</code></pre>496+</td>497+</tr>498+<tr class="code">499+<td class="docs">500+<p>Set <em>Content-Type</em> response header passed through <code>mime.type()</code>.</p>501+502+<h2>Examples</h2>503+504+<pre><code>var filename = 'path/to/image.png';505+res.contentType(filename);506+// res.headers['Content-Type'] is now "image/png"507+</code></pre>508+509+<h2></h2>510+511+<ul>512+<li><p><strong>param</strong>: <em>String</em> type</p></li>513+<li><p><strong>return</strong>: <em>String</em> the resolved mime type</p></li>514+<li><p><strong>api</strong>: <em>public</em></p></li>515+</ul>516+517+</td>518+<td class="code">519+<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>){520+ <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>));521+};</code></pre>522+</td>523+</tr>524+<tr class="code">525+<td class="docs">526+<p>Set <em>Content-Disposition</em> header to <em>attachment</em> with optional <code>filename</code>.</p>527+528+<h2></h2>529+530+<ul>531+<li><p><strong>param</strong>: <em>String</em> filename</p></li>532+<li><p><strong>return</strong>: <em>ServerResponse</em></p></li>533+<li><p><strong>api</strong>: <em>public</em></p></li>534+</ul>535+536+</td>537+<td class="code">538+<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>){539+ <span class="this">this</span>.<span class="variable">header</span>(<span class="string">'Content-Disposition'</span>, <span class="variable">filename</span>540+ ? <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>541+ : <span class="string">'attachment'</span>);542+ <span class="keyword">return</span> <span class="this">this</span>;543+};</code></pre>544+</td>545+</tr>546+<tr class="code">547+<td class="docs">548+<p>Set or get response header <code>name</code> with optional <code>val</code>.</p>549+550+<h2></h2>551+552+<ul>553+<li><p><strong>param</strong>: <em>String</em> name</p></li>554+<li><p><strong>param</strong>: <em>String</em> val</p></li>555+<li><p><strong>return</strong>: <em>String</em></p></li>556+<li><p><strong>api</strong>: <em>public</em></p></li>557+</ul>558+559+</td>560+<td class="code">561+<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>){562+ <span class="keyword">return</span> <span class="variable">val</span> === <span class="variable">undefined</span>563+ ? <span class="this">this</span>.<span class="variable">headers</span>[<span class="variable">name</span>]564+ : <span class="this">this</span>.<span class="variable">headers</span>[<span class="variable">name</span>] = <span class="variable">val</span>;565+};</code></pre>566+</td>567+</tr>568+<tr class="code">569+<td class="docs">570+<p>Redirect to the given <code>url</code> with optional response <code>status</code>571+defauling to 302.</p>572+573+<p>The given <code>url</code> can also be the name of a mapped url, for574+example by default express supports “back” which redirects575+to the <em>Referrer</em> or <em>Referer</em> headers or the application’s576+“home” setting. Express also supports “home” out of the box,577+which can be set via <code>app.set('home', '/blog');</code>, and defaults578+to ‘/’.</p>579+580+<h2>Redirect Mapping</h2>581+582+<p> To extend the redirect mapping capabilities that Express provides,583+ we may use the <code>app.redirect()</code> method:</p>584+585+<pre><code>app.redirect('google', 'http://google.com');586+</code></pre>587+588+<p> Now in a route we may call:</p>589+590+<pre><code>res.redirect('google');591+</code></pre>592+593+<p> We may also map dynamic redirects:</p>594+595+<pre><code> app.redirect('comments', function(req, res, params){596+ return '/post/' + params.id + '/comments';597+ });598+</code></pre>599+600+<p> So now we may do the following, and the redirect will dynamically adjust to601+ the context of the request. If we called this route with <em>GET /post/12</em> our602+ redirect <em>Location</em> would be <em>/post/12/comments</em>.</p>603+604+<pre><code> app.get('/post/:id', function(req, res){605+ res.redirect('comments');606+ });607+</code></pre>608+609+<h2></h2>610+611+<ul>612+<li><p><strong>param</strong>: <em>String</em> url</p></li>613+<li><p><strong>param</strong>: <em>Number</em> code</p></li>614+<li><p><strong>api</strong>: <em>public</em></p></li>615+</ul>616+617+</td>618+<td class="code">619+<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>){620+ <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>;621+622+ <span class="comment">// Setup redirect map</span>623+ <span class="keyword">var</span> <span class="variable">map</span> = {624+ <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>,625+ <span class="variable">home</span>: <span class="variable">basePath</span>626+ };627+628+ <span class="comment">// Support custom redirect map</span>629+ <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>;630+631+ <span class="comment">// Attempt mapped redirect</span>632+ <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>633+ ? <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>)634+ : <span class="variable">map</span>[<span class="variable">url</span>];635+636+ <span class="comment">// Perform redirect</span>637+ <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> });638+ <span class="this">this</span>.<span class="variable">end</span>();639+};640+</code></pre>641+</td>642+</tr><tr class="filename"><td><h2 id="lib/express/utils.js">utils</h2></td><td>lib/express/utils.js</td></tr><tr class="code">643+<td class="docs">644+<p>Parse mini markdown implementation.645+The following conversions are supported,646+primarily for the “flash” middleware:</p>647+648+<p> <em>foo</em> or <em>foo</em> become <em>foo</em>649+ <strong>foo</strong> or <strong>foo</strong> become <strong>foo</strong>650+ <a href="B">A</a> becomes <a href="B">A</a></p>651+652+<h2></h2>653+654+<ul>655+<li><p><strong>param</strong>: <em>String</em> str</p></li>656+<li><p><strong>return</strong>: <em>String</em></p></li>657+<li><p><strong>api</strong>: <em>public</em></p></li>658+</ul>659+ 660+</td>661+<td class="code">662+<pre><code><span class="variable">exports</span>.<span class="variable">miniMarkdown</span> = <span class="keyword">function</span>(<span class="variable">str</span>){663+ <span class="keyword">return</span> <span class="class">String</span>(<span class="variable">str</span>)664+ .<span class="variable">replace</span>(<span class="regexp">/(__|\*\*)(.*?)\1/g</span>, <span class="string">'<strong>$2</strong>'</span>)665+ .<span class="variable">replace</span>(<span class="regexp">/(_|\*)(.*?)\1/g</span>, <span class="string">'<em>$2</em>'</span>)666+ .<span class="variable">replace</span>(<span class="regexp">/\[([^\]]+)\]\(([^)]+)\)/g</span>, <span class="string">'<a href="$2">$1</a>'</span>);667+};</code></pre>668+</td>669+</tr>670+<tr class="code">671+<td class="docs">672+<p>Escape special characters in the given string of html.</p>673+674+<h2></h2>675+676+<ul>677+<li><p><strong>param</strong>: <em>String</em> html</p></li>678+<li><p><strong>return</strong>: <em>String</em></p></li>679+<li><p><strong>api</strong>: <em>public</em></p></li>680+</ul>681+682+</td>683+<td class="code">684+<pre><code><span class="variable">exports</span>.<span class="variable">htmlEscape</span> = <span class="keyword">function</span>(<span class="variable">html</span>) {685+ <span class="keyword">return</span> <span class="class">String</span>(<span class="variable">html</span>)686+ .<span class="variable">replace</span>(<span class="regexp">/&/g</span>, <span class="string">'&'</span>)687+ .<span class="variable">replace</span>(<span class="regexp">/"/g</span>, <span class="string">'"'</span>)688+ .<span class="variable">replace</span>(<span class="regexp">/</g</span>, <span class="string">'<'</span>)689+ .<span class="variable">replace</span>(<span class="regexp">/>/g</span>, <span class="string">'>'</span>);690+};</code></pre>691+</td>692 </tr><tr class="filename"><td><h2 id="lib/express/server.js">server</h2></td><td>lib/express/server.js</td></tr><tr class="code">693 <td class="docs">694 <p>Module dependencies.</p>695 </td>22 unmodified lines718 <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>){719 <span class="keyword">var</span> <span class="variable">self</span> = <span class="this">this</span>;720 <span class="this">this</span>.<span class="variable">config</span> = {};721 <span class="this">this</span>.<span class="variable">settings</span> = {};722+ <span class="this">this</span>.<span class="variable">redirects</span> = {};723 <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> || []);724 725 <span class="comment">// Expose objects to each other</span>726 <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>){20 unmodified lines747 </td>748 </tr>749 <tr class="code">750 <td class="docs">468-<p>Inherit from <code>connect.Server</code>.</p>751+<p>Inherit from <code>connect.Server</code>.</p>�
`752 </td>753 <td class="code">754 <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>755 </td>141 unmodified lines897 </td>898 </tr>899 <tr class="code">900 <td class="docs">618-<p>Disable <code>setting</code>.</p>901+<p>Redirect <code>key</code> to <code>url</code>.</p>902 903 <h2></h2>904 905 <ul>623-<li><p><strong>param</strong>: <em>String</em> setting</p></li>906+<li><p><strong>param</strong>: <em>String</em> key</p></li>907+<li><p><strong>param</strong>: <em>String</em> url</p></li>908 <li><p><strong>return</strong>: <em>Server</em> for chaining</p></li>909 <li><p><strong>api</strong>: <em>public</em></p></li>910 </ul>911 912 </td>913 <td class="code">630-<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>){631- <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>;632- (<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>);914+<pre><code><span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">redirect</span> = <span class="keyword">function</span>(<span class="variable">key</span>, <span class="variable">url</span>){915+ <span class="this">this</span>.<span class="variable">redirects</span>[<span class="variable">key</span>] = <span class="variable">url</span>;916 <span class="keyword">return</span> <span class="this">this</span>;634-};635-636-<span class="comment">// Generate routing methods</span>637-638-(<span class="keyword">function</span>(<span class="variable">method</span>){639- <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>){640- <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>);641- <span class="keyword">return</span> <span class="this">this</span>;642- };643- <span class="keyword">return</span> <span class="variable">arguments</span>.<span class="variable">callee</span>;644-})(<span class="string">'get'</span>)(<span class="string">'post'</span>)(<span class="string">'put'</span>)(<span class="string">'del'</span>);645-</code></pre>646-</td>647-</tr><tr class="filename"><td><h2 id="lib/express/utils.js">utils</h2></td><td>lib/express/utils.js</td></tr><tr class="code">648-<td class="docs">649-<p>Parse mini markdown implementation.650-The following conversions are supported,651-primarily for the “flash” middleware:</p>652-653-<p> <em>foo</em> or <em>foo</em> become <em>foo</em>654- <strong>foo</strong> or <strong>foo</strong> become <strong>foo</strong>655- <a href="B">A</a> becomes <a href="B">A</a></p>656-657-<h2></h2>658-659-<ul>660-<li><p><strong>param</strong>: <em>String</em> str</p></li>661-<li><p><strong>return</strong>: <em>String</em></p></li>662-<li><p><strong>api</strong>: <em>public</em></p></li>663-</ul>664-665-</td>666-<td class="code">667-<pre><code><span class="variable">exports</span>.<span class="variable">miniMarkdown</span> = <span class="keyword">function</span>(<span class="variable">str</span>){668- <span class="keyword">return</span> <span class="class">String</span>(<span class="variable">str</span>)669- .<span class="variable">replace</span>(<span class="regexp">/(__|\*\*)(.*?)\1/g</span>, <span class="string">'<strong>$2</strong>'</span>)670- .<span class="variable">replace</span>(<span class="regexp">/(_|\*)(.*?)\1/g</span>, <span class="string">'<em>$2</em>'</span>)671- .<span class="variable">replace</span>(<span class="regexp">/\[([^\]]+)\]\(([^)]+)\)/g</span>, <span class="string">'<a href="$2">$1</a>'</span>);917 };</code></pre>918 </td>919 </tr>920 <tr class="code">921 <td class="docs">677-<p>Escape special characters in the given string of html.</p>922+<p>Disable <code>setting</code>.</p>923 924 <h2></h2>925 926 <ul>682-<li><p><strong>param</strong>: <em>String</em> html</p></li>683-<li><p><strong>return</strong>: <em>String</em></p></li>927+<li><p><strong>param</strong>: <em>String</em> setting</p></li>928+<li><p><strong>return</strong>: <em>Server</em> for chaining</p></li>929 <li><p><strong>api</strong>: <em>public</em></p></li>930 </ul>686-931+Q932 </td>933 <td class="code">689-<pre><code><span class="variable">exports</span>.<span class="variable">htmlEscape</span> = <span class="keyword">function</span>(<span class="variable">html</span>) {690- <span class="keyword">return</span> <span class="class">String</span>(<span class="variable">html</span>)691- .<span class="variable">replace</span>(<span class="regexp">/&/g</span>, <span class="string">'&'</span>)692- .<span class="variable">replace</span>(<span class="regexp">/"/g</span>, <span class="string">'"'</span>)693- .<span class="variable">replace</span>(<span class="regexp">/</g</span>, <span class="string">'<'</span>)694- .<span class="variable">replace</span>(<span class="regexp">/>/g</span>, <span class="string">'>'</span>);695-};</code></pre>696-</td>697-</tr><tr class="filename"><td><h2 id="lib/express/response.js">response</h2></td><td>lib/express/response.js</td></tr><tr class="code">698-<td class="docs">699-<p>Module dependencies.</p>700-</td>701-<td class="code">702-<pre><code><span class="keyword">var</span> <span class="variable">http</span> = <span class="variable">require</span>(<span class="string">'http'</span>),703- <span class="variable">path</span> = <span class="variable">require</span>(<span class="string">'path'</span>),704- <span class="variable">utils</span> = <span class="variable">require</span>(<span class="string">'connect/utils'</span>),705- <span class="variable">mime</span> = <span class="variable">require</span>(<span class="string">'connect/utils'</span>).<span class="variable">mime</span>,706- <span class="class">Buffer</span> = <span class="variable">require</span>(<span class="string">'buffer'</span>).<span class="class">Buffer</span>;</code></pre>707-</td>708-</tr>709-<tr class="code">710-<td class="docs">711-<p>Send a response with the given <code>body</code> and optional <code>headers</code> and <code>status</code> code.</p>712-713-<h2>Examples</h2>714-715-<pre><code>res.send(new Buffer('wahoo'));716-res.send({ some: 'json' });717-res.send('<p>some html</p>');718-res.send('Sorry, cant find that', 404);719-res.send('text', { 'Content-Type': 'text/plain' }, 201);720-res.send(404);721-</code></pre>722-723-<h2></h2>724-725-<ul>726-<li><p><strong>param</strong>: <em>String | Object | Number | Buffer</em> body or status</p></li>727-<li><p><strong>param</strong>: <em>Object | Number</em> headers or status</p></li>728-<li><p><strong>param</strong>: <em>Number</em> status</p></li>729-<li><p><strong>return</strong>: <em>ServerResponse</em></p></li>730-<li><p><strong>api</strong>: <em>public</em></p></li>731-</ul>732-733-</td>734-<td class="code">735-<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>){736- <span class="comment">// Allow status as second arg</span>737- <span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">headers</span> === <span class="string">'number'</span>) {738- <span class="variable">status</span> = <span class="variable">headers</span>,739- <span class="variable">headers</span> = <span class="keyword">null</span>;740- }741-742- <span class="comment">// Defaults</span>743- <span class="variable">status</span> = <span class="variable">status</span> || <span class="number integer">200</span>;744- <span class="variable">headers</span> = <span class="variable">headers</span> || {};745-746- <span class="comment">// Determine content type</span>747- <span class="keyword">switch</span> (<span class="keyword">typeof</span> <span class="variable">body</span>) {748- <span class="keyword">case</span> <span class="string">'number'</span>:749- <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Type'</span>]) {750- <span class="this">this</span>.<span class="variable">contentType</span>(<span class="string">'.txt'</span>);751- }752- <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>];753- <span class="keyword">break</span>;754- <span class="keyword">case</span> <span class="string">'string'</span>:755- <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Type'</span>]) {756- <span class="this">this</span>.<span class="variable">contentType</span>(<span class="string">'.html'</span>);757- }758- <span class="keyword">break</span>;759- <span class="keyword">case</span> <span class="string">'object'</span>:760- <span class="keyword">if</span> (!(<span class="variable">body</span> <span class="variable">instanceof</span> <span class="class">Buffer</span>)) {761- <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Type'</span>]) {762- <span class="this">this</span>.<span class="variable">contentType</span>(<span class="string">'.json'</span>);763- }764- <span class="variable">body</span> = <span class="class">JSON</span>.<span class="variable">stringify</span>(<span class="variable">body</span>);765- }766- <span class="keyword">break</span>;767- }768-769- <span class="comment">// Populate Content-Length</span>770- <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Length'</span>]) {771- <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>772- ? <span class="variable">body</span>.<span class="variable">length</span>773- : <span class="class">Buffer</span>.<span class="variable">byteLength</span>(<span class="variable">body</span>));774- }775-776- <span class="comment">// Merge headers passed</span>777- <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>);778-779- <span class="comment">// Respond</span>780- <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>);781- <span class="this">this</span>.<span class="variable">end</span>(<span class="variable">body</span>);782-};</code></pre>783-</td>784-</tr>785-<tr class="code">786-<td class="docs">787-<p>Set <em>Content-Type</em> response header passed through <code>mime.type()</code>.</p>788-789-<h2>Examples</h2>790-791-<pre><code>var filename = 'path/to/image.png';792-res.contentType(filename);793-// res.headers['Content-Type'] is now "image/png"794-</code></pre>795-796-<h2></h2>797-798-<ul>799-<li><p><strong>param</strong>: <em>String</em> type</p></li>800-<li><p><strong>return</strong>: <em>String</em> the resolved mime type</p></li>801-<li><p><strong>api</strong>: <em>public</em></p></li>802-</ul>803-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">contentType</span> = <span class="keyword">function</span>(<span class="variable">type</span>){807- <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>));808-};</code></pre>809-</td>810-</tr>811-<tr class="code">812-<td class="docs">813-<p>Set <em>Content-Disposition</em> header to <em>attachment</em> with optional <code>filename</code>.</p>814-815-<h2></h2>816-817-<ul>818-<li><p><strong>param</strong>: <em>String</em> filename</p></li>819-<li><p><strong>return</strong>: <em>ServerResponse</em></p></li>820-<li><p><strong>api</strong>: <em>public</em></p></li>821-</ul>822-823-</td>824-<td class="code">825-<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>){826- <span class="this">this</span>.<span class="variable">header</span>(<span class="string">'Content-Disposition'</span>, <span class="variable">filename</span>827- ? <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>828- : <span class="string">'attachment'</span>);934+<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>){935+ <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>;936+ (<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>);937 <span class="keyword">return</span> <span class="this">this</span>;830-};</code></pre>831-</td>832-</tr>833-<tr class="code">834-<td class="docs">835-<p>Set or get response header <code>name</code> with optional <code>val</code>.</p>836-837-<h2></h2>838-839-<ul>840-<li><p><strong>param</strong>: <em>String</em> name</p></li>841-<li><p><strong>param</strong>: <em>String</em> val</p></li>842-<li><p><strong>return</strong>: <em>String</em></p></li>843-<li><p><strong>api</strong>: <em>public</em></p></li>844-</ul>845-846-</td>847-<td class="code">848-<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>){849- <span class="keyword">return</span> <span class="variable">val</span> === <span class="variable">undefined</span>850- ? <span class="this">this</span>.<span class="variable">headers</span>[<span class="variable">name</span>]851- : <span class="this">this</span>.<span class="variable">headers</span>[<span class="variable">name</span>] = <span class="variable">val</span>;938 };939 854-<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">code</span>){855- <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>;940+<span class="comment">// Generate routing methods</span>941 857- <span class="comment">// Setup redirect map</span>858- <span class="keyword">var</span> <span class="variable">map</span> = {859- <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>,860- <span class="variable">home</span>: <span class="variable">basePath</span>942+(<span class="keyword">function</span>(<span class="variable">method</span>){943+ <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>){944+ <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>);945+ <span class="keyword">return</span> <span class="this">this</span>;946 };862-863- <span class="comment">// Support custom redirect map</span>864- <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">app</span>.<span class="variable">redirect</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">redirect</span>;866- }867-868- <span class="comment">// Perform redirect</span>869- <span class="this">this</span>.<span class="variable">writeHead</span>(<span class="variable">code</span> || <span class="number integer">302</span>, {870- <span class="string">'Location'</span>: <span class="variable">map</span>[<span class="variable">url</span>] || <span class="variable">url</span>871- });872- <span class="this">this</span>.<span class="variable">end</span>();873-};947+ <span class="keyword">return</span> <span class="variable">arguments</span>.<span class="variable">callee</span>;948+})(<span class="string">'get'</span>)(<span class="string">'post'</span>)(<span class="string">'put'</span>)(<span class="string">'del'</span>);949 </code></pre>950 </td>951 </tr><tr class="filename"><td><h2 id="lib/express/view.js">view</h2></td><td>lib/express/view.js</td></tr><tr class="code">952 <td class="docs">42 unmodified lines995 996 <ul>997 <li><strong>api</strong>: <em>public</em></li>998 </ul>924-� %999+r1000+of cal 1001 </td>1002 <td class="code">1003 <pre><code><span class="variable">exports</span>.<span class="variable">clearCache</span> = <span class="keyword">function</span>(){1004 <span class="variable">viewCache</span> = {};6 unmodified lines1011 1012 <ul>1013 <li><strong>type</strong>: <em>Object</em></li>1014 </ul>939-� %1015+P1016 </td>1017 <td class="code">1018 <pre><code><span class="keyword">var</span> <span class="variable">helpers</span> = <span class="variable">exports</span>.<span class="variable">helpers</span> = {};</code></pre>1019 </td>25 unmodified lines1045 <li><p><strong>param</strong>: <em>Object | Array</em> options or collection</p></li>1046 <li><p><strong>return</strong>: <em>String</em></p></li>1047 <li><p><strong>api</strong>: <em>public</em></p></li>1048 </ul>973- %1049+P1050 </td>1051 <td class="code">1052 <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>){1053 <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>) {57 unmodified lines1111 <li><p><strong>param</strong>: <em>Object</em> options</p></li>1112 <li><p><strong>param</strong>: <em>Function</em> fn</p></li>1113 <li><p><strong>api</strong>: <em>public</em></p></li>1114 </ul>1039-1115+(.*?)\/[a-z]*)/1116+})1117 </td>1118 <td class="code">1119 <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>){1120 <span class="variable">options</span> = <span class="variable">options</span> || {};93 unmodified linesmodified:docs/guide.1+72 added
74 unmodified lines75 .SS "Settings"76 Express supports the following settings out of the box:77 .78 .IP "\(bu" 479+\fIhome\fR Application base path used with \fBres\.redirect()\fR80+.81+.IP "\(bu" 482 \fIviews\fR Root views directory defaulting to \fBCWD/views\fR83 .84 .IP "\(bu" 485 \fIview engine\fR Default view engine name for views rendered without extensions123 unmodified lines209 .210 .fi211 .212 .IP "" 0213+.214+.SS "ServerResponse#redirect(url[, status])"215+Redirect to the given \fIurl\fR with a default response \fIstatus\fR of 302\.216+.217+.IP "" 4218+.219+.nf220 221+res\.redirect(\'/\', 301);222+res\.redirect(\'/account\');223+res\.redirect(\'http://google\.com\');224+res\.redirect(\'home\');225+res\.redirect(\'back\');226+.227+.fi228+.229+.IP "" 0230+.231+.P232+Express supports \"redirect mapping\", which by default provides \fIhome\fR, and \fIback\fR\. The \fIback\fR map checks the \fIReferrer\fR and \fIReferer\fR headers, while \fIhome\fR utilizes the \"home\" setting and defaults to \"/\"\.233+.234+.SS "Server#redirect(name, val)"235+For use with \fBres\.redirect()\fR we can map redirects at the application level as shown below:236+.237+.IP "" 4238+.239+.nf240+241+app\.redirect(\'google\', \'http://google\.com\');242+.243+.fi244+.245+.IP "" 0246+.247+.P248+Now in a route we may call:249+.250+.P251+res\.redirect(\'google\');252+.253+.P254+We may also map dynamic redirects:255+.256+.IP "" 4257+.258+.nf259+260+app\.redirect(\'comments\', function(req, res, params){261+ return \'/post/\' + params\.id + \'/comments\';262+});263+.264+.fi265+.266+.IP "" 0267+.268+.P269+So now we may do the following, and the redirect will dynamically adjust to the context of the request\. If we called this route with \fIGET /post/12\fR our redirect \fILocation\fR would be \fI/post/12/comments\fR\.270+.271+.IP "" 4272+.273+.nf274+275+app\.get(\'/post/:id\', function(req, res){276+ res\.redirect(\'comments\');277+});278+.279+.fi280+.281+.IP "" 0282+modified:docs/guide.html+43 added
217 unmodified lines218 219 <p>Express supports the following settings out of the box:</p>220 221 <ul>222+<li><em>home</em> Application base path used with <code>res.redirect()</code></li>223 <li><em>views</em> Root views directory defaulting to <strong>CWD/views</strong></li>224 <li><em>view engine</em> Default view engine name for views rendered without extensions</li>225 <li><em>reload views</em> Reloads altered views, by default watches for <em>mtime</em> changes with226 with a 5 minute interval. Example: <code>app.set('reload views', { interval: 60000 });</code></li>86 unmodified lines313 connect.bodyDecoder()314 );315 </code></pre>316 317+<h3 id="ServerResponse-redirect-url-status-">ServerResponse#redirect(url[, status])</h3>318+319+<p>Redirect to the given <em>url</em> with a default response <em>status</em> of 302.</p>320+321+<pre><code>res.redirect('/', 301);322+res.redirect('/account');323+res.redirect('http://google.com');324+res.redirect('home');325+res.redirect('back');326+</code></pre>327+328+<p>Express supports "redirect mapping", which by default provides <em>home</em>, and <em>back</em>.329+The <em>back</em> map checks the <em>Referrer</em> and <em>Referer</em> headers, while <em>home</em> utilizes330+the "home" setting and defaults to "/".</p>331+332+<h3 id="Server-redirect-name-val-">Server#redirect(name, val)</h3>333+334+<p>For use with <code>res.redirect()</code> we can map redirects at the application level as shown below:</p>335+336+<pre><code>app.redirect('google', 'http://google.com');337+</code></pre>338+339+<p>Now in a route we may call:</p>340+341+<p> res.redirect('google');</p>342+343+<p>We may also map dynamic redirects:</p>344+345+<pre><code>app.redirect('comments', function(req, res, params){346+ return '/post/' + params.id + '/comments';347+});348+</code></pre>349+350+<p>So now we may do the following, and the redirect will dynamically adjust to351+the context of the request. If we called this route with <em>GET /post/12</em> our352+redirect <em>Location</em> would be <em>/post/12/comments</em>.</p>353+354+<pre><code>app.get('/post/:id', function(req, res){355+ res.redirect('comments');356+});357+</code></pre>358+359 </div>360 </div>361 </div>362 </body>1 unmodified linemodified:docs/guide.md+36 added-1 removed
52 unmodified lines53545556575859606178 unmodified lines14014114214314414514614714814915015115214515315415515615715815916016116216316416516616716816917017117217317417517617717817918052 unmodified lines### Settings
Express supports the following settings out of the box:
* _home_ Application base path used with `res.redirect()` * _views_ Root views directory defaulting to **CWD/views** * _view engine_ Default view engine name for views rendered without extensions * _reload views_ Reloads altered views, by default watches for _mtime_ changes with with a 5 minute interval. Example: `app.set('reload views', { interval: 60000 });`78 unmodified lines connect.logger(), connect.bodyDecoder() );
### ServerResponse#redirect(url[, status])
Redirect to the given _url_ with a default response _status_ of 302.
res.redirect('/', 301); res.redirect('/account'); res.redirect('http://google.com'); res.redirect('home'); res.redirect('back');
Express supports "redirect mapping", which by default provides _home_, and _back_.The _back_ map checks the _Referrer_ and _Referer_ headers, while _home_ utilizesthe "home" setting and defaults to "/".
### Server#redirect(name, val)
For use with `res.redirect()` we can map redirects at the application level as shown below:
app.redirect('google', 'http://google.com');
Now in a route we may call:
res.redirect('google');
We may also map dynamic redirects:
app.redirect('comments', function(req, res, params){ return '/post/' + params.id + '/comments'; });
So now we may do the following, and the redirect will dynamically adjust tothe context of the request. If we called this route with _GET /post/12_ ourredirect _Location_ would be _/post/12/comments_.
app.get('/post/:id', function(req, res){ res.redirect('comments'); });