Skip to content
Myr

guide.1

This file

Path

docs/guide.1

495 lines · 10.2 KiB

Last changed by

More docsTj Holowaychukbefore adoptiondfecbe5

History of this file

Take it

Started guideTj Holowaychukbefore adoption9e3a0c6

lines 1–5why, one step back
1.\" generated with Ronn/v0.6.6
2.\" http://github.com/rtomayko/ronn/3.4.TH "GUIDE" "" "July 2010" "" ""5.

Removed annoying padding transitionTj Holowaychukbefore adoption346b2a2

line 6why, one step back
6.SS "Creating An Application"

Moved config docs upTj Holowaychukbefore adoption6e8413f

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

Added configuration sectionTj Holowaychukbefore adoption83d2555

lines 8–24why, one step back
8.
9.IP "" 410.11.nf1213var app = require(\'express\')\.createServer();1415app\.get(\'/\', function(req, res){16 res\.send(\'hello world\');17});1819app\.listen(3000);20.21.fi22.23.IP "" 024.

Moved config docs upTj Holowaychukbefore adoption6e8413f

lines 25–78why, one step back
25.SS "Configuration"
26Express 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\.27.28.P29In 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\.30.31.IP "" 432.33.nf3435app\.configure(function(){36 app\.use(\'/\', connect\.methodOverride());37 app\.use(\'/\', connect\.bodyDecoder());38});3940app\.configure(\'development\', function(){41 app\.use(\'/\', connect\.errorHandler({ dumpExceptions: true, showStack: true }));42});4344app\.configure(\'production\', function(){45 app\.use(\'/\', connect\.errorHandler());46});47.48.fi49.50.IP "" 051.52.P53For internal and arbitrary settings Express provides the \fIset(key[, val])\fR, \fIenable(key)\fR, \fIdisable(key)\fR methods:54.55.IP "" 456.57.nf5859app\.configure(function(){60 app\.set(\'views\', __dirname + \'/views\');61 app\.set(\'views\');62 // => \"\.\.\. views directory \.\.\.\"6364 app\.enable(\'some feature\');65 // same as app\.set(\'some feature\', true);6667 app\.disable(\'some feature\');68 // same as app\.set(\'some feature\', false);69});70.71.fi72.73.IP "" 074.75.SS "Settings"76Express supports the following settings out of the box:77.78.IP "\(bu" 4

More docsTj Holowaychukbefore adoptionbedff32

lines 79–81why, one step back
79\fIhome\fR Application base path used with \fBres\.redirect()\fR
80.81.IP "\(bu" 4

Moved config docs upTj Holowaychukbefore adoption6e8413f

lines 82–86why, one step back
82\fIviews\fR Root views directory defaulting to \fBCWD/views\fR
83.84.IP "\(bu" 485\fIview engine\fR Default view engine name for views rendered without extensions86.

Added more setting docsTj Holowaychukbefore adoption38281e6

lines 87–89why, one step back
87.IP "\(bu" 4
88\fIreload views\fR Reloads altered views, by default watches for \fImtime\fR changes with with a 5 minute interval\. Example: \fBapp\.set(\'reload views\', { interval: 60000 });\fR89.

Moved config docs upTj Holowaychukbefore adoption6e8413f

lines 90–91why, one step back
90.IP "" 0
91.

Started guideTj Holowaychukbefore adoption9e3a0c6

line 92why, one step back
92.SS "Routing"

Route passing docsTj Holowaychukbefore adoption0ace97c

line 93why, one step back
93Express 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 passed as the \fIthird\fR argument, which here we name \fIparams\fR\.

Started guideTj Holowaychukbefore adoption9e3a0c6

lines 94–166why, one step back
94.
95.IP "" 496.97.nf9899app\.get(\'/user/:id\', function(req, res, params){100 res\.send(\'user \' + params\.id);101});102.103.fi104.105.IP "" 0106.107.P108A 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:109.110.IP "" 4111.112.nf113114\\/user\\/([^\\/]+)\\/?115.116.fi117.118.IP "" 0119.120.P121Literal regular expressions may also be passed for complex uses:122.123.IP "" 4124.125.nf126127app\.get(/^\\/foo(bar)?$/, function(){});128.129.fi130.131.IP "" 0132.133.P134Below are some route examples, and the associated paths that they may consume:135.136.IP "" 4137.138.nf139140 \"/user/:id\"141 /user/12142143 \"/users/:id?\"144 /users/5145 /users146147 \"/files/*\"148 /files/jquery\.js149 /files/javascripts/jquery\.js150151 \"/file/*\.*\"152 /files/jquery\.js153 /files/javascripts/jquery\.js154155 \"/user/:id/:operation?\"156 /user/1157 /user/1/edit158159 \"/products\.:format\"160 /products\.json161 /products\.xml162163 \"/products\.:format?\"164 /products\.json165 /products\.xml166 /products

Route passing docsTj Holowaychukbefore adoption0ace97c

lines 167–188why, one step back
167.
168.fi169.170.IP "" 0171.172.SS "Passing Route Control"173We may pass control to the next \fImatching\fR route, by calling the \fIfourth\fR parameter, the \fInext()\fR function\. When a match cannot be made, control is passed back to Connect\.174.175.IP "" 4176.177.nf178179app\.get(\'/users/:id?\', function(req, res, params){180 if (params\.id) {181 // do something182 } else {183 next();184 }185});186187app\.get(\'/users\', function(req, res, params){188 // do something else

Added configuration sectionTj Holowaychukbefore adoption83d2555

lines 189–208why, one step back
189});
190.191.fi192.193.IP "" 0194.195.SS "Middleware"196The Express \fIPlugin\fR is no more! middleware via Connect \fIhttp://github\.com/extjs/Connect\fR can be passed to \fIexpress\.createServer()\fR as you would with a regular Connect server\. For example:197.198.IP "" 4199.200.nf201202var connect = require(\'connect\'),203 express = require(\'express\');204205var app = express\.createServer(206 connect\.logger(),207 connect\.bodyDecoder()208);

More docsTj Holowaychukbefore adoptionbedff32

lines 209–213why, one step back
209.
210.fi211.212.IP "" 0213.

More docsTj Holowaychukbefore adoptiondfecbe5

lines 214–313why, one step back
214.SS "ServerRequest#header(key[, defaultValue])"
215Get the case\-insensitive request header \fIkey\fR, with optional \fIdefaultValue\fR:216.217.IP "" 4218.219.nf220221req\.header(\'Host\');222req\.header(\'host\');223req\.header(\'Accept\', \'*/*\');224.225.fi226.227.IP "" 0228.229.SS "ServerRequest#accepts(type)"230Check if the \fIAccept\fR header is present, and includes the given \fItype\fR\.231.232.P233When 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\.234.235.IP "" 4236.237.nf238239// Accept: text/html240req\.accepts(\'html\');241// => true242243// Accept: text/*; application/json244req\.accepts(\'html\');245req\.accepts(\'text/html\');246req\.accepts(\'text/plain\');247req\.accepts(\'application/json\');248// => true249250req\.accepts(\'image/png\');251req\.accepts(\'png\');252// => false253.254.fi255.256.IP "" 0257.258.SS "ServerRequest#param(name)"259Return the value of param \fIname\fR when present\.260.261.IP "\(bu" 4262Checks route placeholders, ex: /user/:id263.264.IP "\(bu" 4265Checks query string params, ex: ?id=12266.267.IP "\(bu" 4268Checks urlencoded body params, ex: id=12269.270.IP "" 0271.272.P273To utilize urlencoded request bodies, \fIreq\.body\fR should be an object\. This can be done by using the \fIconnect\.bodyDecoder\fR middleware\.274.275.SS "ServerRequest#flash(type[, msg])"276Queue flash \fImsg\fR of the given \fItype\fR\.277.278.IP "" 4279.280.nf281282req\.flash(\'info\', \'email sent\');283req\.flash(\'error\', \'email delivery failed\');284req\.flash(\'info\', \'email re\-sent\');285// => 2286287req\.flash(\'info\');288// => [\'email sent\', \'email re\-sent\']289290req\.flash(\'info\');291// => []292293req\.flash();294// => { error: [\'email delivery failed\'], info: [] }295.296.fi297.298.IP "" 0299.300.SS "ServerRequest#isXMLHttpRequest"301Also aliased as \fIreq\.xhr\fR, this getter checks the \fIX\-Requested\-With\fR header to see if it was issued by an \fIXMLHttpRequest\fR:302.303.IP "" 4304.305.nf306307req\.xhr308req\.isXMLHttpRequest309.310.fi311.312.IP "" 0313.

More docs :)Tj Holowaychukbefore adoption0c5fe61

lines 314–348why, one step back
314.SS "ServerResponse#header(key[, val])"
315Get or set the response header \fIkey\fR\.316.317.IP "" 4318.319.nf320321res\.header(\'Content\-Length\');322// => undefined323324res\.header(\'Content\-Length\', 123);325// => 123326327res\.header(\'Content\-Length\');328// => 123329.330.fi331.332.IP "" 0333.334.SS "ServerResponse#contentType(type)"335Sets the \fIContent\-Type\fR response header to the given \fItype\fR\.336.337.IP "" 4338.339.nf340341 var filename = \'path/to/image\.png\';342 res\.contentType(filename);343 // res\.headers[\'Content\-Type\'] is now \"image/png\"344.345.fi346.347.IP "" 0348.

Docs for res.attachment()Tj Holowaychukbefore adoptioneb235e1

lines 349–361why, one step back
349.SS "ServerResponse#attachment([filename])"
350Sets the \fIContent\-Disposition\fR response header to \"attachment\", with optional \fIfilename\fR\.351.352.IP "" 4353.354.nf355356 res\.attachment(\'path/to/my/image\.png\');357.358.fi359.360.IP "" 0361.

Docs for new res methodsTj Holowaychukbefore adoptionb08dab9

lines 362–405why, one step back
362.SS "ServerResponse#sendfile(path)"
363Used by \fBres\.download()\fR to transfer an arbitrary file\.364.365.IP "" 4366.367.nf368369res\.sendfile(\'path/to/my\.file\');370.371.fi372.373.IP "" 0374.375.P376\fBNOTE\fR: this is \fInot\fR a replacement for Connect\'s \fIstaticProvider\fR middleware, nor does it perform any security checks, use with caution when using in a dynamic manor\.377.378.SS "ServerResponse#download(file[, filename])"379Transfer the given \fIfile\fR as an attachment with optional alternative \fIfilename\fR\.380.381.IP "" 4382.383.nf384385res\.download(\'path/to/image\.png\');386res\.download(\'path/to/image\.png\', \'foo\.png\');387.388.fi389.390.IP "" 0391.392.P393This is equivalent to:394.395.IP "" 4396.397.nf398399res\.attachment(file);400res\.sendfile(file);401.402.fi403.404.IP "" 0405.

Docs for res.send()Tj Holowaychukbefore adoption5a48248

lines 406–426why, one step back
406.SS "ServerRequest#send(body|status[, headers|status[, status]])"
407The \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:408.409.IP "" 4410.411.nf412413 res\.send(new Buffer(\'wahoo\'));414 res\.send({ some: \'json\' });415 res\.send(\'<p>some html</p>\');416 res\.send(\'Sorry, cant find that\', 404);417 res\.send(\'text\', { \'Content\-Type\': \'text/plain\' }, 201);418 res\.send(404);419.420.fi421.422.IP "" 0423.424.P425By 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\.426.

More docsTj Holowaychukbefore adoptionbedff32

lines 427–490why, one step back
427.SS "ServerResponse#redirect(url[, status])"
428Redirect to the given \fIurl\fR with a default response \fIstatus\fR of 302\.429.430.IP "" 4431.432.nf433434res\.redirect(\'/\', 301);435res\.redirect(\'/account\');436res\.redirect(\'http://google\.com\');437res\.redirect(\'home\');438res\.redirect(\'back\');439.440.fi441.442.IP "" 0443.444.P445Express 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 \"/\"\.446.447.SS "Server#redirect(name, val)"448For use with \fBres\.redirect()\fR we can map redirects at the application level as shown below:449.450.IP "" 4451.452.nf453454app\.redirect(\'google\', \'http://google\.com\');455.456.fi457.458.IP "" 0459.460.P461Now in a route we may call:462.463.P464res\.redirect(\'google\');465.466.P467We may also map dynamic redirects:468.469.IP "" 4470.471.nf472473app\.redirect(\'comments\', function(req, res, params){474 return \'/post/\' + params\.id + \'/comments\';475});476.477.fi478.479.IP "" 0480.481.P482So 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\.483.484.IP "" 4485.486.nf487488app\.get(\'/post/:id\', function(req, res){489 res\.redirect(\'comments\');490});

Added configuration sectionTj Holowaychukbefore adoption83d2555

lines 491–492why, one step back
491.
492.fi

Configuration docsTj Holowaychukbefore adoptioncb04364

lines 493–494why, one step back
493.
494.IP "" 0

Started guideTj Holowaychukbefore adoption9e3a0c6

line 495why, one step back
495