Skip to content
Myr

guide.1

This file

Path

docs/guide.1

1,531 lines · 31.0 KiB

Last changed by

Regenerated guideTj Holowaychukbefore adoptionf179979

History of this file

Take it

The walk stopped after 200 commits: lines older than a65c203 point at it.

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

lines 1–2why, one step back
1.\" generated with Ronn/v0.7.3
2.\" http://github.com/rtomayko/ronn/tree/0.7.3

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

line 3why, one step back
3.

HTTP verb docs. Closes #415Tj Holowaychukbefore adoptiond89b951

line 4why, one step back
4.TH "GUIDE" "" "October 2010" "" ""

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

line 5why, one step back
5.

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

lines 6–8why, one step back
6.SH "NAME"
7\fBguide\fR8.

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

line 9why, one step back
9.SS "Installation"

No "or similar" for curl installTj Holowaychukbefore adoption56a1dd9

line 10why, one step back
10curl:

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 11–72why, one step back
11.
12.IP "" 413.14.nf1516$ curl \-# http://expressjs\.com/install\.sh | sh17.18.fi19.20.IP "" 021.22.P23npm:24.25.IP "" 426.27.nf2829$ npm install express30.31.fi32.33.IP "" 034.35.P36git clone, first update the submodules:37.38.IP "" 439.40.nf4142$ git submodule update \-\-init43$ make install44$ make install\-support45.46.fi47.48.IP "" 049.50.SS "Creating An Application"51The \fIexpress\.Server\fR now inherits from \fIhttp\.Server\fR, however follows the same idiom by providing \fIexpress\.createServer()\fR as shown below\. This means that you can utilize Express server\'s transparently with other libraries\.52.53.IP "" 454.55.nf5657var app = require(\'express\')\.createServer();5859app\.get(\'/\', function(req, res){60 res\.send(\'hello world\');61});6263app\.listen(3000);64.65.fi66.67.IP "" 068.69.SS "Configuration"70Express supports arbitrary environments, such as \fIproduction\fR and \fIdevelopment\fR\. Developers can use the \fIconfigure()\fR method to setup needs required by the current environment\. When \fIconfigure()\fR is called without an environment name it will be run in \fIevery\fR environment prior to the environment specific callback\.71.72.P

Updated configuration docsTj Holowaychukbefore adoptionf8ccaa3

line 73why, one step back
73In the example below we only \fIdumpExceptions\fR, and respond with exception stack traces in \fIdevelopment\fR mode, however for both environments we utilize \fImethodOverride\fR and \fIbodyDecoder\fR\. Note the use of \fIapp\.router\fR, which can (optionally) be used to mount the application routes, otherwise the first call to \fIapp\.{get,put,del,post}()\fR will mount the routes\.

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 74–79why, one step back
74.
75.IP "" 476.77.nf7879app\.configure(function(){

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

lines 80–81why, one step back
80 app\.use(express\.methodOverride());
81 app\.use(express\.bodyDecoder());

Updated configuration docsTj Holowaychukbefore adoptionf8ccaa3

line 82why, one step back
82 app\.use(app\.router);

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

line 83why, one step back
83 app\.use(express\.staticProvider(__dirname + \'/public\'));

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 84–86why, one step back
84});
8586app\.configure(\'development\', function(){

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

line 87why, one step back
87 app\.use(express\.errorHandler({ dumpExceptions: true, showStack: true }));

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 88–90why, one step back
88});
8990app\.configure(\'production\', function(){

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

line 91why, one step back
91 app\.use(express\.errorHandler());

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 92–107why, one step back
92});
93.94.fi95.96.IP "" 097.98.P99For internal and arbitrary settings Express provides the \fIset(key[, val])\fR, \fIenable(key)\fR, \fIdisable(key)\fR methods:100.101.IP "" 4102.103.nf104105app\.configure(function(){106 app\.set(\'views\', __dirname + \'/views\');107 app\.set(\'views\');

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

line 108why, one step back
108 // => "\.\.\. views directory \.\.\."

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 109–121why, one step back
109
110 app\.enable(\'some feature\');111 // same as app\.set(\'some feature\', true);112113 app\.disable(\'some feature\');114 // same as app\.set(\'some feature\', false);115});116.117.fi118.119.IP "" 0120.121.P

Rebuilt docsTj Holowaychukbefore adoption2724ce3

line 122why, one step back
122To alter the environment we can set the \fINODE_ENV\fR environment variable, for example:

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 123–127why, one step back
123.
124.IP "" 4125.126.nf127

Rebuilt docsTj Holowaychukbefore adoption2724ce3

line 128why, one step back
128$ NODE_ENV=production node app\.js

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 129–140why, one step back
129.
130.fi131.132.IP "" 0133.134.SS "Settings"135Express supports the following settings out of the box:136.137.IP "\(bu" 4138\fIenv\fR Application environment set internally, use \fIapp\.set(\'env\')\fR on \fIServer#listen()\fR139.140.IP "\(bu" 4

New "home" docsTj Holowaychukbefore adoption1e7a12f

line 141why, one step back
141\fIhome\fR Application base path used for \fIres\.redirect()\fR and transparently handling mounted apps\.

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 142–148why, one step back
142.
143.IP "\(bu" 4144\fIviews\fR Root views directory defaulting to \fBCWD/views\fR145.146.IP "\(bu" 4147\fIview engine\fR Default view engine name for views rendered without extensions148.

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

lines 149–154why, one step back
149.IP "\(bu" 4
150\fIview options\fR An object specifying global view options151.152.IP "\(bu" 4153\fIpartials\fR Root view partials directory defaulting to \fIviews\fR/partials\.154.

Updated docsTj Holowaychukbefore adoptionf0eab06

lines 155–157why, one step back
155.IP "\(bu" 4
156\fIstream threshold\fR Bytesize indicating when a file should be streamed for \fIres\.sendfile()\fR using \fIfs\.ReadStream()\fR and \fIsys\.pump()\fR\.157.

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 158–160why, one step back
158.IP "" 0
159.160.SS "Routing"

Updated docsTj Holowaychukbefore adoptiond691ecb

line 161why, one step back
161Express utilizes the HTTP verbs to provide a meaningful, expressive routing API\. For example we may want to render a user\'s account for the path \fI/user/12\fR, this can be done by defining the route below\. The values associated to the named placeholders are available as \fBreq\.params\fR\.

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 162–166why, one step back
162.
163.IP "" 4164.165.nf166

Updated docsTj Holowaychukbefore adoptiond691ecb

lines 167–168why, one step back
167app\.get(\'/user/:id\', function(req, res){
168 res\.send(\'user \' + req\.params\.id);

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 169–181why, one step back
169});
170.171.fi172.173.IP "" 0174.175.P176A route is simple a string which is compiled to a \fIRegExp\fR internally\. For example when \fI/user/:id\fR is compiled, a simplified version of the regexp may look similar to:177.178.IP "" 4179.180.nf181

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

line 182why, one step back
182\e/user\e/([^\e/]+)\e/?

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 183–188why, one step back
183.
184.fi185.186.IP "" 0187.188.P

Updated docsTj Holowaychukbefore adoptiond691ecb

line 189why, one step back
189Regular expression literals may also be passed for complex uses\. Since capture groups with literal \fIRegExp\fR\'s are anonymous we can access them directly \fBreq\.params\fR\.

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 190–194why, one step back
190.
191.IP "" 4192.193.nf194

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

line 195why, one step back
195app\.get(/^\e/users?(?:\e/(\ed+)(?:\e\.\e\.(\ed+))?)?/, function(req, res){

Updated docsTj Holowaychukbefore adoptiond691ecb

line 196why, one step back
196 res\.send(req\.params);

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 197–214why, one step back
197});
198.199.fi200.201.IP "" 0202.203.P204Curl requests against the previously defined route:205.206.IP "" 4207.208.nf209210 $ curl http://dev:3000/user211 [null,null]212 $ curl http://dev:3000/users213 [null,null]214 $ curl http://dev:3000/users/1

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

line 215why, one step back
215 ["1",null]

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

line 216why, one step back
216 $ curl http://dev:3000/users/1\.\.15

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

line 217why, one step back
217 ["1","15"]

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 218–229why, one step back
218.
219.fi220.221.IP "" 0222.223.P224Below are some route examples, and the associated paths that they may consume:225.226.IP "" 4227.228.nf229

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

line 230why, one step back
230 "/user/:id"

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 231–232why, one step back
231 /user/12
232

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

line 233why, one step back
233 "/users/:id?"

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 234–236why, one step back
234 /users/5
235 /users236

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

line 237why, one step back
237 "/files/*"

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 238–240why, one step back
238 /files/jquery\.js
239 /files/javascripts/jquery\.js240

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

line 241why, one step back
241 "/file/*\.*"

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 242–244why, one step back
242 /files/jquery\.js
243 /files/javascripts/jquery\.js244

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

line 245why, one step back
245 "/user/:id/:operation?"

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 246–248why, one step back
246 /user/1
247 /user/1/edit248

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

line 249why, one step back
249 "/products\.:format"

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 250–252why, one step back
250 /products\.json
251 /products\.xml252

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

line 253why, one step back
253 "/products\.:format?"

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 254–262why, one step back
254 /products\.json
255 /products\.xml256 /products257.258.fi259.260.IP "" 0261.262.SS "Passing Route Control"

Updated docsTj Holowaychukbefore adoptiond691ecb

line 263why, one step back
263We may pass control to the next \fImatching\fR route, by calling the \fIthird\fR argument, the \fInext()\fR function\. When a match cannot be made, control is passed back to Connect, and middleware continue to be invoked\.

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 264–268why, one step back
264.
265.IP "" 4266.267.nf268

Updated docsTj Holowaychukbefore adoptionb819366

line 269why, one step back
269app\.get(\'/users/:id?\', function(req, res, next){

Updated docsTj Holowaychukbefore adoptiond691ecb

lines 270–271why, one step back
270 var id = req\.params\.id;
271 if (id) {

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 272–277why, one step back
272 // do something
273 } else {274 next();275 }276});277

Updated docsTj Holowaychukbefore adoptiond691ecb

line 278why, one step back
278app\.get(\'/users\', function(req, res){

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 279–285why, one step back
279 // do something else
280});281.282.fi283.284.IP "" 0285.

HTTP verb docs. Closes #415Tj Holowaychukbefore adoptiond89b951

lines 286–373why, one step back
286.SS "HTTP Methods"
287We have seen \fIapp\.get()\fR a few times, however Express also exposes other familiar HTTP verbs in the same manor, such as \fIapp\.post()\fR, \fIapp\.del()\fR, etc\.288.289.P290A common example for \fIPOST\fR 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\.291.292.P293.294.IP "" 4295.296.nf297298 <input type="text" name="user[name]" />299 <input type="text" name="user[email]" />300 <input type="submit" value="Submit" />301.302.fi303.304.IP "" 0305.306.P307.308.P309By default Express does not know what to do with this request body, so we should add the \fIbodyDecoder\fR middleware, which will parse \fIapplication/x\-www\-form\-urlencoded\fR request bodies and place the variables in \fIreq\.body\fR\. We can do this by "using" the middleware as shown below:310.311.IP "" 4312.313.nf314315app\.use(express\.bodyDecoder());316.317.fi318.319.IP "" 0320.321.P322Our route below will now have access to the \fIreq\.body\.user\fR object which will contain the \fIname\fR and \fIemail\fR properties when defined\.323.324.IP "" 4325.326.nf327328app\.post(\'/\', function(req, res){329 console\.log(req\.body\.user);330 res\.redirect(\'back\');331});332.333.fi334.335.IP "" 0336.337.P338When using methods such as \fIPUT\fR with a form, we can utilize a hidden input named \fI_method\fR, which can be used to alter the HTTP method\. To do so we first need the \fImethodOverride\fR middleware, which should be placed below \fIbodyDecoder\fR so that it can utilize it\'s \fIreq\.body\fR containing the form values\.339.340.IP "" 4341.342.nf343344app\.use(express\.bodyDecoder());345app\.use(express\.methodOverride());346.347.fi348.349.IP "" 0350.351.P352The 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 \fIPUT\fR and \fIDELETE\fR can still be accessed by clients which can use them directly, although \fImethodOverride\fR provides a great solution for forms\. Below shows what the usage of \fIPUT\fR might look like:353.354.IP "" 4355.356.nf357358<form method="post" action="/">359 <input type="hidden" name="_method" value="put" />360 <input type="text" name="user[name]" />361 <input type="text" name="user[email]" />362 <input type="submit" value="Submit" />363</form>364365app\.put(\'/\', function(){366 console\.log(req\.body\.user);367 res\.redirect(\'back\');368});369.370.fi371.372.IP "" 0373.

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

line 374why, one step back
374.SS "Middleware"

Rebuilt docsTj Holowaychukbefore adoption2724ce3

line 375why, one step back
375Middleware via Connect \fIhttp://github\.com/senchalabs/connect\fR can be passed to \fIexpress\.createServer()\fR as you would with a regular Connect server\. For example:

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 376–380why, one step back
376.
377.IP "" 4378.379.nf380

Rebuilt docsTj Holowaychukbefore adoption2724ce3

line 381why, one step back
381 var express = require(\'express\');

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 382–383why, one step back
382
383var app = express\.createServer(

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

lines 384–385why, one step back
384 express\.logger(),
385 express\.bodyDecoder()

Rebuilt docsTj Holowaychukbefore adoption2724ce3

line 386why, one step back
386 );

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 387–398why, one step back
387.
388.fi389.390.IP "" 0391.392.P393Alternatively we can \fIuse()\fR them which is useful when adding middleware within \fIconfigure()\fR blocks:394.395.IP "" 4396.397.nf398

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

line 399why, one step back
399app\.use(express\.logger({ format: \':method :uri\' }));

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 400–404why, one step back
400.
401.fi402.403.IP "" 0404.

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

lines 405–431why, one step back
405.P
406Typically with connect middleware you would \fIrequire(\'connect\')\fR like so:407.408.IP "" 4409.410.nf411412var connect = require(\'connect\');413app\.use(connect\.logger());414.415.fi416.417.IP "" 0418.419.P420This is somewhat annoying, so express re\-exports these middleware properties, however they are \fIidentical\fR:421.422.IP "" 4423.424.nf425426app\.use(express\.logger());427.428.fi429.430.IP "" 0431.

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 432–461why, one step back
432.SS "Error Handling"
433Express provides the \fIapp\.error()\fR method which receives exceptions thrown within a route, or passed to \fInext(err)\fR\. Below is an example which serves different pages based on our ad\-hoc \fINotFound\fR exception:434.435.IP "" 4436.437.nf438439function NotFound(msg){440 this\.name = \'NotFound\';441 Error\.call(this, msg);442 Error\.captureStackTrace(this, arguments\.callee);443}444445sys\.inherits(NotFound, Error);446447app\.get(\'/404\', function(req, res){448 throw new NotFound;449});450451app\.get(\'/500\', function(req, res){452 throw new Error(\'keyboard cat!\');453});454.455.fi456.457.IP "" 0458.459.P460We can call \fIapp\.error()\fR several times as shown below\. Here we check for an instanceof \fINotFound\fR and show the 404 page, or we pass on to the next error handler\.461.

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

lines 462–464why, one step back
462.P
463Note that these handlers can be defined anywhere, as they will be placed below the route handlers on \fIlisten()\fR\. This allows for definition within \fIconfigure()\fR blocks so we can handle exceptions in different ways based on the environment\.464.

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 465–500why, one step back
465.IP "" 4
466.467.nf468469app\.error(function(err, req, res, next){470 if (err instanceof NotFound) {471 res\.render(\'404\.jade\');472 } else {473 next(err);474 }475});476.477.fi478.479.IP "" 0480.481.P482Here we assume all errors as 500 for the simplicity of this demo, however you can choose whatever you like483.484.IP "" 4485.486.nf487488app\.error(function(err, req, res){489 res\.render(\'500\.jade\', {490 locals: {491 error: err492 }493 });494});495.496.fi497.498.IP "" 0499.500.P

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

line 501why, one step back
501Our apps could also utilize the Connect \fIerrorHandler\fR middleware to report on exceptions\. For example if we wish to output exceptions in "development" mode to \fIstderr\fR we can use:

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 502–506why, one step back
502.
503.IP "" 4504.505.nf506

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

line 507why, one step back
507app\.use(express\.errorHandler({ dumpExceptions: true }));

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 508–519why, one step back
508.
509.fi510.511.IP "" 0512.513.P514Also during development we may want fancy html pages to show exceptions that are passed or thrown, so we can set \fIshowStack\fR to true:515.516.IP "" 4517.518.nf519

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

line 520why, one step back
520app\.use(express\.errorHandler({ showStack: true, dumpExceptions: true }));

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 521–529why, one step back
521.
522.fi523.524.IP "" 0525.526.P527The \fIerrorHandler\fR middleware also responds with \fIjson\fR if \fIAccept: application/json\fR is present, which is useful for developing apps that rely heavily on client\-side JavaScript\.528.529.SS "View Rendering"

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

line 530why, one step back
530View filenames take the form \fINAME\fR\.\fIENGINE\fR, where \fIENGINE\fR is the name of the module that will be required\. For example the view \fIlayout\.ejs\fR will tell the view system to \fIrequire(\'ejs\')\fR, the module being loaded must export the method \fIexports\.render(str, options)\fR to comply with Express, however \fIapp\.register()\fR can be used to map engines to file extensions, so that for example "foo\.html" can be rendered by jade\.

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 531–600why, one step back
531.
532.P533Below is an example using Haml\.js \fIhttp://github\.com/visionmedia/haml\.js\fR to render \fIindex\.html\fR, and since we do not use \fIlayout: false\fR the rendered contents of \fIindex\.html\fR will be passed as the \fIbody\fR local variable in \fIlayout\.haml\fR\.534.535.IP "" 4536.537.nf538539app\.get(\'/\', function(req, res){540 res\.render(\'index\.haml\', {541 locals: { title: \'My Site\' }542 });543});544.545.fi546.547.IP "" 0548.549.P550The new \fIview engine\fR setting allows us to specify our default template engine, so for example when using Jade \fIhttp://github\.com/visionmedia/jade\fR we could set:551.552.IP "" 4553.554.nf555556app\.set(\'view engine\', \'jade\');557.558.fi559.560.IP "" 0561.562.P563Allowing us to render with:564.565.IP "" 4566.567.nf568569res\.render(\'index\');570.571.fi572.573.IP "" 0574.575.P576vs:577.578.IP "" 4579.580.nf581582res\.render(\'index\.jade\');583.584.fi585.586.IP "" 0587.588.P589When \fIview engine\fR is set, extensions are entirely optional, however we can still mix and match template engines:590.591.IP "" 4592.593.nf594595res\.render(\'another\-page\.ejs\');596.597.fi598.599.IP "" 0600.

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

lines 601–629why, one step back
601.P
602Express also provides the \fIview options\fR setting, which is applied each time a view is rendered, so for example if you rarely use layouts you may set:603.604.IP "" 4605.606.nf607608app\.set(\'view options\', {609 layout: false610});611.612.fi613.614.IP "" 0615.616.P617Which can then be overridden within the \fBres\.render()\fR call if need be:618.619.IP "" 4620.621.nf622623res\.render(\'myview\.ejs\', { layout: true });624.625.fi626.627.IP "" 0628.629.P

Rebuilt docsTj Holowaychukbefore adoption2724ce3

lines 630–668why, one step back
630When an alternate layout is required, we may also specify a path\. For example if we have \fIview engine\fR set to \fIjade\fR and a file named \fI\./views/mylayout\.jade\fR we can simply pass:
631.632.IP "" 4633.634.nf635636res\.render(\'page\', { layout: \'mylayout\' });637.638.fi639.640.IP "" 0641.642.P643Otherwise we must specify the extension:644.645.IP "" 4646.647.nf648649res\.render(\'page\', { layout: \'mylayout\.jade\' });650.651.fi652.653.IP "" 0654.655.P656These paths may also be absolute:657.658.IP "" 4659.660.nf661662res\.render(\'page\', { layout: __dirname + \'/\.\./\.\./mylayout\.jade\' });663.664.fi665.666.IP "" 0667.668.P

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

lines 669–683why, one step back
669A good example of this is specifying custom \fIejs\fR opening and closing tags:
670.671.IP "" 4672.673.nf674675app\.set(\'view options\', {676 open: \'{{\',677 close: \'}}\'678});679.680.fi681.682.IP "" 0683.

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

line 684why, one step back
684.SS "View Partials"

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

line 685why, one step back
685The 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:

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 686–723why, one step back
686.
687.IP "" 4688.689.nf690691partial(\'comment\.haml\', { collection: comments });692.693.fi694.695.IP "" 0696.697.P698To 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\.699.700.IP "" 4701.702.nf703704partial(\'comment\', { collection: comments });705.706.fi707.708.IP "" 0709.710.P711And once again even further, when rendering a collection we can simply pass an array, if no other options are desired:712.713.IP "" 4714.715.nf716717partial(\'comments\', comments);718.719.fi720.721.IP "" 0722.723.P

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

line 724why, one step back
724When using the partial collection support a few "magic" variables are provided for free:

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 725–732why, one step back
725.
726.IP "\(bu" 4727\fIfirstInCollection\fR True if this is the first object728.729.IP "\(bu" 4730\fIindexInCollection\fR Index of the object in the collection731.732.IP "\(bu" 4

Merge branch 'fix-toc'Tj Holowaychukbefore adoptionbb18f87

line 733why, one step back
733\fIlastInCollection\fR True if this is the last object

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 734–740why, one step back
734.
735.IP "" 0736.737.SS "Template Engines"738Below are a few template engines commonly used with Express:739.740.IP "\(bu" 4

Merge branch 'fix-toc'Tj Holowaychukbefore adoptionbb18f87

line 741why, one step back
741Jade \fIhttp://jade\-lang\.com\fR haml\.js successor

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 742–743why, one step back
742.
743.IP "\(bu" 4

Merge branch 'fix-toc'Tj Holowaychukbefore adoptionbb18f87

line 744why, one step back
744Haml \fIhttp://github\.com/visionmedia/haml\.js\fR pythonic indented templates

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 745–747why, one step back
745.
746.IP "\(bu" 4747EJS \fIhttp://github\.com/visionmedia/ejs\fR Embedded JavaScript

Rebuilt docsTj Holowaychukbefore adoption2724ce3

lines 748–776why, one step back
748.
749.IP "" 0750.751.SS "Session Support"752Sessions 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\.753.754.IP "" 4755.756.nf757758app\.use(express\.cookieDecoder());759app\.use(express\.session());760.761.fi762.763.IP "" 0764.765.P766By default the \fIsession\fR middleware uses the memory store bundled with Connect, however many implementations exist\. For example connect\-redis \fIhttp://github\.com/visionmedia/connect\-redis\fR supplies a Redis \fIhttp://code\.google\.com/p/redis/\fR session store and can be used as shown below:767.768.IP "" 4769.770.nf771772var RedisStore = require(\'connect\-redis\');773app\.use(express\.cookieDecoder());774app\.use(express\.session({ store: new RedisStore }));775.776.fi

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 777–779why, one step back
777.
778.IP "" 0779.

Regenerated guideTj Holowaychukbefore adoptionf179979

lines 780–816why, one step back
780.P
781Now the \fIreq\.session\fR and \fIreq\.sessionStore\fR properties will be accessible to all routes and subsequent middleware\. Properties on \fIreq\.session\fR are automatically saved on a response, so for example if we wish to shopping cart data:782.783.IP "" 4784.785.nf786787var RedisStore = require(\'connect\-redis\');788app\.use(express\.cookieDecoder());789app\.use(express\.session({ store: new RedisStore }));790app\.use(express\.bodyDecoder());791792app\.post(\'/add\-to\-cart\', function(req, res){793 // Perhaps we posted several items with a form794 // (use the bodyDecoder() middleware for this)795 var items = req\.body\.items;796 req\.session\.items = items;797 res\.redirect(\'back\');798});799800app\.get(\'/add\-to\-cart\', function(req, res){801 // When redirected back to GET /add\-to\-cart802 // we could check req\.session\.items && req\.session\.items\.length803 // to print out a message804 if (req\.session\.items && req\.session\.items\.length) {805 req\.flash(\'info\', \'You have %s items in your cart\', req\.session\.items\.length);806 }807 res\.render(\'shopping\-cart\');808});809.810.fi811.812.IP "" 0813.814.P815The \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\.816.

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 817–835why, one step back
817.SS "req\.header(key[, defaultValue])"
818Get the case\-insensitive request header \fIkey\fR, with optional \fIdefaultValue\fR:819.820.IP "" 4821.822.nf823824req\.header(\'Host\');825req\.header(\'host\');826req\.header(\'Accept\', \'*/*\');827.828.fi829.830.IP "" 0831.832.SS "req\.accepts(type)"833Check if the \fIAccept\fR header is present, and includes the given \fItype\fR\.834.835.P

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

line 836why, one step back
836When the \fIAccept\fR header is not present \fItrue\fR is returned\. Otherwise the given \fItype\fR is matched by an exact match, and then subtypes\. You may pass the subtype such as "html" which is then converted internally to "text/html" using the mime lookup table\.

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 837–864why, one step back
837.
838.IP "" 4839.840.nf841842// Accept: text/html843req\.accepts(\'html\');844// => true845846// Accept: text/*; application/json847req\.accepts(\'html\');848req\.accepts(\'text/html\');849req\.accepts(\'text/plain\');850req\.accepts(\'application/json\');851// => true852853req\.accepts(\'image/png\');854req\.accepts(\'png\');855// => false856.857.fi858.859.IP "" 0860.861.SS "req\.param(name)"862Return the value of param \fIname\fR when present\.863.864.IP "\(bu" 4

Updated docsTj Holowaychukbefore adoptiond691ecb

line 865why, one step back
865Checks route placeholders (\fIreq\.params\fR), ex: /user/:id

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 866–867why, one step back
866.
867.IP "\(bu" 4

Updated docsTj Holowaychukbefore adoptiond691ecb

line 868why, one step back
868Checks query string params (\fIreq\.query\fR), ex: ?id=12

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 869–870why, one step back
869.
870.IP "\(bu" 4

Updated docsTj Holowaychukbefore adoptiond691ecb

line 871why, one step back
871Checks urlencoded body params (\fIreq\.body\fR), ex: id=12

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 872–875why, one step back
872.
873.IP "" 0874.875.P

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

line 876why, one step back
876To utilize urlencoded request bodies, \fIreq\.body\fR should be an object\. This can be done by using the \fIexpress\.bodyDecoder\fR middleware\.

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 877–902why, one step back
877.
878.SS "req\.flash(type[, msg])"879Queue flash \fImsg\fR of the given \fItype\fR\.880.881.IP "" 4882.883.nf884885req\.flash(\'info\', \'email sent\');886req\.flash(\'error\', \'email delivery failed\');887req\.flash(\'info\', \'email re\-sent\');888// => 2889890req\.flash(\'info\');891// => [\'email sent\', \'email re\-sent\']892893req\.flash(\'info\');894// => []895896req\.flash();897// => { error: [\'email delivery failed\'], info: [] }898.899.fi900.901.IP "" 0902.

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

lines 903–915why, one step back
903.P
904Flash notification message may also utilize formatters, by default only the %s string formatter is available:905.906.IP "" 4907.908.nf909910req\.flash(\'info\', \'email delivery to _%s_ from _%s_ failed\.\', toUser, fromUser);911.912.fi913.914.IP "" 0915.

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 916–958why, one step back
916.SS "req\.isXMLHttpRequest"
917Also aliased as \fIreq\.xhr\fR, this getter checks the \fIX\-Requested\-With\fR header to see if it was issued by an \fIXMLHttpRequest\fR:918.919.IP "" 4920.921.nf922923req\.xhr924req\.isXMLHttpRequest925.926.fi927.928.IP "" 0929.930.SS "res\.header(key[, val])"931Get or set the response header \fIkey\fR\.932.933.IP "" 4934.935.nf936937res\.header(\'Content\-Length\');938// => undefined939940res\.header(\'Content\-Length\', 123);941// => 123942943res\.header(\'Content\-Length\');944// => 123945.946.fi947.948.IP "" 0949.950.SS "res\.contentType(type)"951Sets the \fIContent\-Type\fR response header to the given \fItype\fR\.952.953.IP "" 4954.955.nf956957 var filename = \'path/to/image\.png\';958 res\.contentType(filename);

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

line 959why, one step back
959 // res\.headers[\'Content\-Type\'] is now "image/png"

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 960–965why, one step back
960.
961.fi962.963.IP "" 0964.965.SS "res\.attachment([filename])"

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

line 966why, one step back
966Sets the \fIContent\-Disposition\fR response header to "attachment", with optional \fIfilename\fR\.

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 967–991why, one step back
967.
968.IP "" 4969.970.nf971972 res\.attachment(\'path/to/my/image\.png\');973.974.fi975.976.IP "" 0977.978.SS "res\.sendfile(path)"979Used by \fBres\.download()\fR to transfer an arbitrary file\.980.981.IP "" 4982.983.nf984985res\.sendfile(\'path/to/my\.file\');986.987.fi988.989.IP "" 0990.991.P

Updated docsTj Holowaychukbefore adoptionf0eab06

lines 992–1011why, one step back
992This 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\.
993.994.IP "" 4995.996.nf997998res\.sendfile(path, function(err, path){999 if (err) {1000 // handle the error1001 } else {1002 console\.log(\'transferred %s\', path);1003 }1004});1005.1006.fi1007.1008.IP "" 01009.1010.P1011When the filesize exceeds the \fIstream threshold\fR (defaulting to 32k), the file will be streamed using \fIfs\.ReadStream\fR and \fIsys\.pump()\fR\.

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 1012–1047why, one step back
1012.
1013.SS "res\.download(file[, filename])"1014Transfer the given \fIfile\fR as an attachment with optional alternative \fIfilename\fR\.1015.1016.IP "" 41017.1018.nf10191020res\.download(\'path/to/image\.png\');1021res\.download(\'path/to/image\.png\', \'foo\.png\');1022.1023.fi1024.1025.IP "" 01026.1027.P1028This is equivalent to:1029.1030.IP "" 41031.1032.nf10331034res\.attachment(file);1035res\.sendfile(file);1036.1037.fi1038.1039.IP "" 01040.1041.SS "res\.send(body|status[, headers|status[, status]])"1042The \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:1043.1044.IP "" 41045.1046.nf1047

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

line 1048why, one step back
1048 res\.send(); // 204

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 1049–1080why, one step back
1049 res\.send(new Buffer(\'wahoo\'));
1050 res\.send({ some: \'json\' });1051 res\.send(\'<p>some html</p>\');1052 res\.send(\'Sorry, cant find that\', 404);1053 res\.send(\'text\', { \'Content\-Type\': \'text/plain\' }, 201);1054 res\.send(404);1055.1056.fi1057.1058.IP "" 01059.1060.P1061By 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\.1062.1063.SS "res\.redirect(url[, status])"1064Redirect to the given \fIurl\fR with a default response \fIstatus\fR of 302\.1065.1066.IP "" 41067.1068.nf10691070res\.redirect(\'/\', 301);1071res\.redirect(\'/account\');1072res\.redirect(\'http://google\.com\');1073res\.redirect(\'home\');1074res\.redirect(\'back\');1075.1076.fi1077.1078.IP "" 01079.1080.P

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

line 1081why, one step back
1081Express 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 "/"\.

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

line 1082why, one step back
1082.

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

lines 1083–1126why, one step back
1083.SS "res\.cookie(name, val[, options])"
1084Sets the given cookie \fIname\fR to \fIval\fR, with \fIoptions\fR such as "httpOnly: true", "expires", "secure" etc\.1085.1086.IP "" 41087.1088.nf10891090// "Remember me" for 15 minutes1091res\.cookie(\'rememberme\', \'yes\', { expires: new Date(Date\.now() + 900000), httpOnly: true });1092.1093.fi1094.1095.IP "" 01096.1097.P1098To parse incoming \fICookie\fR headers, use the \fIcookieDecoder\fR middleware, which provides the \fIreq\.cookies\fR object:1099.1100.IP "" 41101.1102.nf11031104app\.use(express\.cookieDecoder());11051106app\.get(\'/\', function(req, res){1107 // use req\.cookies\.rememberme1108});1109.1110.fi1111.1112.IP "" 01113.1114.SS "res\.clearCookie(name)"1115Clear cookie \fIname\fR by setting "expires" far in the past\.1116.1117.IP "" 41118.1119.nf11201121res\.clearCookie(\'rememberme\');1122.1123.fi1124.1125.IP "" 01126.

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 1127–1133why, one step back
1127.SS "res\.render(view[, options[, fn]])"
1128Render \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\.1129.1130.P1131Most 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:1132.1133.IP "\(bu" 4

HTTP verb docs. Closes #415Tj Holowaychukbefore adoptiond89b951

line 1134why, one step back
1134\fIscope\fR Template evaluation context (value of \fIthis\fR)

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 1135–1141why, one step back
1135.
1136.IP "\(bu" 41137\fIlocals\fR Object containing local variables1138.1139.IP "\(bu" 41140\fIdebug\fR Output debugging information1141.

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

lines 1142–1147why, one step back
1142.IP "\(bu" 4
1143\fIstatus\fR Response status code, defaults to 2001144.1145.IP "\(bu" 41146\fIheaders\fR Response headers object1147.

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 1148–1189why, one step back
1148.IP "" 0
1149.1150.SS "res\.partial(view[, options])"1151Render \fIview\fR partial with the given \fIoptions\fR\. This method is always available to the view as a local variable\.1152.1153.IP "\(bu" 41154\fIas\fR Variable name for each \fIcollection\fR value, defaults to the view name\.1155.1156.IP "\(bu" 41157as: \'something\' will add the \fIsomething\fR local variable1158.1159.IP "\(bu" 41160as: this will use the collection value as the template context1161.1162.IP "\(bu" 41163as: global will merge the collection value\'s properties with \fIlocals\fR1164.1165.IP "" 011661167.1168.IP "\(bu" 41169\fIcollection\fR Array of objects, the name is derived from the view name itself\. For example \fIvideo\.html\fR will have a object \fIvideo\fR available to it\.1170.1171.IP "" 01172.1173.P1174The following are equivalent, and the name of collection value when passed to the partial will be \fImovie\fR as derived from the name\.1175.1176.IP "" 41177.1178.nf11791180partial(\'movie\.jade\', { collection: movies });1181partial(\'movie\.jade\', movies);1182partial(\'movie\', movies);1183// In view: movie\.director1184.1185.fi1186.1187.IP "" 01188.1189.P

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

line 1190why, one step back
1190To change the local from \fImovie\fR to \fIvideo\fR we can use the "as" option:

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 1191–1217why, one step back
1191.
1192.IP "" 41193.1194.nf11951196partial(\'movie\', { collection: movies, as: \'video\' });1197// In view: video\.director1198.1199.fi1200.1201.IP "" 01202.1203.P1204Also we can make our movie the value of \fIthis\fR within our view so that instead of \fImovie\.director\fR we could use \fIthis\.director\fR\.1205.1206.IP "" 41207.1208.nf12091210partial(\'movie\', { collection: movies, as: this });1211// In view: this\.director1212.1213.fi1214.1215.IP "" 01216.1217.P

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

line 1218why, one step back
1218Another alternative is to "explode" the properties of the collection item into pseudo globals (local variables) by using \fIas: global\fR, which again is syntactic sugar:

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 1219–1237why, one step back
1219.
1220.IP "" 41221.1222.nf12231224partials(\'movie\', { collection: movies, as: global });1225// In view: director1226.1227.fi1228.1229.IP "" 01230.1231.SS "app\.set(name[, val])"1232Apply an application level setting \fIname\fR to \fIval\fR, or get the value of \fIname\fR when \fIval\fR is not present:1233.1234.IP "" 41235.1236.nf1237

Updated docsTj Holowaychukbefore adoption3234329

lines 1238–1240why, one step back
1238app\.set(\'views\', __dirname + \'/views\');
1239app\.set(\'views\');1240// => \.\.\.path\.\.\.

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 1241–1320why, one step back
1241.
1242.fi1243.1244.IP "" 01245.1246.SS "app\.enable(name)"1247Enable the given setting \fIname\fR:1248.1249.IP "" 41250.1251.nf12521253app\.enable(\'some arbitrary setting\');1254app\.set(\'some arbitrary setting\');1255// => true1256.1257.fi1258.1259.IP "" 01260.1261.SS "app\.disable(name)"1262Disable the given setting \fIname\fR:1263.1264.IP "" 41265.1266.nf12671268app\.disable(\'some setting\');1269app\.set(\'some setting\');1270// => false1271.1272.fi1273.1274.IP "" 01275.1276.SS "app\.configure(env|function[, function])"1277Define a callback function for the given \fIenv\fR (or all environments) with callback \fIfunction\fR:1278.1279.IP "" 41280.1281.nf12821283app\.configure(function(){1284 // executed for each env1285});12861287app\.configure(\'development\', function(){1288 // executed for \'development\' only1289});1290.1291.fi1292.1293.IP "" 01294.1295.SS "app\.redirect(name, val)"1296For use with \fBres\.redirect()\fR we can map redirects at the application level as shown below:1297.1298.IP "" 41299.1300.nf13011302app\.redirect(\'google\', \'http://google\.com\');1303.1304.fi1305.1306.IP "" 01307.1308.P1309Now in a route we may call:1310.1311.P1312res\.redirect(\'google\');1313.1314.P1315We may also map dynamic redirects:1316.1317.IP "" 41318.1319.nf1320

Updated docsTj Holowaychukbefore adoptiond691ecb

lines 1321–1322why, one step back
1321app\.redirect(\'comments\', function(req, res){
1322 return \'/post/\' + req\.params\.id + \'/comments\';

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 1323–1389why, one step back
1323});
1324.1325.fi1326.1327.IP "" 01328.1329.P1330So now we may do the following, and the redirect will dynamically adjust to the context of the request\. If we called this route with \fIGET /post/12\fR our redirect \fILocation\fR would be \fI/post/12/comments\fR\.1331.1332.IP "" 41333.1334.nf13351336app\.get(\'/post/:id\', function(req, res){1337 res\.redirect(\'comments\');1338});1339.1340.fi1341.1342.IP "" 01343.1344.SS "app\.error(function)"1345Adds 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:1346.1347.IP "" 41348.1349.nf13501351app\.error(function(err, req, res, next){1352 res\.send(err\.message, 500);1353});1354.1355.fi1356.1357.IP "" 01358.1359.SS "app\.helpers(obj)"1360Registers static view helpers\.1361.1362.IP "" 41363.1364.nf13651366app\.helpers({1367 name: function(first, last){ return first + \', \' + last },1368 firstName: \'tj\',1369 lastName: \'holowaychuk\'1370});1371.1372.fi1373.1374.IP "" 01375.1376.P1377Our view could now utilize the \fIfirstName\fR and \fIlastName\fR variables, as well as the \fIname()\fR function exposed\.1378.1379.IP "" 41380.1381.nf13821383<%= name(firstName, lastName) %>1384.1385.fi1386.1387.IP "" 01388.1389.SS "app\.dynamicHelpers(obj)"

Updated docsTj Holowaychukbefore adoptiond691ecb

line 1390why, one step back
1390Registers dynamic view helpers\. Dynamic view helpers are simply functions which accept \fIreq\fR, \fIres\fR, and are evaluated against the \fIServer\fR instance before a view is rendered\. The \fIreturn value\fR of this function becomes the local variable it is associated with\.

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 1391–1396why, one step back
1391.
1392.IP "" 41393.1394.nf13951396app\.dynamicHelpers({

Updated docsTj Holowaychukbefore adoptiond691ecb

line 1397why, one step back
1397 session: function(req, res){

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 1398–1413why, one step back
1398 return req\.session;
1399 }1400});1401.1402.fi1403.1404.IP "" 01405.1406.P1407All views would now have \fIsession\fR available so that session data can be accessed via \fIsession\.name\fR etc:1408.1409.IP "" 41410.1411.nf14121413<%= session\.name %>

Rebuilt docsTj Holowaychukbefore adoptionba88e48

lines 1414–1430why, one step back
1414.
1415.fi1416.1417.IP "" 01418.1419.SS "app\.mounted(fn)"1420Assign a callback \fIfn\fR which is called when this \fIServer\fR is passed to \fIServer#use()\fR\.1421.1422.IP "" 41423.1424.nf14251426var app = express\.createServer(),1427 blog = express\.createServer();14281429blog\.mounted(function(parent){1430 // parent is app

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

line 1431why, one step back
1431 // "this" is blog

Rebuilt docsTj Holowaychukbefore adoptionba88e48

lines 1432–1434why, one step back
1432});
14331434app\.use(blog);

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

lines 1435–1440why, one step back
1435.
1436.fi1437.1438.IP "" 01439.1440.SS "app\.register(ext, exports)"

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

line 1441why, one step back
1441Register the given template engine \fIexports\fR as \fIext\fR\. For example we may wish to map "\.html" files to jade:

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

lines 1442–1453why, one step back
1442.
1443.IP "" 41444.1445.nf14461447 app\.register(\'\.html\', require(\'jade\'));1448.1449.fi1450.1451.IP "" 01452.1453.P

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

line 1454why, one step back
1454This is also useful for libraries that may not match extensions correctly\. For example my haml\.js library is installed from npm as "hamljs" so instead of layout\.hamljs, we can register the engine as "\.haml":

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

lines 1455–1478why, one step back
1455.
1456.IP "" 41457.1458.nf14591460 app\.register(\'\.haml\', require(\'haml\-js\'));1461.1462.fi1463.1464.IP "" 01465.1466.P1467For engines that do not comply with the Express specification, we can also wrap their api this way\.1468.1469.IP "" 41470.1471.nf14721473 app\.register(\'\.foo\', {1474 render: function(str, options) {1475 // perhaps their api is1476 // foo\.toHTML(str, options);1477 }1478 });

Split blog example into another fileTj Holowaychukbefore adoptiona65c203

lines 1479–1531why, one step back
1479.
1480.fi1481.1482.IP "" 01483.1484.SS "app\.listen([port[, host]])"1485Bind the app server to the given \fIport\fR, which defaults to 3000\. When \fIhost\fR is omitted all connections will be accepted via \fIINADDR_ANY\fR\.1486.1487.IP "" 41488.1489.nf14901491app\.listen();1492app\.listen(3000);1493app\.listen(3000, \'n\.n\.n\.n\');1494.1495.fi1496.1497.IP "" 01498.1499.P1500The \fIport\fR argument may also be a string representing the path to a unix domain socket:1501.1502.IP "" 41503.1504.nf15051506app\.listen(\'/tmp/express\.sock\');1507.1508.fi1509.1510.IP "" 01511.1512.P1513Then try it out:1514.1515.IP "" 41516.1517.nf15181519$ telnet /tmp/express\.sock1520GET / HTTP/1\.115211522HTTP/1\.1 200 OK1523Content\-Type: text/plain1524Content\-Length: 1115251526Hello World1527.1528.fi1529.1530.IP "" 01531