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
Provenance
- Commit
c3365aa- Tree
f8d5c5e- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent 1
64033fc- Parent 2
426e80d
21 files changed+688 added−331 removed
modified:bin/express+23 added-23 removed
77 unmodified lines78 "",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');92 93 /**12 unmodified lines106 '',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 linesmodified:docs/layout/head.html+6 added-3 removed
14 unmodified lines151617181920192021222314 unmodified lines3839404143424344454647484950515253545556132 unmodified linesExpand all14 unmodified lines padding-bottom: 30px; font: 14px/1.4 "Helvetica Neue", "Lucida Grande", "Arial"; font-size: 14px; line-height: 1.5; -webkit-text-stroke: 1px rgba(0, 0, 0, 0.1); -moz-text-stroke: 1px rgba(0, 0, 0, 0.1); -webkit-font-smoothing: antialiased; background: url(images/bg.jpg) 50% 0 no-repeat; color: #8b8b8b; }
* {14 unmodified lines -webkit-transition-timing-function: ease-out; } a:hover { opacity: 0.8; }
h1, h2, h3 { h1, h2, h3, h4 { margin: 45px 0 0 0; color: white; text-shadow: 1px 2px 2px rgba(0,0,0,0.6); }
h3 { font-size: 18px; } h4 { margin-left: 10px; font-size: 14px; }
pre { margin: 20px 10px; padding: 25px 20px;132 unmodified linesExpand allmodified:docs/api.html+274 added-274 removed
534 unmodified lines535 <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 lines734 }, <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 to755-increase performance by lowering the number756-of calls to <code>require()</code>.</p>757-758-<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>767-768-<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>777-as <code>ext</code>. For example we may wish to map ".html"778-files to jade:</p>779-780-<p> app.register('.html', require('jade'));</p>781-782-<p>This is also useful for libraries that may not783-match extensions correctly. For example my haml.js784-library is installed from npm as "hamljs" so instead785-of layout.hamljs, we can register the engine as ".haml":</p>786-787-<p> app.register('.haml', require('haml-js'));</p>788-789-<p>For engines that do not comply with the Express790-specification, we can also wrap their api this way.</p>791-792-<p> app.register('.foo', {793- render: function(str, options) {794- // perhaps their api is795- // foo.toHTML(str, options);796- }797- });</p>798-799-<h2></h2>800-801-<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>812-813-<h2>Options</h2>814-815-<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. 816-For example <em>video.html</em> will have a object <em>video</em> available to it.</p></li></ul>817-818-<h2></h2>819-820-<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- }828-829- <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- }833-834- <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>;839-840- <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>.873-When a callback function is given a response will <em>not</em> be made874-automatically, however otherwise a response of <em>200</em> and <em>text/html</em> is given.</p>875-876-<h2>Options</h2>877-878-<p> Most engines accept one or more of the following options,879- both <em>haml</em> and <em>jade</em> accept all:</p>880-881-<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>882-883-<h2></h2>884-885-<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>);897-898- <span class="comment">// Mixin "view options"</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>;900-901- <span class="comment">// Support "view engine" 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- }905-906- <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>;917-918- <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- }922-923- <span class="comment">// Normalize "context" to "scope", 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>;925-926- <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- }930-931- <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- }937-938- <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>;942-943- <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>;946-947- <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- }961-962- <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>;969-970- <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- }977-978- <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- }984-985- <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>)));987-988- <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- }994-995- <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 lines1118 .<span class="variable">replace</span>(<span class="regexp">/</g</span>, <span class="string">'<'</span>)1119 .<span class="variable">replace</span>(<span class="regexp">/>/g</span>, <span class="string">'>'</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 to1139+increase performance by lowering the number1140+of calls to <code>require()</code>.</p>1141+1142+<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>1151+1152+<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>1161+as <code>ext</code>. For example we may wish to map ".html"1162+files to jade:</p>1163+1164+<p> app.register('.html', require('jade'));</p>1165+1166+<p>This is also useful for libraries that may not1167+match extensions correctly. For example my haml.js1168+library is installed from npm as "hamljs" so instead1169+of layout.hamljs, we can register the engine as ".haml":</p>1170+1171+<p> app.register('.haml', require('haml-js'));</p>1172+1173+<p>For engines that do not comply with the Express1174+specification, we can also wrap their api this way.</p>1175+1176+<p> app.register('.foo', {1177+ render: function(str, options) {1178+ // perhaps their api is1179+ // foo.toHTML(str, options);1180+ }1181+ });</p>1182+1183+<h2></h2>1184+1185+<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>1196+1197+<h2>Options</h2>1198+1199+<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. 1200+For example <em>video.html</em> will have a object <em>video</em> available to it.</p></li></ul>1201+1202+<h2></h2>1203+1204+<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+ }1212+1213+ <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+ }1217+1218+ <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>;1223+1224+ <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>.1257+When a callback function is given a response will <em>not</em> be made1258+automatically, however otherwise a response of <em>200</em> and <em>text/html</em> is given.</p>1259+1260+<h2>Options</h2>1261+1262+<p> Most engines accept one or more of the following options,1263+ both <em>haml</em> and <em>jade</em> accept all:</p>1264+1265+<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>1266+1267+<h2></h2>1268+1269+<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>);1281+1282+ <span class="comment">// Mixin "view options"</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>;1284+1285+ <span class="comment">// Support "view engine" 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+ }1289+1290+ <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>;1301+1302+ <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+ }1306+1307+ <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>;1309+1310+ <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+ }1314+1315+ <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+ }1321+1322+ <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>;1326+1327+ <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>;1330+1331+ <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+ }1345+1346+ <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>;1353+1354+ <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+ }1361+1362+ <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+ }1368+1369+ <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>)));1371+1372+ <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+ }1378+1379+ <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 lines15 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; }23 24 * {14 unmodified lines39 -webkit-transition-timing-function: ease-out; }40 a:hover {41 opacity: 0.8; }42 43- 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); }47 48 h3 {49 font-size: 18px; }50+ h4 {51+ margin-left: 10px;52+ font-size: 14px;53+ }54 55 pre {56 margin: 20px 10px;57 padding: 25px 20px;187 unmodified linesmodified:docs/executable.html+7 added-3 removed
14 unmodified lines15 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; }23 24 * {14 unmodified lines39 -webkit-transition-timing-function: ease-out; }40 a:hover {41 opacity: 0.8; }42 43- 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); }47 48 h3 {49 font-size: 18px; }50+ h4 {51+ margin-left: 10px;52+ font-size: 14px;53+ }54 55 pre {56 margin: 20px 10px;57 padding: 25px 20px;159 unmodified linesmodified: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 lines282 .fi283 .284 .IP "" 0285 .286+.SS "HTTP Methods"287+We 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+.P290+A 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+.nf297+298+ <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+.P309+By 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+.nf314+315+app\.use(express\.bodyDecoder());316+.317+.fi318+.319+.IP "" 0320+.321+.P322+Our 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+.nf327+328+app\.post(\'/\', function(req, res){329+ console\.log(req\.body\.user);330+ res\.redirect(\'back\');331+});332+.333+.fi334+.335+.IP "" 0336+.337+.P338+When 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+.nf343+344+app\.use(express\.bodyDecoder());345+app\.use(express\.methodOverride());346+.347+.fi348+.349+.IP "" 0350+.351+.P352+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 \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+.nf357+358+<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>364+365+app\.put(\'/\', function(){366+ console\.log(req\.body\.user);367+ res\.redirect(\'back\');368+});369+.370+.fi371+.372+.IP "" 0373+.374 .SS "Middleware"375 The 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 "" 4647 unmodified lines1025 .P1026 Most 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 linesmodified:docs/guide.html+58 added-4 removed
14 unmodified lines15 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; }23 24 * {14 unmodified lines39 -webkit-transition-timing-function: ease-out; }40 a:hover {41 opacity: 0.8; }42 43- 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); }47 48 h3 {49 font-size: 18px; }50+ h4 {51+ margin-left: 10px;52+ font-size: 14px;53+ }54 55 pre {56 margin: 20px 10px;57 padding: 25px 20px;123 unmodified lines181 <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 lines425 // do something else426 });427 </code></pre>428 429+<h3 id="HTTP-Methods">HTTP Methods</h3>430+431+<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>432+433+<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>434+435+<p> <form method="post" action="/" /></p>436+437+<pre><code> <input type="text" name="user[name]" />438+ <input type="text" name="user[email]" />439+ <input type="submit" value="Submit" />440+</code></pre>441+442+<p> </p>443+444+<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>445+446+<pre><code>app.use(express.bodyDecoder());447+</code></pre>448+449+<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>450+451+<pre><code>app.post('/', function(req, res){452+ console.log(req.body.user);453+ res.redirect('back');454+});455+</code></pre>456+457+<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>458+459+<pre><code>app.use(express.bodyDecoder());460+app.use(express.methodOverride());461+</code></pre>462+463+<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>464+465+<pre><code><form method="post" action="/">466+ <input type="hidden" name="_method" value="put" />467+ <input type="text" name="user[name]" />468+ <input type="text" name="user[email]" />469+ <input type="submit" value="Submit" /> 470+</form>471+472+app.put('/', function(){473+ console.log(req.body.user);474+ res.redirect('back');475+});476+</code></pre>477+478 <h3 id="Middleware">Middleware</h3>479 480 <p>The Express <em>Plugin</em> is no more! middleware via <a href="http://github.com/senchalabs/connect">Connect</a> can be481 passed to <em>express.createServer()</em> as you would with a regular Connect server. For example:</p>406 unmodified lines888 <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>890 891 <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 linesmodified:docs/guide.md+56 added-3 removed
68 unmodified lines69 app.disable('some feature');70 // same as app.set('some feature', false);71 });72 73-To alter the environment we can set the _CONNECT_ENV_ environment variable,74-or more specifically _EXPRESS_ENV_, for example:73+To alter the environment we can set the _NODE_ENV_ environment variable, for example:74 76- $ EXPRESS_ENV=production node app.js75+ $ NODE_ENV=production node app.js76 77 ### Settings78 79 Express supports the following settings out of the box:89 unmodified lines169 app.get('/users', function(req, res){170 // do something else171 });172 173+### HTTP Methods174+175+We 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.176+177+ 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>184+185+By 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:186+187+ app.use(express.bodyDecoder());188+189+Our route below will now have access to the _req.body.user_ object which will contain the _name_ and _email_ properties when defined.190+191+ app.post('/', function(req, res){192+ console.log(req.body.user);193+ res.redirect('back');194+ });195+196+When 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.197+198+ app.use(express.bodyDecoder());199+ app.use(express.methodOverride());200+201+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 _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:202+203+ <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>209+210+ app.put('/', function(){211+ console.log(req.body.user);212+ res.redirect('back');213+ });214+215 ### Middleware216 217 The Express _Plugin_ is no more! middleware via [Connect](http://github.com/senchalabs/connect) can be218 passed to _express.createServer()_ as you would with a regular Connect server. For example:126 unmodified lines345 Which can then be overridden within the `res.render()` call if need be:346 347 res.render('myview.ejs', { layout: true });348 349+When 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:350+351+ res.render('page', { layout: 'mylayout' });352+353+Otherwise we must specify the extension:354+355+ res.render('page', { layout: 'mylayout.jade' });356+357+These paths may also be absolute:358+359+ res.render('page', { layout: __dirname + '/../../mylayout.jade' });360+361 A good example of this is specifying custom _ejs_ opening and closing tags:362 363 app.set('view options', {364 open: '{{',438 unmodified linesmodified:docs/index.html+7 added-3 removed
14 unmodified lines15 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; }23 24 * {14 unmodified lines39 -webkit-transition-timing-function: ease-out; }40 a:hover {41 opacity: 0.8; }42 43- 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); }47 48 h3 {49 font-size: 18px; }50+ h4 {51+ margin-left: 10px;52+ font-size: 14px;53+ }54 55 pre {56 margin: 20px 10px;57 padding: 25px 20px;209 unmodified linesmodified: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 linesmodified:docs/migrate.html+7 added-3 removed
14 unmodified lines15 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; }23 24 * {14 unmodified lines39 -webkit-transition-timing-function: ease-out; }40 a:hover {41 opacity: 0.8; }42 43- 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); }47 48 h3 {49 font-size: 18px; }50+ h4 {51+ margin-left: 10px;52+ font-size: 14px;53+ }54 55 pre {56 margin: 20px 10px;57 padding: 25px 20px;352 unmodified linesadded: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
1+2+/**3+ * Module dependencies.4+ */5+6+var express = require('../../lib/express'),7+ crypto = require('crypto');8+9+var app = express.createServer();10+11+app.set('views', __dirname + '/views');12+app.set('view engine', 'ejs');13+14+app.use(express.bodyDecoder());15+app.use(express.cookieDecoder());16+app.use(express.session());17+18+// Message helper, ideally we would use req.flash()19+// however this is more light-weight for an example20+21+app.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+});31+32+// Generate a salt for the user to prevent rainbow table attacks33+34+var users = {35+ tj: {36+ name: 'tj',37+ salt: 'randomly-generated-salt',38+ pass: md5('foobar' + 'randomly-generated-salt')39+ }40+};41+42+// Used to generate a hash of the plain-text password + salt43+44+function md5(str) {45+ return crypto.createHash('md5').update(str).digest('hex');46+}47+48+// Authenticate using our plain-object database of doom!49+50+function 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+}61+62+app.get('/', function(req, res){63+ res.redirect('/login');64+});65+66+app.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+});73+74+app.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+});81+82+app.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+});101+102+app.listen(3000);103+console.log('Express started on port 3000');modified:examples/mvc/app.js+1 added-1 removed
7 unmodified lines8 var app = express.createServer();9 10 require('./mvc').boot(app);11 12-app.listen();12+app.listen(3000);13 console.log('Application server started on port 3000');modified:lib/express/server.js+8 added-4 removed
35 unmodified lines36 37 // Default "home" to / 38 this.set('home', '/');39 40- // 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+ }45 46+ // Set "env" to NODE_ENV47+ this.set('env', process.env.NODE_ENV);48+49 // Expose objects to each other50 this.use(function(req, res, next){51 req.query = {};52 res.headers = {};265 unmodified linesmodified: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 lines161 },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 linesmodified:Makefile+1 added-1 removed
35 unmodified lines36 uninstall-docs:37 rm -f $(PREFIX)/share/man/man1/express.138 39 test: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 linesmodified:Readme.md+2 added-2 removed
73 unmodified lines74 ## Node Compatibility75 76 The latest release of Express is compatible with node --version:77 78- v0.2.178+ v0.2.379 80 and connect --version:81 82- 0.2.482+ 0.2.583 84 ## License 85 86 (The MIT License)21 unmodified lines