Skip to content
Myr

guide.1

This file

Path

docs/guide.1

1,494 lines · 29.7 KiB

Last changed by

Rebuilt docsTj Holowaychukbefore adoption2724ce3

History of this file

Take it

The walk stopped after 200 commits: lines older than 6e5af3d 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

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

line 3why, one step back
3.

HTTP verb docs. Closes #415Tj Holowaychukbefore adoptiond89b951

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

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

line 5why, one step back
5.

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

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

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

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

No "or similar" for curl installTj Holowaychukbefore adoption56a1dd9

line 10why, one step back
10curl:

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

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\.

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

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\'));

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

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 }));

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

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());

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

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 \.\.\."

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

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:

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

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

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

lines 129–137why, 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" 4

Updated docsTj Holowaychukbefore adoption4d32762

line 138why, one step back
138\fIenv\fR Application environment set internally, use \fIapp\.set(\'env\')\fR on \fIServer#listen()\fR

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

lines 139–140why, one step back
139.
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\.

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

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.

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

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\.

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

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);

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

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/?

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

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\.

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

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);

Docs for params.captures. Closes #358Tj Holowaychukbefore adoptiond03821b

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]

Docs for params.captures. Closes #358Tj Holowaychukbefore adoptiond03821b

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"]

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

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"

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

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

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

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

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

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

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

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

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

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/*\.*"

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

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?"

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

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"

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

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?"

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

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\.

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

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) {

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

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){

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

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.

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

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:

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

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\');

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

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 );

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

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\' }));

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

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.

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

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.

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

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:

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

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 }));

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

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 }));

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

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\.

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

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.

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

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:

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

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:

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

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

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

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

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

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

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

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

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

lines 777–798why, one step back
777.
778.IP "" 0779.780.SS "req\.header(key[, defaultValue])"781Get the case\-insensitive request header \fIkey\fR, with optional \fIdefaultValue\fR:782.783.IP "" 4784.785.nf786787req\.header(\'Host\');788req\.header(\'host\');789req\.header(\'Accept\', \'*/*\');790.791.fi792.793.IP "" 0794.795.SS "req\.accepts(type)"796Check if the \fIAccept\fR header is present, and includes the given \fItype\fR\.797.798.P

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

line 799why, one step back
799When 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\.

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

lines 800–827why, one step back
800.
801.IP "" 4802.803.nf804805// Accept: text/html806req\.accepts(\'html\');807// => true808809// Accept: text/*; application/json810req\.accepts(\'html\');811req\.accepts(\'text/html\');812req\.accepts(\'text/plain\');813req\.accepts(\'application/json\');814// => true815816req\.accepts(\'image/png\');817req\.accepts(\'png\');818// => false819.820.fi821.822.IP "" 0823.824.SS "req\.param(name)"825Return the value of param \fIname\fR when present\.826.827.IP "\(bu" 4

Updated docsTj Holowaychukbefore adoptiond691ecb

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

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

lines 829–830why, one step back
829.
830.IP "\(bu" 4

Updated docsTj Holowaychukbefore adoptiond691ecb

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

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

lines 832–833why, one step back
832.
833.IP "\(bu" 4

Updated docsTj Holowaychukbefore adoptiond691ecb

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

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

lines 835–838why, one step back
835.
836.IP "" 0837.838.P

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

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

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

lines 840–865why, one step back
840.
841.SS "req\.flash(type[, msg])"842Queue flash \fImsg\fR of the given \fItype\fR\.843.844.IP "" 4845.846.nf847848req\.flash(\'info\', \'email sent\');849req\.flash(\'error\', \'email delivery failed\');850req\.flash(\'info\', \'email re\-sent\');851// => 2852853req\.flash(\'info\');854// => [\'email sent\', \'email re\-sent\']855856req\.flash(\'info\');857// => []858859req\.flash();860// => { error: [\'email delivery failed\'], info: [] }861.862.fi863.864.IP "" 0865.

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

lines 866–878why, one step back
866.P
867Flash notification message may also utilize formatters, by default only the %s string formatter is available:868.869.IP "" 4870.871.nf872873req\.flash(\'info\', \'email delivery to _%s_ from _%s_ failed\.\', toUser, fromUser);874.875.fi876.877.IP "" 0878.

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

lines 879–921why, one step back
879.SS "req\.isXMLHttpRequest"
880Also aliased as \fIreq\.xhr\fR, this getter checks the \fIX\-Requested\-With\fR header to see if it was issued by an \fIXMLHttpRequest\fR:881.882.IP "" 4883.884.nf885886req\.xhr887req\.isXMLHttpRequest888.889.fi890.891.IP "" 0892.893.SS "res\.header(key[, val])"894Get or set the response header \fIkey\fR\.895.896.IP "" 4897.898.nf899900res\.header(\'Content\-Length\');901// => undefined902903res\.header(\'Content\-Length\', 123);904// => 123905906res\.header(\'Content\-Length\');907// => 123908.909.fi910.911.IP "" 0912.913.SS "res\.contentType(type)"914Sets the \fIContent\-Type\fR response header to the given \fItype\fR\.915.916.IP "" 4917.918.nf919920 var filename = \'path/to/image\.png\';921 res\.contentType(filename);

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

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

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

lines 923–928why, one step back
923.
924.fi925.926.IP "" 0927.928.SS "res\.attachment([filename])"

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

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

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

lines 930–954why, one step back
930.
931.IP "" 4932.933.nf934935 res\.attachment(\'path/to/my/image\.png\');936.937.fi938.939.IP "" 0940.941.SS "res\.sendfile(path)"942Used by \fBres\.download()\fR to transfer an arbitrary file\.943.944.IP "" 4945.946.nf947948res\.sendfile(\'path/to/my\.file\');949.950.fi951.952.IP "" 0953.954.P

Updated docsTj Holowaychukbefore adoptionf0eab06

lines 955–974why, one step back
955This 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\.
956.957.IP "" 4958.959.nf960961res\.sendfile(path, function(err, path){962 if (err) {963 // handle the error964 } else {965 console\.log(\'transferred %s\', path);966 }967});968.969.fi970.971.IP "" 0972.973.P974When the filesize exceeds the \fIstream threshold\fR (defaulting to 32k), the file will be streamed using \fIfs\.ReadStream\fR and \fIsys\.pump()\fR\.

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

lines 975–1010why, one step back
975.
976.SS "res\.download(file[, filename])"977Transfer the given \fIfile\fR as an attachment with optional alternative \fIfilename\fR\.978.979.IP "" 4980.981.nf982983res\.download(\'path/to/image\.png\');984res\.download(\'path/to/image\.png\', \'foo\.png\');985.986.fi987.988.IP "" 0989.990.P991This is equivalent to:992.993.IP "" 4994.995.nf996997res\.attachment(file);998res\.sendfile(file);999.1000.fi1001.1002.IP "" 01003.1004.SS "res\.send(body|status[, headers|status[, status]])"1005The \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:1006.1007.IP "" 41008.1009.nf1010

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

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

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

lines 1012–1043why, one step back
1012 res\.send(new Buffer(\'wahoo\'));
1013 res\.send({ some: \'json\' });1014 res\.send(\'<p>some html</p>\');1015 res\.send(\'Sorry, cant find that\', 404);1016 res\.send(\'text\', { \'Content\-Type\': \'text/plain\' }, 201);1017 res\.send(404);1018.1019.fi1020.1021.IP "" 01022.1023.P1024By 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\.1025.1026.SS "res\.redirect(url[, status])"1027Redirect to the given \fIurl\fR with a default response \fIstatus\fR of 302\.1028.1029.IP "" 41030.1031.nf10321033res\.redirect(\'/\', 301);1034res\.redirect(\'/account\');1035res\.redirect(\'http://google\.com\');1036res\.redirect(\'home\');1037res\.redirect(\'back\');1038.1039.fi1040.1041.IP "" 01042.1043.P

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

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

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

line 1045why, one step back
1045.

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

lines 1046–1089why, one step back
1046.SS "res\.cookie(name, val[, options])"
1047Sets the given cookie \fIname\fR to \fIval\fR, with \fIoptions\fR such as "httpOnly: true", "expires", "secure" etc\.1048.1049.IP "" 41050.1051.nf10521053// "Remember me" for 15 minutes1054res\.cookie(\'rememberme\', \'yes\', { expires: new Date(Date\.now() + 900000), httpOnly: true });1055.1056.fi1057.1058.IP "" 01059.1060.P1061To parse incoming \fICookie\fR headers, use the \fIcookieDecoder\fR middleware, which provides the \fIreq\.cookies\fR object:1062.1063.IP "" 41064.1065.nf10661067app\.use(express\.cookieDecoder());10681069app\.get(\'/\', function(req, res){1070 // use req\.cookies\.rememberme1071});1072.1073.fi1074.1075.IP "" 01076.1077.SS "res\.clearCookie(name)"1078Clear cookie \fIname\fR by setting "expires" far in the past\.1079.1080.IP "" 41081.1082.nf10831084res\.clearCookie(\'rememberme\');1085.1086.fi1087.1088.IP "" 01089.

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

lines 1090–1096why, one step back
1090.SS "res\.render(view[, options[, fn]])"
1091Render \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\.1092.1093.P1094Most 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:1095.1096.IP "\(bu" 4

HTTP verb docs. Closes #415Tj Holowaychukbefore adoptiond89b951

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

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

lines 1098–1104why, one step back
1098.
1099.IP "\(bu" 41100\fIlocals\fR Object containing local variables1101.1102.IP "\(bu" 41103\fIdebug\fR Output debugging information1104.

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

lines 1105–1110why, one step back
1105.IP "\(bu" 4
1106\fIstatus\fR Response status code, defaults to 2001107.1108.IP "\(bu" 41109\fIheaders\fR Response headers object1110.

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

lines 1111–1152why, one step back
1111.IP "" 0
1112.1113.SS "res\.partial(view[, options])"1114Render \fIview\fR partial with the given \fIoptions\fR\. This method is always available to the view as a local variable\.1115.1116.IP "\(bu" 41117\fIas\fR Variable name for each \fIcollection\fR value, defaults to the view name\.1118.1119.IP "\(bu" 41120as: \'something\' will add the \fIsomething\fR local variable1121.1122.IP "\(bu" 41123as: this will use the collection value as the template context1124.1125.IP "\(bu" 41126as: global will merge the collection value\'s properties with \fIlocals\fR1127.1128.IP "" 011291130.1131.IP "\(bu" 41132\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\.1133.1134.IP "" 01135.1136.P1137The following are equivalent, and the name of collection value when passed to the partial will be \fImovie\fR as derived from the name\.1138.1139.IP "" 41140.1141.nf11421143partial(\'movie\.jade\', { collection: movies });1144partial(\'movie\.jade\', movies);1145partial(\'movie\', movies);1146// In view: movie\.director1147.1148.fi1149.1150.IP "" 01151.1152.P

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

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

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

lines 1154–1180why, one step back
1154.
1155.IP "" 41156.1157.nf11581159partial(\'movie\', { collection: movies, as: \'video\' });1160// In view: video\.director1161.1162.fi1163.1164.IP "" 01165.1166.P1167Also 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\.1168.1169.IP "" 41170.1171.nf11721173partial(\'movie\', { collection: movies, as: this });1174// In view: this\.director1175.1176.fi1177.1178.IP "" 01179.1180.P

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

line 1181why, one step back
1181Another 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:

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

lines 1182–1200why, one step back
1182.
1183.IP "" 41184.1185.nf11861187partials(\'movie\', { collection: movies, as: global });1188// In view: director1189.1190.fi1191.1192.IP "" 01193.1194.SS "app\.set(name[, val])"1195Apply an application level setting \fIname\fR to \fIval\fR, or get the value of \fIname\fR when \fIval\fR is not present:1196.1197.IP "" 41198.1199.nf1200

Updated docsTj Holowaychukbefore adoption3234329

lines 1201–1203why, one step back
1201app\.set(\'views\', __dirname + \'/views\');
1202app\.set(\'views\');1203// => \.\.\.path\.\.\.

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

lines 1204–1283why, one step back
1204.
1205.fi1206.1207.IP "" 01208.1209.SS "app\.enable(name)"1210Enable the given setting \fIname\fR:1211.1212.IP "" 41213.1214.nf12151216app\.enable(\'some arbitrary setting\');1217app\.set(\'some arbitrary setting\');1218// => true1219.1220.fi1221.1222.IP "" 01223.1224.SS "app\.disable(name)"1225Disable the given setting \fIname\fR:1226.1227.IP "" 41228.1229.nf12301231app\.disable(\'some setting\');1232app\.set(\'some setting\');1233// => false1234.1235.fi1236.1237.IP "" 01238.1239.SS "app\.configure(env|function[, function])"1240Define a callback function for the given \fIenv\fR (or all environments) with callback \fIfunction\fR:1241.1242.IP "" 41243.1244.nf12451246app\.configure(function(){1247 // executed for each env1248});12491250app\.configure(\'development\', function(){1251 // executed for \'development\' only1252});1253.1254.fi1255.1256.IP "" 01257.1258.SS "app\.redirect(name, val)"1259For use with \fBres\.redirect()\fR we can map redirects at the application level as shown below:1260.1261.IP "" 41262.1263.nf12641265app\.redirect(\'google\', \'http://google\.com\');1266.1267.fi1268.1269.IP "" 01270.1271.P1272Now in a route we may call:1273.1274.P1275res\.redirect(\'google\');1276.1277.P1278We may also map dynamic redirects:1279.1280.IP "" 41281.1282.nf1283

Updated docsTj Holowaychukbefore adoptiond691ecb

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

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

lines 1286–1352why, one step back
1286});
1287.1288.fi1289.1290.IP "" 01291.1292.P1293So 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\.1294.1295.IP "" 41296.1297.nf12981299app\.get(\'/post/:id\', function(req, res){1300 res\.redirect(\'comments\');1301});1302.1303.fi1304.1305.IP "" 01306.1307.SS "app\.error(function)"1308Adds 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:1309.1310.IP "" 41311.1312.nf13131314app\.error(function(err, req, res, next){1315 res\.send(err\.message, 500);1316});1317.1318.fi1319.1320.IP "" 01321.1322.SS "app\.helpers(obj)"1323Registers static view helpers\.1324.1325.IP "" 41326.1327.nf13281329app\.helpers({1330 name: function(first, last){ return first + \', \' + last },1331 firstName: \'tj\',1332 lastName: \'holowaychuk\'1333});1334.1335.fi1336.1337.IP "" 01338.1339.P1340Our view could now utilize the \fIfirstName\fR and \fIlastName\fR variables, as well as the \fIname()\fR function exposed\.1341.1342.IP "" 41343.1344.nf13451346<%= name(firstName, lastName) %>1347.1348.fi1349.1350.IP "" 01351.1352.SS "app\.dynamicHelpers(obj)"

Updated docsTj Holowaychukbefore adoptiond691ecb

line 1353why, one step back
1353Registers 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\.

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

lines 1354–1359why, one step back
1354.
1355.IP "" 41356.1357.nf13581359app\.dynamicHelpers({

Updated docsTj Holowaychukbefore adoptiond691ecb

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

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

lines 1361–1376why, one step back
1361 return req\.session;
1362 }1363});1364.1365.fi1366.1367.IP "" 01368.1369.P1370All views would now have \fIsession\fR available so that session data can be accessed via \fIsession\.name\fR etc:1371.1372.IP "" 41373.1374.nf13751376<%= session\.name %>

Rebuilt docsTj Holowaychukbefore adoptionba88e48

lines 1377–1393why, one step back
1377.
1378.fi1379.1380.IP "" 01381.1382.SS "app\.mounted(fn)"1383Assign a callback \fIfn\fR which is called when this \fIServer\fR is passed to \fIServer#use()\fR\.1384.1385.IP "" 41386.1387.nf13881389var app = express\.createServer(),1390 blog = express\.createServer();13911392blog\.mounted(function(parent){1393 // parent is app

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

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

Rebuilt docsTj Holowaychukbefore adoptionba88e48

lines 1395–1397why, one step back
1395});
13961397app\.use(blog);

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

lines 1398–1403why, one step back
1398.
1399.fi1400.1401.IP "" 01402.1403.SS "app\.register(ext, exports)"

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

line 1404why, one step back
1404Register 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 1405–1416why, one step back
1405.
1406.IP "" 41407.1408.nf14091410 app\.register(\'\.html\', require(\'jade\'));1411.1412.fi1413.1414.IP "" 01415.1416.P

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

line 1417why, one step back
1417This 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 1418–1441why, one step back
1418.
1419.IP "" 41420.1421.nf14221423 app\.register(\'\.haml\', require(\'haml\-js\'));1424.1425.fi1426.1427.IP "" 01428.1429.P1430For engines that do not comply with the Express specification, we can also wrap their api this way\.1431.1432.IP "" 41433.1434.nf14351436 app\.register(\'\.foo\', {1437 render: function(str, options) {1438 // perhaps their api is1439 // foo\.toHTML(str, options);1440 }1441 });

Added mkdir to install.sh Closes #362Tj Holowaychukbefore adoption6e5af3d

lines 1442–1494why, one step back
1442.
1443.fi1444.1445.IP "" 01446.1447.SS "app\.listen([port[, host]])"1448Bind 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\.1449.1450.IP "" 41451.1452.nf14531454app\.listen();1455app\.listen(3000);1456app\.listen(3000, \'n\.n\.n\.n\');1457.1458.fi1459.1460.IP "" 01461.1462.P1463The \fIport\fR argument may also be a string representing the path to a unix domain socket:1464.1465.IP "" 41466.1467.nf14681469app\.listen(\'/tmp/express\.sock\');1470.1471.fi1472.1473.IP "" 01474.1475.P1476Then try it out:1477.1478.IP "" 41479.1480.nf14811482$ telnet /tmp/express\.sock1483GET / HTTP/1\.114841485HTTP/1\.1 200 OK1486Content\-Type: text/plain1487Content\-Length: 1114881489Hello World1490.1491.fi1492.1493.IP "" 01494