Skip to content
Myr

guide.1

This file

Path

docs/guide.1

1,426 lines · 28.4 KiB

Last changed by

Merge branch 'master' into rangeTj Holowaychukbefore adoptionc3365aa

History of this file

Take it

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

line 3why, one step back
3.

Merge branch 'master' into rangeTj Holowaychukbefore adoptionc3365aa

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

line 5why, one step back
5.

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

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

No "or similar" for curl installTj Holowaychukbefore adoption56a1dd9

line 10why, one step back
10curl:

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 11–15why, one step back
11.
12.IP "" 413.14.nf15

Updated curl install docsTj Holowaychukbefore adoption3ceb0de

line 16why, one step back
16$ curl \-# http://expressjs\.com/install\.sh | sh

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 17–22why, one step back
17.
18.fi19.20.IP "" 021.22.P

Updated curl install docsTj Holowaychukbefore adoption3ceb0de

line 23why, one step back
23npm:

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 24–28why, one step back
24.
25.IP "" 426.27.nf28

Updated curl install docsTj Holowaychukbefore adoption3ceb0de

line 29why, one step back
29$ npm install express

Docs for session exampleTj Holowaychukbefore adoptionc53883b

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 109–137why, 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.P122To alter the environment we can set the \fICONNECT_ENV\fR environment variable, or more specifically \fIEXPRESS_ENV\fR, for example:123.124.IP "" 4125.126.nf127128$ EXPRESS_ENV=production node app\.js129.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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

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.

Docs for session exampleTj Holowaychukbefore adoptionc53883b

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

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"

Docs for session exampleTj Holowaychukbefore adoptionc53883b

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

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

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

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

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"

Docs for session exampleTj Holowaychukbefore adoptionc53883b

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

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

Merge branch 'master' into rangeTj Holowaychukbefore adoptionc3365aa

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.

Docs for session exampleTj Holowaychukbefore adoptionc53883b

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

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

line 375why, one step back
375The Express \fIPlugin\fR is no more! middleware 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:

Docs for session exampleTj Holowaychukbefore adoptionc53883b

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

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 386–398why, one step back
386);
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\' }));

Docs for session exampleTj Holowaychukbefore adoptionc53883b

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.

Docs for session exampleTj Holowaychukbefore adoptionc53883b

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.

Docs for session exampleTj Holowaychukbefore adoptionc53883b

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:

Docs for session exampleTj Holowaychukbefore adoptionc53883b

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

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–644why, 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.P630A good example of this is specifying custom \fIejs\fR opening and closing tags:631.632.IP "" 4633.634.nf635636app\.set(\'view options\', {637 open: \'{{\',638 close: \'}}\'639});640.641.fi642.643.IP "" 0644.

Docs for session exampleTj Holowaychukbefore adoptionc53883b

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

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 647–684why, one step back
647.
648.IP "" 4649.650.nf651652partial(\'comment\.haml\', { collection: comments });653.654.fi655.656.IP "" 0657.658.P659To 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\.660.661.IP "" 4662.663.nf664665partial(\'comment\', { collection: comments });666.667.fi668.669.IP "" 0670.671.P672And once again even further, when rendering a collection we can simply pass an array, if no other options are desired:673.674.IP "" 4675.676.nf677678partial(\'comments\', comments);679.680.fi681.682.IP "" 0683.684.P

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 686–693why, one step back
686.
687.IP "\(bu" 4688\fIfirstInCollection\fR True if this is the first object689.690.IP "\(bu" 4691\fIindexInCollection\fR Index of the object in the collection692.693.IP "\(bu" 4

Merge branch 'fix-toc'Tj Holowaychukbefore adoptionbb18f87

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 695–701why, one step back
695.
696.IP "" 0697.698.SS "Template Engines"699Below are a few template engines commonly used with Express:700.701.IP "\(bu" 4

Merge branch 'fix-toc'Tj Holowaychukbefore adoptionbb18f87

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 703–704why, one step back
703.
704.IP "\(bu" 4

Merge branch 'fix-toc'Tj Holowaychukbefore adoptionbb18f87

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 706–730why, one step back
706.
707.IP "\(bu" 4708EJS \fIhttp://github\.com/visionmedia/ejs\fR Embedded JavaScript709.710.IP "" 0711.712.SS "req\.header(key[, defaultValue])"713Get the case\-insensitive request header \fIkey\fR, with optional \fIdefaultValue\fR:714.715.IP "" 4716.717.nf718719req\.header(\'Host\');720req\.header(\'host\');721req\.header(\'Accept\', \'*/*\');722.723.fi724.725.IP "" 0726.727.SS "req\.accepts(type)"728Check if the \fIAccept\fR header is present, and includes the given \fItype\fR\.729.730.P

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 732–759why, one step back
732.
733.IP "" 4734.735.nf736737// Accept: text/html738req\.accepts(\'html\');739// => true740741// Accept: text/*; application/json742req\.accepts(\'html\');743req\.accepts(\'text/html\');744req\.accepts(\'text/plain\');745req\.accepts(\'application/json\');746// => true747748req\.accepts(\'image/png\');749req\.accepts(\'png\');750// => false751.752.fi753.754.IP "" 0755.756.SS "req\.param(name)"757Return the value of param \fIname\fR when present\.758.759.IP "\(bu" 4

Updated docsTj Holowaychukbefore adoptiond691ecb

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 761–762why, one step back
761.
762.IP "\(bu" 4

Updated docsTj Holowaychukbefore adoptiond691ecb

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 764–765why, one step back
764.
765.IP "\(bu" 4

Updated docsTj Holowaychukbefore adoptiond691ecb

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 767–770why, one step back
767.
768.IP "" 0769.770.P

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 772–797why, one step back
772.
773.SS "req\.flash(type[, msg])"774Queue flash \fImsg\fR of the given \fItype\fR\.775.776.IP "" 4777.778.nf779780req\.flash(\'info\', \'email sent\');781req\.flash(\'error\', \'email delivery failed\');782req\.flash(\'info\', \'email re\-sent\');783// => 2784785req\.flash(\'info\');786// => [\'email sent\', \'email re\-sent\']787788req\.flash(\'info\');789// => []790791req\.flash();792// => { error: [\'email delivery failed\'], info: [] }793.794.fi795.796.IP "" 0797.

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

lines 798–810why, one step back
798.P
799Flash notification message may also utilize formatters, by default only the %s string formatter is available:800.801.IP "" 4802.803.nf804805req\.flash(\'info\', \'email delivery to _%s_ from _%s_ failed\.\', toUser, fromUser);806.807.fi808.809.IP "" 0810.

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 811–853why, one step back
811.SS "req\.isXMLHttpRequest"
812Also aliased as \fIreq\.xhr\fR, this getter checks the \fIX\-Requested\-With\fR header to see if it was issued by an \fIXMLHttpRequest\fR:813.814.IP "" 4815.816.nf817818req\.xhr819req\.isXMLHttpRequest820.821.fi822.823.IP "" 0824.825.SS "res\.header(key[, val])"826Get or set the response header \fIkey\fR\.827.828.IP "" 4829.830.nf831832res\.header(\'Content\-Length\');833// => undefined834835res\.header(\'Content\-Length\', 123);836// => 123837838res\.header(\'Content\-Length\');839// => 123840.841.fi842.843.IP "" 0844.845.SS "res\.contentType(type)"846Sets the \fIContent\-Type\fR response header to the given \fItype\fR\.847.848.IP "" 4849.850.nf851852 var filename = \'path/to/image\.png\';853 res\.contentType(filename);

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 855–860why, one step back
855.
856.fi857.858.IP "" 0859.860.SS "res\.attachment([filename])"

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 862–886why, one step back
862.
863.IP "" 4864.865.nf866867 res\.attachment(\'path/to/my/image\.png\');868.869.fi870.871.IP "" 0872.873.SS "res\.sendfile(path)"874Used by \fBres\.download()\fR to transfer an arbitrary file\.875.876.IP "" 4877.878.nf879880res\.sendfile(\'path/to/my\.file\');881.882.fi883.884.IP "" 0885.886.P

Updated docsTj Holowaychukbefore adoptionf0eab06

lines 887–906why, one step back
887This 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\.
888.889.IP "" 4890.891.nf892893res\.sendfile(path, function(err, path){894 if (err) {895 // handle the error896 } else {897 console\.log(\'transferred %s\', path);898 }899});900.901.fi902.903.IP "" 0904.905.P906When the filesize exceeds the \fIstream threshold\fR (defaulting to 32k), the file will be streamed using \fIfs\.ReadStream\fR and \fIsys\.pump()\fR\.

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 907–942why, one step back
907.
908.SS "res\.download(file[, filename])"909Transfer the given \fIfile\fR as an attachment with optional alternative \fIfilename\fR\.910.911.IP "" 4912.913.nf914915res\.download(\'path/to/image\.png\');916res\.download(\'path/to/image\.png\', \'foo\.png\');917.918.fi919.920.IP "" 0921.922.P923This is equivalent to:924.925.IP "" 4926.927.nf928929res\.attachment(file);930res\.sendfile(file);931.932.fi933.934.IP "" 0935.936.SS "res\.send(body|status[, headers|status[, status]])"937The \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:938.939.IP "" 4940.941.nf942

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 944–975why, one step back
944 res\.send(new Buffer(\'wahoo\'));
945 res\.send({ some: \'json\' });946 res\.send(\'<p>some html</p>\');947 res\.send(\'Sorry, cant find that\', 404);948 res\.send(\'text\', { \'Content\-Type\': \'text/plain\' }, 201);949 res\.send(404);950.951.fi952.953.IP "" 0954.955.P956By 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\.957.958.SS "res\.redirect(url[, status])"959Redirect to the given \fIurl\fR with a default response \fIstatus\fR of 302\.960.961.IP "" 4962.963.nf964965res\.redirect(\'/\', 301);966res\.redirect(\'/account\');967res\.redirect(\'http://google\.com\');968res\.redirect(\'home\');969res\.redirect(\'back\');970.971.fi972.973.IP "" 0974.975.P

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

line 977why, one step back
977.

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

lines 978–1021why, one step back
978.SS "res\.cookie(name, val[, options])"
979Sets the given cookie \fIname\fR to \fIval\fR, with \fIoptions\fR such as "httpOnly: true", "expires", "secure" etc\.980.981.IP "" 4982.983.nf984985// "Remember me" for 15 minutes986res\.cookie(\'rememberme\', \'yes\', { expires: new Date(Date\.now() + 900000), httpOnly: true });987.988.fi989.990.IP "" 0991.992.P993To parse incoming \fICookie\fR headers, use the \fIcookieDecoder\fR middleware, which provides the \fIreq\.cookies\fR object:994.995.IP "" 4996.997.nf998999app\.use(express\.cookieDecoder());10001001app\.get(\'/\', function(req, res){1002 // use req\.cookies\.rememberme1003});1004.1005.fi1006.1007.IP "" 01008.1009.SS "res\.clearCookie(name)"1010Clear cookie \fIname\fR by setting "expires" far in the past\.1011.1012.IP "" 41013.1014.nf10151016res\.clearCookie(\'rememberme\');1017.1018.fi1019.1020.IP "" 01021.

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 1022–1028why, one step back
1022.SS "res\.render(view[, options[, fn]])"
1023Render \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\.1024.1025.P1026Most 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:1027.1028.IP "\(bu" 4

Merge branch 'master' into rangeTj Holowaychukbefore adoptionc3365aa

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 1030–1036why, one step back
1030.
1031.IP "\(bu" 41032\fIlocals\fR Object containing local variables1033.1034.IP "\(bu" 41035\fIdebug\fR Output debugging information1036.

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

lines 1037–1042why, one step back
1037.IP "\(bu" 4
1038\fIstatus\fR Response status code, defaults to 2001039.1040.IP "\(bu" 41041\fIheaders\fR Response headers object1042.

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 1043–1084why, one step back
1043.IP "" 0
1044.1045.SS "res\.partial(view[, options])"1046Render \fIview\fR partial with the given \fIoptions\fR\. This method is always available to the view as a local variable\.1047.1048.IP "\(bu" 41049\fIas\fR Variable name for each \fIcollection\fR value, defaults to the view name\.1050.1051.IP "\(bu" 41052as: \'something\' will add the \fIsomething\fR local variable1053.1054.IP "\(bu" 41055as: this will use the collection value as the template context1056.1057.IP "\(bu" 41058as: global will merge the collection value\'s properties with \fIlocals\fR1059.1060.IP "" 010611062.1063.IP "\(bu" 41064\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\.1065.1066.IP "" 01067.1068.P1069The following are equivalent, and the name of collection value when passed to the partial will be \fImovie\fR as derived from the name\.1070.1071.IP "" 41072.1073.nf10741075partial(\'movie\.jade\', { collection: movies });1076partial(\'movie\.jade\', movies);1077partial(\'movie\', movies);1078// In view: movie\.director1079.1080.fi1081.1082.IP "" 01083.1084.P

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 1086–1112why, one step back
1086.
1087.IP "" 41088.1089.nf10901091partial(\'movie\', { collection: movies, as: \'video\' });1092// In view: video\.director1093.1094.fi1095.1096.IP "" 01097.1098.P1099Also 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\.1100.1101.IP "" 41102.1103.nf11041105partial(\'movie\', { collection: movies, as: this });1106// In view: this\.director1107.1108.fi1109.1110.IP "" 01111.1112.P

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 1114–1132why, one step back
1114.
1115.IP "" 41116.1117.nf11181119partials(\'movie\', { collection: movies, as: global });1120// In view: director1121.1122.fi1123.1124.IP "" 01125.1126.SS "app\.set(name[, val])"1127Apply an application level setting \fIname\fR to \fIval\fR, or get the value of \fIname\fR when \fIval\fR is not present:1128.1129.IP "" 41130.1131.nf1132

Updated docsTj Holowaychukbefore adoption3234329

lines 1133–1135why, one step back
1133app\.set(\'views\', __dirname + \'/views\');
1134app\.set(\'views\');1135// => \.\.\.path\.\.\.

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 1136–1215why, one step back
1136.
1137.fi1138.1139.IP "" 01140.1141.SS "app\.enable(name)"1142Enable the given setting \fIname\fR:1143.1144.IP "" 41145.1146.nf11471148app\.enable(\'some arbitrary setting\');1149app\.set(\'some arbitrary setting\');1150// => true1151.1152.fi1153.1154.IP "" 01155.1156.SS "app\.disable(name)"1157Disable the given setting \fIname\fR:1158.1159.IP "" 41160.1161.nf11621163app\.disable(\'some setting\');1164app\.set(\'some setting\');1165// => false1166.1167.fi1168.1169.IP "" 01170.1171.SS "app\.configure(env|function[, function])"1172Define a callback function for the given \fIenv\fR (or all environments) with callback \fIfunction\fR:1173.1174.IP "" 41175.1176.nf11771178app\.configure(function(){1179 // executed for each env1180});11811182app\.configure(\'development\', function(){1183 // executed for \'development\' only1184});1185.1186.fi1187.1188.IP "" 01189.1190.SS "app\.redirect(name, val)"1191For use with \fBres\.redirect()\fR we can map redirects at the application level as shown below:1192.1193.IP "" 41194.1195.nf11961197app\.redirect(\'google\', \'http://google\.com\');1198.1199.fi1200.1201.IP "" 01202.1203.P1204Now in a route we may call:1205.1206.P1207res\.redirect(\'google\');1208.1209.P1210We may also map dynamic redirects:1211.1212.IP "" 41213.1214.nf1215

Updated docsTj Holowaychukbefore adoptiond691ecb

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

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 1218–1252why, one step back
1218});
1219.1220.fi1221.1222.IP "" 01223.1224.P1225So 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\.1226.1227.IP "" 41228.1229.nf12301231app\.get(\'/post/:id\', function(req, res){1232 res\.redirect(\'comments\');1233});1234.1235.fi1236.1237.IP "" 01238.1239.SS "app\.error(function)"1240Adds 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:1241.1242.IP "" 41243.1244.nf12451246app\.error(function(err, req, res, next){1247 res\.send(err\.message, 500);1248});1249.1250.fi1251.1252.IP "" 0

Added app.helpers() docsTj Holowaychukbefore adoption7eb088b

lines 1253–1254why, one step back
1253.
1254.SS "app\.helpers(obj)"

Docs for Server#{helpers,dynamicHelpers}()Tj Holowaychukbefore adoption78008d1

line 1255why, one step back
1255Registers static view helpers\.

Added app.helpers() docsTj Holowaychukbefore adoption7eb088b

lines 1256–1261why, one step back
1256.
1257.IP "" 41258.1259.nf12601261app\.helpers({

Docs for Server#{helpers,dynamicHelpers}()Tj Holowaychukbefore adoption78008d1

lines 1262–1284why, one step back
1262 name: function(first, last){ return first + \', \' + last },
1263 firstName: \'tj\',1264 lastName: \'holowaychuk\'1265});1266.1267.fi1268.1269.IP "" 01270.1271.P1272Our view could now utilize the \fIfirstName\fR and \fIlastName\fR variables, as well as the \fIname()\fR function exposed\.1273.1274.IP "" 41275.1276.nf12771278<%= name(firstName, lastName) %>1279.1280.fi1281.1282.IP "" 01283.1284.SS "app\.dynamicHelpers(obj)"

Updated docsTj Holowaychukbefore adoptiond691ecb

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

Docs for Server#{helpers,dynamicHelpers}()Tj Holowaychukbefore adoption78008d1

lines 1286–1291why, one step back
1286.
1287.IP "" 41288.1289.nf12901291app\.dynamicHelpers({

Updated docsTj Holowaychukbefore adoptiond691ecb

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

Added app.helpers() docsTj Holowaychukbefore adoption7eb088b

lines 1293–1301why, one step back
1293 return req\.session;
1294 }1295});1296.1297.fi1298.1299.IP "" 01300.1301.P

Docs for Server#{helpers,dynamicHelpers}()Tj Holowaychukbefore adoption78008d1

lines 1302–1308why, one step back
1302All views would now have \fIsession\fR available so that session data can be accessed via \fIsession\.name\fR etc:
1303.1304.IP "" 41305.1306.nf13071308<%= session\.name %>

Rebuilt docsTj Holowaychukbefore adoptionba88e48

lines 1309–1325why, one step back
1309.
1310.fi1311.1312.IP "" 01313.1314.SS "app\.mounted(fn)"1315Assign a callback \fIfn\fR which is called when this \fIServer\fR is passed to \fIServer#use()\fR\.1316.1317.IP "" 41318.1319.nf13201321var app = express\.createServer(),1322 blog = express\.createServer();13231324blog\.mounted(function(parent){1325 // parent is app

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

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

Rebuilt docsTj Holowaychukbefore adoptionba88e48

lines 1327–1329why, one step back
1327});
13281329app\.use(blog);

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

lines 1330–1335why, one step back
1330.
1331.fi1332.1333.IP "" 01334.1335.SS "app\.register(ext, exports)"

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

line 1336why, one step back
1336Register 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 1337–1348why, one step back
1337.
1338.IP "" 41339.1340.nf13411342 app\.register(\'\.html\', require(\'jade\'));1343.1344.fi1345.1346.IP "" 01347.1348.P

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

line 1349why, one step back
1349This 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 1350–1373why, one step back
1350.
1351.IP "" 41352.1353.nf13541355 app\.register(\'\.haml\', require(\'haml\-js\'));1356.1357.fi1358.1359.IP "" 01360.1361.P1362For engines that do not comply with the Express specification, we can also wrap their api this way\.1363.1364.IP "" 41365.1366.nf13671368 app\.register(\'\.foo\', {1369 render: function(str, options) {1370 // perhaps their api is1371 // foo\.toHTML(str, options);1372 }1373 });

Docs for Server#{helpers,dynamicHelpers}()Tj Holowaychukbefore adoption78008d1

lines 1374–1377why, one step back
1374.
1375.fi1376.1377.IP "" 0

Docs for session exampleTj Holowaychukbefore adoptionc53883b

lines 1378–1426why, one step back
1378.
1379.SS "app\.listen([port[, host]])"1380Bind 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\.1381.1382.IP "" 41383.1384.nf13851386app\.listen();1387app\.listen(3000);1388app\.listen(3000, \'n\.n\.n\.n\');1389.1390.fi1391.1392.IP "" 01393.1394.P1395The \fIport\fR argument may also be a string representing the path to a unix domain socket:1396.1397.IP "" 41398.1399.nf14001401app\.listen(\'/tmp/express\.sock\');1402.1403.fi1404.1405.IP "" 01406.1407.P1408Then try it out:1409.1410.IP "" 41411.1412.nf14131414$ telnet /tmp/express\.sock1415GET / HTTP/1\.114161417HTTP/1\.1 200 OK1418Content\-Type: text/plain1419Content\-Length: 1114201421Hello World1422.1423.fi1424.1425.IP "" 01426