Skip to content
Myr

guide.1

This file

Path

docs/guide.1

534 lines · 10.5 KiB

Last changed by

Install docsTj Holowaychukbefore adoption63d02f1

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.

Install docsTj Holowaychukbefore adoption63d02f1

lines 6–44why, one step back
6.SS "Installation"
7npm:8.9.IP "" 410.11.nf1213$ npm install express14.15.fi16.17.IP "" 018.19.P20curl (or similar):21.22.IP "" 423.24.nf2526$ curl \-# http://github\.com/extjs/Connect/raw/master/install\.sh | sh27.28.fi29.30.IP "" 031.32.P33git clone or tarball for \fBHEAD\fR:34.35.IP "" 436.37.nf3839$ make install40.41.fi42.43.IP "" 044.

Removed annoying padding transitionTj Holowaychukbefore adoption346b2a2

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

Moved config docs upTj Holowaychukbefore adoption6e8413f

line 46why, one step back
46The \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 47–63why, one step back
47.
48.IP "" 449.50.nf5152var app = require(\'express\')\.createServer();5354app\.get(\'/\', function(req, res){55 res\.send(\'hello world\');56});5758app\.listen(3000);59.60.fi61.62.IP "" 063.

Moved config docs upTj Holowaychukbefore adoption6e8413f

lines 64–117why, one step back
64.SS "Configuration"
65Express 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\.66.67.P68In 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\.69.70.IP "" 471.72.nf7374app\.configure(function(){75 app\.use(\'/\', connect\.methodOverride());76 app\.use(\'/\', connect\.bodyDecoder());77});7879app\.configure(\'development\', function(){80 app\.use(\'/\', connect\.errorHandler({ dumpExceptions: true, showStack: true }));81});8283app\.configure(\'production\', function(){84 app\.use(\'/\', connect\.errorHandler());85});86.87.fi88.89.IP "" 090.91.P92For internal and arbitrary settings Express provides the \fIset(key[, val])\fR, \fIenable(key)\fR, \fIdisable(key)\fR methods:93.94.IP "" 495.96.nf9798app\.configure(function(){99 app\.set(\'views\', __dirname + \'/views\');100 app\.set(\'views\');101 // => \"\.\.\. views directory \.\.\.\"102103 app\.enable(\'some feature\');104 // same as app\.set(\'some feature\', true);105106 app\.disable(\'some feature\');107 // same as app\.set(\'some feature\', false);108});109.110.fi111.112.IP "" 0113.114.SS "Settings"115Express supports the following settings out of the box:116.117.IP "\(bu" 4

Install docsTj Holowaychukbefore adoption63d02f1

line 118why, one step back
118\fIhome\fR Application base path used with \fIres\.redirect()\fR

More docsTj Holowaychukbefore adoptionbedff32

lines 119–120why, one step back
119.
120.IP "\(bu" 4

Moved config docs upTj Holowaychukbefore adoption6e8413f

lines 121–125why, one step back
121\fIviews\fR Root views directory defaulting to \fBCWD/views\fR
122.123.IP "\(bu" 4124\fIview engine\fR Default view engine name for views rendered without extensions125.

Added more setting docsTj Holowaychukbefore adoption38281e6

lines 126–128why, one step back
126.IP "\(bu" 4
127\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 });\fR128.

Moved config docs upTj Holowaychukbefore adoption6e8413f

lines 129–130why, one step back
129.IP "" 0
130.

Started guideTj Holowaychukbefore adoption9e3a0c6

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

Route passing docsTj Holowaychukbefore adoption0ace97c

line 132why, one step back
132Express 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 133–205why, one step back
133.
134.IP "" 4135.136.nf137138app\.get(\'/user/:id\', function(req, res, params){139 res\.send(\'user \' + params\.id);140});141.142.fi143.144.IP "" 0145.146.P147A 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:148.149.IP "" 4150.151.nf152153\\/user\\/([^\\/]+)\\/?154.155.fi156.157.IP "" 0158.159.P160Literal regular expressions may also be passed for complex uses:161.162.IP "" 4163.164.nf165166app\.get(/^\\/foo(bar)?$/, function(){});167.168.fi169.170.IP "" 0171.172.P173Below are some route examples, and the associated paths that they may consume:174.175.IP "" 4176.177.nf178179 \"/user/:id\"180 /user/12181182 \"/users/:id?\"183 /users/5184 /users185186 \"/files/*\"187 /files/jquery\.js188 /files/javascripts/jquery\.js189190 \"/file/*\.*\"191 /files/jquery\.js192 /files/javascripts/jquery\.js193194 \"/user/:id/:operation?\"195 /user/1196 /user/1/edit197198 \"/products\.:format\"199 /products\.json200 /products\.xml201202 \"/products\.:format?\"203 /products\.json204 /products\.xml205 /products

Route passing docsTj Holowaychukbefore adoption0ace97c

lines 206–227why, one step back
206.
207.fi208.209.IP "" 0210.211.SS "Passing Route Control"212We 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\.213.214.IP "" 4215.216.nf217218app\.get(\'/users/:id?\', function(req, res, params){219 if (params\.id) {220 // do something221 } else {222 next();223 }224});225226app\.get(\'/users\', function(req, res, params){227 // do something else

Added configuration sectionTj Holowaychukbefore adoption83d2555

lines 228–247why, one step back
228});
229.230.fi231.232.IP "" 0233.234.SS "Middleware"235The 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:236.237.IP "" 4238.239.nf240241var connect = require(\'connect\'),242 express = require(\'express\');243244var app = express\.createServer(245 connect\.logger(),246 connect\.bodyDecoder()247);

More docsTj Holowaychukbefore adoptionbedff32

lines 248–252why, one step back
248.
249.fi250.251.IP "" 0252.

More docsTj Holowaychukbefore adoptiondfecbe5

lines 253–352why, one step back
253.SS "ServerRequest#header(key[, defaultValue])"
254Get the case\-insensitive request header \fIkey\fR, with optional \fIdefaultValue\fR:255.256.IP "" 4257.258.nf259260req\.header(\'Host\');261req\.header(\'host\');262req\.header(\'Accept\', \'*/*\');263.264.fi265.266.IP "" 0267.268.SS "ServerRequest#accepts(type)"269Check if the \fIAccept\fR header is present, and includes the given \fItype\fR\.270.271.P272When 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\.273.274.IP "" 4275.276.nf277278// Accept: text/html279req\.accepts(\'html\');280// => true281282// Accept: text/*; application/json283req\.accepts(\'html\');284req\.accepts(\'text/html\');285req\.accepts(\'text/plain\');286req\.accepts(\'application/json\');287// => true288289req\.accepts(\'image/png\');290req\.accepts(\'png\');291// => false292.293.fi294.295.IP "" 0296.297.SS "ServerRequest#param(name)"298Return the value of param \fIname\fR when present\.299.300.IP "\(bu" 4301Checks route placeholders, ex: /user/:id302.303.IP "\(bu" 4304Checks query string params, ex: ?id=12305.306.IP "\(bu" 4307Checks urlencoded body params, ex: id=12308.309.IP "" 0310.311.P312To utilize urlencoded request bodies, \fIreq\.body\fR should be an object\. This can be done by using the \fIconnect\.bodyDecoder\fR middleware\.313.314.SS "ServerRequest#flash(type[, msg])"315Queue flash \fImsg\fR of the given \fItype\fR\.316.317.IP "" 4318.319.nf320321req\.flash(\'info\', \'email sent\');322req\.flash(\'error\', \'email delivery failed\');323req\.flash(\'info\', \'email re\-sent\');324// => 2325326req\.flash(\'info\');327// => [\'email sent\', \'email re\-sent\']328329req\.flash(\'info\');330// => []331332req\.flash();333// => { error: [\'email delivery failed\'], info: [] }334.335.fi336.337.IP "" 0338.339.SS "ServerRequest#isXMLHttpRequest"340Also aliased as \fIreq\.xhr\fR, this getter checks the \fIX\-Requested\-With\fR header to see if it was issued by an \fIXMLHttpRequest\fR:341.342.IP "" 4343.344.nf345346req\.xhr347req\.isXMLHttpRequest348.349.fi350.351.IP "" 0352.

More docs :)Tj Holowaychukbefore adoption0c5fe61

lines 353–387why, one step back
353.SS "ServerResponse#header(key[, val])"
354Get or set the response header \fIkey\fR\.355.356.IP "" 4357.358.nf359360res\.header(\'Content\-Length\');361// => undefined362363res\.header(\'Content\-Length\', 123);364// => 123365366res\.header(\'Content\-Length\');367// => 123368.369.fi370.371.IP "" 0372.373.SS "ServerResponse#contentType(type)"374Sets the \fIContent\-Type\fR response header to the given \fItype\fR\.375.376.IP "" 4377.378.nf379380 var filename = \'path/to/image\.png\';381 res\.contentType(filename);382 // res\.headers[\'Content\-Type\'] is now \"image/png\"383.384.fi385.386.IP "" 0387.

Docs for res.attachment()Tj Holowaychukbefore adoptioneb235e1

lines 388–400why, one step back
388.SS "ServerResponse#attachment([filename])"
389Sets the \fIContent\-Disposition\fR response header to \"attachment\", with optional \fIfilename\fR\.390.391.IP "" 4392.393.nf394395 res\.attachment(\'path/to/my/image\.png\');396.397.fi398.399.IP "" 0400.

Docs for new res methodsTj Holowaychukbefore adoptionb08dab9

lines 401–444why, one step back
401.SS "ServerResponse#sendfile(path)"
402Used by \fBres\.download()\fR to transfer an arbitrary file\.403.404.IP "" 4405.406.nf407408res\.sendfile(\'path/to/my\.file\');409.410.fi411.412.IP "" 0413.414.P415\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\.416.417.SS "ServerResponse#download(file[, filename])"418Transfer the given \fIfile\fR as an attachment with optional alternative \fIfilename\fR\.419.420.IP "" 4421.422.nf423424res\.download(\'path/to/image\.png\');425res\.download(\'path/to/image\.png\', \'foo\.png\');426.427.fi428.429.IP "" 0430.431.P432This is equivalent to:433.434.IP "" 4435.436.nf437438res\.attachment(file);439res\.sendfile(file);440.441.fi442.443.IP "" 0444.

Docs for res.send()Tj Holowaychukbefore adoption5a48248

lines 445–465why, one step back
445.SS "ServerRequest#send(body|status[, headers|status[, status]])"
446The \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:447.448.IP "" 4449.450.nf451452 res\.send(new Buffer(\'wahoo\'));453 res\.send({ some: \'json\' });454 res\.send(\'<p>some html</p>\');455 res\.send(\'Sorry, cant find that\', 404);456 res\.send(\'text\', { \'Content\-Type\': \'text/plain\' }, 201);457 res\.send(404);458.459.fi460.461.IP "" 0462.463.P464By 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\.465.

More docsTj Holowaychukbefore adoptionbedff32

lines 466–529why, one step back
466.SS "ServerResponse#redirect(url[, status])"
467Redirect to the given \fIurl\fR with a default response \fIstatus\fR of 302\.468.469.IP "" 4470.471.nf472473res\.redirect(\'/\', 301);474res\.redirect(\'/account\');475res\.redirect(\'http://google\.com\');476res\.redirect(\'home\');477res\.redirect(\'back\');478.479.fi480.481.IP "" 0482.483.P484Express 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 \"/\"\.485.486.SS "Server#redirect(name, val)"487For use with \fBres\.redirect()\fR we can map redirects at the application level as shown below:488.489.IP "" 4490.491.nf492493app\.redirect(\'google\', \'http://google\.com\');494.495.fi496.497.IP "" 0498.499.P500Now in a route we may call:501.502.P503res\.redirect(\'google\');504.505.P506We may also map dynamic redirects:507.508.IP "" 4509.510.nf511512app\.redirect(\'comments\', function(req, res, params){513 return \'/post/\' + params\.id + \'/comments\';514});515.516.fi517.518.IP "" 0519.520.P521So 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\.522.523.IP "" 4524.525.nf526527app\.get(\'/post/:id\', function(req, res){528 res\.redirect(\'comments\');529});

Added configuration sectionTj Holowaychukbefore adoption83d2555

lines 530–531why, one step back
530.
531.fi

Configuration docsTj Holowaychukbefore adoptioncb04364

lines 532–533why, one step back
532.
533.IP "" 0

Started guideTj Holowaychukbefore adoption9e3a0c6

line 534why, one step back
534