Skip to content
Myr

Merge branch 'master' into range

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 · parents 64033fc, 426e80d · Browse files · 21 files+688 added−331 removed
This commit

State

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

Diff stats

21 files+688 added−331 removed

  • tests:+1 added−1 removed
  • other:+687 added−330 removed

21 files+688 added−331 removed

modified:bin/express+23 added-23 removed
77 unmodified lines
78 "",79 "var app = require('../app');",80 "",81 "module.exports = {",82 " 'GET /': function(assert){",83 " assert.response(app,",84 " { url: '/' },",85 " { status: 200, headers: { 'Content-Type': 'text/html; charset=utf-8' }},",86 " function(res){",87 " assert.includes(res.body, '<title>Express</title>');",88 " });",89 " }",82 " 'GET /': function(assert){",83 " assert.response(app,",84 " { url: '/' },",85 " { status: 200, headers: { 'Content-Type': 'text/html; charset=utf-8' }},",86 " function(res){",87 " assert.includes(res.body, '<title>Express</title>');",88 " });",89 " }",90 "};",91].join('\n');9293/**
12 unmodified lines
106 '',107 '// Configuration',108 '',109 'app.configure(function(){',110 ' app.set(\'views\', __dirname + \'/views\');',111 ' app.use(express.bodyDecoder());',112 ' app.use(express.methodOverride());',113 ' app.use(express.compiler({ src: __dirname + \'/public\', enable: [\'less\'] }));',114 ' app.use(app.router);',115 ' app.use(express.staticProvider(__dirname + \'/public\'));',110 ' app.set(\'views\', __dirname + \'/views\');',111 ' app.use(express.bodyDecoder());',112 ' app.use(express.methodOverride());',113 ' app.use(express.compiler({ src: __dirname + \'/public\', enable: [\'less\'] }));',114 ' app.use(app.router);',115 ' app.use(express.staticProvider(__dirname + \'/public\'));',116 '});',117 '',118 'app.configure(\'development\', function(){',119 ' app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); ',119 ' app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); ',120 '});',121 '',122 'app.configure(\'production\', function(){',123 ' app.use(express.errorHandler()); ',123 ' app.use(express.errorHandler()); ',124 '});',125 '',126 '// Routes',127 '',128 'app.get(\'/\', function(req, res){',129 ' res.render(\'index.jade\', {',130 ' locals: {',131 ' title: \'Express\'',132 ' }',133 ' });',129 ' res.render(\'index.jade\', {',130 ' locals: {',131 ' title: \'Express\'',132 ' }',133 ' });',134 '});',135 '',136 '// Only listen on $ node app.js',137 '',138 'if (!module.parent) {',139 ' app.listen(3000);',140 ' console.log("Express server listening on port %d", app.address().port)',139 ' app.listen(3000);',140 ' console.log("Express server listening on port %d", app.address().port)',141 '}',142 ''143].join('\n');144
146 unmodified lines
modified:docs/layout/head.html+6 added-3 removed
modified:docs/api.html+274 added-274 removed
534 unmodified lines
535 <span class="variable">self</span>.<span class="variable">contentType</span>(<span class="variable">path</span>);536 <span class="variable">self</span>.<span class="variable">header</span>(<span class="string">'Content-Length'</span>, <span class="variable">stat</span>.<span class="variable">size</span>);537 <span class="variable">self</span>.<span class="variable">writeHead</span>(<span class="number integer">200</span>, <span class="variable">self</span>.<span class="variable">headers</span>);538 <span class="variable">pump</span>(<span class="variable">stream</span>, <span class="variable">self</span>, <span class="keyword">function</span>(<span class="variable">err</span>){539 <span class="variable">fn</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">fn</span>(<span class="variable">err</span>, <span class="variable">path</span>);539 <span class="variable">fn</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">fn</span>(<span class="variable">err</span>, <span class="variable">path</span>, <span class="variable">true</span>);540 });541 } <span class="keyword">else</span> {542 <span class="variable">fs</span>.<span class="variable">readFile</span>(<span class="variable">path</span>, <span class="keyword">function</span>(<span class="variable">err</span>, <span class="variable">buf</span>){543 <span class="keyword">if</span> (<span class="variable">err</span>) <span class="keyword">return</span> <span class="variable">error</span>(<span class="variable">err</span>);
190 unmodified lines
734 }, <span class="variable">status</span> || <span class="number integer">302</span>);735};736</code></pre>737</td>738</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">739<td class="docs">740<p>Module dependencies.741 </p>742</td>743<td class="code">744<pre><code><span class="keyword">var</span> <span class="variable">extname</span> = <span class="variable">require</span>(<span class="string">'path'</span>).<span class="variable">extname</span>,745 <span class="variable">mime</span> = <span class="variable">require</span>(<span class="string">'connect/utils'</span>).<span class="variable">mime</span>,746 <span class="variable">utils</span> = <span class="variable">require</span>(<span class="string">'connect/utils'</span>),747 <span class="variable">http</span> = <span class="variable">require</span>(<span class="string">'http'</span>),748 <span class="variable">sys</span> = <span class="variable">require</span>(<span class="string">'sys'</span>),749 <span class="variable">fs</span> = <span class="variable">require</span>(<span class="string">'fs'</span>);</code></pre>750</td>751</tr>752<tr class="code">753<td class="docs">754<p>Cache supported template engine exports to755increase performance by lowering the number756of calls to <code>require()</code>.</p>757758<ul><li><p><strong>type</strong>: <em>Object</em></p></li></ul>759</td>760<td class="code">761<pre><code><span class="keyword">var</span> <span class="variable">cache</span> = {};</code></pre>762</td>763</tr>764<tr class="code">765<td class="docs">766<p>Cache view contents to prevent I/O hits.</p>767768<ul><li><p><strong>type</strong>: <em>Object</em></p></li></ul>769</td>770<td class="code">771<pre><code><span class="keyword">var</span> <span class="variable">viewCache</span> = {};</code></pre>772</td>773</tr>774<tr class="code">775<td class="docs">776<p>Register the given template engine <code>exports</code>777as <code>ext</code>. For example we may wish to map ".html"778files to jade:</p>779780<p> app.register('.html', require('jade'));</p>781782<p>This is also useful for libraries that may not783match extensions correctly. For example my haml.js784library is installed from npm as "hamljs" so instead785of layout.hamljs, we can register the engine as ".haml":</p>786787<p> app.register('.haml', require('haml-js'));</p>788789<p>For engines that do not comply with the Express790specification, we can also wrap their api this way.</p>791792<p> app.register('.foo', {793 render: function(str, options) {794 // perhaps their api is795 // foo.toHTML(str, options);796 }797 });</p>798799<h2></h2>800801<ul><li><p><strong>param</strong>: <em>String</em> ext</p></li><li><p><strong>param</strong>: <em>Object</em> obj</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>802</td>803<td class="code">804<pre><code><span class="variable">exports</span>.<span class="variable">register</span> = <span class="keyword">function</span>(<span class="variable">ext</span>, <span class="variable">exports</span>) {805 <span class="variable">cache</span>[<span class="variable">ext</span>] = <span class="variable">exports</span>;806};</code></pre>807</td>808</tr>809<tr class="code">810<td class="docs">811<p>Render <code>view</code> partial with the given <code>options</code>.</p>812813<h2>Options</h2>814815<ul><li><p><code>object</code> Single object with name derived from the view (unless <code>as</code> is present) </p></li><li><p><code>as</code> Variable name for each <code>collection</code> value, defaults to the view name.</p><ul><li>as: 'something' will add the <code>something</code> local variable</li><li>as: this will use the collection value as the template context</li><li><p>as: global will merge the collection value's properties with <code>locals</code></p></li></ul></li><li><p><code>collection</code> Array of objects, the name is derived from the view name itself. 816For example <em>video.html</em> will have a object <em>video</em> available to it.</p></li></ul>817818<h2></h2>819820<ul><li><p><strong>param</strong>: <em>String</em> view</p></li><li><p><strong>param</strong>: <em>Object | Array</em> options or collection</p></li><li><p><strong>return</strong>: <em>String</em> </p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>821</td>822<td class="code">823<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>){824 <span class="comment">// Inherit parent view extension when not present</span>825 <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>) {826 <span class="variable">view</span> += <span class="variable">ext</span>;827 }828829 <span class="comment">// Allow collection to be passed as second param</span>830 <span class="keyword">if</span> (<span class="class">Array</span>.<span class="variable">isArray</span>(<span class="variable">options</span>)) {831 <span class="variable">options</span> = { <span class="variable">collection</span>: <span class="variable">options</span> };832 }833834 <span class="comment">// Defaults</span>835 <span class="variable">options</span> = <span class="variable">options</span> || {};836 <span class="variable">options</span>.<span class="variable">locals</span> = <span class="variable">options</span>.<span class="variable">locals</span> || {};837 <span class="variable">options</span>.<span class="variable">partial</span> = <span class="variable">true</span>;838 <span class="variable">options</span>.<span class="variable">layout</span> = <span class="variable">false</span>;839840 <span class="comment">// Collection support</span>841 <span class="keyword">var</span> <span class="variable">collection</span> = <span class="variable">options</span>.<span class="variable">collection</span>;842 <span class="keyword">if</span> (<span class="variable">collection</span>) {843 <span class="keyword">var</span> <span class="variable">name</span> = <span class="variable">options</span>.<span class="variable">as</span> || <span class="variable">view</span>.<span class="variable">split</span>(<span class="string">'.'</span>)[<span class="number integer">0</span>],844 <span class="variable">len</span> = <span class="variable">collection</span>.<span class="variable">length</span>;845 <span class="keyword">delete</span> <span class="variable">options</span>.<span class="variable">collection</span>;846 <span class="variable">options</span>.<span class="variable">locals</span>.<span class="variable">collectionLength</span> = <span class="variable">len</span>;847 <span class="keyword">return</span> <span class="variable">collection</span>.<span class="variable">map</span>(<span class="keyword">function</span>(<span class="variable">val</span>, <span class="variable">i</span>){848 <span class="variable">options</span>.<span class="variable">locals</span>.<span class="variable">firstInCollection</span> = <span class="variable">i</span> === <span class="number integer">0</span>;849 <span class="variable">options</span>.<span class="variable">locals</span>.<span class="variable">indexInCollection</span> = <span class="variable">i</span>;850 <span class="variable">options</span>.<span class="variable">locals</span>.<span class="variable">lastInCollection</span> = <span class="variable">i</span> === <span class="variable">len</span> - <span class="number integer">1</span>;851 <span class="variable">options</span>.<span class="variable">object</span> = <span class="variable">val</span>;852 <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">partial</span>(<span class="variable">view</span>, <span class="variable">options</span>);853 }, <span class="this">this</span>).<span class="variable">join</span>(<span class="string">''</span>);854 } <span class="keyword">else</span> {855 <span class="keyword">if</span> (<span class="variable">options</span>.<span class="variable">object</span>) {856 <span class="keyword">var</span> <span class="variable">name</span> = <span class="variable">options</span>.<span class="variable">as</span> || <span class="variable">view</span>.<span class="variable">split</span>(<span class="string">'.'</span>)[<span class="number integer">0</span>];857 <span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">name</span> === <span class="string">'string'</span>) {858 <span class="variable">options</span>.<span class="variable">locals</span>[<span class="variable">name</span>] = <span class="variable">options</span>.<span class="variable">object</span>;859 } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="variable">name</span> === <span class="variable">global</span>) {860 <span class="variable">utils</span>.<span class="variable">merge</span>(<span class="variable">options</span>.<span class="variable">locals</span>, <span class="variable">options</span>.<span class="variable">object</span>);861 } <span class="keyword">else</span> {862 <span class="variable">options</span>.<span class="variable">context</span> = <span class="variable">options</span>.<span class="variable">object</span>;863 }864 }865 <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">render</span>(<span class="variable">view</span>, <span class="variable">options</span>);866 }867};</code></pre>868</td>869</tr>870<tr class="code">871<td class="docs">872<p>Render <code>view</code> with the given <code>options</code> and optional callback <code>fn</code>.873When a callback function is given a response will <em>not</em> be made874automatically, however otherwise a response of <em>200</em> and <em>text/html</em> is given.</p>875876<h2>Options</h2>877878<p> Most engines accept one or more of the following options,879 both <em>haml</em> and <em>jade</em> accept all:</p>880881<ul><li><code>context|scope</code> Template evaluation context (<code>this</code>)</li><li><code>locals</code> Object containing local variables</li><li><code>debug</code> Output debugging information</li><li><code>status</code> Response status code, defaults to 200</li><li><code>headers</code> Response headers object</li></ul>882883<h2></h2>884885<ul><li><p><strong>param</strong>: <em>String</em> view</p></li><li><p><strong>param</strong>: <em>Object | Function</em> options or callback function</p></li><li><p><strong>param</strong>: <em>Function</em> fn</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>886</td>887<td class="code">888<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>){889 <span class="comment">// Support callback function as second arg</span>890 <span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">options</span> === <span class="string">'function'</span>) {891 <span class="variable">fn</span> = <span class="variable">options</span>, <span class="variable">options</span> = {};892 }893 894 <span class="keyword">var</span> <span class="variable">options</span> = <span class="variable">options</span> || {},895 <span class="variable">viewOptions</span> = <span class="this">this</span>.<span class="variable">app</span>.<span class="variable">set</span>(<span class="string">'view options'</span>),896 <span class="variable">defaultEngine</span> = <span class="this">this</span>.<span class="variable">app</span>.<span class="variable">set</span>(<span class="string">'view engine'</span>);897898 <span class="comment">// Mixin &quot;view options&quot;</span>899 <span class="keyword">if</span> (<span class="variable">viewOptions</span>) <span class="variable">options</span>.<span class="variable">__proto__</span> = <span class="variable">viewOptions</span>;900901 <span class="comment">// Support &quot;view engine&quot; setting</span>902 <span class="keyword">if</span> (<span class="variable">view</span>.<span class="variable">indexOf</span>(<span class="string">'.'</span>) &<span class="variable">lt</span>; <span class="number integer">0</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">defaultEngine</span>) {903 <span class="variable">view</span> += <span class="string">'.'</span> + <span class="variable">defaultEngine</span>;904 }905906 <span class="comment">// Defaults</span>907 <span class="keyword">var</span> <span class="variable">self</span> = <span class="this">this</span>,908 <span class="variable">helpers</span> = <span class="this">this</span>.<span class="variable">app</span>.<span class="variable">viewHelpers</span>,909 <span class="variable">dynamicHelpers</span> = <span class="this">this</span>.<span class="variable">app</span>.<span class="variable">dynamicViewHelpers</span>,910 <span class="variable">root</span> = <span class="variable">viewRoot</span>(<span class="this">this</span>.<span class="variable">app</span>),911 <span class="variable">ext</span> = <span class="variable">extname</span>(<span class="variable">view</span>),912 <span class="variable">partial</span> = <span class="variable">options</span>.<span class="variable">partial</span>,913 <span class="variable">layout</span> = <span class="variable">options</span>.<span class="variable">layout</span> === <span class="variable">undefined</span> ? <span class="variable">true</span> : <span class="variable">options</span>.<span class="variable">layout</span>,914 <span class="variable">layout</span> = <span class="variable">layout</span> === <span class="variable">true</span>915 ? <span class="string">'layout'</span> + <span class="variable">ext</span>916 : <span class="variable">layout</span>;917918 <span class="comment">// Allow layout name without extension</span>919 <span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">layout</span> === <span class="string">'string'</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">layout</span>.<span class="variable">indexOf</span>(<span class="string">'.'</span>) &<span class="variable">lt</span>; <span class="number integer">0</span>) {920 <span class="variable">layout</span> += <span class="variable">ext</span>;921 }922923 <span class="comment">// Normalize &quot;context&quot; to &quot;scope&quot;, defaulting to this response</span>924 <span class="variable">options</span>.<span class="variable">scope</span> = <span class="variable">options</span>.<span class="variable">scope</span> || <span class="variable">options</span>.<span class="variable">context</span> || <span class="this">this</span>;925926 <span class="comment">// Auto-cache in production</span>927 <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">app</span>.<span class="variable">set</span>(<span class="string">'env'</span>) === <span class="string">'production'</span>) {928 <span class="variable">options</span>.<span class="variable">cache</span> = <span class="variable">true</span>;929 }930931 <span class="comment">// Partials support</span>932 <span class="keyword">if</span> (<span class="variable">options</span>.<span class="variable">partial</span>) {933 <span class="variable">root</span> = <span class="this">this</span>.<span class="variable">app</span>.<span class="variable">set</span>(<span class="string">'partials'</span>)934 ? <span class="this">this</span>.<span class="variable">app</span>.<span class="variable">set</span>(<span class="string">'partials'</span>)935 : <span class="variable">root</span> + <span class="string">'/partials'</span>;936 }937938 <span class="comment">// View path</span>939 <span class="keyword">var</span> <span class="variable">path</span> = <span class="variable">view</span>[<span class="number integer">0</span>] === <span class="string">'/'</span>940 ? <span class="variable">view</span>941 : <span class="variable">root</span> + <span class="string">'/'</span> + <span class="variable">view</span>;942943 <span class="comment">// Pass filename to the engine and view</span>944 <span class="variable">options</span>.<span class="variable">locals</span> = <span class="variable">options</span>.<span class="variable">locals</span> || {};945 <span class="variable">options</span>.<span class="variable">locals</span>.<span class="variable">__filename</span> = <span class="variable">options</span>.<span class="variable">filename</span> = <span class="variable">path</span>;946947 <span class="comment">// Dynamic helper support</span>948 <span class="keyword">if</span> (<span class="variable">options</span>.<span class="variable">dynamicHelpers</span> !== <span class="variable">false</span>) {949 <span class="keyword">var</span> <span class="variable">keys</span> = <span class="class">Object</span>.<span class="variable">keys</span>(<span class="variable">dynamicHelpers</span>);950 <span class="keyword">for</span> (<span class="keyword">var</span> <span class="variable">i</span> = <span class="number integer">0</span>, <span class="variable">len</span> = <span class="variable">keys</span>.<span class="variable">length</span>; <span class="variable">i</span> &<span class="variable">lt</span>; <span class="variable">len</span>; ++<span class="variable">i</span>) {951 <span class="keyword">var</span> <span class="variable">key</span> = <span class="variable">keys</span>[<span class="variable">i</span>],952 <span class="variable">val</span> = <span class="variable">dynamicHelpers</span>[<span class="variable">key</span>];953 <span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">val</span> === <span class="string">'function'</span>) {954 <span class="variable">helpers</span>[<span class="variable">key</span>] = <span class="variable">val</span>.<span class="variable">call</span>(955 <span class="this">this</span>.<span class="variable">app</span>, 956 <span class="this">this</span>.<span class="variable">req</span>, 957 <span class="this">this</span>);958 }959 }960 }961962 <span class="comment">// Always expose partial() as a local</span>963 <span class="variable">options</span>.<span class="variable">locals</span>.<span class="variable">partial</span> = <span class="keyword">function</span>(<span class="variable">view</span>, <span class="variable">options</span>){964 <span class="keyword">return</span> <span class="variable">self</span>.<span class="variable">partial</span>.<span class="variable">call</span>(<span class="variable">self</span>, <span class="variable">view</span>, <span class="variable">options</span>, <span class="variable">ext</span>);965 };966 967 <span class="comment">// Merge view helpers</span>968 <span class="variable">options</span>.<span class="variable">locals</span>.<span class="variable">__proto__</span> = <span class="variable">helpers</span>;969970 <span class="keyword">function</span> <span class="variable">error</span>(<span class="variable">err</span>) {971 <span class="keyword">if</span> (<span class="variable">fn</span>) {972 <span class="variable">fn</span>(<span class="variable">err</span>);973 } <span class="keyword">else</span> {974 <span class="variable">self</span>.<span class="variable">req</span>.<span class="variable">next</span>(<span class="variable">err</span>);975 }976 }977978 <span class="comment">// Cache contents</span>979 <span class="keyword">try</span> {980 <span class="keyword">var</span> <span class="variable">str</span> = (<span class="variable">options</span>.<span class="variable">cache</span> ? <span class="variable">viewCache</span>[<span class="variable">path</span>] : <span class="keyword">null</span>) || <span class="variable">cacheViewSync</span>(<span class="variable">path</span>);981 } <span class="keyword">catch</span> (<span class="variable">err</span>) {982 <span class="keyword">return</span> <span class="variable">error</span>(<span class="variable">err</span>);983 }984985 <span class="comment">// Cache template engine exports</span>986 <span class="keyword">var</span> <span class="variable">engine</span> = <span class="variable">cache</span>[<span class="variable">ext</span>] || (<span class="variable">cache</span>[<span class="variable">ext</span>] = <span class="variable">require</span>(<span class="variable">ext</span>.<span class="variable">substr</span>(<span class="number integer">1</span>)));987988 <span class="comment">// Attempt render</span>989 <span class="keyword">try</span> {990 <span class="keyword">var</span> <span class="variable">str</span> = <span class="variable">engine</span>.<span class="variable">render</span>(<span class="variable">str</span>, <span class="variable">options</span>);991 } <span class="keyword">catch</span> (<span class="variable">err</span>) {992 <span class="keyword">return</span> <span class="variable">error</span>(<span class="variable">err</span>);993 }994995 <span class="comment">// Layout support</span>996 <span class="keyword">if</span> (<span class="variable">layout</span>) {997 <span class="variable">options</span>.<span class="variable">layout</span> = <span class="variable">false</span>;998 <span class="variable">options</span>.<span class="variable">locals</span>.<span class="variable">body</span> = <span class="variable">str</span>;999 <span class="variable">options</span>.<span class="variable">isLayout</span> = <span class="variable">true</span>;1000 <span class="variable">self</span>.<span class="variable">render</span>(<span class="variable">layout</span>, <span class="variable">options</span>, <span class="variable">fn</span>);1001 } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="variable">partial</span>) {1002 <span class="keyword">return</span> <span class="variable">str</span>;1003 } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="variable">fn</span>) {1004 <span class="variable">fn</span>(<span class="keyword">null</span>, <span class="variable">str</span>);1005 } <span class="keyword">else</span> {1006 <span class="variable">self</span>.<span class="variable">send</span>(<span class="variable">str</span>, <span class="variable">options</span>.<span class="variable">headers</span>, <span class="variable">options</span>.<span class="variable">status</span>);1007 }1008};1009</code></pre>1010</td>738</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">739<td class="docs">740<p>Module dependencies.741 </p>
376 unmodified lines
1118 .<span class="variable">replace</span>(<span class="regexp">/&lt;/g</span>, <span class="string">'&lt;'</span>)1119 .<span class="variable">replace</span>(<span class="regexp">/&gt;/g</span>, <span class="string">'&gt;'</span>);1120};</code></pre>1121</td>1122</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">1123<td class="docs">1124<p>Module dependencies.1125 </p>1126</td>1127<td class="code">1128<pre><code><span class="keyword">var</span> <span class="variable">extname</span> = <span class="variable">require</span>(<span class="string">'path'</span>).<span class="variable">extname</span>,1129 <span class="variable">mime</span> = <span class="variable">require</span>(<span class="string">'connect/utils'</span>).<span class="variable">mime</span>,1130 <span class="variable">utils</span> = <span class="variable">require</span>(<span class="string">'connect/utils'</span>),1131 <span class="variable">http</span> = <span class="variable">require</span>(<span class="string">'http'</span>),1132 <span class="variable">sys</span> = <span class="variable">require</span>(<span class="string">'sys'</span>),1133 <span class="variable">fs</span> = <span class="variable">require</span>(<span class="string">'fs'</span>);</code></pre>1134</td>1135</tr>1136<tr class="code">1137<td class="docs">1138<p>Cache supported template engine exports to1139increase performance by lowering the number1140of calls to <code>require()</code>.</p>11411142<ul><li><p><strong>type</strong>: <em>Object</em></p></li></ul>1143</td>1144<td class="code">1145<pre><code><span class="keyword">var</span> <span class="variable">cache</span> = {};</code></pre>1146</td>1147</tr>1148<tr class="code">1149<td class="docs">1150<p>Cache view contents to prevent I/O hits.</p>11511152<ul><li><p><strong>type</strong>: <em>Object</em></p></li></ul>1153</td>1154<td class="code">1155<pre><code><span class="keyword">var</span> <span class="variable">viewCache</span> = {};</code></pre>1156</td>1157</tr>1158<tr class="code">1159<td class="docs">1160<p>Register the given template engine <code>exports</code>1161as <code>ext</code>. For example we may wish to map ".html"1162files to jade:</p>11631164<p> app.register('.html', require('jade'));</p>11651166<p>This is also useful for libraries that may not1167match extensions correctly. For example my haml.js1168library is installed from npm as "hamljs" so instead1169of layout.hamljs, we can register the engine as ".haml":</p>11701171<p> app.register('.haml', require('haml-js'));</p>11721173<p>For engines that do not comply with the Express1174specification, we can also wrap their api this way.</p>11751176<p> app.register('.foo', {1177 render: function(str, options) {1178 // perhaps their api is1179 // foo.toHTML(str, options);1180 }1181 });</p>11821183<h2></h2>11841185<ul><li><p><strong>param</strong>: <em>String</em> ext</p></li><li><p><strong>param</strong>: <em>Object</em> obj</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>1186</td>1187<td class="code">1188<pre><code><span class="variable">exports</span>.<span class="variable">register</span> = <span class="keyword">function</span>(<span class="variable">ext</span>, <span class="variable">exports</span>) {1189 <span class="variable">cache</span>[<span class="variable">ext</span>] = <span class="variable">exports</span>;1190};</code></pre>1191</td>1192</tr>1193<tr class="code">1194<td class="docs">1195<p>Render <code>view</code> partial with the given <code>options</code>.</p>11961197<h2>Options</h2>11981199<ul><li><p><code>object</code> Single object with name derived from the view (unless <code>as</code> is present) </p></li><li><p><code>as</code> Variable name for each <code>collection</code> value, defaults to the view name.</p><ul><li>as: 'something' will add the <code>something</code> local variable</li><li>as: this will use the collection value as the template context</li><li><p>as: global will merge the collection value's properties with <code>locals</code></p></li></ul></li><li><p><code>collection</code> Array of objects, the name is derived from the view name itself. 1200For example <em>video.html</em> will have a object <em>video</em> available to it.</p></li></ul>12011202<h2></h2>12031204<ul><li><p><strong>param</strong>: <em>String</em> view</p></li><li><p><strong>param</strong>: <em>Object | Array</em> options or collection</p></li><li><p><strong>return</strong>: <em>String</em> </p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>1205</td>1206<td class="code">1207<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>){1208 <span class="comment">// Inherit parent view extension when not present</span>1209 <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>) {1210 <span class="variable">view</span> += <span class="variable">ext</span>;1211 }12121213 <span class="comment">// Allow collection to be passed as second param</span>1214 <span class="keyword">if</span> (<span class="class">Array</span>.<span class="variable">isArray</span>(<span class="variable">options</span>)) {1215 <span class="variable">options</span> = { <span class="variable">collection</span>: <span class="variable">options</span> };1216 }12171218 <span class="comment">// Defaults</span>1219 <span class="variable">options</span> = <span class="variable">options</span> || {};1220 <span class="variable">options</span>.<span class="variable">locals</span> = <span class="variable">options</span>.<span class="variable">locals</span> || {};1221 <span class="variable">options</span>.<span class="variable">partial</span> = <span class="variable">true</span>;1222 <span class="variable">options</span>.<span class="variable">layout</span> = <span class="variable">false</span>;12231224 <span class="comment">// Collection support</span>1225 <span class="keyword">var</span> <span class="variable">collection</span> = <span class="variable">options</span>.<span class="variable">collection</span>;1226 <span class="keyword">if</span> (<span class="variable">collection</span>) {1227 <span class="keyword">var</span> <span class="variable">name</span> = <span class="variable">options</span>.<span class="variable">as</span> || <span class="variable">view</span>.<span class="variable">split</span>(<span class="string">'.'</span>)[<span class="number integer">0</span>],1228 <span class="variable">len</span> = <span class="variable">collection</span>.<span class="variable">length</span>;1229 <span class="keyword">delete</span> <span class="variable">options</span>.<span class="variable">collection</span>;1230 <span class="variable">options</span>.<span class="variable">locals</span>.<span class="variable">collectionLength</span> = <span class="variable">len</span>;1231 <span class="keyword">return</span> <span class="variable">collection</span>.<span class="variable">map</span>(<span class="keyword">function</span>(<span class="variable">val</span>, <span class="variable">i</span>){1232 <span class="variable">options</span>.<span class="variable">locals</span>.<span class="variable">firstInCollection</span> = <span class="variable">i</span> === <span class="number integer">0</span>;1233 <span class="variable">options</span>.<span class="variable">locals</span>.<span class="variable">indexInCollection</span> = <span class="variable">i</span>;1234 <span class="variable">options</span>.<span class="variable">locals</span>.<span class="variable">lastInCollection</span> = <span class="variable">i</span> === <span class="variable">len</span> - <span class="number integer">1</span>;1235 <span class="variable">options</span>.<span class="variable">object</span> = <span class="variable">val</span>;1236 <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">partial</span>(<span class="variable">view</span>, <span class="variable">options</span>);1237 }, <span class="this">this</span>).<span class="variable">join</span>(<span class="string">''</span>);1238 } <span class="keyword">else</span> {1239 <span class="keyword">if</span> (<span class="variable">options</span>.<span class="variable">object</span>) {1240 <span class="keyword">var</span> <span class="variable">name</span> = <span class="variable">options</span>.<span class="variable">as</span> || <span class="variable">view</span>.<span class="variable">split</span>(<span class="string">'.'</span>)[<span class="number integer">0</span>];1241 <span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">name</span> === <span class="string">'string'</span>) {1242 <span class="variable">options</span>.<span class="variable">locals</span>[<span class="variable">name</span>] = <span class="variable">options</span>.<span class="variable">object</span>;1243 } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="variable">name</span> === <span class="variable">global</span>) {1244 <span class="variable">utils</span>.<span class="variable">merge</span>(<span class="variable">options</span>.<span class="variable">locals</span>, <span class="variable">options</span>.<span class="variable">object</span>);1245 } <span class="keyword">else</span> {1246 <span class="variable">options</span>.<span class="variable">scope</span> = <span class="variable">options</span>.<span class="variable">object</span>;1247 }1248 }1249 <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">render</span>(<span class="variable">view</span>, <span class="variable">options</span>);1250 }1251};</code></pre>1252</td>1253</tr>1254<tr class="code">1255<td class="docs">1256<p>Render <code>view</code> with the given <code>options</code> and optional callback <code>fn</code>.1257When a callback function is given a response will <em>not</em> be made1258automatically, however otherwise a response of <em>200</em> and <em>text/html</em> is given.</p>12591260<h2>Options</h2>12611262<p> Most engines accept one or more of the following options,1263 both <em>haml</em> and <em>jade</em> accept all:</p>12641265<ul><li><code>scope</code> Template evaluation context (the value of <code>this</code>)</li><li><code>locals</code> Object containing local variables</li><li><code>debug</code> Output debugging information</li><li><code>status</code> Response status code, defaults to 200</li><li><code>headers</code> Response headers object</li></ul>12661267<h2></h2>12681269<ul><li><p><strong>param</strong>: <em>String</em> view</p></li><li><p><strong>param</strong>: <em>Object | Function</em> options or callback function</p></li><li><p><strong>param</strong>: <em>Function</em> fn</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>1270</td>1271<td class="code">1272<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>){1273 <span class="comment">// Support callback function as second arg</span>1274 <span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">options</span> === <span class="string">'function'</span>) {1275 <span class="variable">fn</span> = <span class="variable">options</span>, <span class="variable">options</span> = {};1276 }1277 1278 <span class="keyword">var</span> <span class="variable">options</span> = <span class="variable">options</span> || {},1279 <span class="variable">viewOptions</span> = <span class="this">this</span>.<span class="variable">app</span>.<span class="variable">set</span>(<span class="string">'view options'</span>),1280 <span class="variable">defaultEngine</span> = <span class="this">this</span>.<span class="variable">app</span>.<span class="variable">set</span>(<span class="string">'view engine'</span>);12811282 <span class="comment">// Mixin &quot;view options&quot;</span>1283 <span class="keyword">if</span> (<span class="variable">viewOptions</span>) <span class="variable">options</span>.<span class="variable">__proto__</span> = <span class="variable">viewOptions</span>;12841285 <span class="comment">// Support &quot;view engine&quot; setting</span>1286 <span class="keyword">if</span> (<span class="variable">view</span>.<span class="variable">indexOf</span>(<span class="string">'.'</span>) &<span class="variable">lt</span>; <span class="number integer">0</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">defaultEngine</span>) {1287 <span class="variable">view</span> += <span class="string">'.'</span> + <span class="variable">defaultEngine</span>;1288 }12891290 <span class="comment">// Defaults</span>1291 <span class="keyword">var</span> <span class="variable">self</span> = <span class="this">this</span>,1292 <span class="variable">helpers</span> = <span class="this">this</span>.<span class="variable">app</span>.<span class="variable">viewHelpers</span>,1293 <span class="variable">dynamicHelpers</span> = <span class="this">this</span>.<span class="variable">app</span>.<span class="variable">dynamicViewHelpers</span>,1294 <span class="variable">root</span> = <span class="variable">viewRoot</span>(<span class="this">this</span>.<span class="variable">app</span>),1295 <span class="variable">ext</span> = <span class="variable">extname</span>(<span class="variable">view</span>),1296 <span class="variable">partial</span> = <span class="variable">options</span>.<span class="variable">partial</span>,1297 <span class="variable">layout</span> = <span class="variable">options</span>.<span class="variable">layout</span> === <span class="variable">undefined</span> ? <span class="variable">true</span> : <span class="variable">options</span>.<span class="variable">layout</span>,1298 <span class="variable">layout</span> = <span class="variable">layout</span> === <span class="variable">true</span>1299 ? <span class="string">'layout'</span> + <span class="variable">ext</span>1300 : <span class="variable">layout</span>;13011302 <span class="comment">// Allow layout name without extension</span>1303 <span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">layout</span> === <span class="string">'string'</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">layout</span>.<span class="variable">indexOf</span>(<span class="string">'.'</span>) &<span class="variable">lt</span>; <span class="number integer">0</span>) {1304 <span class="variable">layout</span> += <span class="variable">ext</span>;1305 }13061307 <span class="comment">// Default execution scope to the response</span>1308 <span class="variable">options</span>.<span class="variable">scope</span> = <span class="variable">options</span>.<span class="variable">scope</span> || <span class="this">this</span>.<span class="variable">req</span>;13091310 <span class="comment">// Auto-cache in production</span>1311 <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">app</span>.<span class="variable">set</span>(<span class="string">'env'</span>) === <span class="string">'production'</span>) {1312 <span class="variable">options</span>.<span class="variable">cache</span> = <span class="variable">true</span>;1313 }13141315 <span class="comment">// Partials support</span>1316 <span class="keyword">if</span> (<span class="variable">options</span>.<span class="variable">partial</span>) {1317 <span class="variable">root</span> = <span class="this">this</span>.<span class="variable">app</span>.<span class="variable">set</span>(<span class="string">'partials'</span>)1318 ? <span class="this">this</span>.<span class="variable">app</span>.<span class="variable">set</span>(<span class="string">'partials'</span>)1319 : <span class="variable">root</span> + <span class="string">'/partials'</span>;1320 }13211322 <span class="comment">// View path</span>1323 <span class="keyword">var</span> <span class="variable">path</span> = <span class="variable">view</span>[<span class="number integer">0</span>] === <span class="string">'/'</span>1324 ? <span class="variable">view</span>1325 : <span class="variable">root</span> + <span class="string">'/'</span> + <span class="variable">view</span>;13261327 <span class="comment">// Pass filename to the engine and view</span>1328 <span class="variable">options</span>.<span class="variable">locals</span> = <span class="variable">options</span>.<span class="variable">locals</span> || {};1329 <span class="variable">options</span>.<span class="variable">locals</span>.<span class="variable">__filename</span> = <span class="variable">options</span>.<span class="variable">filename</span> = <span class="variable">path</span>;13301331 <span class="comment">// Dynamic helper support</span>1332 <span class="keyword">if</span> (<span class="variable">options</span>.<span class="variable">dynamicHelpers</span> !== <span class="variable">false</span>) {1333 <span class="keyword">var</span> <span class="variable">keys</span> = <span class="class">Object</span>.<span class="variable">keys</span>(<span class="variable">dynamicHelpers</span>);1334 <span class="keyword">for</span> (<span class="keyword">var</span> <span class="variable">i</span> = <span class="number integer">0</span>, <span class="variable">len</span> = <span class="variable">keys</span>.<span class="variable">length</span>; <span class="variable">i</span> &<span class="variable">lt</span>; <span class="variable">len</span>; ++<span class="variable">i</span>) {1335 <span class="keyword">var</span> <span class="variable">key</span> = <span class="variable">keys</span>[<span class="variable">i</span>],1336 <span class="variable">val</span> = <span class="variable">dynamicHelpers</span>[<span class="variable">key</span>];1337 <span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">val</span> === <span class="string">'function'</span>) {1338 <span class="variable">helpers</span>[<span class="variable">key</span>] = <span class="variable">val</span>.<span class="variable">call</span>(1339 <span class="this">this</span>.<span class="variable">app</span>, 1340 <span class="this">this</span>.<span class="variable">req</span>, 1341 <span class="this">this</span>);1342 }1343 }1344 }13451346 <span class="comment">// Always expose partial() as a local</span>1347 <span class="variable">options</span>.<span class="variable">locals</span>.<span class="variable">partial</span> = <span class="keyword">function</span>(<span class="variable">view</span>, <span class="variable">options</span>){1348 <span class="keyword">return</span> <span class="variable">self</span>.<span class="variable">partial</span>.<span class="variable">call</span>(<span class="variable">self</span>, <span class="variable">view</span>, <span class="variable">options</span>, <span class="variable">ext</span>);1349 };1350 1351 <span class="comment">// Merge view helpers</span>1352 <span class="variable">options</span>.<span class="variable">locals</span>.<span class="variable">__proto__</span> = <span class="variable">helpers</span>;13531354 <span class="keyword">function</span> <span class="variable">error</span>(<span class="variable">err</span>) {1355 <span class="keyword">if</span> (<span class="variable">fn</span>) {1356 <span class="variable">fn</span>(<span class="variable">err</span>);1357 } <span class="keyword">else</span> {1358 <span class="variable">self</span>.<span class="variable">req</span>.<span class="variable">next</span>(<span class="variable">err</span>);1359 }1360 }13611362 <span class="comment">// Cache contents</span>1363 <span class="keyword">try</span> {1364 <span class="keyword">var</span> <span class="variable">str</span> = (<span class="variable">options</span>.<span class="variable">cache</span> ? <span class="variable">viewCache</span>[<span class="variable">path</span>] : <span class="keyword">null</span>) || <span class="variable">cacheViewSync</span>(<span class="variable">path</span>);1365 } <span class="keyword">catch</span> (<span class="variable">err</span>) {1366 <span class="keyword">return</span> <span class="variable">error</span>(<span class="variable">err</span>);1367 }13681369 <span class="comment">// Cache template engine exports</span>1370 <span class="keyword">var</span> <span class="variable">engine</span> = <span class="variable">cache</span>[<span class="variable">ext</span>] || (<span class="variable">cache</span>[<span class="variable">ext</span>] = <span class="variable">require</span>(<span class="variable">ext</span>.<span class="variable">substr</span>(<span class="number integer">1</span>)));13711372 <span class="comment">// Attempt render</span>1373 <span class="keyword">try</span> {1374 <span class="keyword">var</span> <span class="variable">str</span> = <span class="variable">engine</span>.<span class="variable">render</span>(<span class="variable">str</span>, <span class="variable">options</span>);1375 } <span class="keyword">catch</span> (<span class="variable">err</span>) {1376 <span class="keyword">return</span> <span class="variable">error</span>(<span class="variable">err</span>);1377 }13781379 <span class="comment">// Layout support</span>1380 <span class="keyword">if</span> (<span class="variable">layout</span>) {1381 <span class="variable">options</span>.<span class="variable">layout</span> = <span class="variable">false</span>;1382 <span class="variable">options</span>.<span class="variable">locals</span>.<span class="variable">body</span> = <span class="variable">str</span>;1383 <span class="variable">options</span>.<span class="variable">isLayout</span> = <span class="variable">true</span>;1384 <span class="variable">self</span>.<span class="variable">render</span>(<span class="variable">layout</span>, <span class="variable">options</span>, <span class="variable">fn</span>);1385 } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="variable">partial</span>) {1386 <span class="keyword">return</span> <span class="variable">str</span>;1387 } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="variable">fn</span>) {1388 <span class="variable">fn</span>(<span class="keyword">null</span>, <span class="variable">str</span>);1389 } <span class="keyword">else</span> {1390 <span class="variable">self</span>.<span class="variable">send</span>(<span class="variable">str</span>, <span class="variable">options</span>.<span class="variable">headers</span>, <span class="variable">options</span>.<span class="variable">status</span>);1391 }1392};1393</code></pre>1394</td>1395</tr> </body>1396</html></tbody></table>
modified:docs/contrib.html+7 added-3 removed
14 unmodified lines
15 padding-bottom: 30px;16 font: 14px/1.4 "Helvetica Neue", "Lucida Grande", "Arial";17 font-size: 14px;18 line-height: 1.5;19 -webkit-text-stroke: 1px rgba(0, 0, 0, 0.1);20 -moz-text-stroke: 1px rgba(0, 0, 0, 0.1);19 -webkit-font-smoothing: antialiased;20 -moz-font-smoothing: antialiased;21 background: url(images/bg.jpg) 50% 0 no-repeat;22 color: #8b8b8b; }2324 * {
14 unmodified lines
39 -webkit-transition-timing-function: ease-out; }40 a:hover {41 opacity: 0.8; }4243 h1, h2, h3 {43 h1, h2, h3, h4 {44 margin: 45px 0 0 0;45 color: white;46 text-shadow: 1px 2px 2px rgba(0,0,0,0.6); }4748 h3 {49 font-size: 18px; }50 h4 {51 margin-left: 10px;52 font-size: 14px;53 }5455 pre {56 margin: 20px 10px;57 padding: 25px 20px;
187 unmodified lines
modified:docs/executable.html+7 added-3 removed
14 unmodified lines
15 padding-bottom: 30px;16 font: 14px/1.4 "Helvetica Neue", "Lucida Grande", "Arial";17 font-size: 14px;18 line-height: 1.5;19 -webkit-text-stroke: 1px rgba(0, 0, 0, 0.1);20 -moz-text-stroke: 1px rgba(0, 0, 0, 0.1);19 -webkit-font-smoothing: antialiased;20 -moz-font-smoothing: antialiased;21 background: url(images/bg.jpg) 50% 0 no-repeat;22 color: #8b8b8b; }2324 * {
14 unmodified lines
39 -webkit-transition-timing-function: ease-out; }40 a:hover {41 opacity: 0.8; }4243 h1, h2, h3 {43 h1, h2, h3, h4 {44 margin: 45px 0 0 0;45 color: white;46 text-shadow: 1px 2px 2px rgba(0,0,0,0.6); }4748 h3 {49 font-size: 18px; }50 h4 {51 margin-left: 10px;52 font-size: 14px;53 }5455 pre {56 margin: 20px 10px;57 padding: 25px 20px;
159 unmodified lines
modified:docs/guide.1+90 added-2 removed
1.\" generated with Ronn/v0.7.32.\" http://github.com/rtomayko/ronn/tree/0.7.33.4.TH "GUIDE" "" "September 2010" "" ""4.TH "GUIDE" "" "October 2010" "" ""5.6.SH "NAME"7\fBguide\fR8.
273 unmodified lines
282.fi283.284.IP "" 0285.286.SS "HTTP Methods"287We have seen \fIapp\.get()\fR a few times, however Express also exposes other familiar HTTP verbs in the same manor, such as \fIapp\.post()\fR, \fIapp\.del()\fR, etc\.288.289.P290A common example for \fIPOST\fR usage, is when "submitting" a form\. Below we simply set our form method to "post" in our html, and control will be given to the route we have defined below it\.291.292.P293.294.IP "" 4295.296.nf297298 <input type="text" name="user[name]" />299 <input type="text" name="user[email]" />300 <input type="submit" value="Submit" />301.302.fi303.304.IP "" 0305.306.P307.308.P309By default Express does not know what to do with this request body, so we should add the \fIbodyDecoder\fR middleware, which will parse \fIapplication/x\-www\-form\-urlencoded\fR request bodies and place the variables in \fIreq\.body\fR\. We can do this by "using" the middleware as shown below:310.311.IP "" 4312.313.nf314315app\.use(express\.bodyDecoder());316.317.fi318.319.IP "" 0320.321.P322Our route below will now have access to the \fIreq\.body\.user\fR object which will contain the \fIname\fR and \fIemail\fR properties when defined\.323.324.IP "" 4325.326.nf327328app\.post(\'/\', function(req, res){329 console\.log(req\.body\.user);330 res\.redirect(\'back\');331});332.333.fi334.335.IP "" 0336.337.P338When using methods such as \fIPUT\fR with a form, we can utilize a hidden input named \fI_method\fR, which can be used to alter the HTTP method\. To do so we first need the \fImethodOverride\fR middleware, which should be placed below \fIbodyDecoder\fR so that it can utilize it\'s \fIreq\.body\fR containing the form values\.339.340.IP "" 4341.342.nf343344app\.use(express\.bodyDecoder());345app\.use(express\.methodOverride());346.347.fi348.349.IP "" 0350.351.P352The reason that these are not always defaults, is simply because these are not required for Express to be fully functional\. Depending on the needs of your application, you may not need these at all, your methods such as \fIPUT\fR and \fIDELETE\fR can still be accessed by clients which can use them directly, although \fImethodOverride\fR provides a great solution for forms\. Below shows what the usage of \fIPUT\fR might look like:353.354.IP "" 4355.356.nf357358<form method="post" action="/">359 <input type="hidden" name="_method" value="put" />360 <input type="text" name="user[name]" />361 <input type="text" name="user[email]" />362 <input type="submit" value="Submit" />363</form>364365app\.put(\'/\', function(){366 console\.log(req\.body\.user);367 res\.redirect(\'back\');368});369.370.fi371.372.IP "" 0373.374.SS "Middleware"375The Express \fIPlugin\fR is no more! middleware via Connect \fIhttp://github\.com/senchalabs/connect\fR can be passed to \fIexpress\.createServer()\fR as you would with a regular Connect server\. For example:376.377.IP "" 4
647 unmodified lines
1025.P1026Most engines accept one or more of the following options, both haml \fIhttp://github\.com/visionmedia/haml\.js\fR and jade \fIhttp://github\.com/visionmedia/jade\fR accept all:1027.1028.IP "\(bu" 4941\fIcontext|scope\fR Template evaluation context (\fIthis\fR)1029\fIscope\fR Template evaluation context (value of \fIthis\fR)1030.1031.IP "\(bu" 41032\fIlocals\fR Object containing local variables1033.
393 unmodified lines
modified:docs/guide.html+58 added-4 removed
14 unmodified lines
15 padding-bottom: 30px;16 font: 14px/1.4 "Helvetica Neue", "Lucida Grande", "Arial";17 font-size: 14px;18 line-height: 1.5;19 -webkit-text-stroke: 1px rgba(0, 0, 0, 0.1);20 -moz-text-stroke: 1px rgba(0, 0, 0, 0.1);19 -webkit-font-smoothing: antialiased;20 -moz-font-smoothing: antialiased;21 background: url(images/bg.jpg) 50% 0 no-repeat;22 color: #8b8b8b; }2324 * {
14 unmodified lines
39 -webkit-transition-timing-function: ease-out; }40 a:hover {41 opacity: 0.8; }4243 h1, h2, h3 {43 h1, h2, h3, h4 {44 margin: 45px 0 0 0;45 color: white;46 text-shadow: 1px 2px 2px rgba(0,0,0,0.6); }4748 h3 {49 font-size: 18px; }50 h4 {51 margin-left: 10px;52 font-size: 14px;53 }5455 pre {56 margin: 20px 10px;57 padding: 25px 20px;
123 unmodified lines
181<li><a href="#Configuration">Configuration</a></li>182<li><a href="#Settings">Settings</a></li>183<li><a href="#Routing">Routing</a></li>184<li><a href="#Passing-Route-Control">Passing Route Control</a></li>185<li><a href="#HTTP-Methods">HTTP Methods</a></li>186<li><a href="#Middleware">Middleware</a></li>187<li><a href="#Error-Handling">Error Handling</a></li>188<li><a href="#View-Rendering">View Rendering</a></li>189<li><a href="#View-Partials">View Partials</a></li>
235 unmodified lines
425 // do something else426});427</code></pre>428429<h3 id="HTTP-Methods">HTTP Methods</h3>430431<p>We have seen <em>app.get()</em> a few times, however Express also exposes other familiar HTTP verbs in the same manor, such as <em>app.post()</em>, <em>app.del()</em>, etc.</p>432433<p> A common example for <em>POST</em> usage, is when "submitting" a form. Below we simply set our form method to "post" in our html, and control will be given to the route we have defined below it.</p>434435<p> <form method="post" action="/" /></p>436437<pre><code> &lt;input type="text" name="user[name]" /&gt;438 &lt;input type="text" name="user[email]" /&gt;439 &lt;input type="submit" value="Submit" /&gt;440</code></pre>441442<p> </p>443444<p>By default Express does not know what to do with this request body, so we should add the <em>bodyDecoder</em> middleware, which will parse <em>application/x-www-form-urlencoded</em> request bodies and place the variables in <em>req.body</em>. We can do this by "using" the middleware as shown below:</p>445446<pre><code>app.use(express.bodyDecoder());447</code></pre>448449<p>Our route below will now have access to the <em>req.body.user</em> object which will contain the <em>name</em> and <em>email</em> properties when defined.</p>450451<pre><code>app.post('/', function(req, res){452 console.log(req.body.user);453 res.redirect('back');454});455</code></pre>456457<p>When using methods such as <em>PUT</em> with a form, we can utilize a hidden input named <em>_method</em>, which can be used to alter the HTTP method. To do so we first need the <em>methodOverride</em> middleware, which should be placed below <em>bodyDecoder</em> so that it can utilize it's <em>req.body</em> containing the form values.</p>458459<pre><code>app.use(express.bodyDecoder());460app.use(express.methodOverride());461</code></pre>462463<p>The reason that these are not always defaults, is simply because these are not required for Express to be fully functional. Depending on the needs of your application, you may not need these at all, your methods such as <em>PUT</em> and <em>DELETE</em> can still be accessed by clients which can use them directly, although <em>methodOverride</em> provides a great solution for forms. Below shows what the usage of <em>PUT</em> might look like:</p>464465<pre><code>&lt;form method="post" action="/"&gt;466 &lt;input type="hidden" name="_method" value="put" /&gt;467 &lt;input type="text" name="user[name]" /&gt;468 &lt;input type="text" name="user[email]" /&gt;469 &lt;input type="submit" value="Submit" /&gt; 470&lt;/form&gt;471472app.put('/', function(){473 console.log(req.body.user);474 res.redirect('back');475});476</code></pre>477478<h3 id="Middleware">Middleware</h3>479480<p>The Express <em>Plugin</em> is no more! middleware via <a href="http://github.com/senchalabs/connect">Connect</a> can be481passed to <em>express.createServer()</em> as you would with a regular Connect server. For example:</p>
406 unmodified lines
888<p> Most engines accept one or more of the following options,889 both <a href="http://github.com/visionmedia/haml.js">haml</a> and <a href="http://github.com/visionmedia/jade">jade</a> accept all:</p>890891<ul>838<li><em>context|scope</em> Template evaluation context (<em>this</em>)</li>892<li><em>scope</em> Template evaluation context (value of <em>this</em>)</li>893<li><em>locals</em> Object containing local variables</li>894<li><em>debug</em> Output debugging information</li>895<li><em>status</em> Response status code, defaults to 200</li>896<li><em>headers</em> Response headers object</li>
241 unmodified lines
modified:docs/guide.md+56 added-3 removed
68 unmodified lines
69 app.disable('some feature');70 // same as app.set('some feature', false);71 });7273To alter the environment we can set the _CONNECT_ENV_ environment variable,74or more specifically _EXPRESS_ENV_, for example:73To alter the environment we can set the _NODE_ENV_ environment variable, for example:7476 $ EXPRESS_ENV=production node app.js75 $ NODE_ENV=production node app.js7677### Settings7879Express supports the following settings out of the box:
89 unmodified lines
169 app.get('/users', function(req, res){170 // do something else171 });172173### HTTP Methods174175We have seen _app.get()_ a few times, however Express also exposes other familiar HTTP verbs in the same manor, such as _app.post()_, _app.del()_, etc.176177 A common example for _POST_ usage, is when "submitting" a form. Below we simply set our form method to "post" in our html, and control will be given to the route we have defined below it.178 179 <form method="post" action="/">180 <input type="text" name="user[name]" />181 <input type="text" name="user[email]" />182 <input type="submit" value="Submit" />183 </form>184185By default Express does not know what to do with this request body, so we should add the _bodyDecoder_ middleware, which will parse _application/x-www-form-urlencoded_ request bodies and place the variables in _req.body_. We can do this by "using" the middleware as shown below:186187 app.use(express.bodyDecoder());188189Our route below will now have access to the _req.body.user_ object which will contain the _name_ and _email_ properties when defined.190191 app.post('/', function(req, res){192 console.log(req.body.user);193 res.redirect('back');194 });195196When using methods such as _PUT_ with a form, we can utilize a hidden input named _\_method_, which can be used to alter the HTTP method. To do so we first need the _methodOverride_ middleware, which should be placed below _bodyDecoder_ so that it can utilize it's _req.body_ containing the form values.197198 app.use(express.bodyDecoder());199 app.use(express.methodOverride());200201The reason that these are not always defaults, is simply because these are not required for Express to be fully functional. Depending on the needs of your application, you may not need these at all, your methods such as _PUT_ and _DELETE_ can still be accessed by clients which can use them directly, although _methodOverride_ provides a great solution for forms. Below shows what the usage of _PUT_ might look like:202203 <form method="post" action="/">204 <input type="hidden" name="_method" value="put" />205 <input type="text" name="user[name]" />206 <input type="text" name="user[email]" />207 <input type="submit" value="Submit" /> 208 </form>209210 app.put('/', function(){211 console.log(req.body.user);212 res.redirect('back');213 });214215### Middleware216217The Express _Plugin_ is no more! middleware via [Connect](http://github.com/senchalabs/connect) can be218passed to _express.createServer()_ as you would with a regular Connect server. For example:
126 unmodified lines
345Which can then be overridden within the `res.render()` call if need be:346347 res.render('myview.ejs', { layout: true });348349When an alternate layout is required, we may also specify a path. For example if we have _view engine_ set to _jade_ and a file named _./views/mylayout.jade_ we can simply pass:350351 res.render('page', { layout: 'mylayout' });352353Otherwise we must specify the extension:354355 res.render('page', { layout: 'mylayout.jade' });356357These paths may also be absolute:358359 res.render('page', { layout: __dirname + '/../../mylayout.jade' });360361A good example of this is specifying custom _ejs_ opening and closing tags:362363 app.set('view options', {364 open: '{{',
438 unmodified lines
modified:docs/index.html+7 added-3 removed
14 unmodified lines
15 padding-bottom: 30px;16 font: 14px/1.4 "Helvetica Neue", "Lucida Grande", "Arial";17 font-size: 14px;18 line-height: 1.5;19 -webkit-text-stroke: 1px rgba(0, 0, 0, 0.1);20 -moz-text-stroke: 1px rgba(0, 0, 0, 0.1);19 -webkit-font-smoothing: antialiased;20 -moz-font-smoothing: antialiased;21 background: url(images/bg.jpg) 50% 0 no-repeat;22 color: #8b8b8b; }2324 * {
14 unmodified lines
39 -webkit-transition-timing-function: ease-out; }40 a:hover {41 opacity: 0.8; }4243 h1, h2, h3 {43 h1, h2, h3, h4 {44 margin: 45px 0 0 0;45 color: white;46 text-shadow: 1px 2px 2px rgba(0,0,0,0.6); }4748 h3 {49 font-size: 18px; }50 h4 {51 margin-left: 10px;52 font-size: 14px;53 }5455 pre {56 margin: 20px 10px;57 padding: 25px 20px;
209 unmodified lines
modified:docs/migrate.1+1 added-1 removed
1.\" generated with Ronn/v0.7.32.\" http://github.com/rtomayko/ronn/tree/0.7.33.4.TH "MIGRATE" "" "August 2010" "" ""4.TH "MIGRATE" "" "September 2010" "" ""5.6.SH "NAME"7\fBmigrate\fR8.
339 unmodified lines
modified:docs/migrate.html+7 added-3 removed
14 unmodified lines
15 padding-bottom: 30px;16 font: 14px/1.4 "Helvetica Neue", "Lucida Grande", "Arial";17 font-size: 14px;18 line-height: 1.5;19 -webkit-text-stroke: 1px rgba(0, 0, 0, 0.1);20 -moz-text-stroke: 1px rgba(0, 0, 0, 0.1);19 -webkit-font-smoothing: antialiased;20 -moz-font-smoothing: antialiased;21 background: url(images/bg.jpg) 50% 0 no-repeat;22 color: #8b8b8b; }2324 * {
14 unmodified lines
39 -webkit-transition-timing-function: ease-out; }40 a:hover {41 opacity: 0.8; }4243 h1, h2, h3 {43 h1, h2, h3, h4 {44 margin: 45px 0 0 0;45 color: white;46 text-shadow: 1px 2px 2px rgba(0,0,0,0.6); }4748 h3 {49 font-size: 18px; }50 h4 {51 margin-left: 10px;52 font-size: 14px;53 }5455 pre {56 margin: 20px 10px;57 padding: 25px 20px;
352 unmodified lines
added:examples/auth/views/layout.ejs+21 added
1<!DOCTYPE html>2<html>3 <head>4 <title>Authentication Example</title>5 <style>6 body {7 padding: 50px;8 font: 13px Helvetica, Arial, sans-serif;9 }10 .error {11 color: red12 }13 .success {14 color: green;15 }16 </style>17 </head>18 <body>19 <%- body %>20 </body>21</html>
added:examples/auth/views/login.ejs+15 added
1<h1>Login</h1>2<%- message %>3<form method="post" action="/login">4 <p>5 <label>Username:</label>6 <input type="text" name="username">7 </p>8 <p>9 <label>Password:</label>10 <input type="text" name="password">11 </p>12 <p>13 <input type="submit" value="Login">14 </p>15</form>
added:examples/auth/app.js+103 added
12/**3 * Module dependencies.4 */56var express = require('../../lib/express'),7 crypto = require('crypto');89var app = express.createServer();1011app.set('views', __dirname + '/views');12app.set('view engine', 'ejs');1314app.use(express.bodyDecoder());15app.use(express.cookieDecoder());16app.use(express.session());1718// Message helper, ideally we would use req.flash()19// however this is more light-weight for an example2021app.dynamicHelpers({22 message: function(req){23 var err = req.session.error,24 msg = req.session.success;25 delete req.session.error;26 delete req.session.success;27 if (err) return '<p class="msg error">' + err + '</p>';28 if (msg) return '<p class="msg success">' + msg + '</p>';29 }30});3132// Generate a salt for the user to prevent rainbow table attacks3334var users = {35 tj: {36 name: 'tj',37 salt: 'randomly-generated-salt',38 pass: md5('foobar' + 'randomly-generated-salt')39 }40};4142// Used to generate a hash of the plain-text password + salt4344function md5(str) {45 return crypto.createHash('md5').update(str).digest('hex');46}4748// Authenticate using our plain-object database of doom!4950function authenticate(name, pass, fn) {51 var user = users[name];52 // query the db for the given username53 if (!user) return fn(new Error('cannot find user'));54 // apply the same algorithm to the POSTed password, applying55 // the md5 against the pass / salt, if there is a match we56 // found the user57 if (user.pass == md5(pass + user.salt)) return fn(null, user);58 // Otherwise password is invalid59 fn(new Error('invalid password'));60}6162app.get('/', function(req, res){63 res.redirect('/login');64});6566app.get('/logout', function(req, res){67 // destroy the user's session to log them out68 // will be re-created next request69 req.session.destroy(function(){70 res.redirect('home');71 });72});7374app.get('/login', function(req, res){75 if (req.session.user) {76 req.session.success = 'Authenticated as ' + req.session.user.name77 + ' click to <a href="/logout">logout</a>.';78 }79 res.render('login');80});8182app.post('/login', function(req, res){83 authenticate(req.body.username, req.body.password, function(err, user){84 if (user) {85 // Regenerate session when signing in86 // to prevent fixation 87 req.session.regenerate(function(){88 // Store the user's primary key 89 // in the session store to be retrieved,90 // or in this case the entire user object91 req.session.user = user;92 });93 } else {94 req.session.error = 'Authentication failed, please check your '95 + ' username and password.'96 + ' (use "tj" and "foobar")';97 }98 res.redirect('back');99 });100});101102app.listen(3000);103console.log('Express started on port 3000');
modified:examples/mvc/app.js+1 added-1 removed
7 unmodified lines
8var app = express.createServer();910require('./mvc').boot(app);1112app.listen();12app.listen(3000);13console.log('Application server started on port 3000');
modified:lib/express/server.js+8 added-4 removed
35 unmodified lines
3637 // Default "home" to / 38 this.set('home', '/');3940 // Set "env" to EXPRESS_ENV or connectEnv.name41 this.set('env',42 process.env.EXPRESS_ENV ||43 process.connectEnv.name);40 // DEPRECATED: remove in 1.041 if (process.env.EXPRESS_ENV) {42 process.env.NODE_ENV = process.env.EXPRESS_ENV;43 console.warn('\x1b[33mWarning\x1b[0m: EXPRESS_ENV is deprecated, use NODE_ENV.');44 }4546 // Set "env" to NODE_ENV47 this.set('env', process.env.NODE_ENV);4849 // Expose objects to each other50 this.use(function(req, res, next){51 req.query = {};52 res.headers = {};
265 unmodified lines
modified:support/connectnot shown

Its text is not shown, modified.

modified:support/jadenot shown

Its text is not shown, modified.

modified:test/express.test.js+1 added-1 removed
160 unmodified lines
161 },162 163 'test #configure()': function(assert, beforeExit){164 var calls = [];165 process.env.EXPRESS_ENV = 'development';165 process.env.NODE_ENV = 'development';166 var server = express.createServer();167 168 // Config blocks169 var ret = server.configure(function(){
156 unmodified lines
modified:Makefile+1 added-1 removed
35 unmodified lines
36uninstall-docs:37 rm -f $(PREFIX)/share/man/man1/express.13839test:40 @CONNECT_ENV=test ./support/expresso/bin/expresso \40 @NODE_ENV=test ./support/expresso/bin/expresso \41 -I lib \42 -I support/connect/lib \43 -I support/haml/lib \44 -I support/jade/lib \
33 unmodified lines
modified:Readme.md+2 added-2 removed
73 unmodified lines
74## Node Compatibility75 76The latest release of Express is compatible with node --version:7778 v0.2.178 v0.2.37980and connect --version:8182 0.2.482 0.2.58384## License 8586(The MIT License)
21 unmodified lines