docs
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent 9d1b3f5 · Browse files · 3 files·+197 added−134 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+197 added−134 removed
- other:+197 added−134 removed
Provenance
- Commit
96327c9- Tree
abc8570- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
9d1b3f5
3 files changed+197 added−134 removed
modified:docs/guide.1+80 added-47 removed
922 unmodified lines923 .924 .IP "" 0925 .926 .SS "View Partials"927-The Express view system has built\-in support for partials and collections, which are sort of "mini" views representing a document fragment\. For example rather than iterating in a view to display comments, we would use a partial with collection support:927+The Express view system has built\-in support for partials and collections, which are "mini" views representing a document fragment\. For example rather than iterating in a view to display comments, we would use a partial with collection support:928 .929 .IP "" 4930 .931 .nf932 933-partial(\'comment\.haml\', { collection: comments });934-.935-.fi936-.937-.IP "" 0938-.939-.P940-To make things even less verbose we can assume the extension as \fI\.haml\fR when omitted, however if we wished we could use an ejs partial, within a haml view for example\.941-.942-.IP "" 4943-.944-.nf945-933 partial(\'comment\', { collection: comments });934 .935 .fi936 .937 .IP "" 0938 .939 .P953-And once again even further, when rendering a collection we can simply pass an array, if no other options are desired:940+If no other options are desired, we can omit the object and simply pass our array, which is equivalent to above:941 .942 .IP "" 4943 .944 .nf7 unmodified lines952 .P953 When using the partial collection support a few "magic" variables are provided for free:954 .955 .IP "\(bu" 4969-\fIfirstInCollection\fR True if this is the first object956+\fIfirstInCollection\fR true if this is the first object957 .958 .IP "\(bu" 4972-\fIindexInCollection\fR Index of the object in the collection959+\fIindexInCollection\fR index of the object in the collection960 .961 .IP "\(bu" 4975-\fIlastInCollection\fR True if this is the last object962+\fIlastInCollection\fR true if this is the last object963 .964 .IP "\(bu" 4978-\fIcollectionLength\fR Length of the collection965+\fIcollectionLength\fR length of the collection966 .967 .IP "" 0968 .969 .P970 For documentation on altering the object name view res\.partial() \fIhttp://expressjs\.com/guide\.html#res\-partial\-view\-options\-\fR\.971 .972 .P986-NOTE: partials are not recommended for large collections (150+) because the view system adds to the overhead\. For example do \fInot\fR implement a simple ul list with partial collection support, simply create a partial and iterate within that single partial, as this is far more efficient than rendering 150+ templates\.973+NOTE: 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\.974 .975 .SS "Template Engines"976 Below are a few template engines commonly used with Express:977 .978 .IP "\(bu" 4979 Jade \fIhttp://jade\-lang\.com\fR haml\.js successor980 .981 .IP "\(bu" 4995-Haml \fIhttp://github\.com/visionmedia/haml\.js\fR pythonic indented templates996-.997-.IP "\(bu" 4982 EJS \fIhttp://github\.com/visionmedia/ejs\fR Embedded JavaScript983 .984 .IP "\(bu" 4985 CoffeeKup \fIhttp://github\.com/mauricemach/coffeekup\fR CoffeeScript based templating3 unmodified lines989 .990 .IP "" 0991 .992 .SS "Session Support"1009-Sessions support can be added by using Connect\'s \fIsession\fR middleware\. To do so we also need the \fIcookieDecoder\fR middleware place above it, which will parse and populate cookie data to \fIreq\.cookies\fR\.993+Sessions support can be added by using Connect\'s \fIsession\fR middleware\. To do so we also need the \fIcookieParser\fR middleware place above it, which will parse and populate cookie data to \fIreq\.cookies\fR\.994 .995 .IP "" 4996 .997 .nf998 1015-app\.use(express\.cookieDecoder());1016-app\.use(express\.session());999+app\.use(express\.cookieParser());1000+app\.use(express\.session({ secret: "keyboard cat" }));1001 .1002 .fi1003 .1004 .IP "" 05 unmodified lines1010 .1011 .nf1012 1013 var RedisStore = require(\'connect\-redis\');1030-app\.use(express\.cookieDecoder());1031-app\.use(express\.session({ store: new RedisStore }));1014+app\.use(express\.cookieParser());1015+app\.use(express\.session({ secret: "keyboard cat", store: new RedisStore }));1016 .1017 .fi1018 .1019 .IP "" 05 unmodified lines1025 .1026 .nf1027 1028 var RedisStore = require(\'connect\-redis\');1045-app\.use(express\.bodyDecoder());1046-app\.use(express\.cookieDecoder());1047-app\.use(express\.session({ store: new RedisStore }));1029+app\.use(express\.bodyParser());1030+app\.use(express\.cookieParser());1031+app\.use(express\.session({ secret: "keyboard cat", store: new RedisStore }));1032 1033 app\.post(\'/add\-to\-cart\', function(req, res){1034 // Perhaps we posted several items with a form1051- // (use the bodyDecoder() middleware for this)1035+ // (use the bodyParser() middleware for this)1036 var items = req\.body\.items;1037 req\.session\.items = items;1038 res\.redirect(\'back\');1039 });12 unmodified lines1052 .1053 .IP "" 01054 .1055 .P1072-The \fIreq\.session\fR object also has methods such as \fISession#touch()\fR, \fISession#destroy()\fR, \fISession#regenerate()\fR among others to maintain and manipulate sessions\. For more information view the Connect Session \fIhttp://senchalabs\.github\.com/connect/session\.html\fR documentation\.1056+The \fIreq\.session\fR object also has methods such as \fISession#touch()\fR, \fISession#destroy()\fR, \fISession#regenerate()\fR among others to maintain and manipulate sessions\. For more information view the Connect Session \fIhttp://senchalabs\.github\.com/connect/middleware\-session\.html\fR documentation\.1057 .1058 .SS "Migration Guide"1075-Pre\-beta Express developers may reference the Migration Guide \fImigrate\.html\fR to get up to speed on how to upgrade your application\.1059+Express 1\.x developers may reference the Migration Guide \fImigrate\.html\fR to get up to speed on how to upgrade your application\.1060 .1061 .SS "req\.header(key[, defaultValue])"1062 Get the case\-insensitive request header \fIkey\fR, with optional \fIdefaultValue\fR:1063 .8 unmodified lines1072 .fi1073 .1074 .IP "" 01075 .1076+.P1077+The \fIReferrer\fR and \fIReferer\fR header fields are special\-cased, either will work:1078+.1079+.IP "" 41080+.1081+.nf1082+1083+// sent Referrer: http://google\.com1084+1085+req\.header(\'Referer\');1086+// => "http://google\.com"1087+1088+req\.header(\'Referrer\');1089+// => "http://google\.com"1090+.1091+.fi1092+.1093+.IP "" 01094+.1095 .SS "req\.accepts(type)"1096 Check if the \fIAccept\fR header is present, and includes the given \fItype\fR\.1097 .1098 .P112 unmodified lines1211 .SS "req\.param(name[, default])"1212 Return the value of param \fIname\fR when present or \fIdefault\fR\.1213 .1214 .IP "\(bu" 41212-Checks route placeholders (\fIreq\.params\fR), ex: /user/:id1215+Checks route params (\fIreq\.params\fR), ex: /user/:id1216 .1217 .IP "\(bu" 41218 Checks query string params (\fIreq\.query\fR), ex: ?id=121219 .2 unmodified lines1222 .1223 .IP "" 01224 .1225 .P1223-To utilize urlencoded request bodies, \fIreq\.body\fR should be an object\. This can be done by using the \fIexpress\.bodyDecoder\fR middleware\.1226+To utilize urlencoded request bodies, \fIreq\.body\fR should be an object\. This can be done by using the _express\.bodyParser middleware\.1227 .1228 .SS "req\.flash(type[, msg])"1229 Queue flash \fImsg\fR of the given \fItype\fR\.1230 .74 unmodified lines1305 .nf1306 1307 var filename = \'path/to/image\.png\';1308 res\.contentType(filename);1306- // res\.headers[\'Content\-Type\'] is now "image/png"1309+ // Content\-Type is now "image/png"1310 .1311 .fi1312 .1313 .IP "" 010 unmodified lines1324 .fi1325 .1326 .IP "" 01327 .1325-.SS "res\.sendfile(path)"1328+.SS "res\.sendfile(path[, options[, callback]])"1329 Used by \fBres\.download()\fR to transfer an arbitrary file\.1330 .1331 .IP "" 41332 .5 unmodified lines1338 .1339 .IP "" 01340 .1341 .P1339-This method accepts a callback which when given will be called on an exception, as well as when the transfer has completed\. When a callback is not given, and the file has \fBnot\fR been streamed, \fInext(err)\fR will be called on an exception\.1342+This method accepts an optional callback which is called when an error occurs, or when the transfer is complete\. By default failures call \fBnext(err)\fR, however when a callback is supplied you must do this explicitly, or act on the error\.1343 .1344 .IP "" 41345 .1346 .nf1347 1345-res\.sendfile(path, function(err, path){1348+res\.sendfile(path, function(err){1349 if (err) {1347- // handle the error1350+ next(err);1351 } else {1352 console\.log(\'transferred %s\', path);1353 }1354 });1 unmodified line1356 .fi1357 .1358 .IP "" 01359 .1360+.P1361+Options may also be passed to the internal \fIfs\.createReadStream()\fR call, for example altering the \fIbufferSize\fR:1362+.1363+.IP "" 41364+.1365+.nf1366+1367+res\.sendfile(path, { bufferSize: 1024 }, function(err){1368+ // handle1369+});1370+.1371+.fi1372+.1373+.IP "" 01374+.1357-.SS "res\.download(file[, filename])"1375+.SS "res\.download(file[, filename[, callback]])"1376 Transfer the given \fIfile\fR as an attachment with optional alternative \fIfilename\fR\.1377 .1378 .IP "" 41379 .19 unmodified lines1399 .fi1400 .1401 .IP "" 01402 .1403+.P1404+An optional callback may be supplied as either the second or third argument, which is passed to \fIres\.sendfile()\fR:1405+.1406+.IP "" 41407+.1408+.nf1409+1410+res\.download(path, \'expenses\.doc\', function(err){1411+ // handle1412+});1413+.1414+.fi1415+.1416+.IP "" 01417+.1418 .SS "res\.send(body|status[, headers|status[, status]])"1386-The \fBres\.send()\fR method is a high level response utility allowing you to pass objects to respond with json, strings for html, arbitrary _Buffer_s or numbers for status code based responses\. The following are all valid uses:1419+The \fIres\.send()\fR method is a high level response utility allowing you to pass objects to respond with json, strings for html, Buffer instances, or numbers representing the status code\. The following are all valid uses:1420 .1421 .IP "" 41422 .1423 .nf13 unmodified lines1437 .P1438 By default the \fIContent\-Type\fR response header is set, however if explicitly assigned through \fBres\.send()\fR or previously with \fBres\.header()\fR or \fBres\.contentType()\fR it will not be set again\.1439 .1440 .P1408-Note that this method \fIend()\fR the response, so you will want to use node\'s \fIres\.writeHead()\fR / \fIres\.write()\fR for multiple writes or streaming\.1441+Note that this method \fIend()\fRs the response, so you will want to use node\'s \fIres\.write()\fR for multiple writes or streaming\.1442 .1443 .SS "res\.redirect(url[, status])"1444 Redirect to the given \fIurl\fR with a default response \fIstatus\fR of 302\.1445 .14 unmodified lines1460 .P1461 Express supports "redirect mapping", which by default provides \fIhome\fR, and \fIback\fR\. The \fIback\fR map checks the \fIReferrer\fR and \fIReferer\fR headers, while \fIhome\fR utilizes the "home" setting and defaults to "/"\.1462 .1463 .SS "res\.cookie(name, val[, options])"1431-Sets the given cookie \fIname\fR to \fIval\fR, with \fIoptions\fR such as "httpOnly: true", "expires", "secure" etc\.1464+Sets the given cookie \fIname\fR to \fIval\fR, with options \fIhttpOnly\fR, \fIsecure\fR, \fIexpires\fR etc\.1465 .1466 .IP "" 41467 .1468 .nf11 unmodified lines1480 .IP "" 41481 .1482 .nf1483 1451-app\.use(express\.cookieDecoder());1484+app\.use(express\.cookieParser());1485 1486 app\.get(\'/\', function(req, res){1454- // use req\.cookies\.rememberme1487+ // use req\.cookies\.rememberme1488 });1489 .1490 .fi1491 .506 unmodified linesmodified:docs/guide.html+60 added-44 removed
206 unmodified lines207 <li><a href="#" class="toggle">+</a> <a class="section-title" href="#">Response</a><ul class="section" id="section-Response">208 <li><a href="#res-header-key-val-">header()</a></li>209 <li><a href="#res-contentType-type-">contentType()</a></li>210 <li><a href="#res-attachment-filename-">attachment()</a></li>211-<li><a href="#res-sendfile-path-">sendfile()</a></li>212-<li><a href="#res-download-file-filename-">download()</a></li>211+<li><a href="#res-sendfile-path-options-callback-">sendfile()</a></li>212+<li><a href="#res-download-file-filename-callback-">download()</a></li>213 <li><a href="#res-send-body-status-headers-status-status-">send()</a></li>214 <li><a href="#res-redirect-url-status-">redirect()</a></li>215 <li><a href="#res-cookie-name-val-options-">cookie()</a></li>216 <li><a href="#res-clearCookie-name-">clearCookie()</a></li>605 unmodified lines822 </code></pre>823 824 <h3 id="View-Partials">View Partials</h3>825 826-<p>The Express view system has built-in support for partials and collections, which are827-sort of "mini" views representing a document fragment. For example rather than iterating826+<p>The Express view system has built-in support for partials and collections, which are "mini" views representing a document fragment. For example rather than iterating827 in a view to display comments, we would use a partial with collection support:</p>828 830-<pre><code>partial('comment.haml', { collection: comments });831-</code></pre>832-833-<p>To make things even less verbose we can assume the extension as <em>.haml</em> when omitted,834-however if we wished we could use an ejs partial, within a haml view for example.</p>835-829 <pre><code>partial('comment', { collection: comments });830 </code></pre>831 839-<p>And once again even further, when rendering a collection we can simply pass840-an array, if no other options are desired:</p>832+<p>If no other options are desired, we can omit the object and simply pass our array, which is equivalent to above:</p>833 834 <pre><code>partial('comment', comments);835 </code></pre>836 837 <p>When using the partial collection support a few "magic" variables are provided838 for free:</p>839 840 <ul>849-<li><em>firstInCollection</em> True if this is the first object</li>850-<li><em>indexInCollection</em> Index of the object in the collection</li>851-<li><em>lastInCollection</em> True if this is the last object</li>852-<li><em>collectionLength</em> Length of the collection</li>841+<li><em>firstInCollection</em> true if this is the first object</li>842+<li><em>indexInCollection</em> index of the object in the collection</li>843+<li><em>lastInCollection</em> true if this is the last object</li>844+<li><em>collectionLength</em> length of the collection</li>845 </ul>846 847 848 <p>For documentation on altering the object name view <a href="http://expressjs.com/guide.html#res-partial-view-options-">res.partial()</a>.</p>849 858-<p>NOTE: partials are not recommended for large collections (150+) because the view system adds to the overhead. For example do <em>not</em> implement a simple ul list with partial collection support, simply create a partial and iterate within that single partial, as this is far more efficient than rendering 150+ templates.</p>850+<p>NOTE: 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>851 852 <h3 id="Template-Engines">Template Engines</h3>853 854 <p>Below are a few template engines commonly used with Express:</p>855 856 <ul>857 <li><a href="http://jade-lang.com">Jade</a> haml.js successor</li>866-<li><a href="http://github.com/visionmedia/haml.js">Haml</a> pythonic indented templates</li>858 <li><a href="http://github.com/visionmedia/ejs">EJS</a> Embedded JavaScript</li>859 <li><a href="http://github.com/mauricemach/coffeekup">CoffeeKup</a> CoffeeScript based templating</li>860 <li><a href="https://github.com/kof/node-jqtpl">jQuery Templates</a> for node</li>861 </ul>862 863 864 <h3 id="Session-Support">Session Support</h3>865 875-<p>Sessions support can be added by using Connect's <em>session</em> middleware. To do so we also need the <em>cookieDecoder</em> middleware place above it, which will parse and populate cookie data to <em>req.cookies</em>.</p>866+<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>867 877-<pre><code>app.use(express.cookieDecoder());878-app.use(express.session());868+<pre><code>app.use(express.cookieParser());869+app.use(express.session({ secret: "keyboard cat" }));870 </code></pre>871 872 <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>873 874 <pre><code>var RedisStore = require('connect-redis');884-app.use(express.cookieDecoder());885-app.use(express.session({ store: new RedisStore }));875+app.use(express.cookieParser());876+app.use(express.session({ secret: "keyboard cat", store: new RedisStore }));877 </code></pre>878 879 <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>880 881 <pre><code>var RedisStore = require('connect-redis');891-app.use(express.bodyDecoder());892-app.use(express.cookieDecoder());893-app.use(express.session({ store: new RedisStore }));882+app.use(express.bodyParser());883+app.use(express.cookieParser());884+app.use(express.session({ secret: "keyboard cat", store: new RedisStore }));885 886 app.post('/add-to-cart', function(req, res){887 // Perhaps we posted several items with a form897- // (use the bodyDecoder() middleware for this)888+ // (use the bodyParser() middleware for this)889 var items = req.body.items;890 req.session.items = items;891 res.redirect('back');892 });8 unmodified lines901 res.render('shopping-cart');902 });903 </code></pre>904 914-<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/session.html">Connect Session</a> documentation.</p>905+<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>906 907 <h3 id="Migration-Guide">Migration Guide</h3>908 918-<p> Pre-beta Express developers may reference the <a href="migrate.html">Migration Guide</a> to get up to speed on how to upgrade your application.</p>909+<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.</p>910 911 <h3 id="req-header-key-defaultValue-">req.header(key[, defaultValue])</h3>912 913 <p>Get the case-insensitive request header <em>key</em>, with optional <em>defaultValue</em>:</p>2 unmodified lines916 req.header('host');917 req.header('Accept', '*/*');918 </code></pre>919 920+<p>The <em>Referrer</em> and <em>Referer</em> header fields are special-cased, either will work:</p>921+922+<pre><code>// sent Referrer: http://google.com923+924+req.header('Referer');925+// => "http://google.com"926+927+req.header('Referrer');928+// => "http://google.com"929+</code></pre>930+931 <h3 id="req-accepts-type-">req.accepts(type)</h3>932 933 <p>Check if the <em>Accept</em> header is present, and includes the given <em>type</em>.</p>934 76 unmodified lines1011 1012 <p>Return the value of param <em>name</em> when present or <em>default</em>.</p>1013 1014 <ul>1013-<li>Checks route placeholders (<em>req.params</em>), ex: /user/:id</li>1015+<li>Checks route params (<em>req.params</em>), ex: /user/:id</li>1016 <li>Checks query string params (<em>req.query</em>), ex: ?id=12</li>1017 <li>Checks urlencoded body params (<em>req.body</em>), ex: id=12</li>1018 </ul>1019 1020 1021 <p>To utilize urlencoded request bodies, <em>req.body</em>1022 should be an object. This can be done by using1021-the <em>express.bodyDecoder</em> middleware.</p>1023+the _express.bodyParser middleware.</p>1024 1025 <h3 id="req-flash-type-msg-">req.flash(type[, msg])</h3>1026 1027 <p>Queue flash <em>msg</em> of the given <em>type</em>.</p>46 unmodified lines1074 <p>Sets the <em>Content-Type</em> response header to the given <em>type</em>.</p>1075 1076 <pre><code> var filename = 'path/to/image.png';1077 res.contentType(filename);1076- // res.headers['Content-Type'] is now "image/png"1078+ // Content-Type is now "image/png"1079 </code></pre>1080 1081 <h3 id="res-attachment-filename-">res.attachment([filename])</h3>1082 1 unmodified line1084 1085 <pre><code> res.attachment('path/to/my/image.png');1086 </code></pre>1087 1086-<h3 id="res-sendfile-path-">res.sendfile(path)</h3>1088+<h3 id="res-sendfile-path-options-callback-">res.sendfile(path[, options[, callback]])</h3>1089 1090 <p>Used by <code>res.download()</code> to transfer an arbitrary file.</p>1091 1092 <pre><code>res.sendfile('path/to/my.file');1093 </code></pre>1094 1093-<p>This method accepts a callback which when given will be called on an exception, as well as when the transfer has completed. When a callback is not given, and the file has <strong>not</strong> been streamed, <em>next(err)</em> will be called on an exception.</p>1095+<p>This method accepts an optional callback which is called when1096+an 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>1097 1095-<pre><code>res.sendfile(path, function(err, path){1098+<pre><code>res.sendfile(path, function(err){1099 if (err) {1097- // handle the error1100+ next(err);1101 } else {1102 console.log('transferred %s', path);1103 }1104 });1105 </code></pre>1106 1104-<h3 id="res-download-file-filename-">res.download(file[, filename])</h3>1107+<p>Options may also be passed to the internal <em>fs.createReadStream()</em> call, for example altering the <em>bufferSize</em>:</p>1108 1109+<pre><code>res.sendfile(path, { bufferSize: 1024 }, function(err){1110+ // handle1111+});1112+</code></pre>1113+1114+<h3 id="res-download-file-filename-callback-">res.download(file[, filename[, callback]])</h3>1115+1116 <p>Transfer the given <em>file</em> as an attachment with optional alternative <em>filename</em>.</p>1117 1118 <pre><code>res.download('path/to/image.png');1119 res.download('path/to/image.png', 'foo.png');4 unmodified lines1124 <pre><code>res.attachment(file);1125 res.sendfile(file);1126 </code></pre>1127 1128+<p>An optional callback may be supplied as either the second or third argument, which is passed to <em>res.sendfile()</em>:</p>1129+1130+<pre><code>res.download(path, 'expenses.doc', function(err){1131+ // handle1132+});1133+</code></pre>1134+1135 <h3 id="res-send-body-status-headers-status-status-">res.send(body|status[, headers|status[, status]])</h3>1136 1120-<p>The <code>res.send()</code> method is a high level response utility allowing you to pass1121-objects to respond with json, strings for html, arbitrary _Buffer_s or numbers for status1122-code based responses. The following are all valid uses:</p>1137+<p>The <em>res.send()</em> method is a high level response utility allowing you to pass1138+objects to respond with json, strings for html, Buffer instances, or numbers representing the status code. The following are all valid uses:</p>1139 1140 <pre><code> res.send(); // 2041141 res.send(new Buffer('wahoo'));1142 res.send({ some: 'json' });6 unmodified lines1149 <p>By default the <em>Content-Type</em> response header is set, however if explicitly1150 assigned through <code>res.send()</code> or previously with <code>res.header()</code> or <code>res.contentType()</code>1151 it will not be set again.</p>1152 1137-<p>Note that this method <em>end()</em> the response, so you will want to use node's <em>res.writeHead()</em> / <em>res.write()</em> for multiple writes or streaming.</p>1153+<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>1154 1155 <h3 id="res-redirect-url-status-">res.redirect(url[, status])</h3>1156 1157 <p>Redirect to the given <em>url</em> with a default response <em>status</em> of 302.</p>10 unmodified lines1168 the "home" setting and defaults to "/".</p>1169 1170 <h3 id="res-cookie-name-val-options-">res.cookie(name, val[, options])</h3>1171 1156-<p>Sets the given cookie <em>name</em> to <em>val</em>, with <em>options</em> such as "httpOnly: true", "expires", "secure" etc.</p>1172+<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>1173 1174 <pre><code>// "Remember me" for 15 minutes 1175 res.cookie('rememberme', 'yes', { expires: new Date(Date.now() + 900000), httpOnly: true });1176 </code></pre>1177 1178 <p>To parse incoming <em>Cookie</em> headers, use the <em>cookieDecoder</em> middleware, which provides the <em>req.cookies</em> object:</p>1179 1164-<pre><code>app.use(express.cookieDecoder());1180+<pre><code>app.use(express.cookieParser());1181 1182 app.get('/', function(req, res){1167- // use req.cookies.rememberme1183+ // use req.cookies.rememberme1184 });1185 </code></pre>1186 1187 <h3 id="res-clearCookie-name-">res.clearCookie(name)</h3>316 unmodified linesmodified:docs/guide.md+57 added-43 removed
520 unmodified lines521 });522 523 ### View Partials524 525-The Express view system has built-in support for partials and collections, which are526-sort of "mini" views representing a document fragment. For example rather than iterating525+The Express view system has built-in support for partials and collections, which are "mini" views representing a document fragment. For example rather than iterating526 in a view to display comments, we would use a partial with collection support:527 529- partial('comment.haml', { collection: comments });530-531-To make things even less verbose we can assume the extension as _.haml_ when omitted,532-however if we wished we could use an ejs partial, within a haml view for example.533-528 partial('comment', { collection: comments });529 536-And once again even further, when rendering a collection we can simply pass537-an array, if no other options are desired:530+If no other options are desired, we can omit the object and simply pass our array, which is equivalent to above:531 532 partial('comment', comments);533 534 When using the partial collection support a few "magic" variables are provided535 for free:536 544- * _firstInCollection_ True if this is the first object545- * _indexInCollection_ Index of the object in the collection546- * _lastInCollection_ True if this is the last object547- * _collectionLength_ Length of the collection537+ * _firstInCollection_ true if this is the first object538+ * _indexInCollection_ index of the object in the collection539+ * _lastInCollection_ true if this is the last object540+ * _collectionLength_ length of the collection541 542 For documentation on altering the object name view [res.partial()](http://expressjs.com/guide.html#res-partial-view-options-).543 551-NOTE: partials are not recommended for large collections (150+) because the view system adds to the overhead. For example do _not_ implement a simple ul list with partial collection support, simply create a partial and iterate within that single partial, as this is far more efficient than rendering 150+ templates.544+NOTE: 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.545 546 ### Template Engines547 548 Below are a few template engines commonly used with Express:549 550 * [Jade](http://jade-lang.com) haml.js successor558- * [Haml](http://github.com/visionmedia/haml.js) pythonic indented templates551 * [EJS](http://github.com/visionmedia/ejs) Embedded JavaScript552 * [CoffeeKup](http://github.com/mauricemach/coffeekup) CoffeeScript based templating553 * [jQuery Templates](https://github.com/kof/node-jqtpl) for node554 555 ### Session Support556 565-Sessions support can be added by using Connect's _session_ middleware. To do so we also need the _cookieDecoder_ middleware place above it, which will parse and populate cookie data to _req.cookies_.557+Sessions support can be added by using Connect's _session_ middleware. To do so we also need the _cookieParser_ middleware place above it, which will parse and populate cookie data to _req.cookies_.558 567- app.use(express.cookieDecoder());568- app.use(express.session());559+ app.use(express.cookieParser());560+ app.use(express.session({ secret: "keyboard cat" }));561 562 By default the _session_ middleware uses the memory store bundled with Connect, however many implementations exist. For example [connect-redis](http://github.com/visionmedia/connect-redis) supplies a [Redis](http://code.google.com/p/redis/) session store and can be used as shown below:563 564 var RedisStore = require('connect-redis');573- app.use(express.cookieDecoder());574- app.use(express.session({ store: new RedisStore }));565+ app.use(express.cookieParser());566+ app.use(express.session({ secret: "keyboard cat", store: new RedisStore }));567 568 Now the _req.session_ and _req.sessionStore_ properties will be accessible to all routes and subsequent middleware. Properties on _req.session_ are automatically saved on a response, so for example if we wish to shopping cart data:569 570 var RedisStore = require('connect-redis');579- app.use(express.bodyDecoder());580- app.use(express.cookieDecoder());581- app.use(express.session({ store: new RedisStore }));571+ app.use(express.bodyParser());572+ app.use(express.cookieParser());573+ app.use(express.session({ secret: "keyboard cat", store: new RedisStore }));574 575 app.post('/add-to-cart', function(req, res){576 // Perhaps we posted several items with a form585- // (use the bodyDecoder() middleware for this)577+ // (use the bodyParser() middleware for this)578 var items = req.body.items;579 req.session.items = items;580 res.redirect('back');581 });7 unmodified lines589 }590 res.render('shopping-cart');591 });592 601-The _req.session_ object also has methods such as _Session#touch()_, _Session#destroy()_, _Session#regenerate()_ among others to maintain and manipulate sessions. For more information view the [Connect Session](http://senchalabs.github.com/connect/session.html) documentation.593+The _req.session_ object also has methods such as _Session#touch()_, _Session#destroy()_, _Session#regenerate()_ among others to maintain and manipulate sessions. For more information view the [Connect Session](http://senchalabs.github.com/connect/middleware-session.html) documentation.594 595 ### Migration Guide596 605- Pre-beta Express developers may reference the [Migration Guide](migrate.html) to get up to speed on how to upgrade your application.597+ Express 1.x developers may reference the [Migration Guide](migrate.html) to get up to speed on how to upgrade your application.598 599 ### req.header(key[, defaultValue])600 601 Get the case-insensitive request header _key_, with optional _defaultValue_:1 unmodified line603 req.header('Host');604 req.header('host');605 req.header('Accept', '*/*');606 607+The _Referrer_ and _Referer_ header fields are special-cased, either will work:608+609+ // sent Referrer: http://google.com610+611+ req.header('Referer');612+ // => "http://google.com"613+614+ req.header('Referrer');615+ // => "http://google.com"616+617 ### req.accepts(type)618 619 Check if the _Accept_ header is present, and includes the given _type_.620 69 unmodified lines690 ### req.param(name[, default])691 692 Return the value of param _name_ when present or _default_.693 692- - Checks route placeholders (_req.params_), ex: /user/:id694+ - Checks route params (_req.params_), ex: /user/:id695 - Checks query string params (_req.query_), ex: ?id=12696 - Checks urlencoded body params (_req.body_), ex: id=12697 698 To utilize urlencoded request bodies, _req.body_699 should be an object. This can be done by using698-the _express.bodyDecoder_ middleware.700+the _express.bodyParser middleware.701 702 ### req.flash(type[, msg])703 704 Queue flash _msg_ of the given _type_.30 unmodified lines735 736 res.header('Content-Length');737 // => undefined738 737- res.header('Content-Length', 123);739+ res.header('Content-Length', 123);740 // => 123739-741+ 742 res.header('Content-Length');743 // => 123744 745 ### res.contentType(type)1 unmodified line747 Sets the _Content-Type_ response header to the given _type_.748 749 var filename = 'path/to/image.png';750 res.contentType(filename);749- // res.headers['Content-Type'] is now "image/png"751+ // Content-Type is now "image/png"752 753 ### res.attachment([filename])754 755 Sets the _Content-Disposition_ response header to "attachment", with optional _filename_.756 757 res.attachment('path/to/my/image.png');758 757-### res.sendfile(path)759+### res.sendfile(path[, options[, callback]])760 761 Used by `res.download()` to transfer an arbitrary file. 762 763 res.sendfile('path/to/my.file');764 763-This method accepts a callback which when given will be called on an exception, as well as when the transfer has completed. When a callback is not given, and the file has __not__ been streamed, _next(err)_ will be called on an exception.765+This method accepts an optional callback which is called when766+an error occurs, or when the transfer is complete. By default failures call `next(err)`, however when a callback is supplied you must do this explicitly, or act on the error.767 765- res.sendfile(path, function(err, path){768+ res.sendfile(path, function(err){769 if (err) {767- // handle the error770+ next(err);771 } else {772 console.log('transferred %s', path);773 }774 });775 773-### res.download(file[, filename])776+Options may also be passed to the internal _fs.createReadStream()_ call, for example altering the _bufferSize_:777 778+ res.sendfile(path, { bufferSize: 1024 }, function(err){779+ // handle780+ });781+782+### res.download(file[, filename[, callback]])783+784 Transfer the given _file_ as an attachment with optional alternative _filename_.785 786 res.download('path/to/image.png');787 res.download('path/to/image.png', 'foo.png');2 unmodified lines790 791 res.attachment(file);792 res.sendfile(file);793 794+An optional callback may be supplied as either the second or third argument, which is passed to _res.sendfile()_:795+796+ res.download(path, 'expenses.doc', function(err){797+ // handle798+ });799+800 ### res.send(body|status[, headers|status[, status]])801 787-The `res.send()` method is a high level response utility allowing you to pass788-objects to respond with json, strings for html, arbitrary _Buffer_s or numbers for status789-code based responses. The following are all valid uses:802+The _res.send()_ method is a high level response utility allowing you to pass803+objects to respond with json, strings for html, Buffer instances, or numbers representing the status code. The following are all valid uses:804 805 res.send(); // 204806 res.send(new Buffer('wahoo'));807 res.send({ some: 'json' });5 unmodified lines813 By default the _Content-Type_ response header is set, however if explicitly814 assigned through `res.send()` or previously with `res.header()` or `res.contentType()`815 it will not be set again.816 803-Note that this method _end()_ the response, so you will want to use node's _res.writeHead()_ / _res.write()_ for multiple writes or streaming.817+Note that this method _end()_s the response, so you will want to use node's _res.write()_ for multiple writes or streaming.818 819 ### res.redirect(url[, status])820 821 Redirect to the given _url_ with a default response _status_ of 302.9 unmodified lines831 the "home" setting and defaults to "/".832 833 ### res.cookie(name, val[, options])834 821-Sets the given cookie _name_ to _val_, with _options_ such as "httpOnly: true", "expires", "secure" etc.835+Sets the given cookie _name_ to _val_, with options _httpOnly_, _secure_, _expires_ etc.836 837 // "Remember me" for 15 minutes 838 res.cookie('rememberme', 'yes', { expires: new Date(Date.now() + 900000), httpOnly: true });839 840 To parse incoming _Cookie_ headers, use the _cookieDecoder_ middleware, which provides the _req.cookies_ object:841 828- app.use(express.cookieDecoder());842+ app.use(express.cookieParser());843 844 app.get('/', function(req, res){831- // use req.cookies.rememberme845+ // use req.cookies.rememberme846 });847 848 ### res.clearCookie(name)849 272 unmodified lines