Skip to content
Myr

fixed docs due to markdown-js not supporting html

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

authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent 187dc5d · Browse files · 10 files+682 added−434 removed
This commit

State

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

Diff stats

10 files+682 added−434 removed

  • other:+682 added−434 removed

10 files+682 added−434 removed

modified:docs/applications.html+18 added-14 removed
188 unmodified lines
189 <li><a href="guide.html">Guide</a></li>190 <li><a href="screencasts.html">Screencasts</a></li>191 <li><a href="applications.html">Applications</a></li>192 </ul>193<p>&lt;br /&gt;</p>193194195<br />196197198<p><a href="http://learnboost.com">Learnboost</a> is a free online gradebook application, aimed to crush the competition with innovative, realtime, enjoyable features.</p>199197<p>[<img alt="LearnBoost" src="images/apps/learnboost.png)](http://learnboost.com" /></p>200<p><a href="http://learnboost.com"><img src="images/apps/learnboost.png" alt="LearnBoost" /></a></p>201202<p><a href="http://storify.com">Storify</a> lets you turn what people post on social media websites into compelling stories.</p>203201<p>[<img alt="Storify" src="images/apps/storify.png)](http://storify.com" /></p>204<p><a href="http://storify.com"><img src="images/apps/storify.png" alt="Storify" /></a></p>205206<p><a href="http://pakistansurvey.org/">Pakistan Survey</a> by <a href="http://developmentseed.org">Development Seed</a>, provides in-depth agency-specific analysis from regional experts with data from 1,000 interviews across 120 villages in all seven tribal agencies and mapping of 142 reported drone strikes in FATA through July 2010.</p>207205<p>[<img alt="Pakistan Survey" src="images/apps/developmentseed.png)](http://pakistansurvey.org" /></p>208<p><a href="http://pakistansurvey.org"><img src="images/apps/developmentseed.png" alt="Pakistan Survey" /></a></p>209210<p><a href="http://markup.io">Markup.IO</a> allows you to draw directly on <em>any</em> website, then share with others to share your thoughts.</p>211209<p>[<img alt="Markup.IO" src="images/apps/markupio.png)](http://markup.io" /></p>212<p><a href="http://markup.io"><img src="images/apps/markupio.png" alt="Markup.IO" /></a></p>213214<p><a href="http://scrabb.ly">Scrabb.ly</a> is a massively multiplayer scrabble game initially created for the <a href="http://nodeknockout.com/">Node Knockout</a> competition.</p>215213<p>[<img alt="Online Realtime Scrabble" src="images/apps/scrabbly.png)](http://scrabb.ly" /></p>216<p><a href="http://scrabb.ly"><img src="images/apps/scrabbly.png" alt="Online Realtime Scrabble" /></a></p>217218<p><a href="http://clickdummy.net/">ClickDummy</a> is a rapid mockup prototyping application for designers and dummies.</p>219217<p>[<img alt="Mockup Prototying" src="images/apps/clickdummy.png)](http://clickdummy.net" /></p>220<p><a href="http://clickdummy.net"><img src="images/apps/clickdummy.png" alt="Mockup Prototying" /></a></p>221222<p><a href="http://nodeknockout.com">Node Knockout</a> organized the first ever node-specific competition with hundreds of contestants.</p>223221<p>[<img alt="Node Knockout Competition Express" src="images/apps/nodeko.png)](http://nodeknockout.com" /></p>224<p><a href="http://nodeknockout.com"><img src="images/apps/nodeko.png" alt="Node Knockout Competition Express" /></a></p>225223<p><a href="http://widescript.com">Widescript</a> is an innovative app that helps you focus and interact with your texts - on your desktop, your couch or on the go.</p>226<p><a href="http://widescript.com">Widescript</a> is an innovative app that helps you focus and interact with your texts &ndash; on your desktop, your couch or on the go.</p>227225<p>[<img alt="Widescript" src="images/apps/widescript.png)](http://widescript.com" /></p>228<p><a href="http://widescript.com"><img src="images/apps/widescript.png" alt="Widescript" /></a></p>229230<p><a href="http://www.e-resistible.co.uk/">e-resistable</a> is an online order takeaway system providing an intuitive way to fill your belly from your computer!</p>231229<p>[<img alt="Online Takeaway" src="images/apps/e-resistable.png)](http://www.e-resistible.co.uk" /></p>232<p><a href="http://www.e-resistible.co.uk"><img src="images/apps/e-resistable.png" alt="Online Takeaway" /></a></p>233234<p><a href="http://toptwittertrends.com">Top Twitter Trends</a> utilizes MongoDB, Socket.IO, jQuery and many other exciting libraries to bring you trending tweets in realtime.</p>235233<p>[<img alt="Twitter Trends" src="images/apps/toptwittertrends.png)](http://toptwittertrends.com" /></p>236<p><a href="http://toptwittertrends.com"><img src="images/apps/toptwittertrends.png" alt="Twitter Trends" /></a></p>237235<p>&lt;br /&gt;</p>238<br />239240241<p>The applications shown above are not listed in any specific order. To have an application added or removed please contact <a href="http://github.com/visionmedia">TJ Holowaychuk</a>.</p>242 </div>243 </div>244 </body>241</html>245</html>
modified:docs/contrib.html+21 added-10 removed
188 unmodified lines
189 <li><a href="guide.html">Guide</a></li>190 <li><a href="screencasts.html">Screencasts</a></li>191 <li><a href="applications.html">Applications</a></li>192 </ul>193<h3 id="Development-Dependencies">Development Dependencies</h3>193<h3>Development Dependencies</h3>194195<p>Express development dependencies are stored within the <em>./support</em> directory. To196update them execute:</p>197198<pre><code>$ git submodule update --init</code></pre>198<pre><code>$ git submodule update --init199</code></pre>200200<h3 id="Running-Tests">Running Tests</h3>201<h3>Running Tests</h3>202203<p>Express uses the <a href="http://github.com/visionmedia/expresso">Expresso</a> TDD204framework to write and run elegant test suites extremely fast. To run all test suites205simply execute:</p>206206<pre><code>$ make test</code></pre>207<pre><code>$ make test208</code></pre>209210<p>To target specific suites we may specify the files via:</p>211210<pre><code>$ make test TESTS=test/view.test.js</code></pre>212<pre><code>$ make test TESTS=test/view.test.js213</code></pre>214215<p>To check test coverage run:</p>216214<pre><code>$ make test-cov</code></pre>217<pre><code>$ make test-cov218</code></pre>219216<h3 id="Contributions">Contributions</h3>220<h3>Contributions</h3>221222<p>To accept a contribution, you should follow these guidelines:</p>223220<ul><li>All tests <em>must</em> pass</li><li>Your alterations or additions <em>must</em> include tests</li><li>Your commit(s) should be <em>focused</em>, do not commit once for several changes</li><li>Do <em>not</em> alter release information such as the <em>version</em>, or <em>History.md</em></li><li>Indents are <em>2</em> spaces.</li></ul>224<ul>225<li>All tests <em>must</em> pass</li>226<li>Your alterations or additions <em>must</em> include tests</li>227<li>Your commit(s) should be <em>focused</em>, do not commit once for several changes</li>228<li>Do <em>not</em> alter release information such as the <em>version</em>, or <em>History.md</em></li>229<li>Indents are <em>2</em> spaces.</li>230</ul>231222<h3 id="Documentation">Documentation</h3>232233<h3>Documentation</h3>234235<p>To contribute documentation edit the markdown files in <em>./docs</em>, however236do <em>not</em> run <em>make docs</em>, as they will be re-built and published with each release.</p>237 </div>238 </div>239 </body>229</html>240</html>
modified:docs/executable.html+9 added-7 removed
188 unmodified lines
189 <li><a href="guide.html">Guide</a></li>190 <li><a href="screencasts.html">Screencasts</a></li>191 <li><a href="applications.html">Applications</a></li>192 </ul>193<h2 id="Synopsis">Synopsis</h2>193<h2>Synopsis</h2>194195<pre><code>express [options] [PATH]</code></pre>195<pre><code>express [options] [PATH]196</code></pre>197197<h2 id="Description">Description</h2>198<h2>Description</h2>199199<p>The <em>express</em> executable generates apps at the given <strong>PATH</strong> or the 200<p>The <em>express</em> executable generates apps at the given <strong>PATH</strong> or the201current working directory. Although Express is not bound to a specific202application structure, this executable creates a maintainable base app.</p>203203<h2 id="Options">Options</h2>204<h2>Options</h2>205206<pre><code> -s, --sessions Add session support207 -t, --template ENGINE Add template ENGINE support (jade|ejs). Defaults to jade208 -c, --css ENGINE Add stylesheet ENGINE support (less|sass|stylus). Defaults to plain css209 -v, --version Output framework version209 -h, --help Output help information</code></pre>210 -h, --help Output help information211</code></pre>212 </div>213 </div>214 </body>213</html>215</html>
modified:docs/guide.html+462 added-304 removed
177 unmodified lines
178 <img alt='Fork me on GitHub' id='ribbon' src='http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png' /> 179 </a>180 <div id="wrapper">181 <div id="container"><ul id="toc">182<li><a href="#Installation">Installation</a></li>183<li><a href="#Creating-A-Server">Creating A Server</a></li>184<li><a href="#Creating-An-HTTPS-Server">Creating An HTTPS Server</a></li>185<li><a href="#Configuration">Configuration</a></li>186<li><a href="#Settings">Settings</a></li>187<li><a href="#Routing">Routing</a></li>188<li><a href="#Passing-Route-Control">Passing Route Control</a></li>189<li><a href="#Middleware">Middleware</a></li>190<li><a href="#Route-Middleware">Route Middleware</a></li>191<li><a href="#HTTP-Methods">HTTP Methods</a></li>192<li><a href="#Error-Handling">Error Handling</a></li>193<li><a href="#Route-Param-Pre-conditions">Route Param Pre-conditions</a></li>194<li><a href="#View-Rendering">View Rendering</a></li>195<li><a href="#View-Partials">View Partials</a></li>196<li><a href="#View-Lookup">View Lookup</a></li>197<li><a href="#Template-Engines">Template Engines</a></li>198<li><a href="#Session-Support">Session Support</a></li>199<li><a href="#Migration-Guide">Migration Guide</a></li>182<li><a href="#installation">Installation</a></li>183<li><a href="#creating-a server">Creating A Server</a></li>184<li><a href="#creating-an https server">Creating An HTTPS Server</a></li>185<li><a href="#configuration">Configuration</a></li>186<li><a href="#settings">Settings</a></li>187<li><a href="#routing">Routing</a></li>188<li><a href="#passing-route control">Passing Route Control</a></li>189<li><a href="#middleware">Middleware</a></li>190<li><a href="#route-middleware">Route Middleware</a></li>191<li><a href="#http-methods">HTTP Methods</a></li>192<li><a href="#error-handling">Error Handling</a></li>193<li><a href="#route-param pre-conditions">Route Param Pre-conditions</a></li>194<li><a href="#view-rendering">View Rendering</a></li>195<li><a href="#view-partials">View Partials</a></li>196<li><a href="#view-lookup">View Lookup</a></li>197<li><a href="#template-engines">Template Engines</a></li>198<li><a href="#session-support">Session Support</a></li>199<li><a href="#migration-guide">Migration Guide</a></li>200<li><a href="#" class="toggle">+</a> <a class="section-title" href="#">Request</a><ul class="section" id="section-Request">201<li><a href="#req-header-key-defaultValue">header()</a></li>202<li><a href="#req-accepts-type">accepts()</a></li>203<li><a href="#req-is-type">is()</a></li>204<li><a href="#req-param-name-default">param()</a></li>205<li><a href="#req-flash-type-msg">flash()</a></li>206<li><a href="#req-isXMLHttpRequest">isXMLHttpRequest</a></li>201<li><a href="#req.header()">header()</a></li>202<li><a href="#req.accepts()">accepts()</a></li>203<li><a href="#req.is()">is()</a></li>204<li><a href="#req.param()">param()</a></li>205<li><a href="#req.flash()">flash()</a></li>206<li><a href="#req.isxmlhttprequest">isXMLHttpRequest</a></li>207</ul></li>208<li><a href="#" class="toggle">+</a> <a class="section-title" href="#">Response</a><ul class="section" id="section-Response">209<li><a href="#res-header-key-val">header()</a></li>210<li><a href="#res-charset">charset</a></li>211<li><a href="#res-contentType-type">contentType()</a></li>212<li><a href="#res-attachment-filename">attachment()</a></li>213<li><a href="#res-sendfile-path-options-callback">sendfile()</a></li>214<li><a href="#res-download-file-filename-callback">download()</a></li>215<li><a href="#res-send-body-status-headers-status-status">send()</a></li>216<li><a href="#res-redirect-url-status">redirect()</a></li>217<li><a href="#res-cookie-name-val-options">cookie()</a></li>218<li><a href="#res-clearCookie-name">clearCookie()</a></li>219<li><a href="#res-render-view-options-fn">render()</a></li>220<li><a href="#res-partial-view-options">partial()</a></li>221<li><a href="#res-local-name-val">local()</a></li>222<li><a href="#res-local-obj">local()</a></li>209<li><a href="#res.header()">header()</a></li>210<li><a href="#res.charset">charset</a></li>211<li><a href="#res.contenttype()">contentType()</a></li>212<li><a href="#res.attachment()">attachment()</a></li>213<li><a href="#res.sendfile()">sendfile()</a></li>214<li><a href="#res.download()">download()</a></li>215<li><a href="#res.send()">send()</a></li>216<li><a href="#res.redirect()">redirect()</a></li>217<li><a href="#res.cookie()">cookie()</a></li>218<li><a href="#res.clearcookie()">clearCookie()</a></li>219<li><a href="#res.render()">render()</a></li>220<li><a href="#res.partial()">partial()</a></li>221<li><a href="#res.local()">local()</a></li>222<li><a href="#res.locals()">locals()</a></li>223</ul></li>224<li><a href="#" class="toggle">+</a> <a class="section-title" href="#">Server</a><ul class="section" id="section-Server">225<li><a href="#app-set-name-val">set()</a></li>226<li><a href="#app-enable-name">enable()</a></li>227<li><a href="#app-enabled-name">enabled()</a></li>228<li><a href="#app-disable-name">disable()</a></li>229<li><a href="#app-disabled-name">disabled()</a></li>230<li><a href="#app-configure-env-function-function">configure()</a></li>231<li><a href="#app-redirect-name-val">redirect()</a></li>232<li><a href="#app-error-function">error()</a></li>233<li><a href="#app-helpers-obj">helpers()</a></li>234<li><a href="#app-dynamicHelpers-obj">dynamicHelpers()</a></li>235<li><a href="#app-mounted-fn">mounted()</a></li>236<li><a href="#app-register-ext-exports">register()</a></li>237<li><a href="#app-listen-port-host">listen()</a></li>225<li><a href="#app.set()">set()</a></li>226<li><a href="#app.enable()">enable()</a></li>227<li><a href="#app.enabled()">enabled()</a></li>228<li><a href="#app.disable()">disable()</a></li>229<li><a href="#app.disabled()">disabled()</a></li>230<li><a href="#app.configure()">configure()</a></li>231<li><a href="#app.redirect()">redirect()</a></li>232<li><a href="#app.error()">error()</a></li>233<li><a href="#app.helpers()">helpers()</a></li>234<li><a href="#app.dynamichelpers()">dynamicHelpers()</a></li>235<li><a href="#app.mounted()">mounted()</a></li>236<li><a href="#app.register()">register()</a></li>237<li><a href="#app.listen()">listen()</a></li>238</ul></li>239</ul>240 <a href='http://github.com/visionmedia/express' id='logo'>Express</a> 241 <p id="tagline">
5 unmodified lines
247 <li><a href="guide.html">Guide</a></li>248 <li><a href="screencasts.html">Screencasts</a></li>249 <li><a href="applications.html">Applications</a></li>250 </ul>251<h3 id="Installation">Installation</h3>251<h3 id="installation">Installation</h3>252253<pre><code>$ npm install express</code></pre>253<pre><code>$ npm install express254</code></pre>255255<h3 id="Creating-A-Server">Creating A Server</h3>256<h3 id="creating-a server">Creating A Server</h3>257258<p> To create an instance of the <em>express.HTTPServer</em>, simply invoke the <em>createServer()</em> method. With our instance <em>app</em> we can then define routes based on the HTTP verbs, in this example <em>app.get()</em>.</p>259260<pre><code>var app = require('express').createServer();
1 unmodified line
262app.get('/', function(req, res){263 res.send('hello world');264});265265app.listen(3000);</code></pre>266app.listen(3000);267</code></pre>268267<h3 id="Creating-An-HTTPS-Server">Creating An HTTPS Server</h3>269<h3 id="creating-an https server">Creating An HTTPS Server</h3>270271<p> To initialize a <em>express.HTTPSServer</em> we do the same as above, however we270 pass an options object, accepting <em>key</em>, <em>cert</em> and the others mentioned in node's <a href="http://nodejs.org/docs/v0.3.7/api/https.html#https.createServer">https documentation</a>.</p>272 pass an options object, accepting <em>key</em>, <em>cert</em> and the others mentioned in node&rsquo;s <a href="http://nodejs.org/docs/v0.3.7/api/https.html#https.createServer">https documentation</a>.</p>273272<pre><code> var app = require('express').createServer({ key: ... });</code></pre>274<pre><code> var app = require('express').createServer({ key: ... });275</code></pre>276274<h3 id="Configuration">Configuration</h3>277<h3 id="configuration">Configuration</h3>278279<p>Express supports arbitrary environments, such as <em>production</em> and <em>development</em>. Developers280can use the <em>configure()</em> method to setup needs required by the current environment. When278<em>configure()</em> is called without an environment name it will be run in <em>every</em> environment 281<em>configure()</em> is called without an environment name it will be run in <em>every</em> environment282prior to the environment specific callback.</p>283284<p>In the example below we only <em>dumpExceptions</em>, and respond with exception stack traces285in <em>development</em> mode, however for both environments we utilize <em>methodOverride</em> and <em>bodyParser</em>.286Note the use of <em>app.router</em>, which can (optionally) be used to mount the application routes,287otherwise the first call to <em>app.get()</em>, <em>app.post()</em>, etc will mount the routes.</p>288289<pre><code>app.configure(function(){287 app.use(express.methodOverride());288 app.use(express.bodyParser());289 app.use(app.router);290 app.use(express.methodOverride());291 app.use(express.bodyParser());292 app.use(app.router);293});294295app.configure('development', function(){293 app.use(express.static(__dirname + '/public'));294 app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));296 app.use(express.static(__dirname + '/public'));297 app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));298});299300app.configure('production', function(){301 var oneYear = 31557600000;302 app.use(express.static(__dirname + '/public', { maxAge: oneYear }));303 app.use(express.errorHandler());304});301});</code></pre>305</code></pre>306307<p>For internal and arbitrary settings Express provides the <em>set(key[, val])</em>, <em>enable(key)</em>, <em>disable(key)</em> methods:</p>308309<pre><code> app.configure(function(){
8 unmodified lines
318 // same as app.set('some feature', false);319320 app.enabled('some feature')321 // =&gt; false322 });318 });</code></pre>323</code></pre>324320<p>To alter the environment we can set the <em>NODE</em>ENV_ environment variable, for example:</p>325<p>To alter the environment we can set the <em>NODE_ENV</em> environment variable, for example:</p>326322<pre><code>$ NODE_ENV=production node app.js</code></pre>327<pre><code>$ NODE_ENV=production node app.js328</code></pre>329330<p>This is <em>very</em> important, as many caching mechanisms are <em>only enabled</em> when in production.</p>331326<h3 id="Settings">Settings</h3>332<h3 id="settings">Settings</h3>333334<p>Express supports the following settings out of the box:</p>335330<ul><li><em>home</em> Application base path used for <em>res.redirect()</em> and transparently handling mounted apps.</li><li><em>views</em> Root views directory defaulting to <strong>CWD/views</strong></li><li><em>view engine</em> Default view engine name for views rendered without extensions</li><li><em>view options</em> An object specifying global view options</li></ul>336<ul>337<li><em>home</em> Application base path used for <em>res.redirect()</em> and transparently handling mounted apps.</li>338<li><em>views</em> Root views directory defaulting to <strong>CWD/views</strong></li>339<li><em>view engine</em> Default view engine name for views rendered without extensions</li>340<li><em>view options</em> An object specifying global view options</li>341</ul>342332<h3 id="Routing">Routing</h3>343344<h3 id="routing">Routing</h3>345346<p>Express utilizes the HTTP verbs to provide a meaningful, expressive routing API.335For example we may want to render a user's account for the path <em>/user/12</em>, this336can be done by defining the route below. The values associated to the named placeholders 347For example we may want to render a user&rsquo;s account for the path <em>/user/12</em>, this348can be done by defining the route below. The values associated to the named placeholders349are available as <code>req.params</code>.</p>350351<pre><code>app.get('/user/:id', function(req, res){340 res.send('user ' + req.params.id);341});</code></pre>352 res.send('user ' + req.params.id);353});354</code></pre>355356<p>A route is simple a string which is compiled to a <em>RegExp</em> internally. For example357when <em>/user/:id</em> is compiled, a simplified version of the regexp may look similar to:</p>358346<pre><code>\/user\/([^\/]+)\/?</code></pre>359<pre><code>\/user\/([^\/]+)\/?360</code></pre>361362<p>Regular expression literals may also be passed for complex uses. Since capture349groups with literal <em>RegExp</em>'s are anonymous we can access them directly <code>req.params</code>. So our first capture group would be <em>req.params[0]</em> and the second would follow as <em>req.params[1]</em>.</p>363groups with literal <em>RegExp</em>&rsquo;s are anonymous we can access them directly <code>req.params</code>. So our first capture group would be <em>req.params[0]</em> and the second would follow as <em>req.params[1]</em>.</p>364365<pre><code>app.get(/^\/users?(?:\/(\d+)(?:\.\.(\d+))?)?/, function(req, res){366 res.send(req.params);367});353});</code></pre>368</code></pre>369370<p>Curl requests against the previously defined route:</p>371372<pre><code> $ curl http://dev:3000/user
2 unmodified lines
375 [null,null]376 $ curl http://dev:3000/users/1377 ["1",null]378 $ curl http://dev:3000/users/1..15364 ["1","15"]</code></pre>379 ["1","15"]380</code></pre>381382<p>Below are some route examples, and the associated paths that they383may consume:</p>384
26 unmodified lines
411 /products412413 "/user/:id.:format?"414 /user/12399 /user/12.json</code></pre>415 /user/12.json416</code></pre>417418<p>For example we can <strong>POST</strong> some json, and echo the json back using the <em>bodyParser</em> middleware which will parse json request bodies (as well as others), and place the result in <em>req.body</em>:</p>419420<pre><code>var express = require('express')
4 unmodified lines
425app.post('/', function(req, res){426 res.send(req.body);427});428412app.listen(3000);</code></pre>429app.listen(3000);430</code></pre>431414<p>Typically we may use a "dumb" placeholder such as "/user/:id" which has no restrictions, however say for example we are limiting a user id to digits, we may use <em>'/user/:id(\d+)'</em> which will <em>not</em> match unless the placeholder value contains only digits.</p>432<p>Typically we may use a &ldquo;dumb&rdquo; placeholder such as &ldquo;/user/:id&rdquo; which has no restrictions, however say for example we are limiting a user id to digits, we may use <em>&lsquo;/user/:id(\d+)&rsquo;</em> which will <em>not</em> match unless the placeholder value contains only digits.</p>433416<h3 id="Passing-Route-Control">Passing Route Control</h3>434<h3 id="passing-route control">Passing Route Control</h3>435436<p>We may pass control to the next <em>matching</em> route, by calling the <em>third</em> argument,437the <em>next()</em> function. When a match cannot be made, control is passed back to Connect,438and middleware continue to be invoked in the order that they are added via <em>use()</em>. The same is true for several routes which have the same path defined, they will simply be executed in order until one does <em>not</em> call <em>next()</em> and decides to respond.</p>439440<pre><code>app.get('/users/:id?', function(req, res, next){423 var id = req.params.id;424 if (id) {425 // do something426 } else {427 next();428 }441 var id = req.params.id;442 if (id) {443 // do something444 } else {445 next();446 }447});448449app.get('/users', function(req, res){432 // do something else433});</code></pre>450 // do something else451});452</code></pre>453454<p>The <em>app.all()</em> method is useful for applying the same logic for all HTTP verbs in a single call. Below we use this to load a user from our fake database, and assign it to <em>req.user</em>.</p>455456<pre><code>var express = require('express')
25 unmodified lines
482app.get('*', function(req, res){483 res.send('what???', 404);484});485467app.listen(3000); </code></pre>486app.listen(3000); 487</code></pre>488469<h3 id="Middleware">Middleware</h3>489<h3 id="middleware">Middleware</h3>490491<p>Middleware via <a href="http://github.com/senchalabs/connect">Connect</a> can be492passed to <em>express.createServer()</em> as you would with a regular Connect server. For example:</p>493494<pre><code> var express = require('express');495496var app = express.createServer(477 express.logger()478 , express.bodyParser()479 );</code></pre>497 express.logger()498 , express.bodyParser()499 );500</code></pre>501502<p>Alternatively we can <em>use()</em> them which is useful when adding middleware within <em>configure()</em> blocks, in a progressive manor.</p>503483<pre><code>app.use(express.logger({ format: ':method :uri' }));</code></pre>504<pre><code>app.use(express.logger({ format: ':method :uri' }));505</code></pre>506485<p>Typically with connect middleware you would <em>require('connect')</em> like so:</p>507<p>Typically with connect middleware you would <em>require(&lsquo;connect&rsquo;)</em> like so:</p>508509<pre><code>var connect = require('connect');510app.use(connect.logger());489app.use(connect.bodyParser());</code></pre>511app.use(connect.bodyParser());512</code></pre>513514<p>This is somewhat annoying, so express re-exports these middleware properties, however they are <em>identical</em>:</p>515516<pre><code>app.use(express.logger());494app.use(express.bodyParser());</code></pre>517app.use(express.bodyParser());518</code></pre>519496<h3 id="Route-Middleware">Route Middleware</h3>520<h3 id="route-middleware">Route Middleware</h3>521522<p>Routes may utilize route-specific middleware by passing one or more additional callbacks (or arrays) to the method. This feature is extremely useful for restricting access, loading data used by the route etc.</p>523500<p>Typically async data retrieval might look similar to below, where we take the <em>:id</em> parameter, and attempt loading a user. </p>524<p>Typically async data retrieval might look similar to below, where we take the <em>:id</em> parameter, and attempt loading a user.</p>525526<pre><code>app.get('/user/:id', function(req, res, next){527 loadUser(req.params.id, function(err, user){528 if (err) return next(err);529 res.send('Viewing user ' + user.name);530 });531});507});</code></pre>532</code></pre>533509<p>To keep things DRY and to increase readability we can apply this logic within a middleware. As you can see below, abstracting this logic into middleware allows us to reuse it, and clean up our route at the same time. </p>534<p>To keep things DRY and to increase readability we can apply this logic within a middleware. As you can see below, abstracting this logic into middleware allows us to reuse it, and clean up our route at the same time.</p>535536<pre><code>function loadUser(req, res, next) {537 // You would fetch your user from the db538 var user = users[req.params.id];
6 unmodified lines
545}546547app.get('/user/:id', loadUser, function(req, res){548 res.send('Viewing user ' + req.user.name);549});524});</code></pre>550</code></pre>551552<p>Multiple route middleware can be applied, and will be executed sequentially to apply further logic such as restricting access to a user account. In the example below only the authenticated user may edit his/her account.</p>553554<pre><code>function andRestrictToSelf(req, res, next) {
3 unmodified lines
558}559560app.get('/user/:id/edit', loadUser, andRestrictToSelf, function(req, res){561 res.send('Editing user ' + req.user.name);562});536});</code></pre>563</code></pre>564565<p>Keeping in mind that middleware are simply functions, we can define function that <em>returns</em> the middleware in order to create a more expressive and flexible solution as shown below.</p>566567<pre><code>function andRestrictTo(role) {
5 unmodified lines
573}574575app.del('/user/:id', loadUser, andRestrictTo('admin'), function(req, res){576 res.send('Deleted user ' + req.user.name);577});550});</code></pre>578</code></pre>579552<p>Commonly used "stacks" of middleware can be passed as an array (<em>applied recursively</em>), which can be mixed and matched to any degree.</p>580<p>Commonly used &ldquo;stacks&rdquo; of middleware can be passed as an array (<em>applied recursively</em>), which can be mixed and matched to any degree.</p>581582<pre><code>var a = [middleware1, middleware2]583 , b = [middleware3, middleware4]584 , all = [a, b];
2 unmodified lines
587app.get('/bar', a, function(){});588589app.get('/', a, middleware3, middleware4, function(){});590app.get('/', a, b, function(){});563app.get('/', all, function(){});</code></pre>591app.get('/', all, function(){});592</code></pre>593594<p>For this example in full, view the <a href="http://github.com/visionmedia/express/blob/master/examples/route-middleware/app.js">route middleware example</a> in the repository.</p>595567<h3 id="HTTP-Methods">HTTP Methods</h3>596<h3 id="http-methods">HTTP Methods</h3>597598<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>599571<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>600<p> A common example for <em>POST</em> usage, is when &ldquo;submitting&rdquo; a form. Below we simply set our form method to &ldquo;post&rdquo; in our html, and control will be given to the route we have defined below it.</p>601602<pre><code> &lt;form method="post" action="/"&gt;603 &lt;input type="text" name="user[name]" /&gt;604 &lt;input type="text" name="user[email]" /&gt;605 &lt;input type="submit" value="Submit" /&gt;577 &lt;/form&gt;</code></pre>606 &lt;/form&gt;607</code></pre>608579<p>By default Express does not know what to do with this request body, so we should add the <em>bodyParser</em> middleware, which will parse <em>application/x-www-form-urlencoded</em> and <em>application/json</em> request bodies and place the variables in <em>req.body</em>. We can do this by "using" the middleware as shown below:</p>609<p>By default Express does not know what to do with this request body, so we should add the <em>bodyParser</em> middleware, which will parse <em>application/x-www-form-urlencoded</em> and <em>application/json</em> request bodies and place the variables in <em>req.body</em>. We can do this by &ldquo;using&rdquo; the middleware as shown below:</p>610581<pre><code>app.use(express.bodyParser());</code></pre>611<pre><code>app.use(express.bodyParser());612</code></pre>613614<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>615616<pre><code>app.post('/', function(req, res){617 console.log(req.body.user);618 res.redirect('back');619});588});</code></pre>620</code></pre>621590<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>bodyParser</em> so that it can utilize it's <em>req.body</em> containing the form values.</p>622<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>bodyParser</em> so that it can utilize it&rsquo;s <em>req.body</em> containing the form values.</p>623624<pre><code>app.use(express.bodyParser());593app.use(express.methodOverride());</code></pre>625app.use(express.methodOverride());626</code></pre>627628<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>629630<pre><code>&lt;form method="post" action="/"&gt;
5 unmodified lines
636637app.put('/', function(){638 console.log(req.body.user);639 res.redirect('back');640});607});</code></pre>641</code></pre>642609<h3 id="Error-Handling">Error Handling</h3>643<h3 id="error-handling">Error Handling</h3>644645<p>Express provides the <em>app.error()</em> method which receives exceptions thrown within a route,646or passed to <em>next(err)</em>. Below is an example which serves different pages based on our647ad-hoc <em>NotFound</em> exception:</p>
3 unmodified lines
651 Error.call(this, msg);652 Error.captureStackTrace(this, arguments.callee);653}654621NotFound.protoype.__proto__ = Error.prototype;655NotFound.prototype.__proto__ = Error.prototype;656657app.get('/404', function(req, res){658 throw new NotFound;659});660661app.get('/500', function(req, res){662 throw new Error('keyboard cat!');663});629});</code></pre>664</code></pre>665666<p>We can call <em>app.error()</em> several times as shown below.667Here we check for an instanceof <em>NotFound</em> and show the668404 page, or we pass on to the next error handler.</p>669670<p>Note that these handlers can be defined anywhere, as they636will be placed below the route handlers on <em>listen()</em>. This 671will be placed below the route handlers on <em>listen()</em>. This672allows for definition within <em>configure()</em> blocks so we can673handle exceptions in different ways based on the environment.</p>674675<pre><code>app.error(function(err, req, res, next){
1 unmodified line
677 res.render('404.jade');678 } else {679 next(err);680 }681});646});</code></pre>682</code></pre>683684<p>Here we assume all errors as 500 for the simplicity of649this demo, however you can choose whatever you like. For example when node performs filesystem syscalls, you may receive an error object with the <em>error.code</em> of <em>ENOENT</em>, meaning "no such file or directory", we can utilize this in our error handling and display a page specific to this if desired.</p>685this demo, however you can choose whatever you like. For example when node performs filesystem syscalls, you may receive an error object with the <em>error.code</em> of <em>ENOENT</em>, meaning &ldquo;no such file or directory&rdquo;, we can utilize this in our error handling and display a page specific to this if desired.</p>686687<pre><code>app.error(function(err, req, res){688 res.render('500.jade', {689 error: err690 });691});655});</code></pre>692</code></pre>693694<p>Our apps could also utilize the Connect <em>errorHandler</em> middleware658to report on exceptions. For example if we wish to output exceptions 659in "development" mode to <em>stderr</em> we can use:</p>695to report on exceptions. For example if we wish to output exceptions696in &ldquo;development&rdquo; mode to <em>stderr</em> we can use:</p>697661<pre><code>app.use(express.errorHandler({ dumpExceptions: true }));</code></pre>698<pre><code>app.use(express.errorHandler({ dumpExceptions: true }));699</code></pre>700701<p>Also during development we may want fancy html pages to show exceptions702that are passed or thrown, so we can set <em>showStack</em> to true:</p>703666<pre><code>app.use(express.errorHandler({ showStack: true, dumpExceptions: true }));</code></pre>704<pre><code>app.use(express.errorHandler({ showStack: true, dumpExceptions: true }));705</code></pre>706707<p>The <em>errorHandler</em> middleware also responds with <em>json</em> if <em>Accept: application/json</em>708is present, which is useful for developing apps that rely heavily on client-side JavaScript.</p>709671<h3 id="Route-Param-Pre-conditions">Route Param Pre-conditions</h3>710<h3 id="route-param pre-conditions">Route Param Pre-conditions</h3>711712<p>Route param pre-conditions can drastically improve the readability of your application, through implicit loading of data, and validation of request urls. For example if you are constantly fetching common data for several routes, such as loading a user for <em>/user/:id</em>, we might typically do something like below:</p>713714<pre><code>app.get('/user/:userId', function(req, res, next){715 User.get(req.params.userId, function(err, user){716 if (err) return next(err);717 res.send('user ' + user.name);718 });719}); 680}); </code></pre>720</code></pre>721722<p>With preconditions our params can be mapped to callbacks which may perform validation, coercion, or even loading data from a database. Below we invoke <em>app.param()</em> with the parameter name we wish to map to some middleware, as you can see we receive the <em>id</em> argument which contains the placeholder value. Using this we load the user and perform error handling as usual, and simple call <em>next()</em> to pass control to the next precondition or route handler.</p>723724<pre><code>app.param('userId', function(req, res, next, id){
2 unmodified lines
727 if (!user) return next(new Error('failed to find user'));728 req.user = user;729 next();730 });731});691});</code></pre>732</code></pre>733734<p>Doing so, as mentioned drastically improves our route readability, and allows us to easily share this logic throughout our application:</p>735736<pre><code>app.get('/user/:userId', function(req, res){737 res.send('user ' + req.user.name);738});697});</code></pre>739</code></pre>740741<p>For simple cases such as route placeholder validation and coercion we can simple pass a callback which has an arity of 1 (accepts one argument). Any errors thrown will be passed to <em>next(err)</em>.</p>742701<pre><code>app.param('number', function(n){ return parseInt(n, 10); });</code></pre>743<pre><code>app.param('number', function(n){ return parseInt(n, 10); });744</code></pre>745746<p>We may also apply the same callback to several placeholders, for example a route GET <em>/commits/:from-:to</em> are both numbers, so we may define them as an array:</p>747705<pre><code>app.param(['from', 'to'], function(n){ return parseInt(n, 10); });</code></pre>748<pre><code>app.param(['from', 'to'], function(n){ return parseInt(n, 10); });749</code></pre>750707<h3 id="View-Rendering">View Rendering</h3>751<h3 id="view-rendering">View Rendering</h3>752709<p>View filenames take the form "&amp;lt;name&amp;gt;.&amp;lt;engine&amp;gt;", where &amp;lt;engine&amp;gt; is the name753<p>View filenames take the form &ldquo;&lt;name&gt;.&lt;engine&gt;&rdquo;, where &lt;engine&gt; is the name754of the module that will be required. For example the view <em>layout.ejs</em> will711tell the view system to <em>require('ejs')</em>, the module being loaded must export the method <em>exports.compile(str, options)</em>, and return a <em>Function</em> to comply with Express. To alter this behaviour712<em>app.register()</em> can be used to map engines to file extensions, so that for example "foo.html" can be rendered by ejs.</p>755tell the view system to <em>require(&lsquo;ejs&rsquo;)</em>, the module being loaded must export the method <em>exports.compile(str, options)</em>, and return a <em>Function</em> to comply with Express. To alter this behaviour756<em>app.register()</em> can be used to map engines to file extensions, so that for example &ldquo;foo.html&rdquo; can be rendered by ejs.</p>757758<p>Below is an example using <a href="http://github.com/visionmedia/jade">Jade</a> to render <em>index.html</em>,715and since we do not use <em>layout: false</em> the rendered contents of <em>index.jade</em> will be passed as 759and since we do not use <em>layout: false</em> the rendered contents of <em>index.jade</em> will be passed as760the <em>body</em> local variable in <em>layout.jade</em>.</p>761762<pre><code>app.get('/', function(req, res){719 res.render('index.jade', { title: 'My Site' });720});</code></pre>763 res.render('index.jade', { title: 'My Site' });764});765</code></pre>766767<p>The new <em>view engine</em> setting allows us to specify our default template engine,768so for example when using jade we could set:</p>769725<pre><code>app.set('view engine', 'jade');</code></pre>770<pre><code>app.set('view engine', 'jade');771</code></pre>772773<p>Allowing us to render with:</p>774729<pre><code>res.render('index');</code></pre>775<pre><code>res.render('index');776</code></pre>777778<p>vs:</p>779733<pre><code>res.render('index.jade');</code></pre>780<pre><code>res.render('index.jade');781</code></pre>782783<p>When <em>view engine</em> is set, extensions are entirely optional, however we can still784mix and match template engines:</p>785738<pre><code>res.render('another-page.ejs');</code></pre>786<pre><code>res.render('another-page.ejs');787</code></pre>788789<p>Express also provides the <em>view options</em> setting, which is applied each time a view is rendered, so for example if you rarely use layouts you may set:</p>790791<pre><code>app.set('view options', {792 layout: false793});744});</code></pre>794</code></pre>795796<p>Which can then be overridden within the <em>res.render()</em> call if need be:</p>797748<pre><code>res.render('myview.ejs', { layout: true });</code></pre>798<pre><code>res.render('myview.ejs', { layout: true });799</code></pre>800801<p>When an alternate layout is required, we may also specify a path. For example if we have <em>view engine</em> set to <em>jade</em> and a file named <em>./views/mylayout.jade</em> we can simply pass:</p>802752<pre><code>res.render('page', { layout: 'mylayout' });</code></pre>803<pre><code>res.render('page', { layout: 'mylayout' });804</code></pre>805806<p>Otherwise we must specify the extension:</p>807756<pre><code>res.render('page', { layout: 'mylayout.jade' });</code></pre>808<pre><code>res.render('page', { layout: 'mylayout.jade' });809</code></pre>810811<p>These paths may also be absolute:</p>812760<pre><code>res.render('page', { layout: __dirname + '/../../mylayout.jade' });</code></pre>813<pre><code>res.render('page', { layout: __dirname + '/../../mylayout.jade' });814</code></pre>815816<p>A good example of this is specifying custom <em>ejs</em> opening and closing tags:</p>817818<pre><code>app.set('view options', {819 open: '{{',820 close: '}}'821});767});</code></pre>822</code></pre>823769<h3 id="View-Partials">View Partials</h3>824<h3 id="view-partials">View Partials</h3>825771<p>The Express view system has built-in support for partials and collections, which are "mini" views representing a document fragment. For example rather than iterating826<p>The Express view system has built-in support for partials and collections, which are &ldquo;mini&rdquo; views representing a document fragment. For example rather than iterating827in a view to display comments, we could use partial collection:</p>828774<pre><code>partial('comment', { collection: comments });</code></pre>829<pre><code>partial('comment', { collection: comments });830</code></pre>831832<p>If no other options or local variables are desired, we can omit the object and simply pass our array, which is equivalent to above:</p>833778<pre><code>partial('comment', comments);</code></pre>834<pre><code>partial('comment', comments);835</code></pre>836780<p>When using the partial collection support a few "magic" locals are provided837<p>When using the partial collection support a few &ldquo;magic&rdquo; locals are provided838for free:</p>839783<ul><li><em>firstInCollection</em> true if this is the first object</li><li><em>indexInCollection</em> index of the object in the collection</li><li><em>lastInCollection</em> true if this is the last object</li><li><em>collectionLength</em> length of the collection</li></ul>840<ul>841<li><em>firstInCollection</em> true if this is the first object</li>842<li><em>indexInCollection</em> index of the object in the collection</li>843<li><em>lastInCollection</em> true if this is the last object</li>844<li><em>collectionLength</em> length of the collection</li>845</ul>846785<p>Local variables passed (or generated) take precedence, however locals passed to the parent view are available in the child view as well. So for example if we were to render a blog post with <em>partial('blog/post', post)</em> it would generate the <em>post</em> local, but the view calling this function had the local <em>user</em>, it would be available to the <em>blog/post</em> view as well.</p>847848<p>Local variables passed (or generated) take precedence, however locals passed to the parent view are available in the child view as well. So for example if we were to render a blog post with <em>partial(&lsquo;blog/post&rsquo;, post)</em> it would generate the <em>post</em> local, but the view calling this function had the local <em>user</em>, it would be available to the <em>blog/post</em> view as well.</p>849850<p>For documentation on altering the object name view <a href="http://expressjs.com/guide.html#res-partial-view-options-">res.partial()</a>.</p>851852<p><strong>NOTE:</strong> be careful about when you use partial collections, as rendering an array with a length of 100 means we have to render 100 views. For simple collections you may inline the iteration instead of using partial collection support to decrease overhead.</p>853791<h3 id="View-Lookup">View Lookup</h3>854<h3 id="view-lookup">View Lookup</h3>855793<p>View lookup is performed relative to the parent view, for example if we had a page view named <em>views/user/list.jade</em>, and within that view we did <em>partial('edit')</em> it would attempt to load <em>views/user/edit.jade</em>, whereas <em>partial('../messages')</em> would load <em>views/messages.jade</em>.</p>856<p>View lookup is performed relative to the parent view, for example if we had a page view named <em>views/user/list.jade</em>, and within that view we did <em>partial(&lsquo;edit&rsquo;)</em> it would attempt to load <em>views/user/edit.jade</em>, whereas <em>partial(&lsquo;../messages&rsquo;)</em> would load <em>views/messages.jade</em>.</p>857795<p>The view system also allows for index templates, allowing you to have a directory of the same name. For example within a route we may have <em>res.render('users')</em> either <em>views/users.jade</em>, or <em>views/users/index.jade</em>.</p>858<p>The view system also allows for index templates, allowing you to have a directory of the same name. For example within a route we may have <em>res.render(&lsquo;users&rsquo;)</em> either <em>views/users.jade</em>, or <em>views/users/index.jade</em>.</p>859797<p>When utilizing index views as shown above, we may reference <em>views/users/index.jade</em> from a view in the same directory by <em>partial('users')</em>, and the view system will try <em>../users/index</em>, preventing us from needing to call <em>partial('index')</em>.</p>860<p>When utilizing index views as shown above, we may reference <em>views/users/index.jade</em> from a view in the same directory by <em>partial(&lsquo;users&rsquo;)</em>, and the view system will try <em>../users/index</em>, preventing us from needing to call <em>partial(&lsquo;index&rsquo;)</em>.</p>861799<h3 id="Template-Engines">Template Engines</h3>862<h3 id="template-engines">Template Engines</h3>863864<p>Below are a few template engines commonly used with Express:</p>865803<ul><li><a href="http://github.com/visionmedia/haml.js">Haml</a> haml implementation</li><li><a href="http://jade-lang.com">Jade</a> haml.js successor</li><li><a href="http://github.com/visionmedia/ejs">EJS</a> Embedded JavaScript</li><li><a href="http://github.com/mauricemach/coffeekup">CoffeeKup</a> CoffeeScript based templating</li><li><a href="https://github.com/kof/node-jqtpl">jQuery Templates</a> for node</li></ul>866<ul>867<li><a href="http://github.com/visionmedia/haml.js">Haml</a> haml implementation</li>868<li><a href="http://jade-lang.com">Jade</a> haml.js successor</li>869<li><a href="http://github.com/visionmedia/ejs">EJS</a> Embedded JavaScript</li>870<li><a href="http://github.com/mauricemach/coffeekup">CoffeeKup</a> CoffeeScript based templating</li>871<li><a href="https://github.com/kof/node-jqtpl">jQuery Templates</a> for node</li>872</ul>873805<h3 id="Session-Support">Session Support</h3>874807<p>Sessions support can be added by using Connect's <em>session</em> middleware. To do so we also need the <em>cookieParser</em> middleware place above it, which will parse and populate cookie data to <em>req.cookies</em>.</p>875<h3 id="session-support">Session Support</h3>876877<p>Sessions support can be added by using Connect&rsquo;s <em>session</em> middleware. To do so we also need the <em>cookieParser</em> middleware place above it, which will parse and populate cookie data to <em>req.cookies</em>.</p>878879<pre><code>app.use(express.cookieParser());810app.use(express.session({ secret: "keyboard cat" }));</code></pre>880app.use(express.session({ secret: "keyboard cat" }));881</code></pre>882883<p>By default the <em>session</em> middleware uses the memory store bundled with Connect, however many implementations exist. For example <a href="http://github.com/visionmedia/connect-redis">connect-redis</a> supplies a <a href="http://code.google.com/p/redis/">Redis</a> session store and can be used as shown below:</p>884885<pre><code>var RedisStore = require('connect-redis');886app.use(express.cookieParser());816app.use(express.session({ secret: "keyboard cat", store: new RedisStore }));</code></pre>887app.use(express.session({ secret: "keyboard cat", store: new RedisStore }));888</code></pre>889890<p>Now the <em>req.session</em> and <em>req.sessionStore</em> properties will be accessible to all routes and subsequent middleware. Properties on <em>req.session</em> are automatically saved on a response, so for example if we wish to shopping cart data:</p>891892<pre><code>var RedisStore = require('connect-redis');
16 unmodified lines
909 if (req.session.items &amp;&amp; req.session.items.length) {910 req.flash('info', 'You have %s items in your cart', req.session.items.length);911 }912 res.render('shopping-cart');913});841});</code></pre>914</code></pre>915916<p>The <em>req.session</em> object also has methods such as <em>Session#touch()</em>, <em>Session#destroy()</em>, <em>Session#regenerate()</em> among others to maintain and manipulate sessions. For more information view the <a href="http://senchalabs.github.com/connect/middleware-session.html">Connect Session</a> documentation.</p>917845<h3 id="Migration-Guide">Migration Guide</h3>918<h3 id="migration-guide">Migration Guide</h3>919920<p> Express 1.x developers may reference the <a href="migrate.html">Migration Guide</a> to get up to speed on how to upgrade your application to work with Express 2.x, Connect 1.x, and Node 0.4.x.</p>921849<h3 id="req-header-key-defaultValue">req.header(key[, defaultValue])</h3>922<h3 id="req.header()">req.header(key[, defaultValue])</h3>923924<p>Get the case-insensitive request header <em>key</em>, with optional <em>defaultValue</em>:</p>925926<pre><code>req.header('Host');927req.header('host');855req.header('Accept', '*/*');</code></pre>928req.header('Accept', '*/*');929</code></pre>930931<p>The <em>Referrer</em> and <em>Referer</em> header fields are special-cased, either will work:</p>932933<pre><code>// sent Referrer: http://google.com
1 unmodified line
935req.header('Referer');936// =&gt; "http://google.com"937938req.header('Referrer');865// =&gt; "http://google.com"</code></pre>939// =&gt; "http://google.com"940</code></pre>941867<h3 id="req-accepts-type">req.accepts(type)</h3>942<h3 id="req.accepts()">req.accepts(type)</h3>943944<p>Check if the <em>Accept</em> header is present, and includes the given <em>type</em>.</p>945946<p>When the <em>Accept</em> header is not present <em>true</em> is returned. Otherwise947the given <em>type</em> is matched by an exact match, and then subtypes. You873may pass the subtype such as "html" which is then converted internally874to "text/html" using the mime lookup table.</p>948may pass the subtype such as &ldquo;html&rdquo; which is then converted internally949to &ldquo;text/html&rdquo; using the mime lookup table.</p>950951<pre><code>// Accept: text/html952req.accepts('html');953// =&gt; true
6 unmodified lines
960// =&gt; true961962req.accepts('image/png');963req.accepts('png');889// =&gt; false</code></pre>964// =&gt; false965</code></pre>966891<h3 id="req-is-type">req.is(type)</h3>967<h3 id="req.is()">req.is(type)</h3>968969<p>Check if the incoming request contains the <em>Content-Type</em>970header field, and it contains the give mime <em>type</em>.</p>971
7 unmodified lines
979 req.is('application/json');980 // =&gt; true981982 req.is('html');907 // =&gt; false</code></pre>983 // =&gt; false984</code></pre>985986<p>Ad-hoc callbacks can also be registered with Express, to perform987assertions again the request, for example if we need an expressive911way to check if our incoming request is an image, we can register <em>"an image"</em>988way to check if our incoming request is an image, we can register <em>&ldquo;an image&rdquo;</em>989callback:</p>990991<pre><code> app.is('an image', function(req){992 return 0 == req.headers['content-type'].indexOf('image');993 });916 });</code></pre>994</code></pre>995996<p>Now within our route callbacks, we can use to to assert content types919such as <em>"image/jpeg"</em>, <em>"image/png"</em>, etc.</p>997such as <em>&ldquo;image/jpeg&rdquo;</em>, <em>&ldquo;image/png&rdquo;</em>, etc.</p>998999<pre><code> app.post('/image/upload', function(req, res, next){1000 if (req.is('an image')) {1001 // do something1002 } else {1003 next();1004 }1005 });927 });</code></pre>1006</code></pre>10071008<p>Keep in mind this method is <em>not</em> limited to checking <em>Content-Type</em>, you1009can perform any request assertion you wish.</p>1010932<p>Wildcard matches can also be made, simplifying our example above for <em>"an image"</em>, by asserting the <em>subtype</em> only:</p>1011<p>Wildcard matches can also be made, simplifying our example above for <em>&ldquo;an image&rdquo;</em>, by asserting the <em>subtype</em> only:</p>1012934<pre><code>req.is('image/*');</code></pre>1013<pre><code>req.is('image/*');1014</code></pre>1015936<p>We may also assert the <em>type</em> as shown below, which would return true for <em>"application/json"</em>, and <em>"text/json"</em>.</p>1016<p>We may also assert the <em>type</em> as shown below, which would return true for <em>&ldquo;application/json&rdquo;</em>, and <em>&ldquo;text/json&rdquo;</em>.</p>1017938<pre><code>req.is('*/json');</code></pre>1018<pre><code>req.is('*/json');1019</code></pre>1020940<h3 id="req-param-name-default">req.param(name[, default])</h3>1021<h3 id="req.param()">req.param(name[, default])</h3>10221023<p>Return the value of param <em>name</em> when present or <em>default</em>.</p>1024944<ul><li>Checks route params (<em>req.params</em>), ex: /user/:id</li><li>Checks query string params (<em>req.query</em>), ex: ?id=12</li><li>Checks urlencoded body params (<em>req.body</em>), ex: id=12</li></ul>1025<ul>1026<li>Checks route params (<em>req.params</em>), ex: /user/:id</li>1027<li>Checks query string params (<em>req.query</em>), ex: ?id=12</li>1028<li>Checks urlencoded body params (<em>req.body</em>), ex: id=12</li>1029</ul>103010311032<p>To utilize urlencoded request bodies, <em>req.body</em>1033should be an object. This can be done by using1034the _express.bodyParser middleware.</p>1035950<h3 id="req-flash-type-msg">req.flash(type[, msg])</h3>1036<h3 id="req.flash()">req.flash(type[, msg])</h3>10371038<p>Queue flash <em>msg</em> of the given <em>type</em>.</p>10391040<pre><code>req.flash('info', 'email sent');
7 unmodified lines
1048req.flash('info');1049// =&gt; []10501051req.flash();966// =&gt; { error: ['email delivery failed'], info: [] }</code></pre>1052// =&gt; { error: ['email delivery failed'], info: [] }1053</code></pre>10541055<p>Flash notification message may also utilize formatters, by default only the %s string formatter is available:</p>1056970<pre><code>req.flash('info', 'email delivery to _%s_ from _%s_ failed.', toUser, fromUser);</code></pre>1057<pre><code>req.flash('info', 'email delivery to _%s_ from _%s_ failed.', toUser, fromUser);1058</code></pre>1059972<h3 id="req-isXMLHttpRequest">req.isXMLHttpRequest</h3>1060<h3 id="req.isxmlhttprequest">req.isXMLHttpRequest</h3>10611062<p>Also aliased as <em>req.xhr</em>, this getter checks the <em>X-Requested-With</em> header1063to see if it was issued by an <em>XMLHttpRequest</em>:</p>10641065<pre><code>req.xhr978req.isXMLHttpRequest</code></pre>1066req.isXMLHttpRequest1067</code></pre>1068980<h3 id="res-header-key-val">res.header(key[, val])</h3>1069<h3 id="res.header()">res.header(key[, val])</h3>10701071<p>Get or set the response header <em>key</em>.</p>10721073<pre><code>res.header('Content-Length');
2 unmodified lines
1076res.header('Content-Length', 123);1077// =&gt; 12310781079res.header('Content-Length');991// =&gt; 123</code></pre>1080// =&gt; 1231081</code></pre>1082993<h3 id="res-charset">res.charset</h3>1083<h3 id="res.charset">res.charset</h3>1084995<p>Sets the charset for subsequent <code>Content-Type</code> header fields. For example <code>res.send()</code> and <code>res.render()</code> default to "utf8", so we may explicitly set the charset before rendering a template:</p>1085<p>Sets the charset for subsequent <code>Content-Type</code> header fields. For example <code>res.send()</code> and <code>res.render()</code> default to &ldquo;utf8&rdquo;, so we may explicitly set the charset before rendering a template:</p>10861087<pre><code>res.charset = 'ISO-8859-1';998res.render('users');</code></pre>1088res.render('users');1089</code></pre>10901091<p>or before responding with <code>res.send()</code>:</p>10921093<pre><code>res.charset = 'ISO-8859-1';1003res.send(str);</code></pre>1094res.send(str);1095</code></pre>10961005<p>or with node's <code>res.end()</code>:</p>1097<p>or with node&rsquo;s <code>res.end()</code>:</p>10981099<pre><code>res.charset = 'ISO-8859-1';1100res.header('Content-Type', 'text/plain');1009res.end(str);</code></pre>1101res.end(str);1102</code></pre>11031011<h3 id="res-contentType-type">res.contentType(type)</h3>1104<h3 id="res.contenttype()">res.contentType(type)</h3>11051106<p>Sets the <em>Content-Type</em> response header to the given <em>type</em>.</p>11071108<pre><code> var filename = 'path/to/image.png';1109 res.contentType(filename);1017 // Content-Type is now "image/png"</code></pre>1110 // Content-Type is now "image/png"1111</code></pre>11121113<p>A literal <em>Content-Type</em> works as well:</p>11141021<pre><code> res.contentType('application/json');</code></pre>1115<pre><code> res.contentType('application/json');1116</code></pre>11171118<p>Or simply the extension without leading <code>.</code>:</p>11191025<pre><code> res.contentType('json');</code></pre>1120<pre><code> res.contentType('json');1121</code></pre>11221027<h3 id="res-attachment-filename">res.attachment([filename])</h3>1123<h3 id="res.attachment()">res.attachment([filename])</h3>11241029<p>Sets the <em>Content-Disposition</em> response header to "attachment", with optional <em>filename</em>.</p>1125<p>Sets the <em>Content-Disposition</em> response header to &ldquo;attachment&rdquo;, with optional <em>filename</em>.</p>11261031<pre><code> res.attachment('path/to/my/image.png');</code></pre>1127<pre><code> res.attachment('path/to/my/image.png');1128</code></pre>11291033<h3 id="res-sendfile-path-options-callback">res.sendfile(path[, options[, callback]])</h3>1130<h3 id="res.sendfile()">res.sendfile(path[, options[, callback]])</h3>11311035<p>Used by <code>res.download()</code> to transfer an arbitrary file. </p>1132<p>Used by <code>res.download()</code> to transfer an arbitrary file.</p>11331037<pre><code>res.sendfile('path/to/my.file');</code></pre>1134<pre><code>res.sendfile('path/to/my.file');1135</code></pre>11361137<p>This method accepts an optional callback which is called when1138an error occurs, or when the transfer is complete. By default failures call <code>next(err)</code>, however when a callback is supplied you must do this explicitly, or act on the error.</p>1139
2 unmodified lines
1142 next(err);1143 } else {1144 console.log('transferred %s', path);1145 }1146});1048});</code></pre>1147</code></pre>11481149<p>Options may also be passed to the internal <em>fs.createReadStream()</em> call, for example altering the <em>bufferSize</em>:</p>11501151<pre><code>res.sendfile(path, { bufferSize: 1024 }, function(err){1152 // handle1153});1054});</code></pre>1154</code></pre>11551056<h3 id="res-download-file-filename-callback">res.download(file[, filename[, callback]])</h3>1156<h3 id="res.download()">res.download(file[, filename[, callback]])</h3>11571158<p>Transfer the given <em>file</em> as an attachment with optional alternative <em>filename</em>.</p>11591160<pre><code>res.download('path/to/image.png');1061res.download('path/to/image.png', 'foo.png');</code></pre>1161res.download('path/to/image.png', 'foo.png');1162</code></pre>11631164<p>This is equivalent to:</p>11651166<pre><code>res.attachment(file);1066res.sendfile(file);</code></pre>1167res.sendfile(file);1168</code></pre>11691170<p>An optional callback may be supplied as either the second or third argument, which is passed to <em>res.sendfile()</em>:</p>11711172<pre><code>res.download(path, 'expenses.doc', function(err){1173 // handle1174});1072});</code></pre>1175</code></pre>11761074<h3 id="res-send-body-status-headers-status-status">res.send(body|status[, headers|status[, status]])</h3>1177<h3 id="res.send()">res.send(body|status[, headers|status[, status]])</h3>11781179<p>The <em>res.send()</em> method is a high level response utility allowing you to pass1180objects to respond with json, strings for html, Buffer instances, or numbers representing the status code. The following are all valid uses:</p>1181
2 unmodified lines
1184 res.send({ some: 'json' });1185 res.send('&lt;p&gt;some html&lt;/p&gt;');1186 res.send('Sorry, cant find that', 404);1187 res.send('text', { 'Content-Type': 'text/plain' }, 201);1085 res.send(404);</code></pre>1188 res.send(404);1189</code></pre>11901191<p>By default the <em>Content-Type</em> response header is set, however if explicitly1192assigned through <code>res.send()</code> or previously with <code>res.header()</code> or <code>res.contentType()</code>1193it will not be set again.</p>11941091<p>Note that this method <em>end()</em>s the response, so you will want to use node's <em>res.write()</em> for multiple writes or streaming.</p>1195<p>Note that this method <em>end()</em>s the response, so you will want to use node&rsquo;s <em>res.write()</em> for multiple writes or streaming.</p>11961093<h3 id="res-redirect-url-status">res.redirect(url[, status])</h3>1197<h3 id="res.redirect()">res.redirect(url[, status])</h3>11981199<p>Redirect to the given <em>url</em> with a default response <em>status</em> of 302.</p>12001201<pre><code>res.redirect('/', 301);1202res.redirect('/account');1203res.redirect('http://google.com');1204res.redirect('home');1101res.redirect('back');</code></pre>1205res.redirect('back');1206</code></pre>12071103<p>Express supports "redirect mapping", which by default provides <em>home</em>, and <em>back</em>.1208<p>Express supports &ldquo;redirect mapping&rdquo;, which by default provides <em>home</em>, and <em>back</em>.1209The <em>back</em> map checks the <em>Referrer</em> and <em>Referer</em> headers, while <em>home</em> utilizes1105the "home" setting and defaults to "/".</p>1210the &ldquo;home&rdquo; setting and defaults to &ldquo;/&rdquo;.</p>12111107<h3 id="res-cookie-name-val-options">res.cookie(name, val[, options])</h3>1212<h3 id="res.cookie()">res.cookie(name, val[, options])</h3>12131214<p>Sets the given cookie <em>name</em> to <em>val</em>, with options <em>httpOnly</em>, <em>secure</em>, <em>expires</em> etc.</p>12151216<pre><code>// "Remember me" for 15 minutes 1112res.cookie('rememberme', 'yes', { expires: new Date(Date.now() + 900000), httpOnly: true });</code></pre>1217res.cookie('rememberme', 'yes', { expires: new Date(Date.now() + 900000), httpOnly: true });1218</code></pre>12191220<p>The <em>maxAge</em> property may be used to set <em>expires</em> relative to <em>Date.now()</em> in milliseconds, so our example above can now become:</p>12211116<pre><code>res.cookie('rememberme', 'yes', { maxAge: 900000 });</code></pre>1222<pre><code>res.cookie('rememberme', 'yes', { maxAge: 900000 });1223</code></pre>12241225<p>To parse incoming <em>Cookie</em> headers, use the <em>cookieDecoder</em> middleware, which provides the <em>req.cookies</em> object:</p>12261227<pre><code>app.use(express.cookieParser());12281229app.get('/', function(req, res){1230 // use req.cookies.rememberme1231});1124});</code></pre>1232</code></pre>12331126<h3 id="res-clearCookie-name">res.clearCookie(name)</h3>1234<h3 id="res.clearcookie()">res.clearCookie(name)</h3>12351128<p>Clear cookie <em>name</em> by setting "expires" far in the past.</p>1236<p>Clear cookie <em>name</em> by setting &ldquo;expires&rdquo; far in the past.</p>12371130<pre><code>res.clearCookie('rememberme');</code></pre>1238<pre><code>res.clearCookie('rememberme');1239</code></pre>12401132<h3 id="res-render-view-options-fn">res.render(view[, options[, fn]])</h3>1241<h3 id="res.render()">res.render(view[, options[, fn]])</h3>12421243<p>Render <em>view</em> with the given <em>options</em> and optional callback <em>fn</em>.1244When a callback function is given a response will <em>not</em> be made1245automatically, however otherwise a response of <em>200</em> and <em>text/html</em> is given.</p>12461138<p>The <em>options</em> passed are the local variables as well, for example if we want to expose "user" to the view, and prevent a local we do so within the same object:</p>1247<p>The <em>options</em> passed are the local variables as well, for example if we want to expose &ldquo;user&rdquo; to the view, and prevent a local we do so within the same object:</p>12481249<pre><code>var user = { name: 'tj' };1141res.render('index', { layout: false, user: user });</code></pre>1250res.render('index', { layout: false, user: user });1251</code></pre>12521143<h3 id="res-partial-view-options">res.partial(view[, options])</h3>1253<h3 id="res.partial()">res.partial(view[, options])</h3>12541255<p>Render <em>view</em> partial with the given <em>options</em>. This method is always available1256to the view as a local variable.</p>12571148<ul><li><em>object</em> the object named by <em>as</em> or derived from the view name</li><li><p><em>as</em> Variable name for each <em>collection</em> or <em>object</em> value, defaults to the view name.</p><ul><li>as: 'something' will add the <em>something</em> 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 <em>locals</em></p></li></ul></li><li><p><em>collection</em> Array of objects, the name is derived from the view name itself. 1149For example <em>video.html</em> will have a object <em>video</em> available to it.</p></li></ul>1258<ul>1259<li><em>object</em> the object named by <em>as</em> or derived from the view name</li>1260<li><p><em>as</em> Variable name for each <em>collection</em> or <em>object</em> value, defaults to the view name.</p>12611262<ul>1263<li>as: &lsquo;something&rsquo; will add the <em>something</em> local variable</li>1264<li>as: this will use the collection value as the template context</li>1265<li>as: global will merge the collection value&rsquo;s properties with <em>locals</em></li>1266</ul>1267</li>1268<li><p><em>collection</em> Array of objects, the name is derived from the view name itself.1269For example <em>video.html</em> will have a object <em>video</em> available to it.</p></li>1270</ul>127112721273<p>The following are equivalent, and the name of collection value when passed1274to the partial will be <em>movie</em> as derived from the name.</p>12751276<pre><code>partial('theatre/movie.jade', { collection: movies });1277partial('theatre/movie.jade', movies);1278partial('movie.jade', { collection: movies });1279partial('movie.jade', movies);1280partial('movie', movies);1159// In view: movie.director</code></pre>1281// In view: movie.director1282</code></pre>12831161<p>To change the local from <em>movie</em> to <em>video</em> we can use the "as" option:</p>1284<p>To change the local from <em>movie</em> to <em>video</em> we can use the &ldquo;as&rdquo; option:</p>12851286<pre><code>partial('movie', { collection: movies, as: 'video' });1164// In view: video.director</code></pre>1287// In view: video.director1288</code></pre>12891290<p>Also we can make our movie the value of <em>this</em> within our view so that instead1291of <em>movie.director</em> we could use <em>this.director</em>.</p>12921293<pre><code>partial('movie', { collection: movies, as: this });1170// In view: this.director</code></pre>1294// In view: this.director1295</code></pre>12961172<p>Another alternative is to "expand" the properties of the collection item into1297<p>Another alternative is to &ldquo;expand&rdquo; the properties of the collection item into1298pseudo globals (local variables) by using <em>as: global</em>, which again is syntactic sugar:</p>12991300<pre><code>partial('movie', { collection: movies, as: global });1176// In view: director</code></pre>1301// In view: director1302</code></pre>13031304<p>This same logic applies to a single partial object usage:</p>13051306<pre><code>partial('movie', { object: movie, as: this });
5 unmodified lines
1312partial('movie', { object: movie, as: 'video' });1313// In view: video.director13141315partial('movie', { object: movie });1190// In view: movie.director</code></pre>1316// In view: movie.director1317</code></pre>13181192<p>When a non-collection (does <em>not</em> have <em>.length</em>) is passed as the second argument, it is assumed to be the <em>object</em>, after which the object's local variable name is derived from the view name:</p>1319<p>When a non-collection (does <em>not</em> have <em>.length</em>) is passed as the second argument, it is assumed to be the <em>object</em>, after which the object&rsquo;s local variable name is derived from the view name:</p>13201321<pre><code>var movie = new Movie('Nightmare Before Christmas', 'Tim Burton')1322partial('movie', movie)1196// =&gt; In view: movie.director</code></pre>1323// =&gt; In view: movie.director1324</code></pre>13251198<p>The exception of this, is when a "plain" object, aka "{}" or "new Object" is passed, which is considered an object with local variable. For example some may expect a "movie" local with the following, however since it is a plain object "director" and "title" are simply locals:</p>1326<p>The exception of this, is when a &ldquo;plain&rdquo; object, aka &ldquo;{}&rdquo; or &ldquo;new Object&rdquo; is passed, which is considered an object with local variable. For example some may expect a &ldquo;movie&rdquo; local with the following, however since it is a plain object &ldquo;director&rdquo; and &ldquo;title&rdquo; are simply locals:</p>13271328<pre><code>var movie = { title: 'Nightmare Before Christmas', director: 'Tim Burton' }; 1201partial('movie', movie)</code></pre>1329partial('movie', movie)1330</code></pre>13311332<p>For cases like this where passing a plain object is desired, simply assign it to a key, or use the <code>object</code> key which will use the filename-derived variable name. The examples below are equivalent:</p>13331334<pre><code> partial('movie', { locals: { movie: movie }})1335 partial('movie', { movie: movie })1207 partial('movie', { object: movie })</code></pre>1336 partial('movie', { object: movie })1337</code></pre>13381339<p>This exact API can be utilized from within a route, to respond with a fragment via Ajax or WebSockets, for example we can render a collection of users directly from a route:</p>13401341<pre><code>app.get('/users', function(req, res){
6 unmodified lines
1348 // which internally does partial('user', users)1349 // along with other UI1350 res.render('users', { users: users });1351 }1352});1222});</code></pre>1353</code></pre>13541224<h3 id="res-local-name-val">res.local(name[, val])</h3>1355<h3 id="res.local()">res.local(name[, val])</h3>13561357<p>Get or set the given local variable <em>name</em>. The locals built up for a response are applied to those given to the view rendering methods such as <code>res.render()</code>.</p>13581359<pre><code> app.all('/movie/:id', function(req, res, next){
6 unmodified lines
1366 app.get('/movie/:id', function(req, res){1367 // movie is already a local, however we1368 // can pass more if we wish1369 res.render('movie', { displayReviews: true });1370 });1239 });</code></pre>1371</code></pre>13721241<h3 id="res-local-obj">res.local(obj)</h3>1373<h3 id="res.locals()">res.locals(obj)</h3>13741375<p> Assign several locals with the given <em>obj</em>. The following are equivalent:</p>13761377<pre><code> res.local('foo', bar);1378 res.local('bar', baz);13791248 res.locals({ foo: bar, bar, baz });</code></pre>1380 res.locals({ foo: bar, bar, baz });1381</code></pre>13821250<h3 id="app-set-name-val">app.set(name[, val])</h3>1383<h3 id="app.set()">app.set(name[, val])</h3>13841385<p>Apply an application level setting <em>name</em> to <em>val</em>, or1386get the value of <em>name</em> when <em>val</em> is not present:</p>13871388<pre><code>app.set('views', __dirname + '/views');1389app.set('views');1257// =&gt; ...path...</code></pre>1390// =&gt; ...path...1391</code></pre>13921393<p>Alternatively you may simply access the settings via <em>app.settings</em>:</p>13941395<pre><code>app.settings.views1262// =&gt; ...path...</code></pre>1396// =&gt; ...path...1397</code></pre>13981264<h3 id="app-enable-name">app.enable(name)</h3>1399<h3 id="app.enable()">app.enable(name)</h3>14001401<p>Enable the given setting <em>name</em>:</p>14021403<pre><code>app.enable('some arbitrary setting');1404app.set('some arbitrary setting');1405// =&gt; true14061407app.enabled('some arbitrary setting');1273// =&gt; true</code></pre>1408// =&gt; true1409</code></pre>14101275<h3 id="app-enabled-name">app.enabled(name)</h3>1411<h3 id="app.enabled()">app.enabled(name)</h3>14121413<p>Check if setting <em>name</em> is enabled:</p>14141415<pre><code>app.enabled('view cache');1416// =&gt; false14171418app.enable('view cache');1419app.enabled('view cache');1284// =&gt; true</code></pre>1420// =&gt; true1421</code></pre>14221286<h3 id="app-disable-name">app.disable(name)</h3>1423<h3 id="app.disable()">app.disable(name)</h3>14241425<p>Disable the given setting <em>name</em>:</p>14261427<pre><code>app.disable('some setting');1428app.set('some setting');1429// =&gt; false14301431app.disabled('some setting');1295// =&gt; false</code></pre>1432// =&gt; false1433</code></pre>14341297<h3 id="app-disabled-name">app.disabled(name)</h3>1435<h3 id="app.disabled()">app.disabled(name)</h3>14361437<p>Check if setting <em>name</em> is disabled:</p>14381439<pre><code>app.enable('view cache');
2 unmodified lines
1442// =&gt; false14431444app.disable('view cache');1445app.disabled('view cache');1308// =&gt; true</code></pre>1446// =&gt; true1447</code></pre>14481310<h3 id="app-configure-env-function-function">app.configure(env|function[, function])</h3>1449<h3 id="app.configure()">app.configure(env|function[, function])</h3>14501451<p>Define a callback function for the given <em>env</em> (or all environments) with callback <em>function</em>:</p>14521453<pre><code>app.configure(function(){
1 unmodified line
1455});14561457app.configure('development', function(){1458 // executed for 'development' only1459});1320});</code></pre>1460</code></pre>14611322<h3 id="app-redirect-name-val">app.redirect(name, val)</h3>1462<h3 id="app.redirect()">app.redirect(name, val)</h3>14631464<p>For use with <em>res.redirect()</em> we can map redirects at the application level as shown below:</p>14651326<pre><code>app.redirect('google', 'http://google.com');</code></pre>1466<pre><code>app.redirect('google', 'http://google.com');1467</code></pre>14681469<p>Now in a route we may call:</p>14701330<p> res.redirect('google');</p>1471<p> res.redirect(&lsquo;google&rsquo;);</p>14721473<p>We may also map dynamic redirects:</p>14741475<pre><code>app.redirect('comments', function(req, res){1476 return '/post/' + req.params.id + '/comments';1477});1336});</code></pre>1478</code></pre>14791480<p>So now we may do the following, and the redirect will dynamically adjust to1481the context of the request. If we called this route with <em>GET /post/12</em> our1482redirect <em>Location</em> would be <em>/post/12/comments</em>.</p>14831484<pre><code>app.get('/post/:id', function(req, res){1485 res.redirect('comments');1486});1344});</code></pre>1487</code></pre>14881489<p>When mounted, <em>res.redirect()</em> will respect the mount-point. For example if a blog app is mounted at <em>/blog</em>, the following will redirect to <em>/blog/posts</em>:</p>14901348<pre><code>res.redirect('/posts');</code></pre>1491<pre><code>res.redirect('/posts');1492</code></pre>14931350<h3 id="app-error-function">app.error(function)</h3>1494<h3 id="app.error()">app.error(function)</h3>14951496<p>Adds an error handler <em>function</em> which will receive the exception as the first parameter as shown below.1497Note that we may set several error handlers by making several calls to this method, however the handler1498should call <em>next(err)</em> if it does not wish to deal with the exception:</p>14991500<pre><code>app.error(function(err, req, res, next){1501 res.send(err.message, 500);1502});1358});</code></pre>1503</code></pre>15041360<h3 id="app-helpers-obj">app.helpers(obj)</h3>1505<h3 id="app.helpers()">app.helpers(obj)</h3>15061507<p>Registers static view helpers.</p>15081364<p> app.helpers({1365 name: function(first, last){ return first + ', ' + last }1366 , firstName: 'tj'1367 , lastName: 'holowaychuk'1368 });</p>1509<p> app.helpers({</p>15101511<pre><code> name: function(first, last){ return first + ', ' + last }1512, firstName: 'tj'1513, lastName: 'holowaychuk'1514</code></pre>15151516<p> });</p>15171518<p>Our view could now utilize the <em>firstName</em> and <em>lastName</em> variables,1519as well as the <em>name()</em> function exposed.</p>15201373<pre><code>&lt;%= name(firstName, lastName) %&gt;</code></pre>1521<pre><code>&lt;%= name(firstName, lastName) %&gt;1522</code></pre>15231375<h3 id="app-dynamicHelpers-obj">app.dynamicHelpers(obj)</h3>1524<h3 id="app.dynamichelpers()">app.dynamicHelpers(obj)</h3>15251526<p>Registers dynamic view helpers. Dynamic view helpers1527are simply functions which accept <em>req</em>, <em>res</em>, and are1528evaluated against the <em>Server</em> instance before a view is rendered. The <em>return value</em> of this function
2 unmodified lines
1531<pre><code>app.dynamicHelpers({1532 session: function(req, res){1533 return req.session;1534 }1535});1386});</code></pre>1536</code></pre>15371538<p>All views would now have <em>session</em> available so that session data can be accessed via <em>session.name</em> etc:</p>15391390<pre><code>&lt;%= session.name %&gt;</code></pre>1540<pre><code>&lt;%= session.name %&gt;1541</code></pre>15421392<h3 id="app-mounted-fn">app.mounted(fn)</h3>1543<h3 id="app.mounted()">app.mounted(fn)</h3>15441545<p>Assign a callback <em>fn</em> which is called when this <em>Server</em> is passed to <em>Server#use()</em>.</p>15461547<pre><code>var app = express.createServer(),
3 unmodified lines
1551 // parent is app1552 // "this" is blog1553});15541404app.use(blog);</code></pre>1555app.use(blog);1556</code></pre>15571406<h3 id="app-register-ext-exports">app.register(ext, exports)</h3>1558<h3 id="app.register()">app.register(ext, exports)</h3>15591560<p>Register the given template engine <em>exports</em>1409as <em>ext</em>. For example we may wish to map ".html"1561as <em>ext</em>. For example we may wish to map &ldquo;.html&rdquo;1562files to jade:</p>15631412<pre><code> app.register('.html', require('jade'));</code></pre>1564<pre><code> app.register('.html', require('jade'));1565</code></pre>15661567<p>This is also useful for libraries that may not1568match extensions correctly. For example my haml.js1416library is installed from npm as "hamljs" so instead1417of layout.hamljs, we can register the engine as ".haml":</p>1569library is installed from npm as &ldquo;hamljs&rdquo; so instead1570of layout.hamljs, we can register the engine as &ldquo;.haml&rdquo;:</p>15711419<pre><code> app.register('.haml', require('haml-js'));</code></pre>1572<pre><code> app.register('.haml', require('haml-js'));1573</code></pre>15741575<p>For engines that do not comply with the Express1576specification, we can also wrap their api this way. Below1577we map <em>.md</em> to render markdown files, rendering the html once1578since it will not change on subsequent calls, and support local substitution1425in the form of "{name}".</p>1579in the form of &ldquo;{name}&rdquo;.</p>15801581<pre><code> app.register('.md', {1582 compile: function(str, options){1583 var html = md.toHTML(str);
2 unmodified lines
1586 return locals[name];1587 });1588 };1589 }1590 });1436 });</code></pre>1591</code></pre>15921438<h3 id="app-listen-port-host">app.listen([port[, host]])</h3>1593<h3 id="app.listen()">app.listen([port[, host]])</h3>15941595<p>Bind the app server to the given <em>port</em>, which defaults to 3000. When <em>host</em> is omitted all1441connections will be accepted via <em>INADDR</em>ANY_.</p>1596connections will be accepted via <em>INADDR_ANY</em>.</p>15971598<pre><code>app.listen();1599app.listen(3000);1445app.listen(3000, 'n.n.n.n');</code></pre>1600app.listen(3000, 'n.n.n.n');1601</code></pre>16021603<p>The <em>port</em> argument may also be a string representing the path to a unix domain socket:</p>16041449<pre><code>app.listen('/tmp/express.sock');</code></pre>1605<pre><code>app.listen('/tmp/express.sock');1606</code></pre>16071608<p>Then try it out:</p>16091610<pre><code>$ telnet /tmp/express.sock
2 unmodified lines
1613HTTP/1.1 200 OK1614Content-Type: text/plain1615Content-Length: 1116161460Hello World</code></pre>1617Hello World1618</code></pre>1619 </div>1620 </div>1621 </body>1464</html>1622</html>
modified:docs/index.html+47 added-10 removed
194 unmodified lines
195app.get('/', function(req, res){196 res.send('Hello World');197});198199app.listen(3000);</code></pre>199app.listen(3000);200</code></pre>201201<h2 id="Features">Features</h2>202<h2>Features</h2>203203<ul><li>Robust routing</li><li>Redirection helpers</li><li>Dynamic view helpers</li><li>Application level view options</li><li>Content negotiation</li><li>Application mounting</li><li>Focus on high performance</li><li>View rendering and partials support</li><li>Environment based configuration</li><li>Session based flash notifications</li><li>Built on <a href="http://github.com/senchalabs/connect">Connect</a></li><li><a href="executable.html">Executable</a> for generating applications quickly</li><li>High test coverage</li></ul>204<ul>205<li>Robust routing</li>206<li>Redirection helpers</li>207<li>Dynamic view helpers</li>208<li>Application level view options</li>209<li>Content negotiation</li>210<li>Application mounting</li>211<li>Focus on high performance</li>212<li>View rendering and partials support</li>213<li>Environment based configuration</li>214<li>Session based flash notifications</li>215<li>Built on <a href="http://github.com/senchalabs/connect">Connect</a></li>216<li><a href="executable.html">Executable</a> for generating applications quickly</li>217<li>High test coverage</li>218</ul>219205<h2 id="Contributors">Contributors</h2>220221<h2>Contributors</h2>222223<p>The following are the major contributors of Express (in no specific order).</p>224209<ul><li>TJ Holowaychuk (<a href="http://github.com/visionmedia)">visionmedia</a></li><li>Ciaran Jessup (<a href="http://github.com/ciaranj)">ciaranj</a></li><li>Aaron Heckmann (<a href="http://github.com/aheckmann)">aheckmann</a></li><li>Guillermo Rauch (<a href="http://github.com/guille)">guille</a></li></ul>225<ul>226<li>TJ Holowaychuk (<a href="http://github.com/visionmedia">visionmedia</a>)</li>227<li>Ciaran Jessup (<a href="http://github.com/ciaranj">ciaranj</a>)</li>228<li>Aaron Heckmann (<a href="http://github.com/aheckmann">aheckmann</a>)</li>229<li>Guillermo Rauch (<a href="http://github.com/guille">guille</a>)</li>230</ul>231211<h2 id="Third-Party-Modules">Third-Party Modules</h2>232233<h2>Third-Party Modules</h2>234235<p>The following modules compliment or extend Express directly:</p>236215<ul><li><a href="http://github.com/visionmedia/express-resource">express-resource</a> provides resourceful routing</li><li><a href="http://github.com/visionmedia/express-messages">express-messages</a> flash message notification rendering</li><li><a href="http://github.com/visionmedia/express-configure">express-configure</a> async configuration support (load settings from redis etc)</li><li><a href="http://github.com/visionmedia/express-namespace">express-namespace</a> namespaced routing support</li></ul>237<ul>238<li><a href="http://github.com/visionmedia/express-resource">express-resource</a> provides resourceful routing</li>239<li><a href="http://github.com/visionmedia/express-messages">express-messages</a> flash message notification rendering</li>240<li><a href="http://github.com/visionmedia/express-configure">express-configure</a> async configuration support (load settings from redis etc)</li>241<li><a href="http://github.com/visionmedia/express-namespace">express-namespace</a> namespaced routing support</li>242</ul>243217<h2 id="More-Information">More Information</h2>244219<ul><li><a href="http://groups.google.com/group/express-js">Google Group</a> for discussion</li><li>Follow <a href="http://twitter.com/tjholowaychuk">tjholowaychuk</a> on twitter for updates</li><li>View the <a href="http://senchalabs.github.com/connect">Connect</a> documentation</li><li>View the <a href="http://wiki.github.com/senchalabs/connect/">Connect Wiki</a> for contrib middleware</li><li>View the <a href="http://github.com/visionmedia/express/tree/master/examples/">examples</a></li><li>View the <a href="http://github.com/visionmedia/express">source</a></li><li>View the <a href="contrib.html">contrib guide</a></li></ul>245<h2>More Information</h2>246247<ul>248<li><a href="http://groups.google.com/group/express-js">Google Group</a> for discussion</li>249<li>Follow <a href="http://twitter.com/tjholowaychuk">tjholowaychuk</a> on twitter for updates</li>250<li>View the <a href="http://senchalabs.github.com/connect">Connect</a> documentation</li>251<li>View the <a href="http://wiki.github.com/senchalabs/connect/">Connect Wiki</a> for contrib middleware</li>252<li>View the <a href="http://github.com/visionmedia/express/tree/master/examples/">examples</a></li>253<li>View the <a href="http://github.com/visionmedia/express">source</a></li>254<li>View the <a href="contrib.html">contrib guide</a></li>255</ul>256257 </div>258 </div>259 </body>223</html>260</html>
modified:docs/migrate.html+64 added-42 removed
188 unmodified lines
189 <li><a href="guide.html">Guide</a></li>190 <li><a href="screencasts.html">Screencasts</a></li>191 <li><a href="applications.html">Applications</a></li>192 </ul>193<h3 id="Express-1-x-to-2-x-Migration">Express 1.x to 2.x Migration</h3>193<h3>Express 1.x to 2.x Migration</h3>194195<h3 id="HTTPS">HTTPS</h3>195<h3>HTTPS</h3>196197<p> Creating an HTTPS server is simply, simply pass the TLS options to <em>express.createServer()</em>:</p>198199<pre><code> var app = express.createServer({200 key: ...201 , cert: ...202 });203204 app.listen(443);</code></pre>204 app.listen(443);205</code></pre>206206<h3 id="req-header-Referrer">req.header() Referrer</h3>207<h3>req.header() Referrer</h3>208209<p> Previously if anyone was doing something similar to:</p>210211<pre><code> req.headers.referrer || req.headers.referer211 req.header('Referrer') || req.header('Referer')</code></pre>212 req.header('Referrer') || req.header('Referer')213</code></pre>214215<p> With the new special-case we may now simply use <em>Referrer</em> which will return either if defined:</p>216215<pre><code> req.header('Referrer')</code></pre>217<pre><code> req.header('Referrer')218</code></pre>219217<h3 id="res-local-name-val">res.local(name, val)</h3>220<h3>res.local(name, val)</h3>221222<p> Previously all local variables had to be passed to <em>res.render()</em>, or either <em>app.helpers()</em> or <em>app.dynamicHelpers()</em>, now we may do this at the request-level progressively. The <em>res.local()</em> method accepts a <em>name</em> and <em>val</em>, however the locals passed to <em>res.render()</em> will take precedence.</p>223224<p> For example we may utilize this feature to create locals in middleware:</p>
6 unmodified lines
231 }232233 app.get('/user/:id', loadUser, function(req, res){234 res.render('user');235 });232 });</code></pre>236</code></pre>237234<h3 id="req-param-name-defaultValue">req.param(name[, defaultValue])</h3>238<h3>req.param(name[, defaultValue])</h3>239240<p> Previously only <em>name</em> was accepted, so some of you may have been doing the following:</p>241238<pre><code> var id = req.param('id') || req.user.id;</code></pre>242<pre><code> var id = req.param('id') || req.user.id;243</code></pre>244245<p> The new <em>defaultValue</em> argument can handle this nicely:</p>246242<pre><code> var id = req.param('id', req.user.id);</code></pre>247<pre><code> var id = req.param('id', req.user.id);248</code></pre>249244<h3 id="app-helpers-app-locals">app.helpers() / app.locals()</h3>250<h3>app.helpers() / app.locals()</h3>251252<p> <em>app.locals()</em> is now an alias of <em>app.helpers()</em>, as helpers makes more sense for functions.</p>253248<h3 id="req-accepts-type">req.accepts(type)</h3>254<h3>req.accepts(type)</h3>255256<p> <em>req.accepts()</em> now accepts extensions:</p>257258<pre><code> // Accept: text/html
9 unmodified lines
268 // =&gt; true269270 req.accepts('image/png');271 req.accepts('png');266 // =&gt; false</code></pre>272 // =&gt; false273</code></pre>274268<h3 id="res-cookie">res.cookie()</h3>275<h3>res.cookie()</h3>276277<p> Previously only directly values could be passed, so for example:</p>278272<pre><code>res.cookie('rememberme', 'yes', { expires: new Date(Date.now() + 900000) });</code></pre>279<pre><code>res.cookie('rememberme', 'yes', { expires: new Date(Date.now() + 900000) });280</code></pre>281282<p>However now we have the alternative <em>maxAge</em> property which may be used to set <em>expires</em> relative to <em>Date.now()</em> in milliseconds, so our example above can now become:</p>283276<pre><code>res.cookie('rememberme', 'yes', { maxAge: 900000 });</code></pre>284<pre><code>res.cookie('rememberme', 'yes', { maxAge: 900000 });285</code></pre>286278<h3 id="res-download-res-sendfile">res.download() / res.sendfile()</h3>287<h3>res.download() / res.sendfile()</h3>288280<p> Both of these methods now utilize Connect's static file server behind the scenes (actually the previous Express code was ported to Connect 1.0). With this change comes a change to the callback as well. Previously the <em>path</em> and <em>stream</em> were passed, however now only an <em>error</em> is passed, when no error has occurred the callback will be invoked indicating that the file transfer is complete. The callback remains optional:</p>289<p> Both of these methods now utilize Connect&rsquo;s static file server behind the scenes (actually the previous Express code was ported to Connect 1.0). With this change comes a change to the callback as well. Previously the <em>path</em> and <em>stream</em> were passed, however now only an <em>error</em> is passed, when no error has occurred the callback will be invoked indicating that the file transfer is complete. The callback remains optional:</p>290291<pre><code> res.download('/path/to/file');292293 res.download('/path/to/file', function(err){
1 unmodified line
295 console.error(err);296 } else {297 console.log('transferred');298 }299 });290 });</code></pre>300</code></pre>301302<p> The <em>stream threshold</em> setting was removed.</p>303294<h3 id="res-render">res.render()</h3>304<h3>res.render()</h3>305306<p> Previously locals were passed as a separate key:</p>307298<pre><code> res.render('user', { layout: false, locals: { user: user }});</code></pre>308<pre><code> res.render('user', { layout: false, locals: { user: user }});309</code></pre>310311<p> In Express 2.0 both the locals and the options are one in the same, meaning you cannot have a local variable named <em>layout</em> as it is reserved for express, however this cleans up the API:</p>312302<pre><code> res.render('user', { layout: false, user: user });</code></pre>313<pre><code> res.render('user', { layout: false, user: user });314</code></pre>315304<h3 id="res-partial">res.partial()</h3>316<h3>res.partial()</h3>317318<p> Express 2.0 adds the <em>res.partial()</em> method, helpful for rendering partial fragments over WebSockets or Ajax requests etc. The API is identical to the <em>partial()</em> calls within views.</p>319320<pre><code> // render a collection of comments321 res.partial('comment', [comment1, comment2]); 322323 // render a single comment312 res.partial('comment', comment);</code></pre>324 res.partial('comment', comment);325</code></pre>326314<h3 id="partial-locals">partial() locals</h3>327<h3>partial() locals</h3>328329<p> Both <em>res.partial()</em> and the <em>partial()</em> functions accept an single object consisting of both the options and the locals. Previously with Express 1.x you may pass <em>user</em> to a partial, along with <em>date</em> like so:</p>330318<pre><code> partial('user', { object: user, locals: { date: new Date }})</code></pre>331<pre><code> partial('user', { object: user, locals: { date: new Date }})332</code></pre>333334<p>or perhaps if you preferred not to use the inferred name <em>user</em> you may used a local for this as well:</p>335322<pre><code> partial('user', { locals: { user: user, date: new Date }})</code></pre>336<pre><code> partial('user', { locals: { user: user, date: new Date }})337</code></pre>338339<p> With recent changes to Express 2.x the object passed is now both, so the following is valid for the <em>object</em> option and locals:</p>340326<pre><code> partial('user', { object: user, date: new Date })</code></pre>341<pre><code> partial('user', { object: user, date: new Date })342</code></pre>343344<p> Or the following which is equivalent, however the local var name is explicitly set to <em>user</em> instead of deduced from the filename.</p>345330<pre><code> partial('user', { user: user, date: new Date })</code></pre>346<pre><code> partial('user', { user: user, date: new Date })347</code></pre>348332<p> When a "basic" object aka <em>{}</em> or <em>new Object</em> is passed, it is considered options, otherwise it is considered the <em>object</em>. The following are equivalent:</p>349<p> When a &ldquo;basic&rdquo; object aka <em>{}</em> or <em>new Object</em> is passed, it is considered options, otherwise it is considered the <em>object</em>. The following are equivalent:</p>350351<pre><code> partial('user', user); 335 partial('user', { object: user }); </code></pre>352 partial('user', { object: user }); 353</code></pre>354337<h3 id="Template-Engine-Compliance">Template Engine Compliance</h3>355<h3>Template Engine Compliance</h3>356357<p> To comply with Express previously engines needed the following signature:</p>358341<pre><code> engine.render(str, options, function(err){});</code></pre>359<pre><code> engine.render(str, options, function(err){});360</code></pre>361362<p> Now they must export a <em>compile()</em> function, returning a function which when called with local variables will render the template. This allows Express to cache the compiled function in memory during production.</p>363364<pre><code> var fn = engine.compile(str, options);346 fn(locals);</code></pre>365 fn(locals);366</code></pre>367348<h3 id="View-Partial-Lookup">View Partial Lookup</h3>368<h3>View Partial Lookup</h3>369370<p> Previously partials were loaded relative to the now removed <em>view partials</em> directory setting, or by default <em>views/partials</em>, now they are relative to the view calling them, read more on <a href="guide.html#View-Lookup">view lookup</a>.</p>371352<h3 id="Mime-Types">Mime Types</h3>372<h3>Mime Types</h3>373374<p> Express and Connect now utilize the <em>mime</em> module in npm, so to add more use:</p>375356<pre><code> require('mime').define({ 'foo/bar': ['foo', 'bar'] });</code></pre>376<pre><code> require('mime').define({ 'foo/bar': ['foo', 'bar'] });377</code></pre>378358<h3 id="static-middleware">static() middleware</h3>379<h3>static() middleware</h3>380381<p> Previously named <code>staticProvider()</code>, the now <code>static()</code> middleware takes a single directory path, followed by options.</p>382362<pre><code> app.use(express.static(__dirname + '/public', { maxAge: oneYear }));</code></pre>383<pre><code> app.use(express.static(__dirname + '/public', { maxAge: oneYear }));384</code></pre>385386<p>Previously when using options the <code>root</code> option would be used for this:</p>387388<pre><code> app.use(express.staticProvider({ root: __dirname + '/public', maxAge: oneYear }));367 </code></pre>389</code></pre>390 </div>391 </div>392 </body>371</html>393</html>
modified:docs/screencasts.html+13 added-9 removed
188 unmodified lines
189 <li><a href="guide.html">Guide</a></li>190 <li><a href="screencasts.html">Screencasts</a></li>191 <li><a href="applications.html">Applications</a></li>192 </ul>193<h3 id="Introduction">Introduction</h3>193<h3>Introduction</h3>194195<p>This introduction screencast covers the basics of Express, and how to get started with your first application.</p>196197<p>&lt;object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,115,0' width='560' height='345'&gt;&lt;param name='movie' value='http://screenr.com/Content/assets/screenr<em>1116090935.swf' /&gt;&lt;param name='flashvars' value='i=139583' /&gt;&lt;param name='allowFullScreen' value='true' /&gt;&lt;embed src='http://screenr.com/Content/assets/screenr</em>1116090935.swf' flashvars='i=139583' allowFullScreen='true' width='560' height='345' pluginspage='http://www.macromedia.com/go/getflashplayer'&gt;&lt;/embed&gt;&lt;/object&gt;</p>197<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,115,0' width='560' height='345'><param name='movie' value='http://screenr.com/Content/assets/screenr_1116090935.swf' /><param name='flashvars' value='i=139583' /><param name='allowFullScreen' value='true' /><embed src='http://screenr.com/Content/assets/screenr_1116090935.swf' flashvars='i=139583' allowFullScreen='true' width='560' height='345' pluginspage='http://www.macromedia.com/go/getflashplayer'></embed></object>198199<h3 id="View-Partials">View Partials</h3>199200<h3>View Partials</h3>201202<p>In this screencast we work with partials to display a collection of users using the <a href="http://jade-lang.com">Jade</a> template engine, and learn about view path resolution.</p>203203<p>&lt;object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,115,0' width='560' height='345'&gt;&lt;param name='movie' value='http://screenr.com/Content/assets/screenr<em>1116090935.swf' /&gt;&lt;param name='flashvars' value='i=139591' /&gt;&lt;param name='allowFullScreen' value='true' /&gt;&lt;embed src='http://screenr.com/Content/assets/screenr</em>1116090935.swf' flashvars='i=139591' allowFullScreen='true' width='560' height='345' pluginspage='http://www.macromedia.com/go/getflashplayer'&gt;&lt;/embed&gt;&lt;/object&gt;</p>204<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,115,0' width='560' height='345'><param name='movie' value='http://screenr.com/Content/assets/screenr_1116090935.swf' /><param name='flashvars' value='i=139591' /><param name='allowFullScreen' value='true' /><embed src='http://screenr.com/Content/assets/screenr_1116090935.swf' flashvars='i=139591' allowFullScreen='true' width='560' height='345' pluginspage='http://www.macromedia.com/go/getflashplayer'></embed></object>205205<h3 id="Route-Specific-Middleware">Route Specific Middleware</h3>206207<h3>Route Specific Middleware</h3>208209<p>In the screencast below we learn about the benefits of route-specific middleware.</p>210209<p>&lt;object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,115,0' width='560' height='345'&gt;&lt;param name='movie' value='http://screenr.com/Content/assets/screenr<em>1116090935.swf' /&gt;&lt;param name='flashvars' value='i=140296' /&gt;&lt;param name='allowFullScreen' value='true' /&gt;&lt;embed src='http://screenr.com/Content/assets/screenr</em>1116090935.swf' flashvars='i=140296' allowFullScreen='true' width='560' height='345' pluginspage='http://www.macromedia.com/go/getflashplayer'&gt;&lt;/embed&gt;&lt;/object&gt;</p>211<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,115,0' width='560' height='345'><param name='movie' value='http://screenr.com/Content/assets/screenr_1116090935.swf' /><param name='flashvars' value='i=140296' /><param name='allowFullScreen' value='true' /><embed src='http://screenr.com/Content/assets/screenr_1116090935.swf' flashvars='i=140296' allowFullScreen='true' width='560' height='345' pluginspage='http://www.macromedia.com/go/getflashplayer'></embed></object>212211<h3 id="Route-Param-Preconditions">Route Param Preconditions</h3>213214<h3>Route Param Preconditions</h3>215216<p>Learn about route parameter (<em>/user/:id</em>) pre-conditions, providing automated validation, and loading of data via the named route param segments.</p>217215<p>&lt;object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,115,0' width='560' height='345'&gt;&lt;param name='movie' value='http://screenr.com/Content/assets/screenr<em>1116090935.swf' /&gt;&lt;param name='flashvars' value='i=140300' /&gt;&lt;param name='allowFullScreen' value='true' /&gt;&lt;embed src='http://screenr.com/Content/assets/screenr</em>1116090935.swf' flashvars='i=140300' allowFullScreen='true' width='560' height='345' pluginspage='http://www.macromedia.com/go/getflashplayer'&gt;&lt;/embed&gt;&lt;/object&gt;</p>218<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,115,0' width='560' height='345'><param name='movie' value='http://screenr.com/Content/assets/screenr_1116090935.swf' /><param name='flashvars' value='i=140300' /><param name='allowFullScreen' value='true' /><embed src='http://screenr.com/Content/assets/screenr_1116090935.swf' flashvars='i=140300' allowFullScreen='true' width='560' height='345' pluginspage='http://www.macromedia.com/go/getflashplayer'></embed></object>219220 </div>221 </div>222 </body>219</html>223</html>
modified:support/toc.js+34 added-36 removed
modified:History.md+13 added
12n.n.n / 2011-03-18 3==================45 * connect 1.1.16 * refactored res.redirect()7 * doc typo8 * doc typo9 * regenerated docs10 * removed manpages from make11 * Release 2.0.012 * Fixed up index view path alternative13 * Changed; res.locals() without object returns the locals14152.0.0 / 2011-03-17 16==================1718 * Fixed up index view path alternative.
537 unmodified lines
modified:Makefile+1 added-2 removed
20 unmodified lines
21 @./support/toc.js docs/guide.html2223%.html: %.md24 @echo "... $< -> $@"25 @ronn --fragment $< \25 @markdown $< \26 | cat docs/layout/head.html - docs/layout/foot.html \27 | sed 's/NAME/Express/g' \27 > $@2829site:30 rm -fr /tmp/docs \
9 unmodified lines