Skip to content
Myr

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

3 files+242 added−85 removed

modified:docs/guide.1+104 added-31 removed
1506 unmodified lines
1507.SS "res\.render(view[, options[, fn]])"1508Render \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.P1511Most 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:1511The \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.nf15161517var user = { name: \'tj\' };1518res\.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])"1525Render \fIview\fR partial with the given \fIoptions\fR\. This method is always available to the view as a local variable\.
117 unmodified lines
1643.fi1644.1645.IP "" 01646.1647.P1648This 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.nf16531654app\.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])"1672Get 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 "" 4
63 unmodified lines
1738.fi1739.1740.IP "" 01741.1742.SS "app\.enabled(name)"1743Check if setting \fIname\fR is enabled:1744.1745.IP "" 41746.1747.nf17481749app\.enabled(\'view cache\');1750// => false17511752app\.enable(\'view cache\');1753app\.enabled(\'view cache\');1754// => true1755.1756.fi1757.1758.IP "" 01759.1760.SS "app\.disable(name)"1761Disable the given setting \fIname\fR:1762.1763.IP "" 4
10 unmodified lines
1774.fi1775.1776.IP "" 01777.1778.SS "app\.disabled(name)"1779Check if setting \fIname\fR is disabled:1780.1781.IP "" 41782.1783.nf17841785app\.enable(\'view cache\');17861787app\.disabled(\'view cache\');1788// => false17891790app\.disable(\'view cache\');1791app\.disabled(\'view cache\');1792// => true1793.1794.fi1795.1796.IP "" 01797.1798.SS "app\.configure(env|function[, function])"1799Define a callback function for the given \fIenv\fR (or all environments) with callback \fIfunction\fR:1800.1801.IP "" 4
12 unmodified lines
1814.1815.IP "" 01816.1817.SS "app\.redirect(name, val)"1762For use with \fBres\.redirect()\fR we can map redirects at the application level as shown below:1818For use with \fIres\.redirect()\fR we can map redirects at the application level as shown below:1819.1820.IP "" 41821.1822.nf
17 unmodified lines
1840.1841.nf18421843app\.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 lines
1855.1856.nf18571858app\.get(\'/post/:id\', function(req, res){1803 res\.redirect(\'comments\');1859 res\.redirect(\'comments\');1860});1861.1862.fi1863.1864.IP "" 01865.1866.P1867When 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.nf18721873res\.redirect(\'/posts\');1874.1875.fi1876.1877.IP "" 01878.1879.SS "app\.error(function)"1880Adds 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.nf18851886app\.error(function(err, req, res, next){1818 res\.send(err\.message, 500);1887 res\.send(err\.message, 500);1888});1889.1890.fi1891.
1 unmodified line
1893.1894.SS "app\.helpers(obj)"1895Registers static view helpers\.1896.1897.P1898app\.helpers({1899.1900.IP "" 41901.1902.nf19031832app\.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.P1916Our 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 lines
1932.1933.nf19341935app\.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 lines
1965var app = express\.createServer(),1966 blog = express\.createServer();19671968blog\.mounted(function(parent){1896 // parent is app1897 // "this" is blog1969 // parent is app1970 // "this" is blog1971});19721973app\.use(blog);1974.
34 unmodified lines
2009.2010.nf20112012 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 lines
modified:docs/guide.html+73 added-28 removed
219 unmodified lines
220</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 lines
1198<p>Render <em>view</em> with the given <em>options</em> and optional callback <em>fn</em>.1199When a callback function is given a response will <em>not</em> be made1200automatically, however otherwise a response of <em>200</em> and <em>text/html</em> is given.</p>12011200<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>12031203<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' };1205res.render('index', { layout: false, user: user });1206</code></pre>120712111208<h3 id="res-partial-view-options-">res.partial(view[, options])</h3>12091210<p>Render <em>view</em> partial with the given <em>options</em>. This method is always available1211to the view as a local variable.</p>
64 unmodified lines
1276<pre><code>partial('movie', movie);1277// =&gt; In view: movie.director1278</code></pre>12791280<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>12811282<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>12951296<h3 id="res-local-name-val-">res.local(name[, val])</h3>12971298<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 lines
1338app.enabled('some arbitrary setting');1339// =&gt; true1340</code></pre>13411342<h3 id="app-enabled-name-">app.enabled(name)</h3>13431344<p>Check if setting <em>name</em> is enabled:</p>13451346<pre><code>app.enabled('view cache');1347// =&gt; false13481349app.enable('view cache');1350app.enabled('view cache');1351// =&gt; true1352</code></pre>13531354<h3 id="app-disable-name-">app.disable(name)</h3>13551356<p>Disable the given setting <em>name</em>:</p>1357
4 unmodified lines
1362app.disabled('some setting');1363// =&gt; false1364</code></pre>13651366<h3 id="app-disabled-name-">app.disabled(name)</h3>13671368<p>Check if setting <em>name</em> is disabled:</p>13691370<pre><code>app.enable('view cache');13711372app.disabled('view cache');1373// =&gt; false13741375app.disable('view cache');1376app.disabled('view cache');1377// =&gt; true1378</code></pre>13791380<h3 id="app-configure-env-function-function-">app.configure(env|function[, function])</h3>13811382<p>Define a callback function for the given <em>env</em> (or all environments) with callback <em>function</em>:</p>1383
7 unmodified lines
1391</code></pre>13921393<h3 id="app-redirect-name-val-">app.redirect(name, val)</h3>13941357<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>13961397<pre><code>app.redirect('google', 'http://google.com');1398</code></pre>1399
3 unmodified lines
14031404<p>We may also map dynamic redirects:</p>14051406<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>14101411<p>So now we may do the following, and the redirect will dynamically adjust to1412the context of the request. If we called this route with <em>GET /post/12</em> our1413redirect <em>Location</em> would be <em>/post/12/comments</em>.</p>14141415<pre><code>app.get('/post/:id', function(req, res){1378 res.redirect('comments');1416 res.redirect('comments');1417});1418</code></pre>14191420<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>14211422<pre><code>res.redirect('/posts');1423</code></pre>14241425<h3 id="app-error-function-">app.error(function)</h3>14261427<p>Adds an error handler <em>function</em> which will receive the exception as the first parameter as shown below.1428Note that we may set several error handlers by making several calls to this method, however the handler1429should call <em>next(err)</em> if it does not wish to deal with the exception:</p>14301431<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>14351436<h3 id="app-helpers-obj-">app.helpers(obj)</h3>14371438<p>Registers static view helpers.</p>14391397<pre><code>app.helpers({1398 name: function(first, last){ return first + ', ' + last },1399 firstName: 'tj',1400 lastName: 'holowaychuk'1401});1440<p> app.helpers({</p>14411442<pre><code> name: function(first, last){ return first + ', ' + last }1443, firstName: 'tj'1444, lastName: 'holowaychuk'1445</code></pre>14461447<p> });</p>14481449<p>Our view could now utilize the <em>firstName</em> and <em>lastName</em> variables,1450as well as the <em>name()</em> function exposed.</p>14511452<pre><code>&lt;%= name(firstName, lastName) %>
6 unmodified lines
1459evaluated against the <em>Server</em> instance before a view is rendered. The <em>return value</em> of this function1460becomes the local variable it is associated with.</p>14611462<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>14681469<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 lines
1478<pre><code>var app = express.createServer(),1479 blog = express.createServer();14801481blog.mounted(function(parent){1437 // parent is app1438 // "this" is blog1482 // parent is app1483 // "this" is blog1484});14851486app.use(blog);1487</code></pre>
18 unmodified lines
1506<p>For engines that do not comply with the Express1507specification, we can also wrap their api this way.</p>15081509<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>15161517<h3 id="app-listen-port-host-">app.listen([port[, host]])</h3>
31 unmodified lines
modified:docs/guide.md+65 added-26 removed
855 unmodified lines
856Render _view_ with the given _options_ and optional callback _fn_.857When a callback function is given a response will _not_ be made858automatically, however otherwise a response of _200_ and _text/html_ is given.859860 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:860The _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:861863 - _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 });864865### res.partial(view[, options])866867Render _view_ partial with the given _options_. This method is always available
53 unmodified lines
921922 partial('movie', movie);923 // => In view: movie.director924925This 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:926927 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 });939940### res.local(name[, val])941942Get 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 lines
978979 app.enabled('some arbitrary setting');980 // => true981982### app.enabled(name)983984Check if setting _name_ is enabled:985986 app.enabled('view cache');987 // => false988989 app.enable('view cache');990 app.enabled('view cache');991 // => true992993### app.disable(name)994995Disable the given setting _name_:996
3 unmodified lines
1000 1001 app.disabled('some setting');1002 // => false10031004### app.disabled(name)10051006Check if setting _name_ is disabled:10071008 app.enable('view cache');10091010 app.disabled('view cache');1011 // => false10121013 app.disable('view cache');1014 app.disabled('view cache');1015 // => true10161017### app.configure(env|function[, function])10181019Define a callback function for the given _env_ (or all environments) with callback _function_:1020
6 unmodified lines
1027 });10281029### app.redirect(name, val)1030996For use with `res.redirect()` we can map redirects at the application level as shown below:1031For use with _res.redirect()_ we can map redirects at the application level as shown below:10321033 app.redirect('google', 'http://google.com');10341035Now in a route we may call:
2 unmodified lines
10381039We may also map dynamic redirects:10401041 app.redirect('comments', function(req, res){1007 return '/post/' + req.params.id + '/comments';1042 return '/post/' + req.params.id + '/comments';1043 });10441045So now we may do the following, and the redirect will dynamically adjust to1046the context of the request. If we called this route with _GET /post/12_ our1047redirect _Location_ would be _/post/12/comments_.10481049 app.get('/post/:id', function(req, res){1015 res.redirect('comments');1050 res.redirect('comments');1051 });10521053When 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_:10541055 res.redirect('/posts');10561057### app.error(function)10581059Adds an error handler _function_ which will receive the exception as the first parameter as shown below.1060Note that we may set several error handlers by making several calls to this method, however the handler1061should call _next(err)_ if it does not wish to deal with the exception:10621063 app.error(function(err, req, res, next){1025 res.send(err.message, 500);1026 });1064 res.send(err.message, 500);1065 });10661067### app.helpers(obj)10681069Registers static view helpers.10701032 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 });10761077Our view could now utilize the _firstName_ and _lastName_ variables,1078as well as the _name()_ function exposed.1079
6 unmodified lines
1086evaluated against the _Server_ instance before a view is rendered. The _return value_ of this function1087becomes the local variable it is associated with.10881089 app.dynamicHelpers({1051 session: function(req, res){1052 return req.session;1053 }1090 session: function(req, res){1091 return req.session;1092 }1093 });10941095All views would now have _session_ available so that session data can be accessed via _session.name_ etc:1096
6 unmodified lines
1103 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 lines
1128For engines that do not comply with the Express1129specification, we can also wrap their api this way.11301131 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 });11371138### app.listen([port[, host]])1139
21 unmodified lines