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 96327c9 · Browse files · 3 files·+242 added−85 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+242 added−85 removed
- other:+242 added−85 removed
Provenance
- Commit
402d37d- Tree
849a7c3- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
96327c9
3 files changed+242 added−85 removed
modified:docs/guide.1+104 added-31 removed
1506 unmodified lines1507 .SS "res\.render(view[, options[, fn]])"1508 Render \fIview\fR with the given \fIoptions\fR and optional callback \fIfn\fR\. When a callback function is given a response will \fInot\fR be made automatically, however otherwise a response of \fI200\fR and \fItext/html\fR is given\.1509 .1510 .P1511-Most engines accept one or more of the following options, both haml \fIhttp://github\.com/visionmedia/haml\.js\fR and jade \fIhttp://github\.com/visionmedia/jade\fR accept all:1511+The \fIoptions\fR 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:1512 .1513-.IP "\(bu" 41514-\fIscope\fR Template evaluation context (value of \fIthis\fR)1513+.IP "" 41514 .1516-.IP "\(bu" 41517-\fIlocals\fR Object containing local variables1515+.nf1516+1517+var user = { name: \'tj\' };1518+res\.render(\'index\', { layout: false, user: user });1519 .1519-.IP "\(bu" 41520-\fIdebug\fR Output debugging information1520+.fi1521 .1522-.IP "\(bu" 41523-\fIstatus\fR Response status code, defaults to 2001524-.1525-.IP "\(bu" 41526-\fIheaders\fR Response headers object1527-.1522 .IP "" 01523 .1524 .SS "res\.partial(view[, options])"1525 Render \fIview\fR partial with the given \fIoptions\fR\. This method is always available to the view as a local variable\.117 unmodified lines1643 .fi1644 .1645 .IP "" 01646 .1647+.P1648+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:1649+.1650+.IP "" 41651+.1652+.nf1653+1654+app\.get(\'/users\', function(req, res){1655+ if (req\.xhr) {1656+ // respond with the each user in the collection1657+ // passed to the "user" view1658+ res\.partial(\'user\', users);1659+ } else {1660+ // respond with layout, and users page1661+ // which internally does partial(\'user\', users)1662+ // along with other UI1663+ res\.render(\'users\', { users: users });1664+ }1665+});1666+.1667+.fi1668+.1669+.IP "" 01670+.1671 .SS "res\.local(name[, val])"1672 Get or set the given local variable \fIname\fR\. The locals built up for a response are applied to those given to the view rendering methods such as \fBres\.render()\fR\.1673 .1674 .IP "" 463 unmodified lines1738 .fi1739 .1740 .IP "" 01741 .1742+.SS "app\.enabled(name)"1743+Check if setting \fIname\fR is enabled:1744+.1745+.IP "" 41746+.1747+.nf1748+1749+app\.enabled(\'view cache\');1750+// => false1751+1752+app\.enable(\'view cache\');1753+app\.enabled(\'view cache\');1754+// => true1755+.1756+.fi1757+.1758+.IP "" 01759+.1760 .SS "app\.disable(name)"1761 Disable the given setting \fIname\fR:1762 .1763 .IP "" 410 unmodified lines1774 .fi1775 .1776 .IP "" 01777 .1778+.SS "app\.disabled(name)"1779+Check if setting \fIname\fR is disabled:1780+.1781+.IP "" 41782+.1783+.nf1784+1785+app\.enable(\'view cache\');1786+1787+app\.disabled(\'view cache\');1788+// => false1789+1790+app\.disable(\'view cache\');1791+app\.disabled(\'view cache\');1792+// => true1793+.1794+.fi1795+.1796+.IP "" 01797+.1798 .SS "app\.configure(env|function[, function])"1799 Define a callback function for the given \fIenv\fR (or all environments) with callback \fIfunction\fR:1800 .1801 .IP "" 412 unmodified lines1814 .1815 .IP "" 01816 .1817 .SS "app\.redirect(name, val)"1762-For use with \fBres\.redirect()\fR we can map redirects at the application level as shown below:1818+For use with \fIres\.redirect()\fR we can map redirects at the application level as shown below:1819 .1820 .IP "" 41821 .1822 .nf17 unmodified lines1840 .1841 .nf1842 1843 app\.redirect(\'comments\', function(req, res){1788- return \'/post/\' + req\.params\.id + \'/comments\';1844+ return \'/post/\' + req\.params\.id + \'/comments\';1845 });1846 .1847 .fi1848 .6 unmodified lines1855 .1856 .nf1857 1858 app\.get(\'/post/:id\', function(req, res){1803- res\.redirect(\'comments\');1859+ res\.redirect(\'comments\');1860 });1861 .1862 .fi1863 .1864 .IP "" 01865 .1866+.P1867+When mounted, \fIres\.redirect()\fR will respect the mount\-point\. For example if a blog app is mounted at \fI/blog\fR, the following will redirect to \fI/blog/posts\fR:1868+.1869+.IP "" 41870+.1871+.nf1872+1873+res\.redirect(\'/posts\');1874+.1875+.fi1876+.1877+.IP "" 01878+.1879 .SS "app\.error(function)"1880 Adds an error handler \fIfunction\fR which will receive the exception as the first parameter as shown below\. Note that we may set several error handlers by making several calls to this method, however the handler should call \fInext(err)\fR if it does not wish to deal with the exception:1881 .1882 .IP "" 41883 .1884 .nf1885 1886 app\.error(function(err, req, res, next){1818- res\.send(err\.message, 500);1887+ res\.send(err\.message, 500);1888 });1889 .1890 .fi1891 .1 unmodified line1893 .1894 .SS "app\.helpers(obj)"1895 Registers static view helpers\.1896 .1897+.P1898+app\.helpers({1899+.1900 .IP "" 41901 .1902 .nf1903 1832-app\.helpers({1833- name: function(first, last){ return first + \', \' + last },1834- firstName: \'tj\',1835- lastName: \'holowaychuk\'1904+ name: function(first, last){ return first + \', \' + last }1905+, firstName: \'tj\'1906+, lastName: \'holowaychuk\'1836-});1907 .1908 .fi1909 .1910 .IP "" 01911 .1912 .P1913+});1914+.1915+.P1916 Our view could now utilize the \fIfirstName\fR and \fIlastName\fR variables, as well as the \fIname()\fR function exposed\.1917 .1918 .IP "" 41919 .12 unmodified lines1932 .1933 .nf1934 1935 app\.dynamicHelpers({1863- session: function(req, res){1864- return req\.session;1865- }1936+ session: function(req, res){1937+ return req\.session;1938+ }1939 });1940 .1941 .fi1942 .22 unmodified lines1965 var app = express\.createServer(),1966 blog = express\.createServer();1967 1968 blog\.mounted(function(parent){1896- // parent is app1897- // "this" is blog1969+ // parent is app1970+ // "this" is blog1971 });1972 1973 app\.use(blog);1974 .34 unmodified lines2009 .2010 .nf2011 2012 app\.register(\'\.foo\', {1940- render: function(str, options) {1941- // perhaps their api is1942- // return foo\.toHTML(str, options);1943- }2013+ render: function(str, options) {2014+ // perhaps their api is2015+ // return foo\.toHTML(str, options);2016+ }2017 });2018 .2019 .fi2020 .50 unmodified linesmodified:docs/guide.html+73 added-28 removed
219 unmodified lines220 </ul></li>221 <li><a href="#" class="toggle">+</a> <a class="section-title" href="#">Server</a><ul class="section" id="section-Server">222 <li><a href="#app-set-name-val-">set()</a></li>223 <li><a href="#app-enable-name-">enable()</a></li>224+<li><a href="#app-enabled-name-">enabled()</a></li>225 <li><a href="#app-disable-name-">disable()</a></li>226+<li><a href="#app-disabled-name-">disabled()</a></li>227 <li><a href="#app-configure-env-function-function-">configure()</a></li>228 <li><a href="#app-redirect-name-val-">redirect()</a></li>229 <li><a href="#app-error-function-">error()</a></li>230 <li><a href="#app-helpers-obj-">helpers()</a></li>967 unmodified lines1198 <p>Render <em>view</em> with the given <em>options</em> and optional callback <em>fn</em>.1199 When a callback function is given a response will <em>not</em> be made1200 automatically, however otherwise a response of <em>200</em> and <em>text/html</em> is given.</p>1201 1200-<p> Most engines accept one or more of the following options,1201- both <a href="http://github.com/visionmedia/haml.js">haml</a> and <a href="http://github.com/visionmedia/jade">jade</a> accept all:</p>1202+<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>1203 1203-<ul>1204-<li><em>scope</em> Template evaluation context (value of <em>this</em>)</li>1205-<li><em>locals</em> Object containing local variables</li>1206-<li><em>debug</em> Output debugging information</li>1207-<li><em>status</em> Response status code, defaults to 200</li>1208-<li><em>headers</em> Response headers object</li>1209-</ul>1204+<pre><code>var user = { name: 'tj' };1205+res.render('index', { layout: false, user: user });1206+</code></pre>1207 1211-1208 <h3 id="res-partial-view-options-">res.partial(view[, options])</h3>1209 1210 <p>Render <em>view</em> partial with the given <em>options</em>. This method is always available1211 to the view as a local variable.</p>64 unmodified lines1276 <pre><code>partial('movie', movie);1277 // => In view: movie.director1278 </code></pre>1279 1280+<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>1281+1282+<pre><code>app.get('/users', function(req, res){1283+ if (req.xhr) {1284+ // respond with the each user in the collection1285+ // passed to the "user" view1286+ res.partial('user', users);1287+ } else {1288+ // respond with layout, and users page1289+ // which internally does partial('user', users)1290+ // along with other UI1291+ res.render('users', { users: users });1292+ }1293+});1294+</code></pre>1295+1296 <h3 id="res-local-name-val-">res.local(name[, val])</h3>1297 1298 <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>1299 38 unmodified lines1338 app.enabled('some arbitrary setting');1339 // => true1340 </code></pre>1341 1342+<h3 id="app-enabled-name-">app.enabled(name)</h3>1343+1344+<p>Check if setting <em>name</em> is enabled:</p>1345+1346+<pre><code>app.enabled('view cache');1347+// => false1348+1349+app.enable('view cache');1350+app.enabled('view cache');1351+// => true1352+</code></pre>1353+1354 <h3 id="app-disable-name-">app.disable(name)</h3>1355 1356 <p>Disable the given setting <em>name</em>:</p>1357 4 unmodified lines1362 app.disabled('some setting');1363 // => false1364 </code></pre>1365 1366+<h3 id="app-disabled-name-">app.disabled(name)</h3>1367+1368+<p>Check if setting <em>name</em> is disabled:</p>1369+1370+<pre><code>app.enable('view cache');1371+1372+app.disabled('view cache');1373+// => false1374+1375+app.disable('view cache');1376+app.disabled('view cache');1377+// => true1378+</code></pre>1379+1380 <h3 id="app-configure-env-function-function-">app.configure(env|function[, function])</h3>1381 1382 <p>Define a callback function for the given <em>env</em> (or all environments) with callback <em>function</em>:</p>1383 7 unmodified lines1391 </code></pre>1392 1393 <h3 id="app-redirect-name-val-">app.redirect(name, val)</h3>1394 1357-<p>For use with <code>res.redirect()</code> we can map redirects at the application level as shown below:</p>1395+<p>For use with <em>res.redirect()</em> we can map redirects at the application level as shown below:</p>1396 1397 <pre><code>app.redirect('google', 'http://google.com');1398 </code></pre>1399 3 unmodified lines1403 1404 <p>We may also map dynamic redirects:</p>1405 1406 <pre><code>app.redirect('comments', function(req, res){1369- return '/post/' + req.params.id + '/comments';1407+ return '/post/' + req.params.id + '/comments';1408 });1409 </code></pre>1410 1411 <p>So now we may do the following, and the redirect will dynamically adjust to1412 the context of the request. If we called this route with <em>GET /post/12</em> our1413 redirect <em>Location</em> would be <em>/post/12/comments</em>.</p>1414 1415 <pre><code>app.get('/post/:id', function(req, res){1378- res.redirect('comments');1416+ res.redirect('comments');1417 });1418 </code></pre>1419 1420+<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>1421+1422+<pre><code>res.redirect('/posts');1423+</code></pre>1424+1425 <h3 id="app-error-function-">app.error(function)</h3>1426 1427 <p>Adds an error handler <em>function</em> which will receive the exception as the first parameter as shown below.1428 Note that we may set several error handlers by making several calls to this method, however the handler1429 should call <em>next(err)</em> if it does not wish to deal with the exception:</p>1430 1431 <pre><code>app.error(function(err, req, res, next){1389- res.send(err.message, 500);1432+ res.send(err.message, 500);1433 });1434 </code></pre>1435 1436 <h3 id="app-helpers-obj-">app.helpers(obj)</h3>1437 1438 <p>Registers static view helpers.</p>1439 1397-<pre><code>app.helpers({1398- name: function(first, last){ return first + ', ' + last },1399- firstName: 'tj',1400- lastName: 'holowaychuk'1401-});1440+<p> app.helpers({</p>1441+1442+<pre><code> name: function(first, last){ return first + ', ' + last }1443+, firstName: 'tj'1444+, lastName: 'holowaychuk'1445 </code></pre>1446 1447+<p> });</p>1448+1449 <p>Our view could now utilize the <em>firstName</em> and <em>lastName</em> variables,1450 as well as the <em>name()</em> function exposed.</p>1451 1452 <pre><code><%= name(firstName, lastName) %>6 unmodified lines1459 evaluated against the <em>Server</em> instance before a view is rendered. The <em>return value</em> of this function1460 becomes the local variable it is associated with.</p>1461 1462 <pre><code>app.dynamicHelpers({1418- session: function(req, res){1419- return req.session;1420- }1463+ session: function(req, res){1464+ return req.session;1465+ }1466 });1467 </code></pre>1468 1469 <p>All views would now have <em>session</em> available so that session data can be accessed via <em>session.name</em> etc:</p>8 unmodified lines1478 <pre><code>var app = express.createServer(),1479 blog = express.createServer();1480 1481 blog.mounted(function(parent){1437- // parent is app1438- // "this" is blog1482+ // parent is app1483+ // "this" is blog1484 });1485 1486 app.use(blog);1487 </code></pre>18 unmodified lines1506 <p>For engines that do not comply with the Express1507 specification, we can also wrap their api this way.</p>1508 1509 <pre><code> app.register('.foo', {1465- render: function(str, options) {1466- // perhaps their api is1467- // return foo.toHTML(str, options);1468- }1510+ render: function(str, options) {1511+ // perhaps their api is1512+ // return foo.toHTML(str, options);1513+ }1514 });1515 </code></pre>1516 1517 <h3 id="app-listen-port-host-">app.listen([port[, host]])</h3>31 unmodified linesmodified:docs/guide.md+65 added-26 removed
855 unmodified lines856 Render _view_ with the given _options_ and optional callback _fn_.857 When a callback function is given a response will _not_ be made858 automatically, however otherwise a response of _200_ and _text/html_ is given.859 860- Most engines accept one or more of the following options,861- both [haml](http://github.com/visionmedia/haml.js) and [jade](http://github.com/visionmedia/jade) accept all:860+The _options_ 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:861 863- - _scope_ Template evaluation context (value of _this_)864- - _locals_ Object containing local variables865- - _debug_ Output debugging information866- - _status_ Response status code, defaults to 200867- - _headers_ Response headers object862+ var user = { name: 'tj' };863+ res.render('index', { layout: false, user: user });864 865 ### res.partial(view[, options])866 867 Render _view_ partial with the given _options_. This method is always available53 unmodified lines921 922 partial('movie', movie);923 // => In view: movie.director924 925+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:926+927+ app.get('/users', function(req, res){928+ if (req.xhr) {929+ // respond with the each user in the collection930+ // passed to the "user" view931+ res.partial('user', users);932+ } else {933+ // respond with layout, and users page934+ // which internally does partial('user', users)935+ // along with other UI936+ res.render('users', { users: users });937+ }938+ });939+940 ### res.local(name[, val])941 942 Get or set the given local variable _name_. The locals built up for a response are applied to those given to the view rendering methods such as `res.render()`.943 34 unmodified lines978 979 app.enabled('some arbitrary setting');980 // => true981 982+### app.enabled(name)983+984+Check if setting _name_ is enabled:985+986+ app.enabled('view cache');987+ // => false988+989+ app.enable('view cache');990+ app.enabled('view cache');991+ // => true992+993 ### app.disable(name)994 995 Disable the given setting _name_:996 3 unmodified lines1000 1001 app.disabled('some setting');1002 // => false1003 1004+### app.disabled(name)1005+1006+Check if setting _name_ is disabled:1007+1008+ app.enable('view cache');1009+1010+ app.disabled('view cache');1011+ // => false1012+1013+ app.disable('view cache');1014+ app.disabled('view cache');1015+ // => true1016+1017 ### app.configure(env|function[, function])1018 1019 Define a callback function for the given _env_ (or all environments) with callback _function_:1020 6 unmodified lines1027 });1028 1029 ### app.redirect(name, val)1030 996-For use with `res.redirect()` we can map redirects at the application level as shown below:1031+For use with _res.redirect()_ we can map redirects at the application level as shown below:1032 1033 app.redirect('google', 'http://google.com');1034 1035 Now in a route we may call:2 unmodified lines1038 1039 We may also map dynamic redirects:1040 1041 app.redirect('comments', function(req, res){1007- return '/post/' + req.params.id + '/comments';1042+ return '/post/' + req.params.id + '/comments';1043 });1044 1045 So now we may do the following, and the redirect will dynamically adjust to1046 the context of the request. If we called this route with _GET /post/12_ our1047 redirect _Location_ would be _/post/12/comments_.1048 1049 app.get('/post/:id', function(req, res){1015- res.redirect('comments');1050+ res.redirect('comments');1051 });1052 1053+When mounted, _res.redirect()_ will respect the mount-point. For example if a blog app is mounted at _/blog_, the following will redirect to _/blog/posts_:1054+1055+ res.redirect('/posts');1056+1057 ### app.error(function)1058 1059 Adds an error handler _function_ which will receive the exception as the first parameter as shown below.1060 Note that we may set several error handlers by making several calls to this method, however the handler1061 should call _next(err)_ if it does not wish to deal with the exception:1062 1063 app.error(function(err, req, res, next){1025- res.send(err.message, 500);1026- });1064+ res.send(err.message, 500);1065+ });1066 1067 ### app.helpers(obj)1068 1069 Registers static view helpers.1070 1032- app.helpers({1033- name: function(first, last){ return first + ', ' + last },1034- firstName: 'tj',1035- lastName: 'holowaychuk'1036- });1071+ app.helpers({1072+ name: function(first, last){ return first + ', ' + last }1073+ , firstName: 'tj'1074+ , lastName: 'holowaychuk'1075+ });1076 1077 Our view could now utilize the _firstName_ and _lastName_ variables,1078 as well as the _name()_ function exposed.1079 6 unmodified lines1086 evaluated against the _Server_ instance before a view is rendered. The _return value_ of this function1087 becomes the local variable it is associated with.1088 1089 app.dynamicHelpers({1051- session: function(req, res){1052- return req.session;1053- }1090+ session: function(req, res){1091+ return req.session;1092+ }1093 });1094 1095 All views would now have _session_ available so that session data can be accessed via _session.name_ etc:1096 6 unmodified lines1103 var app = express.createServer(),1104 blog = express.createServer();1105 1106 blog.mounted(function(parent){1068- // parent is app1069- // "this" is blog1107+ // parent is app1108+ // "this" is blog1109 });1110 1111 app.use(blog);1112 15 unmodified lines1128 For engines that do not comply with the Express1129 specification, we can also wrap their api this way.1130 1131 app.register('.foo', {1093- render: function(str, options) {1094- // perhaps their api is1095- // return foo.toHTML(str, options);1096- }1132+ render: function(str, options) {1133+ // perhaps their api is1134+ // return foo.toHTML(str, options);1135+ }1136 });1137 1138 ### app.listen([port[, host]])1139 21 unmodified lines