Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Updated docs

  • Loading branch information...
commit 6234268f101adb62f2cfa4254e1bfac7b32b4a42 1 parent 7a1525c
@tj tj authored
Showing with 359 additions and 359 deletions.
  1. +357 −357 docs/api.html
  2. +1 −1  docs/guide.1
  3. +1 −1  docs/guide.html
View
714 docs/api.html
@@ -337,583 +337,583 @@
<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>);
<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>
</td>
-</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">
+</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">
<td class="docs">
-<p>Parse mini markdown implementation.
-The following conversions are supported,
-primarily for the "flash" middleware:</p>
-
-<p> <em>foo</em> or <em>foo</em> become &lt;em&gt;foo&lt;/em&gt;
- <strong>foo</strong> or <strong>foo</strong> become &lt;strong&gt;foo&lt;/strong&gt;
- <a href="B">A</a> becomes &lt;a href="B"&gt;A&lt;/a&gt;</p>
-
-<h2></h2>
-
-<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>
+<p>Module dependencies.
+ </p>
</td>
<td class="code">
-<pre><code><span class="variable">exports</span>.<span class="variable">miniMarkdown</span> = <span class="keyword">function</span>(<span class="variable">str</span>){
- <span class="keyword">return</span> <span class="class">String</span>(<span class="variable">str</span>)
- .<span class="variable">replace</span>(<span class="regexp">/(__|\*\*)(.*?)\1/g</span>, <span class="string">'&lt;strong&gt;$2&lt;/strong&gt;'</span>)
- .<span class="variable">replace</span>(<span class="regexp">/(_|\*)(.*?)\1/g</span>, <span class="string">'&lt;em&gt;$2&lt;/em&gt;'</span>)
- .<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>);
-};</code></pre>
+<pre><code><span class="keyword">var</span> <span class="variable">sys</span> = <span class="variable">require</span>(<span class="string">'sys'</span>),
+ <span class="variable">url</span> = <span class="variable">require</span>(<span class="string">'url'</span>),
+ <span class="variable">view</span> = <span class="variable">require</span>(<span class="string">'./view'</span>),
+ <span class="variable">connect</span> = <span class="variable">require</span>(<span class="string">'connect'</span>),
+ <span class="variable">utils</span> = <span class="variable">require</span>(<span class="string">'connect/utils'</span>),
+ <span class="variable">queryString</span> = <span class="variable">require</span>(<span class="string">'querystring'</span>),
+ <span class="variable">router</span> = <span class="variable">require</span>(<span class="string">'connect/middleware/router'</span>);</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
-<p>Escape special characters in the given string of html.</p>
+<p>Initialize a new <code>Server</code> with optional <code>middleware</code>.</p>
<h2></h2>
-<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>
+<ul><li><p><strong>param</strong>: <em>Array</em> middleware</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
-<pre><code><span class="variable">exports</span>.<span class="variable">htmlEscape</span> = <span class="keyword">function</span>(<span class="variable">html</span>) {
- <span class="keyword">return</span> <span class="class">String</span>(<span class="variable">html</span>)
- .<span class="variable">replace</span>(<span class="regexp">/&amp;/g</span>, <span class="string">'&amp;'</span>)
- .<span class="variable">replace</span>(<span class="regexp">/&quot;/g</span>, <span class="string">'&quot;'</span>)
- .<span class="variable">replace</span>(<span class="regexp">/&lt;/g</span>, <span class="string">'&lt;'</span>)
- .<span class="variable">replace</span>(<span class="regexp">/&gt;/g</span>, <span class="string">'&gt;'</span>);
+<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>){
+ <span class="keyword">var</span> <span class="variable">self</span> = <span class="this">this</span>;
+ <span class="this">this</span>.<span class="variable">config</span> = {};
+ <span class="this">this</span>.<span class="variable">settings</span> = {};
+ <span class="this">this</span>.<span class="variable">redirects</span> = {};
+ <span class="this">this</span>.<span class="variable">viewHelpers</span> = {};
+ <span class="this">this</span>.<span class="variable">dynamicViewHelpers</span> = {};
+ <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> || []);
+
+ <span class="comment">// Default &quot;home&quot; to / </span>
+ <span class="this">this</span>.<span class="variable">set</span>(<span class="string">'home'</span>, <span class="string">'/'</span>);
+
+ <span class="comment">// Set &quot;env&quot; to {EXPRESS,CONNECT}_ENV</span>
+ <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>);
+
+ <span class="comment">// Expose objects to each other</span>
+ <span class="this">this</span>.<span class="variable">use</span>(<span class="keyword">function</span>(<span class="variable">req</span>, <span class="variable">res</span>, <span class="variable">next</span>){
+ <span class="variable">req</span>.<span class="variable">query</span> = {};
+ <span class="variable">res</span>.<span class="variable">headers</span> = {};
+ <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>;
+ <span class="variable">req</span>.<span class="variable">res</span> = <span class="variable">res</span>;
+ <span class="variable">res</span>.<span class="variable">req</span> = <span class="variable">req</span>;
+ <span class="variable">req</span>.<span class="variable">next</span> = <span class="variable">next</span>;
+ <span class="comment">// Assign req.params.get</span>
+ <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>) {
+ <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>;
+ <span class="variable">req</span>.<span class="variable">query</span> = <span class="variable">queryString</span>.<span class="variable">parse</span>(<span class="variable">query</span>);
+ }
+ <span class="variable">next</span>();
+ });
+
+ <span class="comment">// Use router, expose as app.get(), etc</span>
+ <span class="keyword">var</span> <span class="variable">fn</span> = <span class="variable">router</span>(<span class="keyword">function</span>(<span class="variable">app</span>){ <span class="variable">self</span>.<span class="variable">routes</span> = <span class="variable">app</span>; });
+ <span class="this">this</span>.<span class="variable">__defineGetter__</span>(<span class="string">'router'</span>, <span class="keyword">function</span>(){
+ <span class="this">this</span>.<span class="variable">__usedRouter</span> = <span class="variable">true</span>;
+ <span class="keyword">return</span> <span class="variable">fn</span>;
+ });
};</code></pre>
</td>
-</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">
+</tr>
+<tr class="code">
<td class="docs">
-<p>Module dependencies.
+<p>Inherit from <code>connect.Server</code>.
</p>
</td>
<td class="code">
-<pre><code><span class="keyword">var</span> <span class="variable">fs</span> = <span class="variable">require</span>(<span class="string">'fs'</span>),
- <span class="variable">http</span> = <span class="variable">require</span>(<span class="string">'http'</span>),
- <span class="variable">path</span> = <span class="variable">require</span>(<span class="string">'path'</span>),
- <span class="variable">utils</span> = <span class="variable">require</span>(<span class="string">'connect/utils'</span>),
- <span class="variable">mime</span> = <span class="variable">require</span>(<span class="string">'connect/utils'</span>).<span class="variable">mime</span>,
- <span class="class">Buffer</span> = <span class="variable">require</span>(<span class="string">'buffer'</span>).<span class="class">Buffer</span>;</code></pre>
+<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>
</td>
</tr>
<tr class="code">
<td class="docs">
-<p>Send a response with the given <code>body</code> and optional <code>headers</code> and <code>status</code> code.</p>
-
-<h2>Examples</h2>
-
-<pre><code>res.send(new Buffer('wahoo'));
-res.send({ some: 'json' });
-res.send('&lt;p&gt;some html&lt;/p&gt;');
-res.send('Sorry, cant find that', 404);
-res.send('text', { 'Content-Type': 'text/plain' }, 201);
-res.send(404);</code></pre>
+<p>Start listening on the given <code>port</code> / <code>host</code>.</p>
<h2></h2>
-<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>
+<ul><li><p><strong>param</strong>: <em>Number</em> port</p></li><li><p><strong>param</strong>: <em>String</em> host</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
-<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>){
- <span class="comment">// Allow status as second arg</span>
- <span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">headers</span> === <span class="string">'number'</span>) {
- <span class="variable">status</span> = <span class="variable">headers</span>,
- <span class="variable">headers</span> = <span class="keyword">null</span>;
- }
-
- <span class="comment">// Defaults</span>
- <span class="variable">status</span> = <span class="variable">status</span> || <span class="number integer">200</span>;
- <span class="variable">headers</span> = <span class="variable">headers</span> || {};
-
- <span class="comment">// Determine content type</span>
- <span class="keyword">switch</span> (<span class="keyword">typeof</span> <span class="variable">body</span>) {
- <span class="keyword">case</span> <span class="string">'number'</span>:
- <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Type'</span>]) {
- <span class="this">this</span>.<span class="variable">contentType</span>(<span class="string">'.txt'</span>);
- }
- <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>];
- <span class="keyword">break</span>;
- <span class="keyword">case</span> <span class="string">'string'</span>:
- <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Type'</span>]) {
- <span class="this">this</span>.<span class="variable">contentType</span>(<span class="string">'.html'</span>);
- }
- <span class="keyword">break</span>;
- <span class="keyword">case</span> <span class="string">'object'</span>:
- <span class="keyword">if</span> (<span class="variable">body</span> <span class="variable">instanceof</span> <span class="class">Buffer</span>) {
- <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Type'</span>]) {
- <span class="this">this</span>.<span class="variable">contentType</span>(<span class="string">'.bin'</span>);
- }
- } <span class="keyword">else</span> {
- <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Type'</span>]) {
- <span class="this">this</span>.<span class="variable">contentType</span>(<span class="string">'.json'</span>);
- }
- <span class="variable">body</span> = <span class="class">JSON</span>.<span class="variable">stringify</span>(<span class="variable">body</span>);
- }
- <span class="keyword">break</span>;
- }
-
- <span class="comment">// Populate Content-Length</span>
- <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Length'</span>]) {
- <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>
- ? <span class="variable">body</span>.<span class="variable">length</span>
- : <span class="class">Buffer</span>.<span class="variable">byteLength</span>(<span class="variable">body</span>));
+<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>, <span class="variable">host</span>){
+ <span class="comment">// Setup view reloading</span>
+ <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">set</span>(<span class="string">'reload views'</span>)) {
+ <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>));
}
-
- <span class="comment">// Merge headers passed</span>
- <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>);
-
- <span class="comment">// Respond</span>
- <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>);
- <span class="this">this</span>.<span class="variable">end</span>(<span class="variable">body</span>);
+ <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>, <span class="variable">host</span>);
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
-<p>Transfer the given <code>file</code>. Automatically sets the <em>Content-Type</em>
-response header, and responds with 404 / 500 appropriately.</p>
+<p>Proxy <code>connect.Server#use()</code> to apply settings to
+mounted applications.</p>
<h2></h2>
-<ul><li><p><strong>param</strong>: <em>String</em> file</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
+<ul><li><p><strong>param</strong>: <em>String | Function | Server</em> route</p></li><li><p><strong>param</strong>: <em>Function | Server</em> middleware</p></li><li><p><strong>return</strong>: <em>Server</em> for chaining</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
-<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>){
- <span class="keyword">var</span> <span class="variable">self</span> = <span class="this">this</span>;
- <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>){
- <span class="keyword">if</span> (<span class="variable">err</span>) {
- <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>
- ? <span class="number integer">404</span>
- : <span class="number integer">500</span>);
- } <span class="keyword">else</span> {
- <span class="variable">self</span>.<span class="variable">contentType</span>(<span class="variable">file</span>);
- <span class="variable">self</span>.<span class="variable">send</span>(<span class="variable">buf</span>);
- }
- });
+<pre><code><span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">use</span> = <span class="keyword">function</span>(<span class="variable">route</span>, <span class="variable">middleware</span>){
+ <span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">route</span> !== <span class="string">'string'</span>) {
+ <span class="variable">middleware</span> = <span class="variable">route</span>, <span class="variable">route</span> = <span class="string">'/'</span>;
+ }
+
+ <span class="variable">connect</span>.<span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">use</span>.<span class="variable">call</span>(<span class="this">this</span>, <span class="variable">route</span>, <span class="variable">middleware</span>);
+
+ <span class="comment">// Mounted an app</span>
+ <span class="keyword">if</span> (<span class="variable">middleware</span> <span class="variable">instanceof</span> <span class="class">Server</span>) {
+ <span class="comment">// Home is /:route/:home</span>
+ <span class="keyword">var</span> <span class="variable">app</span> = <span class="variable">middleware</span>,
+ <span class="variable">home</span> = <span class="variable">app</span>.<span class="variable">set</span>(<span class="string">'home'</span>);
+ <span class="keyword">if</span> (<span class="variable">home</span> === <span class="string">'/'</span>) <span class="variable">home</span> = <span class="string">''</span>;
+ <span class="variable">app</span>.<span class="variable">set</span>(<span class="string">'home'</span>, (<span class="variable">app</span>.<span class="variable">route</span> || <span class="string">''</span>) + <span class="variable">home</span>);
+ <span class="comment">// Mounted hook</span>
+ <span class="keyword">if</span> (<span class="variable">app</span>.<span class="variable">__mounted</span>) <span class="variable">app</span>.<span class="variable">__mounted</span>.<span class="variable">call</span>(<span class="variable">app</span>, <span class="this">this</span>);
+ }
+
+ <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
-<p>Set <em>Content-Type</em> response header passed through <code>mime.type()</code>.</p>
+<p>Assign a callback <code>fn</code> which is called
+when this <code>Server</code> is passed to <code>Server#use()</code>.</p>
<h2>Examples</h2>
-<pre><code>var filename = 'path/to/image.png';
-res.contentType(filename);
-// res.headers['Content-Type'] is now "image/png"</code></pre>
+<p> var app = express.createServer(),
+ blog = express.createServer();</p>
+
+<p> blog.mounted(function(parent){
+ // parent is app
+ // "this" is blog
+ });</p>
+
+<p> app.use(blog);</p>
<h2></h2>
-<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>
+<ul><li><p><strong>param</strong>: <em>Function</em> fn</p></li><li><p><strong>return</strong>: <em>Server</em> for chaining</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
-<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>){
- <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>));
+<pre><code><span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">mounted</span> = <span class="keyword">function</span>(<span class="variable">fn</span>){
+ <span class="this">this</span>.<span class="variable">__mounted</span> = <span class="variable">fn</span>;
+ <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
-<p>Set <em>Content-Disposition</em> header to <em>attachment</em> with optional <code>filename</code>.</p>
+<p>Register the given view helpers <code>obj</code>. This method
+can be called several times to apply additional helpers.</p>
<h2></h2>
-<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>
+<ul><li><p><strong>param</strong>: <em>Object</em> obj</p></li><li><p><strong>return</strong>: <em>Server</em> for chaining</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
-<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>){
- <span class="this">this</span>.<span class="variable">header</span>(<span class="string">'Content-Disposition'</span>, <span class="variable">filename</span>
- ? <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>
- : <span class="string">'attachment'</span>);
+<pre><code><span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">helpers</span> = <span class="keyword">function</span>(<span class="variable">obj</span>){
+ <span class="variable">utils</span>.<span class="variable">merge</span>(<span class="this">this</span>.<span class="variable">viewHelpers</span>, <span class="variable">obj</span>);
<span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
-<p>Transfer the given <code>file</code> with optional <code>filename</code> as an attachment.</p>
+<p>Register the given dynamic view helpers <code>obj</code>. This method
+can be called several times to apply additional helpers.</p>
<h2></h2>
-<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>
+<ul><li><p><strong>param</strong>: <em>Object</em> obj</p></li><li><p><strong>return</strong>: <em>Server</em> for chaining</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
-<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>){
- <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>);
+<pre><code><span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">dynamicHelpers</span> = <span class="keyword">function</span>(<span class="variable">obj</span>){
+ <span class="variable">utils</span>.<span class="variable">merge</span>(<span class="this">this</span>.<span class="variable">dynamicViewHelpers</span>, <span class="variable">obj</span>);
+ <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
-<p>Set or get response header <code>name</code> with optional <code>val</code>.</p>
+<p>Assign a custom exception handler callback <code>fn</code>.</p>
<h2></h2>
-<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>
+<ul><li><p><strong>param</strong>: <em>Function</em> fn</p></li><li><p><strong>return</strong>: <em>Server</em> for chaining</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
-<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>){
- <span class="keyword">return</span> <span class="variable">val</span> === <span class="variable">undefined</span>
- ? <span class="this">this</span>.<span class="variable">headers</span>[<span class="variable">name</span>]
- : <span class="this">this</span>.<span class="variable">headers</span>[<span class="variable">name</span>] = <span class="variable">val</span>;
+<pre><code><span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">error</span> = <span class="keyword">function</span>(<span class="variable">fn</span>){
+ <span class="this">this</span>.<span class="variable">use</span>(<span class="keyword">function</span>(<span class="variable">err</span>, <span class="variable">req</span>, <span class="variable">res</span>, <span class="variable">next</span>){
+ <span class="variable">fn</span>.<span class="variable">apply</span>(<span class="this">this</span>, <span class="variable">arguments</span>);
+ });
+ <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
-<p>Redirect to the given <code>url</code> with optional response <code>status</code>
-defauling to 302.</p>
-
-<p>The given <code>url</code> can also be the name of a mapped url, for
-example by default express supports "back" which redirects
-to the <em>Referrer</em> or <em>Referer</em> headers or the application's
-"home" setting. Express also supports "home" out of the box,
-which can be set via <code>app.set('home', '/blog');</code>, and defaults
-to '/'.</p>
-
-<h2>Redirect Mapping</h2>
-
-<p> To extend the redirect mapping capabilities that Express provides,
- we may use the <code>app.redirect()</code> method:</p>
-
-<pre><code>app.redirect('google', 'http://google.com');</code></pre>
-
-<p> Now in a route we may call:</p>
-
-<pre><code>res.redirect('google');</code></pre>
-
-<p> We may also map dynamic redirects:</p>
-
-<pre><code> app.redirect('comments', function(req, res, params){
- return '/post/' + params.id + '/comments';
- });</code></pre>
-
-<p> 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 <em>GET /post/12</em> our
- redirect <em>Location</em> would be <em>/post/12/comments</em>.</p>
-
-<pre><code> app.get('/post/:id', function(req, res){
- res.redirect('comments');
- });</code></pre>
+<p>Assign <code>setting</code> to <code>val</code>, or return <code>setting</code>'s value.</p>
<h2></h2>
-<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>
-</td>
-<td class="code">
-<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>){
- <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>;
-
- <span class="comment">// Setup redirect map</span>
- <span class="keyword">var</span> <span class="variable">map</span> = {
- <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>,
- <span class="variable">home</span>: <span class="variable">basePath</span>
- };
-
- <span class="comment">// Support custom redirect map</span>
- <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>;
-
- <span class="comment">// Attempt mapped redirect</span>
- <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>
- ? <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>)
- : <span class="variable">map</span>[<span class="variable">url</span>];
-
- <span class="comment">// Perform redirect</span>
- <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> });
- <span class="this">this</span>.<span class="variable">end</span>();
-};
-</code></pre>
-</td>
-</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">
-<td class="docs">
-<p>Module dependencies.
- </p>
+<ul><li><p><strong>param</strong>: <em>String</em> setting</p></li><li><p><strong>param</strong>: <em>String</em> val</p></li><li><p><strong>return</strong>: <em>Server | Mixed</em> for chaining, or the setting value</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
-<pre><code><span class="keyword">var</span> <span class="variable">sys</span> = <span class="variable">require</span>(<span class="string">'sys'</span>),
- <span class="variable">url</span> = <span class="variable">require</span>(<span class="string">'url'</span>),
- <span class="variable">view</span> = <span class="variable">require</span>(<span class="string">'./view'</span>),
- <span class="variable">connect</span> = <span class="variable">require</span>(<span class="string">'connect'</span>),
- <span class="variable">utils</span> = <span class="variable">require</span>(<span class="string">'connect/utils'</span>),
- <span class="variable">queryString</span> = <span class="variable">require</span>(<span class="string">'querystring'</span>),
- <span class="variable">router</span> = <span class="variable">require</span>(<span class="string">'connect/middleware/router'</span>);</code></pre>
+<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>){
+ <span class="keyword">if</span> (<span class="variable">val</span> === <span class="variable">undefined</span>) {
+ <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">settings</span>[<span class="variable">setting</span>];
+ } <span class="keyword">else</span> {
+ <span class="this">this</span>.<span class="variable">settings</span>[<span class="variable">setting</span>] = <span class="variable">val</span>;
+ <span class="keyword">return</span> <span class="this">this</span>;
+ }
+};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
-<p>Initialize a new <code>Server</code> with optional <code>middleware</code>.</p>
+<p>Enable <code>setting</code>.</p>
<h2></h2>
-<ul><li><p><strong>param</strong>: <em>Array</em> middleware</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
+<ul><li><p><strong>param</strong>: <em>String</em> setting</p></li><li><p><strong>return</strong>: <em>Server</em> for chaining</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
-<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>){
- <span class="keyword">var</span> <span class="variable">self</span> = <span class="this">this</span>;
- <span class="this">this</span>.<span class="variable">config</span> = {};
- <span class="this">this</span>.<span class="variable">settings</span> = {};
- <span class="this">this</span>.<span class="variable">redirects</span> = {};
- <span class="this">this</span>.<span class="variable">viewHelpers</span> = {};
- <span class="this">this</span>.<span class="variable">dynamicViewHelpers</span> = {};
- <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> || []);
-
- <span class="comment">// Default &quot;home&quot; to / </span>
- <span class="this">this</span>.<span class="variable">set</span>(<span class="string">'home'</span>, <span class="string">'/'</span>);
-
- <span class="comment">// Set &quot;env&quot; to {EXPRESS,CONNECT}_ENV</span>
- <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>);
-
- <span class="comment">// Expose objects to each other</span>
- <span class="this">this</span>.<span class="variable">use</span>(<span class="keyword">function</span>(<span class="variable">req</span>, <span class="variable">res</span>, <span class="variable">next</span>){
- <span class="variable">req</span>.<span class="variable">query</span> = {};
- <span class="variable">res</span>.<span class="variable">headers</span> = {};
- <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>;
- <span class="variable">req</span>.<span class="variable">res</span> = <span class="variable">res</span>;
- <span class="variable">res</span>.<span class="variable">req</span> = <span class="variable">req</span>;
- <span class="variable">req</span>.<span class="variable">next</span> = <span class="variable">next</span>;
- <span class="comment">// Assign req.params.get</span>
- <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>) {
- <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>;
- <span class="variable">req</span>.<span class="variable">query</span> = <span class="variable">queryString</span>.<span class="variable">parse</span>(<span class="variable">query</span>);
- }
- <span class="variable">next</span>();
- });
-
- <span class="comment">// Use router, expose as app.get(), etc</span>
- <span class="keyword">var</span> <span class="variable">fn</span> = <span class="variable">router</span>(<span class="keyword">function</span>(<span class="variable">app</span>){ <span class="variable">self</span>.<span class="variable">routes</span> = <span class="variable">app</span>; });
- <span class="this">this</span>.<span class="variable">__defineGetter__</span>(<span class="string">'router'</span>, <span class="keyword">function</span>(){
- <span class="this">this</span>.<span class="variable">__usedRouter</span> = <span class="variable">true</span>;
- <span class="keyword">return</span> <span class="variable">fn</span>;
- });
+<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>){
+ <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>);
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
-<p>Inherit from <code>connect.Server</code>.
- </p>
+<p>Disable <code>setting</code>.</p>
+
+<h2></h2>
+
+<ul><li><p><strong>param</strong>: <em>String</em> setting</p></li><li><p><strong>return</strong>: <em>Server</em> for chaining</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
-<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>
+<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>){
+ <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>);
+};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
-<p>Start listening on the given <code>port</code> / <code>host</code>.</p>
+<p>Redirect <code>key</code> to <code>url</code>.</p>
<h2></h2>
-<ul><li><p><strong>param</strong>: <em>Number</em> port</p></li><li><p><strong>param</strong>: <em>String</em> host</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
+<ul><li><p><strong>param</strong>: <em>String</em> key</p></li><li><p><strong>param</strong>: <em>String</em> url</p></li><li><p><strong>return</strong>: <em>Server</em> for chaining</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
-<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>, <span class="variable">host</span>){
- <span class="comment">// Setup view reloading</span>
- <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">set</span>(<span class="string">'reload views'</span>)) {
- <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>));
- }
- <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>, <span class="variable">host</span>);
+<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>){
+ <span class="this">this</span>.<span class="variable">redirects</span>[<span class="variable">key</span>] = <span class="variable">url</span>;
+ <span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
-<p>Proxy <code>connect.Server#use()</code> to apply settings to
-mounted applications.</p>
+<p>Configure callback for the given <code>env</code>.</p>
<h2></h2>
-<ul><li><p><strong>param</strong>: <em>String | Function | Server</em> route</p></li><li><p><strong>param</strong>: <em>Function | Server</em> middleware</p></li><li><p><strong>return</strong>: <em>Server</em> for chaining</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
+<ul><li><p><strong>param</strong>: <em>String</em> env</p></li><li><p><strong>param</strong>: <em>Function</em> fn</p></li><li><p><strong>return</strong>: <em>Server</em> for chaining</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
-<pre><code><span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">use</span> = <span class="keyword">function</span>(<span class="variable">route</span>, <span class="variable">middleware</span>){
- <span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">route</span> !== <span class="string">'string'</span>) {
- <span class="variable">middleware</span> = <span class="variable">route</span>, <span class="variable">route</span> = <span class="string">'/'</span>;
+<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>){
+ <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">'all'</span>;
}
-
- <span class="variable">connect</span>.<span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">use</span>.<span class="variable">call</span>(<span class="this">this</span>, <span class="variable">route</span>, <span class="variable">middleware</span>);
-
- <span class="comment">// Mounted an app</span>
- <span class="keyword">if</span> (<span class="variable">middleware</span> <span class="variable">instanceof</span> <span class="class">Server</span>) {
- <span class="comment">// Home is /:route/:home</span>
- <span class="keyword">var</span> <span class="variable">app</span> = <span class="variable">middleware</span>,
- <span class="variable">home</span> = <span class="variable">app</span>.<span class="variable">set</span>(<span class="string">'home'</span>);
- <span class="keyword">if</span> (<span class="variable">home</span> === <span class="string">'/'</span>) <span class="variable">home</span> = <span class="string">''</span>;
- <span class="variable">app</span>.<span class="variable">set</span>(<span class="string">'home'</span>, (<span class="variable">app</span>.<span class="variable">route</span> || <span class="string">''</span>) + <span class="variable">home</span>);
- <span class="comment">// Mounted hook</span>
- <span class="keyword">if</span> (<span class="variable">app</span>.<span class="variable">__mounted</span>) <span class="variable">app</span>.<span class="variable">__mounted</span>.<span class="variable">call</span>(<span class="variable">app</span>, <span class="this">this</span>);
+ <span class="keyword">if</span> (<span class="variable">env</span> === <span class="string">'all'</span> || <span class="this">this</span>.<span class="variable">set</span>(<span class="string">'env'</span>) === <span class="variable">env</span>) {
+ <span class="variable">fn</span>.<span class="variable">call</span>(<span class="this">this</span>);
}
-
<span class="keyword">return</span> <span class="this">this</span>;
-};</code></pre>
+};
+
+<span class="comment">// Generate routing methods</span>
+
+(<span class="keyword">function</span>(<span class="variable">method</span>){
+ <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>){
+ <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">__usedRouter</span>) {
+ <span class="this">this</span>.<span class="variable">use</span>(<span class="this">this</span>.<span class="variable">router</span>);
+ }
+ <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>);
+ <span class="keyword">return</span> <span class="this">this</span>;
+ };
+ <span class="keyword">return</span> <span class="variable">arguments</span>.<span class="variable">callee</span>;
+})(<span class="string">'get'</span>)(<span class="string">'post'</span>)(<span class="string">'put'</span>)(<span class="string">'del'</span>);
+</code></pre>
+</td>
+</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">
+<td class="docs">
+<p>Module dependencies.
+ </p>
+</td>
+<td class="code">
+<pre><code><span class="keyword">var</span> <span class="variable">fs</span> = <span class="variable">require</span>(<span class="string">'fs'</span>),
+ <span class="variable">http</span> = <span class="variable">require</span>(<span class="string">'http'</span>),
+ <span class="variable">path</span> = <span class="variable">require</span>(<span class="string">'path'</span>),
+ <span class="variable">utils</span> = <span class="variable">require</span>(<span class="string">'connect/utils'</span>),
+ <span class="variable">mime</span> = <span class="variable">require</span>(<span class="string">'connect/utils'</span>).<span class="variable">mime</span>,
+ <span class="class">Buffer</span> = <span class="variable">require</span>(<span class="string">'buffer'</span>).<span class="class">Buffer</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
-<p>Assign a callback <code>fn</code> which is called
-when this <code>Server</code> is passed to <code>Server#use()</code>.</p>
+<p>Send a response with the given <code>body</code> and optional <code>headers</code> and <code>status</code> code.</p>
<h2>Examples</h2>
-<p> var app = express.createServer(),
- blog = express.createServer();</p>
-
-<p> blog.mounted(function(parent){
- // parent is app
- // "this" is blog
- });</p>
-
-<p> app.use(blog);</p>
+<pre><code>res.send(new Buffer('wahoo'));
+res.send({ some: 'json' });
+res.send('&lt;p&gt;some html&lt;/p&gt;');
+res.send('Sorry, cant find that', 404);
+res.send('text', { 'Content-Type': 'text/plain' }, 201);
+res.send(404);</code></pre>
<h2></h2>
-<ul><li><p><strong>param</strong>: <em>Function</em> fn</p></li><li><p><strong>return</strong>: <em>Server</em> for chaining</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
+<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>
</td>
<td class="code">
-<pre><code><span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">mounted</span> = <span class="keyword">function</span>(<span class="variable">fn</span>){
- <span class="this">this</span>.<span class="variable">__mounted</span> = <span class="variable">fn</span>;
- <span class="keyword">return</span> <span class="this">this</span>;
+<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>){
+ <span class="comment">// Allow status as second arg</span>
+ <span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">headers</span> === <span class="string">'number'</span>) {
+ <span class="variable">status</span> = <span class="variable">headers</span>,
+ <span class="variable">headers</span> = <span class="keyword">null</span>;
+ }
+
+ <span class="comment">// Defaults</span>
+ <span class="variable">status</span> = <span class="variable">status</span> || <span class="number integer">200</span>;
+ <span class="variable">headers</span> = <span class="variable">headers</span> || {};
+
+ <span class="comment">// Determine content type</span>
+ <span class="keyword">switch</span> (<span class="keyword">typeof</span> <span class="variable">body</span>) {
+ <span class="keyword">case</span> <span class="string">'number'</span>:
+ <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Type'</span>]) {
+ <span class="this">this</span>.<span class="variable">contentType</span>(<span class="string">'.txt'</span>);
+ }
+ <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>];
+ <span class="keyword">break</span>;
+ <span class="keyword">case</span> <span class="string">'string'</span>:
+ <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Type'</span>]) {
+ <span class="this">this</span>.<span class="variable">contentType</span>(<span class="string">'.html'</span>);
+ }
+ <span class="keyword">break</span>;
+ <span class="keyword">case</span> <span class="string">'object'</span>:
+ <span class="keyword">if</span> (<span class="variable">body</span> <span class="variable">instanceof</span> <span class="class">Buffer</span>) {
+ <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Type'</span>]) {
+ <span class="this">this</span>.<span class="variable">contentType</span>(<span class="string">'.bin'</span>);
+ }
+ } <span class="keyword">else</span> {
+ <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Type'</span>]) {
+ <span class="this">this</span>.<span class="variable">contentType</span>(<span class="string">'.json'</span>);
+ }
+ <span class="variable">body</span> = <span class="class">JSON</span>.<span class="variable">stringify</span>(<span class="variable">body</span>);
+ }
+ <span class="keyword">break</span>;
+ }
+
+ <span class="comment">// Populate Content-Length</span>
+ <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">headers</span>[<span class="string">'Content-Length'</span>]) {
+ <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>
+ ? <span class="variable">body</span>.<span class="variable">length</span>
+ : <span class="class">Buffer</span>.<span class="variable">byteLength</span>(<span class="variable">body</span>));
+ }
+
+ <span class="comment">// Merge headers passed</span>
+ <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>);
+
+ <span class="comment">// Respond</span>
+ <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>);
+ <span class="this">this</span>.<span class="variable">end</span>(<span class="variable">body</span>);
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
-<p>Register the given view helpers <code>obj</code>. This method
-can be called several times to apply additional helpers.</p>
+<p>Transfer the given <code>file</code>. Automatically sets the <em>Content-Type</em>
+response header, and responds with 404 / 500 appropriately.</p>
<h2></h2>
-<ul><li><p><strong>param</strong>: <em>Object</em> obj</p></li><li><p><strong>return</strong>: <em>Server</em> for chaining</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
+<ul><li><p><strong>param</strong>: <em>String</em> file</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
-<pre><code><span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">helpers</span> = <span class="keyword">function</span>(<span class="variable">obj</span>){
- <span class="variable">utils</span>.<span class="variable">merge</span>(<span class="this">this</span>.<span class="variable">viewHelpers</span>, <span class="variable">obj</span>);
- <span class="keyword">return</span> <span class="this">this</span>;
+<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>){
+ <span class="keyword">var</span> <span class="variable">self</span> = <span class="this">this</span>;
+ <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>){
+ <span class="keyword">if</span> (<span class="variable">err</span>) {
+ <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>
+ ? <span class="number integer">404</span>
+ : <span class="number integer">500</span>);
+ } <span class="keyword">else</span> {
+ <span class="variable">self</span>.<span class="variable">contentType</span>(<span class="variable">file</span>);
+ <span class="variable">self</span>.<span class="variable">send</span>(<span class="variable">buf</span>);
+ }
+ });
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
-<p>Register the given dynamic view helpers <code>obj</code>. This method
-can be called several times to apply additional helpers.</p>
+<p>Set <em>Content-Type</em> response header passed through <code>mime.type()</code>.</p>
+
+<h2>Examples</h2>
+
+<pre><code>var filename = 'path/to/image.png';
+res.contentType(filename);
+// res.headers['Content-Type'] is now "image/png"</code></pre>
<h2></h2>
-<ul><li><p><strong>param</strong>: <em>Object</em> obj</p></li><li><p><strong>return</strong>: <em>Server</em> for chaining</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
+<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>
</td>
<td class="code">
-<pre><code><span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">dynamicHelpers</span> = <span class="keyword">function</span>(<span class="variable">obj</span>){
- <span class="variable">utils</span>.<span class="variable">merge</span>(<span class="this">this</span>.<span class="variable">dynamicViewHelpers</span>, <span class="variable">obj</span>);
- <span class="keyword">return</span> <span class="this">this</span>;
+<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>){
+ <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>));
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
-<p>Assign a custom exception handler callback <code>fn</code>.</p>
+<p>Set <em>Content-Disposition</em> header to <em>attachment</em> with optional <code>filename</code>.</p>
<h2></h2>
-<ul><li><p><strong>param</strong>: <em>Function</em> fn</p></li><li><p><strong>return</strong>: <em>Server</em> for chaining</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
+<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>
</td>
<td class="code">
-<pre><code><span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">error</span> = <span class="keyword">function</span>(<span class="variable">fn</span>){
- <span class="this">this</span>.<span class="variable">use</span>(<span class="keyword">function</span>(<span class="variable">err</span>, <span class="variable">req</span>, <span class="variable">res</span>, <span class="variable">next</span>){
- <span class="variable">fn</span>.<span class="variable">apply</span>(<span class="this">this</span>, <span class="variable">arguments</span>);
- });
+<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>){
+ <span class="this">this</span>.<span class="variable">header</span>(<span class="string">'Content-Disposition'</span>, <span class="variable">filename</span>
+ ? <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>
+ : <span class="string">'attachment'</span>);
<span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
-<p>Assign <code>setting</code> to <code>val</code>, or return <code>setting</code>'s value.</p>
+<p>Transfer the given <code>file</code> with optional <code>filename</code> as an attachment.</p>
<h2></h2>
-<ul><li><p><strong>param</strong>: <em>String</em> setting</p></li><li><p><strong>param</strong>: <em>String</em> val</p></li><li><p><strong>return</strong>: <em>Server | Mixed</em> for chaining, or the setting value</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
+<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>
</td>
<td class="code">
-<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>){
- <span class="keyword">if</span> (<span class="variable">val</span> === <span class="variable">undefined</span>) {
- <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">settings</span>[<span class="variable">setting</span>];
- } <span class="keyword">else</span> {
- <span class="this">this</span>.<span class="variable">settings</span>[<span class="variable">setting</span>] = <span class="variable">val</span>;
- <span class="keyword">return</span> <span class="this">this</span>;
- }
+<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>){
+ <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>);
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
-<p>Enable <code>setting</code>.</p>
+<p>Set or get response header <code>name</code> with optional <code>val</code>.</p>
<h2></h2>
-<ul><li><p><strong>param</strong>: <em>String</em> setting</p></li><li><p><strong>return</strong>: <em>Server</em> for chaining</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
+<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>
</td>
<td class="code">
-<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>){
- <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>);
+<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>){
+ <span class="keyword">return</span> <span class="variable">val</span> === <span class="variable">undefined</span>
+ ? <span class="this">this</span>.<span class="variable">headers</span>[<span class="variable">name</span>]
+ : <span class="this">this</span>.<span class="variable">headers</span>[<span class="variable">name</span>] = <span class="variable">val</span>;
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
-<p>Disable <code>setting</code>.</p>
+<p>Redirect to the given <code>url</code> with optional response <code>status</code>
+defauling to 302.</p>
+
+<p>The given <code>url</code> can also be the name of a mapped url, for
+example by default express supports "back" which redirects
+to the <em>Referrer</em> or <em>Referer</em> headers or the application's
+"home" setting. Express also supports "home" out of the box,
+which can be set via <code>app.set('home', '/blog');</code>, and defaults
+to '/'.</p>
+
+<h2>Redirect Mapping</h2>
+
+<p> To extend the redirect mapping capabilities that Express provides,
+ we may use the <code>app.redirect()</code> method:</p>
+
+<pre><code>app.redirect('google', 'http://google.com');</code></pre>
+
+<p> Now in a route we may call:</p>
+
+<pre><code>res.redirect('google');</code></pre>
+
+<p> We may also map dynamic redirects:</p>
+
+<pre><code> app.redirect('comments', function(req, res, params){
+ return '/post/' + params.id + '/comments';
+ });</code></pre>
+
+<p> 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 <em>GET /post/12</em> our
+ redirect <em>Location</em> would be <em>/post/12/comments</em>.</p>
+
+<pre><code> app.get('/post/:id', function(req, res){
+ res.redirect('comments');
+ });</code></pre>
<h2></h2>
-<ul><li><p><strong>param</strong>: <em>String</em> setting</p></li><li><p><strong>return</strong>: <em>Server</em> for chaining</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
+<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>
</td>
<td class="code">
-<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>){
- <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>);
-};</code></pre>
+<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>){
+ <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>;
+
+ <span class="comment">// Setup redirect map</span>
+ <span class="keyword">var</span> <span class="variable">map</span> = {
+ <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>,
+ <span class="variable">home</span>: <span class="variable">basePath</span>
+ };
+
+ <span class="comment">// Support custom redirect map</span>
+ <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>;
+
+ <span class="comment">// Attempt mapped redirect</span>
+ <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>
+ ? <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>)
+ : <span class="variable">map</span>[<span class="variable">url</span>];
+
+ <span class="comment">// Perform redirect</span>
+ <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> });
+ <span class="this">this</span>.<span class="variable">end</span>();
+};
+</code></pre>
</td>
-</tr>
-<tr class="code">
+</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">
<td class="docs">
-<p>Redirect <code>key</code> to <code>url</code>.</p>
+<p>Parse mini markdown implementation.
+The following conversions are supported,
+primarily for the "flash" middleware:</p>
+
+<p> <em>foo</em> or <em>foo</em> become &lt;em&gt;foo&lt;/em&gt;
+ <strong>foo</strong> or <strong>foo</strong> become &lt;strong&gt;foo&lt;/strong&gt;
+ <a href="B">A</a> becomes &lt;a href="B"&gt;A&lt;/a&gt;</p>
<h2></h2>
-<ul><li><p><strong>param</strong>: <em>String</em> key</p></li><li><p><strong>param</strong>: <em>String</em> url</p></li><li><p><strong>return</strong>: <em>Server</em> for chaining</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
+<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>
</td>
<td class="code">
-<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>){
- <span class="this">this</span>.<span class="variable">redirects</span>[<span class="variable">key</span>] = <span class="variable">url</span>;
- <span class="keyword">return</span> <span class="this">this</span>;
+<pre><code><span class="variable">exports</span>.<span class="variable">miniMarkdown</span> = <span class="keyword">function</span>(<span class="variable">str</span>){
+ <span class="keyword">return</span> <span class="class">String</span>(<span class="variable">str</span>)
+ .<span class="variable">replace</span>(<span class="regexp">/(__|\*\*)(.*?)\1/g</span>, <span class="string">'&lt;strong&gt;$2&lt;/strong&gt;'</span>)
+ .<span class="variable">replace</span>(<span class="regexp">/(_|\*)(.*?)\1/g</span>, <span class="string">'&lt;em&gt;$2&lt;/em&gt;'</span>)
+ .<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>);
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
-<p>Configure callback for the given <code>env</code>.</p>
+<p>Escape special characters in the given string of html.</p>
<h2></h2>
-<ul><li><p><strong>param</strong>: <em>String</em> env</p></li><li><p><strong>param</strong>: <em>Function</em> fn</p></li><li><p><strong>return</strong>: <em>Server</em> for chaining</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
+<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>
</td>
<td class="code">
-<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>){
- <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">'all'</span>;
- }
- <span class="keyword">if</span> (<span class="variable">env</span> === <span class="string">'all'</span> || <span class="this">this</span>.<span class="variable">set</span>(<span class="string">'env'</span>) === <span class="variable">env</span>) {
- <span class="variable">fn</span>.<span class="variable">call</span>(<span class="this">this</span>);
- }
- <span class="keyword">return</span> <span class="this">this</span>;
-};
-
-<span class="comment">// Generate routing methods</span>
-
-(<span class="keyword">function</span>(<span class="variable">method</span>){
- <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>){
- <span class="keyword">if</span> (!<span class="this">this</span>.<span class="variable">__usedRouter</span>) {
- <span class="this">this</span>.<span class="variable">use</span>(<span class="this">this</span>.<span class="variable">router</span>);
- }
- <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>);
- <span class="keyword">return</span> <span class="this">this</span>;
- };
- <span class="keyword">return</span> <span class="variable">arguments</span>.<span class="variable">callee</span>;
-})(<span class="string">'get'</span>)(<span class="string">'post'</span>)(<span class="string">'put'</span>)(<span class="string">'del'</span>);
-</code></pre>
+<pre><code><span class="variable">exports</span>.<span class="variable">htmlEscape</span> = <span class="keyword">function</span>(<span class="variable">html</span>) {
+ <span class="keyword">return</span> <span class="class">String</span>(<span class="variable">html</span>)
+ .<span class="variable">replace</span>(<span class="regexp">/&amp;/g</span>, <span class="string">'&amp;'</span>)
+ .<span class="variable">replace</span>(<span class="regexp">/&quot;/g</span>, <span class="string">'&quot;'</span>)
+ .<span class="variable">replace</span>(<span class="regexp">/&lt;/g</span>, <span class="string">'&lt;'</span>)
+ .<span class="variable">replace</span>(<span class="regexp">/&gt;/g</span>, <span class="string">'&gt;'</span>);
+};</code></pre>
</td>
</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">
<td class="docs">
View
2  docs/guide.1
@@ -145,7 +145,7 @@ Express supports the following settings out of the box:
\fIview engine\fR Default view engine name for views rendered without extensions
.
.IP "\(bu" 4
-\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);\fR
+\fIreload views\fR Reloads altered views, by default watches for \fImtime\fR changes with with a 500 millisecond interval\.
.
.IP "" 0
.
View
2  docs/guide.html
@@ -321,7 +321,7 @@ <h3 id="Settings">Settings</h3>
<li><em>views</em> Root views directory defaulting to <strong>CWD/views</strong></li>
<li><em>view engine</em> Default view engine name for views rendered without extensions</li>
<li><em>reload views</em> Reloads altered views, by default watches for <em>mtime</em> changes with
- with a 5 minute interval. Example: <em>app.set('reload views', 60000);</em></li>
+ with a 500 millisecond interval.</li>
</ul>
Please sign in to comment.
Something went wrong with that request. Please try again.