Skip to content
Myr

guide.1

This file

Path

docs/guide.1

351 lines · 7.5 KiB

Last changed by

Docs for res.attachment()Tj Holowaychukbefore adoptioneb235e1

History of this file

Take it

1.\" generated with Ronn/v0.6.62.\" http://github.com/rtomayko/ronn/3.4.TH "GUIDE" "" "July 2010" "" ""5.6.SS "Creating An Application"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\.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.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" 479\fIhome\fR Application base path used with \fBres\.redirect()\fR80.81.IP "\(bu" 482\fIviews\fR Root views directory defaulting to \fBCWD/views\fR83.84.IP "\(bu" 485\fIview engine\fR Default view engine name for views rendered without extensions86.87.IP "\(bu" 488\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.90.IP "" 091.92.SS "Routing"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\.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 /products167.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 else189});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);209.210.fi211.212.IP "" 0213.214.SS "ServerResponse#header(key[, val])"215Get or set the response header \fIkey\fR\.216.217.IP "" 4218.219.nf220221res\.header(\'Content\-Length\');222// => undefined223224res\.header(\'Content\-Length\', 123);225// => 123226227res\.header(\'Content\-Length\');228// => 123229.230.fi231.232.IP "" 0233.234.SS "ServerResponse#contentType(type)"235Sets the \fIContent\-Type\fR response header to the given \fItype\fR\.236.237.IP "" 4238.239.nf240241  var filename = \'path/to/image\.png\';242  res\.contentType(filename);243  // res\.headers[\'Content\-Type\'] is now \"image/png\"244.245.fi246.247.IP "" 0248.249.SS "ServerResponse#attachment([filename])"250Sets the \fIContent\-Disposition\fR response header to \"attachment\", with optional \fIfilename\fR\.251.252.IP "" 4253.254.nf255256  res\.attachment(\'path/to/my/image\.png\');257.258.fi259.260.IP "" 0261.262.SS "ServerRequest#send(body|status[, headers|status[, status]])"263The \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:264.265.IP "" 4266.267.nf268269 res\.send(new Buffer(\'wahoo\'));270 res\.send({ some: \'json\' });271 res\.send(\'<p>some html</p>\');272 res\.send(\'Sorry, cant find that\', 404);273 res\.send(\'text\', { \'Content\-Type\': \'text/plain\' }, 201);274 res\.send(404);275.276.fi277.278.IP "" 0279.280.P281By 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\.282.283.SS "ServerResponse#redirect(url[, status])"284Redirect to the given \fIurl\fR with a default response \fIstatus\fR of 302\.285.286.IP "" 4287.288.nf289290res\.redirect(\'/\', 301);291res\.redirect(\'/account\');292res\.redirect(\'http://google\.com\');293res\.redirect(\'home\');294res\.redirect(\'back\');295.296.fi297.298.IP "" 0299.300.P301Express 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 \"/\"\.302.303.SS "Server#redirect(name, val)"304For use with \fBres\.redirect()\fR we can map redirects at the application level as shown below:305.306.IP "" 4307.308.nf309310app\.redirect(\'google\', \'http://google\.com\');311.312.fi313.314.IP "" 0315.316.P317Now in a route we may call:318.319.P320res\.redirect(\'google\');321.322.P323We may also map dynamic redirects:324.325.IP "" 4326.327.nf328329app\.redirect(\'comments\', function(req, res, params){330    return \'/post/\' + params\.id + \'/comments\';331});332.333.fi334.335.IP "" 0336.337.P338So 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\.339.340.IP "" 4341.342.nf343344app\.get(\'/post/:id\', function(req, res){345    res\.redirect(\'comments\');346});347.348.fi349.350.IP "" 0351