guide.html
This file
Path
docs/guide.html
1,776 lines · 62.9 KiB · HTML
Last changed by
docsTj Holowaychukbefore adoption3588c1e
History of this fileTake it
The walk stopped after 200 commits: lines older than d867cc9 point at it.
Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1–181why, one step back1<html>2 <head>3 <title>Express - node web framework</title>4 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>5 <style>6 #tagline {7 margin-left: 75px;8 margin-bottom: 30px;9 color: rgba(255,255,255,0.7); }10 html {11 background: #1c1c1c url(images/bg.tile.jpg); }1213 body {14 margin: 0;15 padding-bottom: 30px;16 font: 14px/1.4 "Helvetica Neue", "Lucida Grande", "Arial";17 font-size: 14px;18 line-height: 1.5;19 -webkit-font-smoothing: antialiased;20 background: url(images/bg.jpg) 50% 0 no-repeat;21 color: #8b8b8b; }2223 * {24 outline: none; }2526 em {27 color: white; }2829 a img {30 border: none !important; }3132 a {33 font-weight: bold;34 text-decoration: none;35 color: white;36 -webkit-transition-property: opacity, -webkit-transform, color, background-color, padding, -webkit-box-shadow;37 -webkit-transition-duration: 0.15s;38 -webkit-transition-timing-function: ease-out; }39 a:hover {40 opacity: 0.8; }4142 h1, h2, h3, h4 {43 margin: 45px 0 0 0;44 color: white;45 text-shadow: 1px 2px 2px rgba(0,0,0,0.6); }4647 h3 {48 font-size: 18px; }49 h4 {50 margin-left: 10px;51 font-size: 14px;52 }5354 pre {55 margin: 20px 10px;56 padding: 25px 20px;57 background: rgba(0,0,0,0.5);58 border: 1px solid #323232;59 -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);60 -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);61 -webkit-border-radius: 5px;62 -moz-border-radius: 5px; }6364 code {65 font-family: "Helvetica Neue", "Lucida Grande", "Arial"; }6667 ul {68 margin: 15px 0;69 padding: 0 0 0 35px; }70 ul li {71 margin: 0;72 padding: 2px 0;73 list-style: square; }74 ul li ul {75 margin: 0;76 padding-left: 12px;77 }7879 .man-name, #Express { display:none; }8081 .sect {82 margin-left: 40px; }83 img {84 margin-left: 20px;85 margin-bottom: 15px;86 }8788 #logo {89 display: block;90 margin-left: 30%;91 margin-bottom: 30px;92 width: 194px;93 height: 51px;94 background: url(images/logo.png) 0 0 no-repeat;95 text-indent: -99999px; }96 #logo:hover {97 opacity: 0.7; }98 #logo:active {99 opacity: 0.3; }100101 #ribbon {102 position: fixed;103 top: 0;104 right: 0;105 z-index: 2; }106107 #wrapper {108 width: 100%;109 min-height: 800px;110 background: url(images/top.png) 0 0 repeat-x; }111112 #container {113 margin: 0 auto;114 padding-top: 80px;115 width: 550px; }116117 #toc {118 position: fixed;119 top: 0;120 left: 0;121 margin: 0 0 0 15px;122 padding: 15px;123 height: 100%;124 background: rgba(0,0,0,0.2);125 overflow: auto;126 border-right: 1px solid rgba(255,255,255,0.05);127 }128 #toc li {129 padding: 0;130 list-style: none;131 }132 #toc li a {133 font-size: 11px;134 }135 #menu {136 margin-left: 75px;137 padding: 0;138 padding-bottom: 30px; }139 #menu li {140 display: inline;141 list-style: none; }142 #menu li a {143 display: block;144 float: left;145 margin: 0 2px;146 padding: 3px 15px;147 background: rgba(0,0,0,0.2);148 -webkit-border-radius: 8px;149 -moz-border-radius: 8px;150 -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);151 -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);152 -webkit-transition-property: opacity, -webkit-transform, color, background-color, -webkit-box-shadow;153 -webkit-transition-duration: 0.15s;154 -webkit-transition-timing-function: ease-out; }155 #menu li a:hover,156 #menu li a.active {157 background: rgba(0,0,0,0.5); }158 #menu li a:active {159 background: rgba(0,0,0,0.1);160 -webkit-box-shadow: 1px 1px 1px rgba(0,0,0,0.4);161 -moz-box-shadow: 1px 1px 1px rgba(0,0,0,0.4); }162 </style>163 <script>164 $(function(){165 $('.section').hide();166 $('.toggle, a.section-title').toggle(function(){167 $(this).siblings('ul').fadeIn(300);168 return false;169 }, function(){170 $(this).siblings('ul').fadeOut(300);171 return false;172 });173 });174 </script>175 </head>176 <body>177 <a href='http://github.com/visionmedia/express'> 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">fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 182–199why, one step back182<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>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 200why, one step back200<li><a href="#" class="toggle">+</a> <a class="section-title" href="#">Request</a><ul class="section" id="section-Request">fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 201–206why, one step back201<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>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 207–208why, one step back207</ul></li>208<li><a href="#" class="toggle">+</a> <a class="section-title" href="#">Response</a><ul class="section" id="section-Response">fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 209–222why, one step back209<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>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 223–224why, one step back223</ul></li>224<li><a href="#" class="toggle">+</a> <a class="section-title" href="#">Server</a><ul class="section" id="section-Server">fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 225–234why, one step back225<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>updated docsTj Holowaychukbefore adoption99b49d2
lines 235–236why, one step back235<li><a href="#app.lookup">lookup</a></li>236<li><a href="#app.match">match</a></li>fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 237–239why, one step back237<li><a href="#app.mounted()">mounted()</a></li>238<li><a href="#app.register()">register()</a></li>239<li><a href="#app.listen()">listen()</a></li>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 240–252why, one step back240</ul></li>241</ul>242 <a href='http://github.com/visionmedia/express' id='logo'>Express</a> 243 <p id="tagline">244 High performance, high class web development for245 <a href="http://nodejs.org">Node.js</a>246 </p>247 <ul id="menu">248 <li><a href="index.html">Home</a></li>249 <li><a href="guide.html">Guide</a></li>250 <li><a href="screencasts.html">Screencasts</a></li>251 <li><a href="applications.html">Applications</a></li>252 </ul>fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 253why, one step back253<h3 id="installation">Installation</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 254why, one step back254fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 255–256why, one step back255<pre><code>$ npm install express256</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 257why, one step back257fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 258why, one step back258<h3 id="creating-a server">Creating A Server</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 259–267why, one step back259260<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>261262<pre><code>var app = require('express').createServer();263264app.get('/', function(req, res){265 res.send('hello world');266});267fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 268–269why, one step back268app.listen(3000);269</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 270why, one step back270fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 271why, one step back271<h3 id="creating-an https server">Creating An HTTPS Server</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 272–273why, one step back272273<p> To initialize a <em>express.HTTPSServer</em> we do the same as above, however wefixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 274why, one step back274 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>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 275why, one step back275fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 276–277why, one step back276<pre><code> var app = require('express').createServer({ key: ... });277</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 278why, one step back278fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 279why, one step back279<h3 id="configuration">Configuration</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 280–282why, one step back280281<p>Express supports arbitrary environments, such as <em>production</em> and <em>development</em>. Developers282can use the <em>configure()</em> method to setup needs required by the current environment. Whenfixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 283why, one step back283<em>configure()</em> is called without an environment name it will be run in <em>every</em> environmentFixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 284–291why, one step back284prior to the environment specific callback.</p>285286<p>In the example below we only <em>dumpExceptions</em>, and respond with exception stack traces287in <em>development</em> mode, however for both environments we utilize <em>methodOverride</em> and <em>bodyParser</em>.288Note the use of <em>app.router</em>, which can (optionally) be used to mount the application routes,289otherwise the first call to <em>app.get()</em>, <em>app.post()</em>, etc will mount the routes.</p>290291<pre><code>app.configure(function(){fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 292–294why, one step back292 app.use(express.methodOverride());293 app.use(express.bodyParser());294 app.use(app.router);Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 295–297why, one step back295});296297app.configure('development', function(){fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 298–299why, one step back298 app.use(express.static(__dirname + '/public'));299 app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 300–303why, one step back300});301302app.configure('production', function(){303 var oneYear = 31557600000;regenerated docsTj Holowaychukbefore adoptiondd7158a
line 304why, one step back304 app.use(express.static(__dirname + '/public', { maxAge: oneYear }));Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 305why, one step back305 app.use(express.errorHandler());fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 306–307why, one step back306});307</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 308–323why, one step back308309<p>For internal and arbitrary settings Express provides the <em>set(key[, val])</em>, <em>enable(key)</em>, <em>disable(key)</em> methods:</p>310311<pre><code> app.configure(function(){312 app.set('views', __dirname + '/views');313 app.set('views');314 // => "/absolute/path/to/views"315316 app.enable('some feature');317 // same as app.set('some feature', true);318319 app.disable('some feature');320 // same as app.set('some feature', false);321322 app.enabled('some feature')323 // => falsefixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 324–325why, one step back324 });325</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 326why, one step back326fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 327why, one step back327<p>To alter the environment we can set the <em>NODE_ENV</em> environment variable, for example:</p>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 328why, one step back328fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 329–330why, one step back329<pre><code>$ NODE_ENV=production node app.js330</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 331–333why, one step back331332<p>This is <em>very</em> important, as many caching mechanisms are <em>only enabled</em> when in production.</p>333fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 334why, one step back334<h3 id="settings">Settings</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 335–337why, one step back335336<p>Express supports the following settings out of the box:</p>337fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 338–342why, one step back338<ul>339<li><em>home</em> Application base path used for <em>res.redirect()</em> and transparently handling mounted apps.</li>340<li><em>views</em> Root views directory defaulting to <strong>CWD/views</strong></li>341<li><em>view engine</em> Default view engine name for views rendered without extensions</li>342<li><em>view options</em> An object specifying global view options</li>docsTj Holowaychukbefore adoption3588c1e
lines 343–344why, one step back343<li><em>view cache</em> Enable view caching (enabled in production)</li>344<li><em>case sensitive routes</em> Enable case-sensitive routing</li>fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 345why, one step back345</ul>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 346–347why, one step back346347fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 348–349why, one step back348<h3 id="routing">Routing</h3>349Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 350why, one step back350<p>Express utilizes the HTTP verbs to provide a meaningful, expressive routing API.fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 351–352why, one step back351For example we may want to render a user’s account for the path <em>/user/12</em>, this352can be done by defining the route below. The values associated to the named placeholdersFixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 353–355why, one step back353are available as <code>req.params</code>.</p>354355<pre><code>app.get('/user/:id', function(req, res){fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 356–358why, one step back356 res.send('user ' + req.params.id);357});358</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 359why, one step back359docsTj Holowaychukbefore adoption3588c1e
line 360why, one step back360<p>A route is simple a string which is compiled to a <em>RegExp</em> internally. For exampleFixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 361–362why, one step back361when <em>/user/:id</em> is compiled, a simplified version of the regexp may look similar to:</p>362fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 363–364why, one step back363<pre><code>\/user\/([^\/]+)\/?364</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 365–366why, one step back365366<p>Regular expression literals may also be passed for complex uses. Since capturefixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 367why, one step back367groups 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>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 368–370why, one step back368369<pre><code>app.get(/^\/users?(?:\/(\d+)(?:\.\.(\d+))?)?/, function(req, res){370 res.send(req.params);fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 371–372why, one step back371});372</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 373–382why, one step back373374<p>Curl requests against the previously defined route:</p>375376<pre><code> $ curl http://dev:3000/user377 [null,null]378 $ curl http://dev:3000/users379 [null,null]380 $ curl http://dev:3000/users/1381 ["1",null]382 $ curl http://dev:3000/users/1..15fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 383–384why, one step back383 ["1","15"]384</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 385–418why, one step back385386<p>Below are some route examples, and the associated paths that they387may consume:</p>388389<pre><code> "/user/:id"390 /user/12391392 "/users/:id?"393 /users/5394 /users395396 "/files/*"397 /files/jquery.js398 /files/javascripts/jquery.js399400 "/file/*.*"401 /files/jquery.js402 /files/javascripts/jquery.js403404 "/user/:id/:operation?"405 /user/1406 /user/1/edit407408 "/products.:format"409 /products.json410 /products.xml411412 "/products.:format?"413 /products.json414 /products.xml415 /products416417 "/user/:id.:format?"418 /user/12fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 419–420why, one step back419 /user/12.json420</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 421–432why, one step back421422<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>423424<pre><code>var express = require('express')425 , app = express.createServer();426427app.use(express.bodyParser());428429app.post('/', function(req, res){430 res.send(req.body);431});432fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 433–434why, one step back433app.listen(3000);434</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 435why, one step back435docsTj Holowaychukbefore adoption3588c1e
line 436why, one step back436<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([0-9]+)’</em> which will <em>not</em> match unless the placeholder value contains only digits.</p>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 437why, one step back437fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 438why, one step back438<h3 id="passing-route control">Passing Route Control</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 439–444why, one step back439440<p>We may pass control to the next <em>matching</em> route, by calling the <em>third</em> argument,441the <em>next()</em> function. When a match cannot be made, control is passed back to Connect,442and 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>443444<pre><code>app.get('/users/:id?', function(req, res, next){fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 445–450why, one step back445 var id = req.params.id;446 if (id) {447 // do something448 } else {449 next();450 }Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 451–453why, one step back451});452453app.get('/users', function(req, res){fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 454–456why, one step back454 // do something else455});456</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 457–489why, one step back457458<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>459460<pre><code>var express = require('express')461 , app = express.createServer();462463var users = [{ name: 'tj' }];464465app.all('/user/:id/:op?', function(req, res, next){466 req.user = users[req.params.id];467 if (req.user) {468 next();469 } else {470 next(new Error('cannot find user ' + req.params.id));471 }472});473474app.get('/user/:id', function(req, res){475 res.send('viewing ' + req.user.name);476});477478app.get('/user/:id/edit', function(req, res){479 res.send('editing ' + req.user.name);480});481482app.put('/user/:id', function(req, res){483 res.send('updating ' + req.user.name);484});485486app.get('*', function(req, res){487 res.send('what???', 404);488});489fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 490–491why, one step back490app.listen(3000); 491</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 492why, one step back492fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 493why, one step back493<h3 id="middleware">Middleware</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 494–500why, one step back494495<p>Middleware via <a href="http://github.com/senchalabs/connect">Connect</a> can be496passed to <em>express.createServer()</em> as you would with a regular Connect server. For example:</p>497498<pre><code> var express = require('express');499500var app = express.createServer(fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 501–504why, one step back501 express.logger()502 , express.bodyParser()503 );504</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 505–507why, one step back505506<p>Alternatively we can <em>use()</em> them which is useful when adding middleware within <em>configure()</em> blocks, in a progressive manor.</p>507fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 508–509why, one step back508<pre><code>app.use(express.logger({ format: ':method :uri' }));509</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 510why, one step back510fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 511why, one step back511<p>Typically with connect middleware you would <em>require(‘connect’)</em> like so:</p>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 512–514why, one step back512513<pre><code>var connect = require('connect');514app.use(connect.logger());fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 515–516why, one step back515app.use(connect.bodyParser());516</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 517–520why, one step back517518<p>This is somewhat annoying, so express re-exports these middleware properties, however they are <em>identical</em>:</p>519520<pre><code>app.use(express.logger());fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 521–522why, one step back521app.use(express.bodyParser());522</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 523why, one step back523docsTj Holowaychukbefore adoption3588c1e
lines 524–551why, one step back524<p>Middleware ordering is important, when Connect receives a request the <em>first</em> middleware we pass to <em>createServer()</em> or <em>use()</em> is executed with three parameters, <em>request</em>, <em>response</em>, and a callback function usually named <em>next</em>. When <em>next()</em> is invoked the second middleware will then have it’s turn and so on. This is important to note because many middleware depend on each other, for example <em>methodOverride()</em> checks <em>req.body.</em>method<em> for the HTTP method override, however </em>bodyParser()<em> parses the request body and populates </em>req.body<em>. Another example of this is cookie parsing and session support, we must first </em>use()<em> </em>cookieParser()<em> followed by </em>session()_.</p>525526<p>Many Express applications may contain the line <em>app.use(app.router)</em>, while this may appear strange, it’s simply the middleware function that contains all defined routes, and performs route lookup based on the current request url and HTTP method. Express allows you to position this middleware, though by default it will be added to the bottom. By positioning the router, we can alter middleware precedence, for example we may want to add error reporting as the <em>last</em> middleware so that any exception passed to <em>next()</em> will be handled by it, or perhaps we want static file serving to have low precedence, allowing our routes to intercept requests to a static file to count downloads etc. This may look a little like below</p>527528<pre><code>app.use(express.logger(...));529app.use(express.bodyParser(...));530app.use(express.cookieParser(...));531app.use(express.session(...));532app.use(app.router);533app.use(express.static(...));534app.use(express.errorHandler(...));535</code></pre>536537<p>First we add <em>logger()</em> so that it may wrap node’s <em>req.end()</em> method, providing us with response-time data. Next the request’s body will be parsed (if any), followed by cookie parsing and session support, meaning <em>req.session</em> will be defined by the time we hit our routes in <em>app.router</em>. If a request such as <em>GET /javascripts/jquery.js</em> is handled by our routes, and we do not call <em>next()</em> then the <em>static()</em> middleware will never see this request, however if were to define a route as shown below, we can record stats, refuse downloads, consume download credits etc.</p>538539<pre><code>var downloads = {};540541app.use(app.router);542app.use(express.static(__dirname + '/public'));543544app.get('/*', function(req, res, next){545 var file = req.params[0];546 downloads[file] = downloads[file] || 0;547 downloads[file]++;548 next();549});550</code></pre>551fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 552why, one step back552<h3 id="route-middleware">Route Middleware</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 553–555why, one step back553554<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>555fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 556why, one step back556<p>Typically async data retrieval might look similar to below, where we take the <em>:id</em> parameter, and attempt loading a user.</p>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 557–562why, one step back557558<pre><code>app.get('/user/:id', function(req, res, next){559 loadUser(req.params.id, function(err, user){560 if (err) return next(err);561 res.send('Viewing user ' + user.name);562 });fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 563–564why, one step back563});564</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 565why, one step back565fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 566why, one step back566<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>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 567–580why, one step back567568<pre><code>function loadUser(req, res, next) {569 // You would fetch your user from the db570 var user = users[req.params.id];571 if (user) {572 req.user = user;573 next();574 } else {575 next(new Error('Failed to load user ' + req.params.id));576 }577}578579app.get('/user/:id', loadUser, function(req, res){580 res.send('Viewing user ' + req.user.name);fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 581–582why, one step back581});582</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 583–593why, one step back583584<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>585586<pre><code>function andRestrictToSelf(req, res, next) {587 req.authenticatedUser.id == req.user.id588 ? next()589 : next(new Error('Unauthorized'));590}591592app.get('/user/:id/edit', loadUser, andRestrictToSelf, function(req, res){593 res.send('Editing user ' + req.user.name);fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 594–595why, one step back594});595</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 596–608why, one step back596597<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>598599<pre><code>function andRestrictTo(role) {600 return function(req, res, next) {601 req.authenticatedUser.role == role602 ? next()603 : next(new Error('Unauthorized'));604 }605}606607app.del('/user/:id', loadUser, andRestrictTo('admin'), function(req, res){608 res.send('Deleted user ' + req.user.name);fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 609–610why, one step back609});610</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 611why, one step back611fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 612why, one step back612<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>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 613–622why, one step back613614<pre><code>var a = [middleware1, middleware2]615 , b = [middleware3, middleware4]616 , all = [a, b];617618app.get('/foo', a, function(){});619app.get('/bar', a, function(){});620621app.get('/', a, middleware3, middleware4, function(){});622app.get('/', a, b, function(){});fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 623–624why, one step back623app.get('/', all, function(){});624</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 625–627why, one step back625626<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>627docsTj Holowaychukbefore adoption3588c1e
lines 628–629why, one step back628<p>There are times when we may want to “skip” passed remaining route middleware, but continue matching subsequent routes. To do this we invoke <code>next()</code> with the string “route” <code>next('route')</code>. If no remaining routes match the request url then Express will respond with 404 Not Found.</p>629fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 630why, one step back630<h3 id="http-methods">HTTP Methods</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 631–633why, one step back631632<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>633fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 634why, one step back634<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>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 635–639why, one step back635636<pre><code> <form method="post" action="/">637 <input type="text" name="user[name]" />638 <input type="text" name="user[email]" />639 <input type="submit" value="Submit" />fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 640–641why, one step back640 </form>641</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 642why, one step back642fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 643why, one step back643<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>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 644why, one step back644fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 645–646why, one step back645<pre><code>app.use(express.bodyParser());646</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 647–652why, one step back647648<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>649650<pre><code>app.post('/', function(req, res){651 console.log(req.body.user);652 res.redirect('back');fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 653–654why, one step back653});654</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 655why, one step back655fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 656why, one step back656<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>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 657–658why, one step back657658<pre><code>app.use(express.bodyParser());fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 659–660why, one step back659app.use(express.methodOverride());660</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 661–673why, one step back661662<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>663664<pre><code><form method="post" action="/">665 <input type="hidden" name="_method" value="put" />666 <input type="text" name="user[name]" />667 <input type="text" name="user[email]" />668 <input type="submit" value="Submit" /> 669</form>670671app.put('/', function(){672 console.log(req.body.user);673 res.redirect('back');fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 674–675why, one step back674});675</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 676why, one step back676fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 677why, one step back677<h3 id="error-handling">Error Handling</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 678–688why, one step back678679<p>Express provides the <em>app.error()</em> method which receives exceptions thrown within a route,680or passed to <em>next(err)</em>. Below is an example which serves different pages based on our681ad-hoc <em>NotFound</em> exception:</p>682683<pre><code>function NotFound(msg){684 this.name = 'NotFound';685 Error.call(this, msg);686 Error.captureStackTrace(this, arguments.callee);687}688fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 689why, one step back689NotFound.prototype.__proto__ = Error.prototype;Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 690–696why, one step back690691app.get('/404', function(req, res){692 throw new NotFound;693});694695app.get('/500', function(req, res){696 throw new Error('keyboard cat!');fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 697–698why, one step back697});698</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 699–704why, one step back699700<p>We can call <em>app.error()</em> several times as shown below.701Here we check for an instanceof <em>NotFound</em> and show the702404 page, or we pass on to the next error handler.</p>703704<p>Note that these handlers can be defined anywhere, as theyfixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 705why, one step back705will be placed below the route handlers on <em>listen()</em>. ThisFixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 706–714why, one step back706allows for definition within <em>configure()</em> blocks so we can707handle exceptions in different ways based on the environment.</p>708709<pre><code>app.error(function(err, req, res, next){710 if (err instanceof NotFound) {711 res.render('404.jade');712 } else {713 next(err);714 }fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 715–716why, one step back715});716</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 717–718why, one step back717718<p>Here we assume all errors as 500 for the simplicity offixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 719why, one step back719this 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>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 720–724why, one step back720721<pre><code>app.error(function(err, req, res){722 res.render('500.jade', {723 error: err724 });fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 725–726why, one step back725});726</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 727–728why, one step back727728<p>Our apps could also utilize the Connect <em>errorHandler</em> middlewarefixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 729–730why, one step back729to report on exceptions. For example if we wish to output exceptions730in “development” mode to <em>stderr</em> we can use:</p>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 731why, one step back731fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 732–733why, one step back732<pre><code>app.use(express.errorHandler({ dumpExceptions: true }));733</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 734–737why, one step back734735<p>Also during development we may want fancy html pages to show exceptions736that are passed or thrown, so we can set <em>showStack</em> to true:</p>737fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 738–739why, one step back738<pre><code>app.use(express.errorHandler({ showStack: true, dumpExceptions: true }));739</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 740–743why, one step back740741<p>The <em>errorHandler</em> middleware also responds with <em>json</em> if <em>Accept: application/json</em>742is present, which is useful for developing apps that rely heavily on client-side JavaScript.</p>743fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 744why, one step back744<h3 id="route-param pre-conditions">Route Param Pre-conditions</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 745–752why, one step back745746<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>747748<pre><code>app.get('/user/:userId', function(req, res, next){749 User.get(req.params.userId, function(err, user){750 if (err) return next(err);751 res.send('user ' + user.name);752 });fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 753–754why, one step back753}); 754</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 755–764why, one step back755756<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>757758<pre><code>app.param('userId', function(req, res, next, id){759 User.get(id, function(err, user){760 if (err) return next(err);761 if (!user) return next(new Error('failed to find user'));762 req.user = user;763 next();764 });fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 765–766why, one step back765});766</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 767–771why, one step back767768<p>Doing so, as mentioned drastically improves our route readability, and allows us to easily share this logic throughout our application:</p>769770<pre><code>app.get('/user/:userId', function(req, res){771 res.send('user ' + req.user.name);fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 772–773why, one step back772});773</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 774–776why, one step back774775<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>776fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 777–778why, one step back777<pre><code>app.param('number', function(n){ return parseInt(n, 10); });778</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 779–781why, one step back779780<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>781fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 782–783why, one step back782<pre><code>app.param(['from', 'to'], function(n){ return parseInt(n, 10); });783</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 784why, one step back784fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 785why, one step back785<h3 id="view-rendering">View Rendering</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 786why, one step back786fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 787why, one step back787<p>View filenames take the form “<name>.<engine>”, where <engine> is the nameFixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 788why, one step back788of the module that will be required. For example the view <em>layout.ejs</em> willfixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 789–790why, one step back789tell 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 behaviour790<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>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 791–792why, one step back791792<p>Below is an example using <a href="http://github.com/visionmedia/jade">Jade</a> to render <em>index.html</em>,fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 793why, one step back793and since we do not use <em>layout: false</em> the rendered contents of <em>index.jade</em> will be passed asFixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 794–796why, one step back794the <em>body</em> local variable in <em>layout.jade</em>.</p>795796<pre><code>app.get('/', function(req, res){fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 797–799why, one step back797 res.render('index.jade', { title: 'My Site' });798});799</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 800–803why, one step back800801<p>The new <em>view engine</em> setting allows us to specify our default template engine,802so for example when using jade we could set:</p>803fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 804–805why, one step back804<pre><code>app.set('view engine', 'jade');805</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 806–808why, one step back806807<p>Allowing us to render with:</p>808fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 809–810why, one step back809<pre><code>res.render('index');810</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 811–813why, one step back811812<p>vs:</p>813fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 814–815why, one step back814<pre><code>res.render('index.jade');815</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 816–819why, one step back816817<p>When <em>view engine</em> is set, extensions are entirely optional, however we can still818mix and match template engines:</p>819fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 820–821why, one step back820<pre><code>res.render('another-page.ejs');821</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 822–826why, one step back822823<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>824825<pre><code>app.set('view options', {826 layout: falsefixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 827–828why, one step back827});828</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 829–831why, one step back829830<p>Which can then be overridden within the <em>res.render()</em> call if need be:</p>831fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 832–833why, one step back832<pre><code>res.render('myview.ejs', { layout: true });833</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 834–836why, one step back834835<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>836fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 837–838why, one step back837<pre><code>res.render('page', { layout: 'mylayout' });838</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 839–841why, one step back839840<p>Otherwise we must specify the extension:</p>841fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 842–843why, one step back842<pre><code>res.render('page', { layout: 'mylayout.jade' });843</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 844–846why, one step back844845<p>These paths may also be absolute:</p>846fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 847–848why, one step back847<pre><code>res.render('page', { layout: __dirname + '/../../mylayout.jade' });848</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 849–854why, one step back849850<p>A good example of this is specifying custom <em>ejs</em> opening and closing tags:</p>851852<pre><code>app.set('view options', {853 open: '{{',854 close: '}}'fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 855–856why, one step back855});856</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 857why, one step back857fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 858why, one step back858<h3 id="view-partials">View Partials</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 859why, one step back859fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 860why, one step back860<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 iteratingregenerated docsTj Holowaychukbefore adoptiondd7158a
line 861why, one step back861in a view to display comments, we could use partial collection:</p>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 862why, one step back862fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 863–864why, one step back863<pre><code>partial('comment', { collection: comments });864</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 865why, one step back865regenerated docsTj Holowaychukbefore adoptiondd7158a
line 866why, one step back866<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>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 867why, one step back867fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 868–869why, one step back868<pre><code>partial('comment', comments);869</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 870why, one step back870fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 871why, one step back871<p>When using the partial collection support a few “magic” locals are providedFixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 872–873why, one step back872for free:</p>873fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 874–879why, one step back874<ul>875<li><em>firstInCollection</em> true if this is the first object</li>876<li><em>indexInCollection</em> index of the object in the collection</li>877<li><em>lastInCollection</em> true if this is the last object</li>878<li><em>collectionLength</em> length of the collection</li>879</ul>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 880–881why, one step back880881fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 882–883why, one step back882<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>883Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 884–887why, one step back884<p>For documentation on altering the object name view <a href="http://expressjs.com/guide.html#res-partial-view-options-">res.partial()</a>.</p>885886<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>887fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 888why, one step back888<h3 id="view-lookup">View Lookup</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 889why, one step back889fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 890why, one step back890<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>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 891why, one step back891fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 892why, one step back892<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>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 893why, one step back893fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 894why, one step back894<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>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 895why, one step back895fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 896why, one step back896<h3 id="template-engines">Template Engines</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 897–899why, one step back897898<p>Below are a few template engines commonly used with Express:</p>899fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 900–906why, one step back900<ul>901<li><a href="http://github.com/visionmedia/haml.js">Haml</a> haml implementation</li>902<li><a href="http://jade-lang.com">Jade</a> haml.js successor</li>903<li><a href="http://github.com/visionmedia/ejs">EJS</a> Embedded JavaScript</li>904<li><a href="http://github.com/mauricemach/coffeekup">CoffeeKup</a> CoffeeScript based templating</li>905<li><a href="https://github.com/kof/node-jqtpl">jQuery Templates</a> for node</li>906</ul>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 907–908why, one step back907908fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 909why, one step back909<h3 id="session-support">Session Support</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 910why, one step back910fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 911–912why, one step back911<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>912Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 913why, one step back913<pre><code>app.use(express.cookieParser());fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 914–915why, one step back914app.use(express.session({ secret: "keyboard cat" }));915</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 916–920why, one step back916917<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>918919<pre><code>var RedisStore = require('connect-redis');920app.use(express.cookieParser());fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 921–922why, one step back921app.use(express.session({ secret: "keyboard cat", store: new RedisStore }));922</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 923–946why, one step back923924<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>925926<pre><code>var RedisStore = require('connect-redis');927app.use(express.bodyParser());928app.use(express.cookieParser());929app.use(express.session({ secret: "keyboard cat", store: new RedisStore }));930931app.post('/add-to-cart', function(req, res){932 // Perhaps we posted several items with a form933 // (use the bodyParser() middleware for this)934 var items = req.body.items;935 req.session.items = items;936 res.redirect('back');937});938939app.get('/add-to-cart', function(req, res){940 // When redirected back to GET /add-to-cart941 // we could check req.session.items && req.session.items.length942 // to print out a message943 if (req.session.items && req.session.items.length) {944 req.flash('info', 'You have %s items in your cart', req.session.items.length);945 }946 res.render('shopping-cart');fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 947–948why, one step back947});948</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 949–951why, one step back949950<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>951fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 952why, one step back952<h3 id="migration-guide">Migration Guide</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 953–955why, one step back953954<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>955fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 956why, one step back956<h3 id="req.header()">req.header(key[, defaultValue])</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 957–961why, one step back957958<p>Get the case-insensitive request header <em>key</em>, with optional <em>defaultValue</em>:</p>959960<pre><code>req.header('Host');961req.header('host');fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 962–963why, one step back962req.header('Accept', '*/*');963</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 964–972why, one step back964965<p>The <em>Referrer</em> and <em>Referer</em> header fields are special-cased, either will work:</p>966967<pre><code>// sent Referrer: http://google.com968969req.header('Referer');970// => "http://google.com"971972req.header('Referrer');fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 973–974why, one step back973// => "http://google.com"974</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 975why, one step back975fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 976why, one step back976<h3 id="req.accepts()">req.accepts(type)</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 977–981why, one step back977978<p>Check if the <em>Accept</em> header is present, and includes the given <em>type</em>.</p>979980<p>When the <em>Accept</em> header is not present <em>true</em> is returned. Otherwise981the given <em>type</em> is matched by an exact match, and then subtypes. Youfixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 982–983why, one step back982may pass the subtype such as “html” which is then converted internally983to “text/html” using the mime lookup table.</p>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 984–997why, one step back984985<pre><code>// Accept: text/html986req.accepts('html');987// => true988989// Accept: text/*; application/json990req.accepts('html');991req.accepts('text/html');992req.accepts('text/plain');993req.accepts('application/json');994// => true995996req.accepts('image/png');997req.accepts('png');fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 998–999why, one step back998// => false999</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1000why, one step back1000fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1001why, one step back1001<h3 id="req.is()">req.is(type)</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1002–1016why, one step back10021003<p>Check if the incoming request contains the <em>Content-Type</em>1004header field, and it contains the give mime <em>type</em>.</p>10051006<pre><code> // With Content-Type: text/html; charset=utf-81007 req.is('html');1008 req.is('text/html');1009 // => true10101011 // When Content-Type is application/json1012 req.is('json');1013 req.is('application/json');1014 // => true10151016 req.is('html');fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1017–1018why, one step back1017 // => false1018</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1019–1021why, one step back10191020<p>Ad-hoc callbacks can also be registered with Express, to perform1021assertions again the request, for example if we need an expressivefixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1022why, one step back1022way to check if our incoming request is an image, we can register <em>“an image”</em>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1023–1026why, one step back1023callback:</p>10241025<pre><code> app.is('an image', function(req){1026 return 0 == req.headers['content-type'].indexOf('image');fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1027–1028why, one step back1027 });1028</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1029–1030why, one step back10291030<p>Now within our route callbacks, we can use to to assert content typesfixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1031why, one step back1031such as <em>“image/jpeg”</em>, <em>“image/png”</em>, etc.</p>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1032–1038why, one step back10321033<pre><code> app.post('/image/upload', function(req, res, next){1034 if (req.is('an image')) {1035 // do something1036 } else {1037 next();1038 }fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1039–1040why, one step back1039 });1040</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1041–1044why, one step back10411042<p>Keep in mind this method is <em>not</em> limited to checking <em>Content-Type</em>, you1043can perform any request assertion you wish.</p>1044fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1045why, one step back1045<p>Wildcard matches can also be made, simplifying our example above for <em>“an image”</em>, by asserting the <em>subtype</em> only:</p>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1046why, one step back1046fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1047–1048why, one step back1047<pre><code>req.is('image/*');1048</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1049why, one step back1049fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1050why, one step back1050<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>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1051why, one step back1051fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1052–1053why, one step back1052<pre><code>req.is('*/json');1053</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1054why, one step back1054fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1055why, one step back1055<h3 id="req.param()">req.param(name[, default])</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1056–1058why, one step back10561057<p>Return the value of param <em>name</em> when present or <em>default</em>.</p>1058fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1059–1063why, one step back1059<ul>1060<li>Checks route params (<em>req.params</em>), ex: /user/:id</li>1061<li>Checks query string params (<em>req.query</em>), ex: ?id=12</li>1062<li>Checks urlencoded body params (<em>req.body</em>), ex: id=12</li>1063</ul>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1064why, one step back1064fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1065why, one step back1065Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1066–1069why, one step back1066<p>To utilize urlencoded request bodies, <em>req.body</em>1067should be an object. This can be done by using1068the _express.bodyParser middleware.</p>1069fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1070why, one step back1070<h3 id="req.flash()">req.flash(type[, msg])</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1071–1085why, one step back10711072<p>Queue flash <em>msg</em> of the given <em>type</em>.</p>10731074<pre><code>req.flash('info', 'email sent');1075req.flash('error', 'email delivery failed');1076req.flash('info', 'email re-sent');1077// => 210781079req.flash('info');1080// => ['email sent', 'email re-sent']10811082req.flash('info');1083// => []10841085req.flash();fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1086–1087why, one step back1086// => { error: ['email delivery failed'], info: [] }1087</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1088–1090why, one step back10881089<p>Flash notification message may also utilize formatters, by default only the %s string formatter is available:</p>1090fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1091–1092why, one step back1091<pre><code>req.flash('info', 'email delivery to _%s_ from _%s_ failed.', toUser, fromUser);1092</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1093why, one step back1093fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1094why, one step back1094<h3 id="req.isxmlhttprequest">req.isXMLHttpRequest</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1095–1099why, one step back10951096<p>Also aliased as <em>req.xhr</em>, this getter checks the <em>X-Requested-With</em> header1097to see if it was issued by an <em>XMLHttpRequest</em>:</p>10981099<pre><code>req.xhrfixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1100–1101why, one step back1100req.isXMLHttpRequest1101</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1102why, one step back1102fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1103why, one step back1103<h3 id="res.header()">res.header(key[, val])</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1104–1113why, one step back11041105<p>Get or set the response header <em>key</em>.</p>11061107<pre><code>res.header('Content-Length');1108// => undefined11091110res.header('Content-Length', 123);1111// => 12311121113res.header('Content-Length');fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1114–1115why, one step back1114// => 1231115</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1116why, one step back1116fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1117why, one step back1117<h3 id="res.charset">res.charset</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1118why, one step back1118fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1119why, one step back1119<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>regenerated docsTj Holowaychukbefore adoptiondd7158a
lines 1120–1121why, one step back11201121<pre><code>res.charset = 'ISO-8859-1';fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1122–1123why, one step back1122res.render('users');1123</code></pre>regenerated docsTj Holowaychukbefore adoptiondd7158a
lines 1124–1127why, one step back11241125<p>or before responding with <code>res.send()</code>:</p>11261127<pre><code>res.charset = 'ISO-8859-1';fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1128–1129why, one step back1128res.send(str);1129</code></pre>regenerated docsTj Holowaychukbefore adoptiondd7158a
line 1130why, one step back1130fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1131why, one step back1131<p>or with node’s <code>res.end()</code>:</p>regenerated docsTj Holowaychukbefore adoptiondd7158a
lines 1132–1134why, one step back11321133<pre><code>res.charset = 'ISO-8859-1';1134res.header('Content-Type', 'text/plain');fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1135–1136why, one step back1135res.end(str);1136</code></pre>regenerated docsTj Holowaychukbefore adoptiondd7158a
line 1137why, one step back1137fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1138why, one step back1138<h3 id="res.contenttype()">res.contentType(type)</h3>regenerated docsTj Holowaychukbefore adoptiondd7158a
line 1139why, one step back1139Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1140–1143why, one step back1140<p>Sets the <em>Content-Type</em> response header to the given <em>type</em>.</p>11411142<pre><code> var filename = 'path/to/image.png';1143 res.contentType(filename);fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1144–1145why, one step back1144 // Content-Type is now "image/png"1145</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1146why, one step back1146regenerated docsTj Holowaychukbefore adoptiondd7158a
line 1147why, one step back1147<p>A literal <em>Content-Type</em> works as well:</p>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1148why, one step back1148fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1149–1150why, one step back1149<pre><code> res.contentType('application/json');1150</code></pre>regenerated docsTj Holowaychukbefore adoptiondd7158a
lines 1151–1153why, one step back11511152<p>Or simply the extension without leading <code>.</code>:</p>1153fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1154–1155why, one step back1154<pre><code> res.contentType('json');1155</code></pre>regenerated docsTj Holowaychukbefore adoptiondd7158a
line 1156why, one step back1156fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1157why, one step back1157<h3 id="res.attachment()">res.attachment([filename])</h3>regenerated docsTj Holowaychukbefore adoptiondd7158a
line 1158why, one step back1158fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1159why, one step back1159<p>Sets the <em>Content-Disposition</em> response header to “attachment”, with optional <em>filename</em>.</p>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1160why, one step back1160fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1161–1162why, one step back1161<pre><code> res.attachment('path/to/my/image.png');1162</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1163why, one step back1163fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1164why, one step back1164<h3 id="res.sendfile()">res.sendfile(path[, options[, callback]])</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1165why, one step back1165fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1166why, one step back1166<p>Used by <code>res.download()</code> to transfer an arbitrary file.</p>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1167why, one step back1167fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1168–1169why, one step back1168<pre><code>res.sendfile('path/to/my.file');1169</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1170–1179why, one step back11701171<p>This method accepts an optional callback which is called when1172an 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>11731174<pre><code>res.sendfile(path, function(err){1175 if (err) {1176 next(err);1177 } else {1178 console.log('transferred %s', path);1179 }fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1180–1181why, one step back1180});1181</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1182–1186why, one step back11821183<p>Options may also be passed to the internal <em>fs.createReadStream()</em> call, for example altering the <em>bufferSize</em>:</p>11841185<pre><code>res.sendfile(path, { bufferSize: 1024 }, function(err){1186 // handlefixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1187–1188why, one step back1187});1188</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1189why, one step back1189docsTj Holowaychukbefore adoption3588c1e
line 1190why, one step back1190<h3 id="res.download()">res.download(file[, filename[, callback[, callback2]]])</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1191–1194why, one step back11911192<p>Transfer the given <em>file</em> as an attachment with optional alternative <em>filename</em>.</p>11931194<pre><code>res.download('path/to/image.png');fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1195–1196why, one step back1195res.download('path/to/image.png', 'foo.png');1196</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1197–1200why, one step back11971198<p>This is equivalent to:</p>11991200<pre><code>res.attachment(file);fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1201–1202why, one step back1201res.sendfile(file);1202</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1203why, one step back1203docsTj Holowaychukbefore adoption3588c1e
line 1204why, one step back1204<p>An optional callback may be supplied as either the second or third argument, which is passed to <em>res.sendfile()</em>. Within this callback you may still respond, as the header has not been sent.</p>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1205–1207why, one step back12051206<pre><code>res.download(path, 'expenses.doc', function(err){1207 // handlefixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1208–1209why, one step back1208});1209</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1210why, one step back1210docsTj Holowaychukbefore adoption3588c1e
lines 1211–1219why, one step back1211<p>An optional second callback, <em>callback2</em> may be given to allow you to act on connection related errors, however you should not attempt to respond.</p>12121213<pre><code>res.download(path, function(err){1214 // error or finished1215}, function(err){1216 // connection related error1217});1218</code></pre>1219fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1220why, one step back1220<h3 id="res.send()">res.send(body|status[, headers|status[, status]])</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1221–1230why, one step back12211222<p>The <em>res.send()</em> method is a high level response utility allowing you to pass1223objects to respond with json, strings for html, Buffer instances, or numbers representing the status code. The following are all valid uses:</p>12241225<pre><code> res.send(); // 2041226 res.send(new Buffer('wahoo'));1227 res.send({ some: 'json' });1228 res.send('<p>some html</p>');1229 res.send('Sorry, cant find that', 404);1230 res.send('text', { 'Content-Type': 'text/plain' }, 201);fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1231–1232why, one step back1231 res.send(404);1232</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1233–1237why, one step back12331234<p>By default the <em>Content-Type</em> response header is set, however if explicitly1235assigned through <code>res.send()</code> or previously with <code>res.header()</code> or <code>res.contentType()</code>1236it will not be set again.</p>1237fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1238why, one step back1238<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>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1239why, one step back1239fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1240why, one step back1240<h3 id="res.redirect()">res.redirect(url[, status])</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1241–1247why, one step back12411242<p>Redirect to the given <em>url</em> with a default response <em>status</em> of 302.</p>12431244<pre><code>res.redirect('/', 301);1245res.redirect('/account');1246res.redirect('http://google.com');1247res.redirect('home');fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1248–1249why, one step back1248res.redirect('back');1249</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1250why, one step back1250fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1251why, one step back1251<p>Express supports “redirect mapping”, which by default provides <em>home</em>, and <em>back</em>.Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1252why, one step back1252The <em>back</em> map checks the <em>Referrer</em> and <em>Referer</em> headers, while <em>home</em> utilizesfixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1253why, one step back1253the “home” setting and defaults to “/”.</p>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1254why, one step back1254fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1255why, one step back1255<h3 id="res.cookie()">res.cookie(name, val[, options])</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1256–1259why, one step back12561257<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>12581259<pre><code>// "Remember me" for 15 minutes fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1260–1261why, one step back1260res.cookie('rememberme', 'yes', { expires: new Date(Date.now() + 900000), httpOnly: true });1261</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1262–1264why, one step back12621263<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>1264fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1265–1266why, one step back1265<pre><code>res.cookie('rememberme', 'yes', { maxAge: 900000 });1266</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1267why, one step back1267updated docsTj Holowaychukbefore adoption99b49d2
line 1268why, one step back1268<p>To parse incoming <em>Cookie</em> headers, use the <em>cookieParser</em> middleware, which provides the <em>req.cookies</em> object:</p>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1269–1273why, one step back12691270<pre><code>app.use(express.cookieParser());12711272app.get('/', function(req, res){1273 // use req.cookies.remembermefixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1274–1275why, one step back1274});1275</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1276why, one step back1276docsTj Holowaychukbefore adoption3588c1e
line 1277why, one step back1277<h3 id="res.clearcookie()">res.clearCookie(name[, options])</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1278why, one step back1278fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1279why, one step back1279<p>Clear cookie <em>name</em> by setting “expires” far in the past.</p>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1280why, one step back1280fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1281–1282why, one step back1281<pre><code>res.clearCookie('rememberme');1282</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1283why, one step back1283fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1284why, one step back1284<h3 id="res.render()">res.render(view[, options[, fn]])</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1285–1289why, one step back12851286<p>Render <em>view</em> with the given <em>options</em> and optional callback <em>fn</em>.1287When a callback function is given a response will <em>not</em> be made1288automatically, however otherwise a response of <em>200</em> and <em>text/html</em> is given.</p>1289fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1290why, one step back1290<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>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1291–1292why, one step back12911292<pre><code>var user = { name: 'tj' };fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1293–1294why, one step back1293res.render('index', { layout: false, user: user });1294</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1295why, one step back1295fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1296why, one step back1296<h3 id="res.partial()">res.partial(view[, options])</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1297–1300why, one step back12971298<p>Render <em>view</em> partial with the given <em>options</em>. This method is always available1299to the view as a local variable.</p>1300fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1301–1303why, one step back1301<ul>1302<li><em>object</em> the object named by <em>as</em> or derived from the view name</li>1303<li><p><em>as</em> Variable name for each <em>collection</em> or <em>object</em> value, defaults to the view name.</p>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1304why, one step back1304fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1305–1315why, one step back1305<ul>1306<li>as: ‘something’ will add the <em>something</em> local variable</li>1307<li>as: this will use the collection value as the template context</li>1308<li>as: global will merge the collection value’s properties with <em>locals</em></li>1309</ul>1310</li>1311<li><p><em>collection</em> Array of objects, the name is derived from the view name itself.1312For example <em>video.html</em> will have a object <em>video</em> available to it.</p></li>1313</ul>13141315Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1316–1323why, one step back1316<p>The following are equivalent, and the name of collection value when passed1317to the partial will be <em>movie</em> as derived from the name.</p>13181319<pre><code>partial('theatre/movie.jade', { collection: movies });1320partial('theatre/movie.jade', movies);1321partial('movie.jade', { collection: movies });1322partial('movie.jade', movies);1323partial('movie', movies);fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1324–1325why, one step back1324// In view: movie.director1325</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1326why, one step back1326fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1327why, one step back1327<p>To change the local from <em>movie</em> to <em>video</em> we can use the “as” option:</p>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1328–1329why, one step back13281329<pre><code>partial('movie', { collection: movies, as: 'video' });fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1330–1331why, one step back1330// In view: video.director1331</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1332–1336why, one step back13321333<p>Also we can make our movie the value of <em>this</em> within our view so that instead1334of <em>movie.director</em> we could use <em>this.director</em>.</p>13351336<pre><code>partial('movie', { collection: movies, as: this });fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1337–1338why, one step back1337// In view: this.director1338</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1339why, one step back1339fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1340why, one step back1340<p>Another alternative is to “expand” the properties of the collection item intoFixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1341–1343why, one step back1341pseudo globals (local variables) by using <em>as: global</em>, which again is syntactic sugar:</p>13421343<pre><code>partial('movie', { collection: movies, as: global });fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1344–1345why, one step back1344// In view: director1345</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1346–1358why, one step back13461347<p>This same logic applies to a single partial object usage:</p>13481349<pre><code>partial('movie', { object: movie, as: this });1350// In view: this.director13511352partial('movie', { object: movie, as: global });1353// In view: director13541355partial('movie', { object: movie, as: 'video' });1356// In view: video.director13571358partial('movie', { object: movie });fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1359–1360why, one step back1359// In view: movie.director1360</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1361why, one step back1361fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1362why, one step back1362<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>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1363why, one step back1363regenerated docsTj Holowaychukbefore adoptiondd7158a
lines 1364–1365why, one step back1364<pre><code>var movie = new Movie('Nightmare Before Christmas', 'Tim Burton')1365partial('movie', movie)fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1366–1367why, one step back1366// => In view: movie.director1367</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1368why, one step back1368fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1369why, one step back1369<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>regenerated docsTj Holowaychukbefore adoptiondd7158a
lines 1370–1371why, one step back13701371<pre><code>var movie = { title: 'Nightmare Before Christmas', director: 'Tim Burton' }; fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1372–1373why, one step back1372partial('movie', movie)1373</code></pre>regenerated docsTj Holowaychukbefore adoptiondd7158a
lines 1374–1378why, one step back13741375<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>13761377<pre><code> partial('movie', { locals: { movie: movie }})1378 partial('movie', { movie: movie })fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1379–1380why, one step back1379 partial('movie', { object: movie })1380</code></pre>regenerated docsTj Holowaychukbefore adoptiondd7158a
line 1381why, one step back1381Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1382–1394why, one step back1382<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>13831384<pre><code>app.get('/users', function(req, res){1385 if (req.xhr) {1386 // respond with the each user in the collection1387 // passed to the "user" view1388 res.partial('user', users);1389 } else {1390 // respond with layout, and users page1391 // which internally does partial('user', users)1392 // along with other UI1393 res.render('users', { users: users });1394 }fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1395–1396why, one step back1395});1396</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1397why, one step back1397fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1398why, one step back1398<h3 id="res.local()">res.local(name[, val])</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1399–1412why, one step back13991400<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>14011402<pre><code> app.all('/movie/:id', function(req, res, next){1403 Movie.get(req.params.id, function(err, movie){1404 // Assigns res.locals.movie = movie1405 res.local('movie', movie);1406 });1407 });14081409 app.get('/movie/:id', function(req, res){1410 // movie is already a local, however we1411 // can pass more if we wish1412 res.render('movie', { displayReviews: true });fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1413–1414why, one step back1413 });1414</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1415why, one step back1415fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1416why, one step back1416<h3 id="res.locals()">res.locals(obj)</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1417why, one step back1417regenerated docsTj Holowaychukbefore adoptiondd7158a
lines 1418–1422why, one step back1418<p> Assign several locals with the given <em>obj</em>. The following are equivalent:</p>14191420<pre><code> res.local('foo', bar);1421 res.local('bar', baz);1422fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1423–1424why, one step back1423 res.locals({ foo: bar, bar, baz });1424</code></pre>regenerated docsTj Holowaychukbefore adoptiondd7158a
line 1425why, one step back1425fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1426why, one step back1426<h3 id="app.set()">app.set(name[, val])</h3>regenerated docsTj Holowaychukbefore adoptiondd7158a
line 1427why, one step back1427Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1428–1432why, one step back1428<p>Apply an application level setting <em>name</em> to <em>val</em>, or1429get the value of <em>name</em> when <em>val</em> is not present:</p>14301431<pre><code>app.set('views', __dirname + '/views');1432app.set('views');fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1433–1434why, one step back1433// => ...path...1434</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1435–1438why, one step back14351436<p>Alternatively you may simply access the settings via <em>app.settings</em>:</p>14371438<pre><code>app.settings.viewsfixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1439–1440why, one step back1439// => ...path...1440</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1441why, one step back1441fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1442why, one step back1442<h3 id="app.enable()">app.enable(name)</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1443–1450why, one step back14431444<p>Enable the given setting <em>name</em>:</p>14451446<pre><code>app.enable('some arbitrary setting');1447app.set('some arbitrary setting');1448// => true14491450app.enabled('some arbitrary setting');fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1451–1452why, one step back1451// => true1452</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1453why, one step back1453fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1454why, one step back1454<h3 id="app.enabled()">app.enabled(name)</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1455–1462why, one step back14551456<p>Check if setting <em>name</em> is enabled:</p>14571458<pre><code>app.enabled('view cache');1459// => false14601461app.enable('view cache');1462app.enabled('view cache');fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1463–1464why, one step back1463// => true1464</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1465why, one step back1465fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1466why, one step back1466<h3 id="app.disable()">app.disable(name)</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1467–1474why, one step back14671468<p>Disable the given setting <em>name</em>:</p>14691470<pre><code>app.disable('some setting');1471app.set('some setting');1472// => false14731474app.disabled('some setting');fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1475–1476why, one step back1475// => false1476</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1477why, one step back1477fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1478why, one step back1478<h3 id="app.disabled()">app.disabled(name)</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1479–1488why, one step back14791480<p>Check if setting <em>name</em> is disabled:</p>14811482<pre><code>app.enable('view cache');14831484app.disabled('view cache');1485// => false14861487app.disable('view cache');1488app.disabled('view cache');fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1489–1490why, one step back1489// => true1490</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1491why, one step back1491fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1492why, one step back1492<h3 id="app.configure()">app.configure(env|function[, function])</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1493–1501why, one step back14931494<p>Define a callback function for the given <em>env</em> (or all environments) with callback <em>function</em>:</p>14951496<pre><code>app.configure(function(){1497 // executed for each env1498});14991500app.configure('development', function(){1501 // executed for 'development' onlyfixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1502–1503why, one step back1502});1503</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1504why, one step back1504fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1505why, one step back1505<h3 id="app.redirect()">app.redirect(name, val)</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1506–1508why, one step back15061507<p>For use with <em>res.redirect()</em> we can map redirects at the application level as shown below:</p>1508fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1509–1510why, one step back1509<pre><code>app.redirect('google', 'http://google.com');1510</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1511–1513why, one step back15111512<p>Now in a route we may call:</p>1513fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1514why, one step back1514<p> res.redirect(‘google’);</p>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1515–1519why, one step back15151516<p>We may also map dynamic redirects:</p>15171518<pre><code>app.redirect('comments', function(req, res){1519 return '/post/' + req.params.id + '/comments';fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1520–1521why, one step back1520});1521</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1522–1528why, one step back15221523<p>So now we may do the following, and the redirect will dynamically adjust to1524the context of the request. If we called this route with <em>GET /post/12</em> our1525redirect <em>Location</em> would be <em>/post/12/comments</em>.</p>15261527<pre><code>app.get('/post/:id', function(req, res){1528 res.redirect('comments');fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1529–1530why, one step back1529});1530</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1531–1533why, one step back15311532<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>1533fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1534–1535why, one step back1534<pre><code>res.redirect('/posts');1535</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1536why, one step back1536fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1537why, one step back1537<h3 id="app.error()">app.error(function)</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1538–1544why, one step back15381539<p>Adds an error handler <em>function</em> which will receive the exception as the first parameter as shown below.1540Note that we may set several error handlers by making several calls to this method, however the handler1541should call <em>next(err)</em> if it does not wish to deal with the exception:</p>15421543<pre><code>app.error(function(err, req, res, next){1544 res.send(err.message, 500);fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1545–1546why, one step back1545});1546</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1547why, one step back1547fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1548why, one step back1548<h3 id="app.helpers()">app.helpers(obj)</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1549–1551why, one step back15491550<p>Registers static view helpers.</p>1551updated docsTj Holowaychukbefore adoption99b49d2
lines 1552–1556why, one step back1552<pre><code>app.helpers({1553 name: function(first, last){ return first + ', ' + last }1554 , firstName: 'tj'1555 , lastName: 'holowaychuk'1556});fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1557–1558why, one step back1557</code></pre>1558Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1559–1561why, one step back1559<p>Our view could now utilize the <em>firstName</em> and <em>lastName</em> variables,1560as well as the <em>name()</em> function exposed.</p>1561fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1562–1563why, one step back1562<pre><code><%= name(firstName, lastName) %>1563</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1564why, one step back1564updated docsTj Holowaychukbefore adoption99b49d2
lines 1565–1568why, one step back1565<p>Express also provides a few locals by default:</p>15661567<pre><code>- `settings` the app's settings object1568- `filename` the view's filenamedocsTj Holowaychukbefore adoption3588c1e
line 1569why, one step back1569- `layout(path)` specify the layout from within a viewupdated docsTj Holowaychukbefore adoption99b49d2
lines 1570–1573why, one step back1570</code></pre>15711572<p>This method is aliased as <em>app.locals()</em>.</p>1573fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1574why, one step back1574<h3 id="app.dynamichelpers()">app.dynamicHelpers(obj)</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1575–1584why, one step back15751576<p>Registers dynamic view helpers. Dynamic view helpers1577are simply functions which accept <em>req</em>, <em>res</em>, and are1578evaluated against the <em>Server</em> instance before a view is rendered. The <em>return value</em> of this function1579becomes the local variable it is associated with.</p>15801581<pre><code>app.dynamicHelpers({1582 session: function(req, res){1583 return req.session;1584 }fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1585–1586why, one step back1585});1586</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1587–1589why, one step back15871588<p>All views would now have <em>session</em> available so that session data can be accessed via <em>session.name</em> etc:</p>1589fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1590why, one step back1590<pre><code><%= session.name %>updated docsTj Holowaychukbefore adoption99b49d2
lines 1591–1694why, one step back1591</code></pre>15921593<h3 id="app.lookup">app.lookup</h3>15941595<p> The <em>app.lookup</em> http methods returns an array of callback functions1596 associated with the given <em>path</em>.</p>15971598<p> Suppose we define the following routes:</p>15991600<pre><code> app.get('/user/:id', function(){});1601 app.put('/user/:id', function(){});1602 app.get('/user/:id/:op?', function(){});1603</code></pre>16041605<p> We can utilize this lookup functionality to check which routes1606 have been defined, which can be extremely useful for higher level1607 frameworks built on Express.</p>16081609<pre><code> app.lookup.get('/user/:id');1610 // => [Function]16111612 app.lookup.get('/user/:id/:op?');1613 // => [Function]16141615 app.lookup.put('/user/:id');1616 // => [Function]16171618 app.lookup.all('/user/:id');1619 // => [Function, Function]16201621 app.lookup.all('/hey');1622 // => []1623</code></pre>16241625<p> To alias <em>app.lookup.VERB()</em>, we can simply invoke <em>app.VERB()</em>1626 without a callback, as a shortcut, for example the following are1627 equivalent:</p>16281629<pre><code> app.lookup.get('/user');1630 app.get('/user');1631</code></pre>16321633<p> Each function returned has the following properties:</p>16341635<pre><code> var fn = app.get('/user/:id/:op?')[0];16361637 fn.regexp1638 // => /^\/user\/(?:([^\/]+?))(?:\/([^\/]+?))?\/?$/i16391640 fn.keys1641 // => ['id', 'op']16421643 fn.path1644 // => '/user/:id/:op?'16451646 fn.method1647 // => 'GET'1648</code></pre>16491650<h3 id="app.match">app.match</h3>16511652<p> The <em>app.match</em> http methods return an array of callback functions1653 which match the given <em>url</em>, which may include a query string etc. This1654 is useful when you want reflect on which routes have the opportunity to1655 respond.</p>16561657<p> Suppose we define the following routes:</p>16581659<pre><code> app.get('/user/:id', function(){});1660 app.put('/user/:id', function(){});1661 app.get('/user/:id/:op?', function(){});1662</code></pre>16631664<p> Our match against <strong>GET</strong> will return two functions, since the <em>:op</em>1665 in our second route is optional.</p>16661667<pre><code> app.match.get('/user/1');1668 // => [Function, Function]1669</code></pre>16701671<p> This second call returns only the callback for <em>/user/:id/:op?</em>.</p>16721673<pre><code> app.match.get('/user/23/edit');1674 // => [Function]1675</code></pre>16761677<p> We can also use <em>all()</em> to disregard the http method:</p>16781679<pre><code> app.match.all('/user/20');1680 // => [Function, Function, Function]1681</code></pre>16821683<p> Each function matched has the following properties:</p>16841685<pre><code> var fn = app.match.get('/user/23/edit')[0];16861687 fn.keys1688 // => ['id', 'op']16891690 fn.params1691 // => { id: '23', op: 'edit' }16921693 fn.method1694 // => 'GET'fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1695why, one step back1695</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1696why, one step back1696fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1697why, one step back1697<h3 id="app.mounted()">app.mounted(fn)</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1698–1708why, one step back16981699<p>Assign a callback <em>fn</em> which is called when this <em>Server</em> is passed to <em>Server#use()</em>.</p>17001701<pre><code>var app = express.createServer(),1702 blog = express.createServer();17031704blog.mounted(function(parent){1705 // parent is app1706 // "this" is blog1707});1708fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1709–1710why, one step back1709app.use(blog);1710</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1711why, one step back1711fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1712why, one step back1712<h3 id="app.register()">app.register(ext, exports)</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1713–1714why, one step back17131714<p>Register the given template engine <em>exports</em>fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1715why, one step back1715as <em>ext</em>. For example we may wish to map “.html”Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1716–1717why, one step back1716files to jade:</p>1717fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1718–1719why, one step back1718<pre><code> app.register('.html', require('jade'));1719</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1720–1722why, one step back17201721<p>This is also useful for libraries that may not1722match extensions correctly. For example my haml.jsfixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1723–1724why, one step back1723library is installed from npm as “hamljs” so instead1724of layout.hamljs, we can register the engine as “.haml”:</p>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1725why, one step back1725fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1726–1727why, one step back1726<pre><code> app.register('.haml', require('haml-js'));1727</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1728–1732why, one step back17281729<p>For engines that do not comply with the Express1730specification, we can also wrap their api this way. Below1731we map <em>.md</em> to render markdown files, rendering the html once1732since it will not change on subsequent calls, and support local substitutionfixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1733why, one step back1733in the form of “{name}”.</p>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1734–1743why, one step back17341735<pre><code> app.register('.md', {1736 compile: function(str, options){1737 var html = md.toHTML(str);1738 return function(locals){1739 return html.replace(/\{([^}]+)\}/g, function(_, name){1740 return locals[name];1741 });1742 };1743 }fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1744–1745why, one step back1744 });1745</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
line 1746why, one step back1746fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1747why, one step back1747<h3 id="app.listen()">app.listen([port[, host]])</h3>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1748–1749why, one step back17481749<p>Bind the app server to the given <em>port</em>, which defaults to 3000. When <em>host</em> is omitted allfixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
line 1750why, one step back1750connections will be accepted via <em>INADDR_ANY</em>.</p>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1751–1753why, one step back17511752<pre><code>app.listen();1753app.listen(3000);fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1754–1755why, one step back1754app.listen(3000, 'n.n.n.n');1755</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1756–1758why, one step back17561757<p>The <em>port</em> argument may also be a string representing the path to a unix domain socket:</p>1758fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1759–1760why, one step back1759<pre><code>app.listen('/tmp/express.sock');1760</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1761–1770why, one step back17611762<p>Then try it out:</p>17631764<pre><code>$ telnet /tmp/express.sock1765GET / HTTP/1.117661767HTTP/1.1 200 OK1768Content-Type: text/plain1769Content-Length: 111770fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a
lines 1771–1772why, one step back1771Hello World1772</code></pre>Fixed express(1) default test charset. Cloeses #579 [reported by secoif]Tj Holowaychukbefore adoptiond867cc9
lines 1773–1776why, one step back1773 </div>1774 </div>1775 </body>1776</html>