guide.1
This file
Path
docs/guide.1
1,056 lines · 20.8 KiB
Last changed by
Updated curl install docsTj Holowaychukbefore adoption3ceb0de
History of this fileTake it
Started guideTj Holowaychukbefore adoption9e3a0c6
lines 1–5why, one step back1.\" generated with Ronn/v0.6.62.\" http://github.com/rtomayko/ronn/3.4.TH "GUIDE" "" "July 2010" "" ""5.Install docsTj Holowaychukbefore adoption63d02f1
line 6why, one step back6.SS "Installation"Updated curl install docsTj Holowaychukbefore adoption3ceb0de
line 7why, one step back7curl (or similar):Install docsTj Holowaychukbefore adoption63d02f1
lines 8–12why, one step back8.9.IP "" 410.11.nf12Updated curl install docsTj Holowaychukbefore adoption3ceb0de
line 13why, one step back13$ curl \-# http://expressjs\.com/install\.sh | shInstall docsTj Holowaychukbefore adoption63d02f1
lines 14–19why, one step back14.15.fi16.17.IP "" 018.19.PUpdated curl install docsTj Holowaychukbefore adoption3ceb0de
line 20why, one step back20npm:Install docsTj Holowaychukbefore adoption63d02f1
lines 21–25why, one step back21.22.IP "" 423.24.nf25Updated curl install docsTj Holowaychukbefore adoption3ceb0de
lines 26–27why, one step back26$ npm install connect27$ npm install expressInstall docsTj Holowaychukbefore adoption63d02f1
lines 28–33why, one step back28.29.fi30.31.IP "" 032.33.PUpdated install docsTj Holowaychukbefore adoption193c4a9
line 34why, one step back34git clone, first update the submodules:Install docsTj Holowaychukbefore adoption63d02f1
lines 35–39why, one step back35.36.IP "" 437.38.nf39Updated install docsTj Holowaychukbefore adoption193c4a9
line 40why, one step back40$ git submodule update \-\-initInstall docsTj Holowaychukbefore adoption63d02f1
line 41why, one step back41$ make installUpdated install docsTj Holowaychukbefore adoption193c4a9
line 42why, one step back42$ make install\-supportInstall docsTj Holowaychukbefore adoption63d02f1
lines 43–47why, one step back43.44.fi45.46.IP "" 047.Removed annoying padding transitionTj Holowaychukbefore adoption346b2a2
line 48why, one step back48.SS "Creating An Application"Moved config docs upTj Holowaychukbefore adoption6e8413f
line 49why, one step back49The \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 50–66why, one step back50.51.IP "" 452.53.nf5455var app = require(\'express\')\.createServer();5657app\.get(\'/\', function(req, res){58 res\.send(\'hello world\');59});6061app\.listen(3000);62.63.fi64.65.IP "" 066.Moved config docs upTj Holowaychukbefore adoption6e8413f
lines 67–77why, one step back67.SS "Configuration"68Express 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\.69.70.P71In 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\.72.73.IP "" 474.75.nf7677app\.configure(function(){Updated docsTj Holowaychukbefore adoptioncacada8
lines 78–79why, one step back78 app\.use(connect\.methodOverride());79 app\.use(connect\.bodyDecoder());Moved config docs upTj Holowaychukbefore adoption6e8413f
lines 80–82why, one step back80});8182app\.configure(\'development\', function(){Updated docsTj Holowaychukbefore adoptioncacada8
line 83why, one step back83 app\.use(connect\.errorHandler({ dumpExceptions: true, showStack: true }));Moved config docs upTj Holowaychukbefore adoption6e8413f
lines 84–86why, one step back84});8586app\.configure(\'production\', function(){Updated docsTj Holowaychukbefore adoptioncacada8
line 87why, one step back87 app\.use(connect\.errorHandler());Moved config docs upTj Holowaychukbefore adoption6e8413f
lines 88–116why, one step back88});89.90.fi91.92.IP "" 093.94.P95For internal and arbitrary settings Express provides the \fIset(key[, val])\fR, \fIenable(key)\fR, \fIdisable(key)\fR methods:96.97.IP "" 498.99.nf100101app\.configure(function(){102 app\.set(\'views\', __dirname + \'/views\');103 app\.set(\'views\');104 // => \"\.\.\. views directory \.\.\.\"105106 app\.enable(\'some feature\');107 // same as app\.set(\'some feature\', true);108109 app\.disable(\'some feature\');110 // same as app\.set(\'some feature\', false);111});112.113.fi114.115.IP "" 0116.More docsTj Holowaychukbefore adoptionde81ee7
lines 117–129why, one step back117.P118To alter the environment we can set the \fICONNECT_ENV\fR environment variable, or more specifically \fIEXPRESS_ENV\fR, for example:119.120.IP "" 4121.122.nf123124$ EXPRESS_ENV=production node app\.js125.126.fi127.128.IP "" 0129.Moved config docs upTj Holowaychukbefore adoption6e8413f
lines 130–133why, one step back130.SS "Settings"131Express supports the following settings out of the box:132.133.IP "\(bu" 4More docsTj Holowaychukbefore adoptionde81ee7
lines 134–136why, one step back134\fIenv\fR Application environment set internally, use \fIapp\.set(\'env\')\fR to utilize135.136.IP "\(bu" 4Install docsTj Holowaychukbefore adoption63d02f1
line 137why, one step back137\fIhome\fR Application base path used with \fIres\.redirect()\fRMore docsTj Holowaychukbefore adoptionbedff32
lines 138–139why, one step back138.139.IP "\(bu" 4Moved config docs upTj Holowaychukbefore adoption6e8413f
lines 140–144why, one step back140\fIviews\fR Root views directory defaulting to \fBCWD/views\fR141.142.IP "\(bu" 4143\fIview engine\fR Default view engine name for views rendered without extensions144.Added more setting docsTj Holowaychukbefore adoption38281e6
line 145why, one step back145.IP "\(bu" 4Fixed reload views exampleTj Holowaychukbefore adoption5fcf971
line 146why, one step back146\fIreload views\fR Reloads altered views, by default watches for \fImtime\fR changes with with a 5 minute interval\. Example: \fIapp\.set(\'reload views\', 60000);\fRAdded more setting docsTj Holowaychukbefore adoption38281e6
line 147why, one step back147.Moved config docs upTj Holowaychukbefore adoption6e8413f
lines 148–149why, one step back148.IP "" 0149.Started guideTj Holowaychukbefore adoption9e3a0c6
line 150why, one step back150.SS "Routing"Route passing docsTj Holowaychukbefore adoption0ace97c
line 151why, one step back151Express 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 152–224why, one step back152.153.IP "" 4154.155.nf156157app\.get(\'/user/:id\', function(req, res, params){158 res\.send(\'user \' + params\.id);159});160.161.fi162.163.IP "" 0164.165.P166A 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:167.168.IP "" 4169.170.nf171172\\/user\\/([^\\/]+)\\/?173.174.fi175.176.IP "" 0177.178.P179Literal regular expressions may also be passed for complex uses:180.181.IP "" 4182.183.nf184185app\.get(/^\\/foo(bar)?$/, function(){});186.187.fi188.189.IP "" 0190.191.P192Below are some route examples, and the associated paths that they may consume:193.194.IP "" 4195.196.nf197198 \"/user/:id\"199 /user/12200201 \"/users/:id?\"202 /users/5203 /users204205 \"/files/*\"206 /files/jquery\.js207 /files/javascripts/jquery\.js208209 \"/file/*\.*\"210 /files/jquery\.js211 /files/javascripts/jquery\.js212213 \"/user/:id/:operation?\"214 /user/1215 /user/1/edit216217 \"/products\.:format\"218 /products\.json219 /products\.xml220221 \"/products\.:format?\"222 /products\.json223 /products\.xml224 /productsRoute passing docsTj Holowaychukbefore adoption0ace97c
lines 225–246why, one step back225.226.fi227.228.IP "" 0229.230.SS "Passing Route Control"231We 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\.232.233.IP "" 4234.235.nf236237app\.get(\'/users/:id?\', function(req, res, params){238 if (params\.id) {239 // do something240 } else {241 next();242 }243});244245app\.get(\'/users\', function(req, res, params){246 // do something elseAdded configuration sectionTj Holowaychukbefore adoption83d2555
lines 247–266why, one step back247});248.249.fi250.251.IP "" 0252.253.SS "Middleware"254The 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:255.256.IP "" 4257.258.nf259260var connect = require(\'connect\'),261 express = require(\'express\');262263var app = express\.createServer(264 connect\.logger(),265 connect\.bodyDecoder()266);More docsTj Holowaychukbefore adoptionbedff32
lines 267–271why, one step back267.268.fi269.270.IP "" 0271.Error handling docsTj Holowaychukbefore adoption9fbb9fa
lines 272–278why, one step back272.P273Alternatively we can \fIuse()\fR them which is useful when adding middleware within \fIconfigure()\fR blocks:274.275.IP "" 4276.277.nf278Updated docsTj Holowaychukbefore adoptioncacada8
line 279why, one step back279app\.use(connect\.logger({ format: \':method :uri\' }));Error handling docsTj Holowaychukbefore adoption9fbb9fa
lines 280–349why, one step back280.281.fi282.283.IP "" 0284.285.SS "Error Handling"286Express 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:287.288.IP "" 4289.290.nf291292function NotFound(msg){293 this\.name = \'NotFound\';294 Error\.call(this, msg);295 Error\.captureStackTrace(this, arguments\.callee);296}297298sys\.inherits(NotFound, Error);299300app\.get(\'/404\', function(req, res){301 throw new NotFound;302});303304app\.get(\'/500\', function(req, res){305 throw new Error(\'keyboard cat!\');306});307.308.fi309.310.IP "" 0311.312.P313We 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\.314.315.IP "" 4316.317.nf318319app\.error(function(err, req, res, next){320 if (err instanceof NotFound) {321 res\.render(\'404\.jade\');322 } else {323 next(err);324 }325});326.327.fi328.329.IP "" 0330.331.P332Here we assume all errors as 500 for the simplicity of this demo, however you can choose whatever you like333.334.IP "" 4335.336.nf337338app\.error(function(err, req, res){339 res\.render(\'500\.jade\', {340 locals: {341 error: err342 }343 });344});345.346.fi347.348.IP "" 0349.More error handling docsTj Holowaychukbefore adoptioncea401c
lines 350–356why, one step back350.P351Our 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:352.353.IP "" 4354.355.nf356Updated docsTj Holowaychukbefore adoptioncacada8
line 357why, one step back357app\.use(connect\.errorHandler({ dumpExceptions: true }));More error handling docsTj Holowaychukbefore adoptioncea401c
lines 358–369why, one step back358.359.fi360.361.IP "" 0362.363.P364Also during development we may want fancy html pages to show exceptions that are passed or thrown, so we can set \fIshowStack\fR to true:365.366.IP "" 4367.368.nf369Updated docsTj Holowaychukbefore adoptioncacada8
line 370why, one step back370app\.use(connect\.errorHandler({ showStack: true, dumpExceptions: true }));More error handling docsTj Holowaychukbefore adoptioncea401c
lines 371–378why, one step back371.372.fi373.374.IP "" 0375.376.P377The \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\.378.Added more view docsTj Holowaychukbefore adoption2f96506
lines 379–398why, one step back379.SS "View Rendering"380View 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 (currently) export the method \fIexports\.render(str, options)\fR to comply with Express, however with will likely be extensible in the future\.381.382.P383Below 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\.384.385.IP "" 4386.387.nf388389app\.get(\'/\', function(req, res){390 res\.render(\'index\.haml\', {391 locals: { title: \'My Site\' }392 });393});394.395.fi396.397.IP "" 0398.Docs for "view engine" settingTj Holowaychukbefore adoption1810c9b
lines 399–450why, one step back399.P400The 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:401.402.IP "" 4403.404.nf405406app\.set(\'view engine\', \'jade\');407.408.fi409.410.IP "" 0411.412.P413Allowing us to render with:414.415.IP "" 4416.417.nf418419res\.render(\'index\');420.421.fi422.423.IP "" 0424.425.P426vs:427.428.IP "" 4429.430.nf431432res\.render(\'index\.jade\');433.434.fi435.436.IP "" 0437.438.P439When \fIview engine\fR is set, extensions are entirely optional, however we can still mix and match template engines:440.441.IP "" 4442.443.nf444445res\.render(\'another\-page\.ejs\');446.447.fi448.449.IP "" 0450.Added more view docsTj Holowaychukbefore adoption2f96506
lines 451–489why, one step back451.SS "View Partials"452The 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:453.454.IP "" 4455.456.nf457458partial(\'comment\.haml\', { collection: comments });459.460.fi461.462.IP "" 0463.464.P465To 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\.466.467.IP "" 4468.469.nf470471partial(\'comment\', { collection: comments });472.473.fi474.475.IP "" 0476.477.P478And once again even further, when rendering a collection we can simply pass an array, if no other options are desired:479.480.IP "" 4481.482.nf483484partial(\'comments\', comments);485.486.fi487.488.IP "" 0489.Added partial magic var docs. Closes #342Tj Holowaychukbefore adoptionbe85701
lines 490–503why, one step back490.P491When using the partial collection support a few \"magic\" variables are provided for free:492.493.IP "\(bu" 4494\fIfirstInCollection\fR True if this is the first object495.496.IP "\(bu" 4497\fIindexInCollection\fR Index of the object in the collection498.499.IP "\(bu" 4500_lastInCollection _ True if this is the last object501.502.IP "" 0503.Added view helper docs. Closes #344Tj Holowaychukbefore adoptionc4b5208
lines 504–532why, one step back504.SS "View Helpers"505View helpers are merged with all template local variables, exposing them as pseudo globals\. Below is an example of exposing \fIfoo()\fR to all views:506.507.IP "" 4508.509.nf510511var view = require(\'express/view\');512view\.helpers\.foo = function(){513 return \'bar\';514};515.516.fi517.518.IP "" 0519.520.P521Which we could then utilize in a template as shown below:522.523.IP "" 4524.525.nf526527<%= foo() %>528.529.fi530.531.IP "" 0532.Added more view docsTj Holowaychukbefore adoption2f96506
lines 533–546why, one step back533.SS "Template Engines"534Below are a few template engines commonly used with Express:535.536.IP "\(bu" 4537Jade \fIhttp://github\.com/visionmedia/jade\fR haml\.js successor538.539.IP "\(bu" 4540Haml \fIhttp://github\.com/visionmedia/haml\.js\fR indented templates541.542.IP "\(bu" 4543EJS \fIhttp://github\.com/visionmedia/ejs\fR Embedded JavaScript544.545.IP "" 0546.More docsTj Holowaychukbefore adoptionde81ee7
line 547why, one step back547.SS "req\.header(key[, defaultValue])"More docsTj Holowaychukbefore adoptiondfecbe5
lines 548–561why, one step back548Get the case\-insensitive request header \fIkey\fR, with optional \fIdefaultValue\fR:549.550.IP "" 4551.552.nf553554req\.header(\'Host\');555req\.header(\'host\');556req\.header(\'Accept\', \'*/*\');557.558.fi559.560.IP "" 0561.More docsTj Holowaychukbefore adoptionde81ee7
line 562why, one step back562.SS "req\.accepts(type)"More docsTj Holowaychukbefore adoptiondfecbe5
lines 563–590why, one step back563Check if the \fIAccept\fR header is present, and includes the given \fItype\fR\.564.565.P566When 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\.567.568.IP "" 4569.570.nf571572// Accept: text/html573req\.accepts(\'html\');574// => true575576// Accept: text/*; application/json577req\.accepts(\'html\');578req\.accepts(\'text/html\');579req\.accepts(\'text/plain\');580req\.accepts(\'application/json\');581// => true582583req\.accepts(\'image/png\');584req\.accepts(\'png\');585// => false586.587.fi588.589.IP "" 0590.More docsTj Holowaychukbefore adoptionde81ee7
line 591why, one step back591.SS "req\.param(name)"More docsTj Holowaychukbefore adoptiondfecbe5
lines 592–607why, one step back592Return the value of param \fIname\fR when present\.593.594.IP "\(bu" 4595Checks route placeholders, ex: /user/:id596.597.IP "\(bu" 4598Checks query string params, ex: ?id=12599.600.IP "\(bu" 4601Checks urlencoded body params, ex: id=12602.603.IP "" 0604.605.P606To utilize urlencoded request bodies, \fIreq\.body\fR should be an object\. This can be done by using the \fIconnect\.bodyDecoder\fR middleware\.607.More docsTj Holowaychukbefore adoptionde81ee7
line 608why, one step back608.SS "req\.flash(type[, msg])"More docsTj Holowaychukbefore adoptiondfecbe5
lines 609–632why, one step back609Queue flash \fImsg\fR of the given \fItype\fR\.610.611.IP "" 4612.613.nf614615req\.flash(\'info\', \'email sent\');616req\.flash(\'error\', \'email delivery failed\');617req\.flash(\'info\', \'email re\-sent\');618// => 2619620req\.flash(\'info\');621// => [\'email sent\', \'email re\-sent\']622623req\.flash(\'info\');624// => []625626req\.flash();627// => { error: [\'email delivery failed\'], info: [] }628.629.fi630.631.IP "" 0632.More docsTj Holowaychukbefore adoptionde81ee7
line 633why, one step back633.SS "req\.isXMLHttpRequest"More docsTj Holowaychukbefore adoptiondfecbe5
lines 634–646why, one step back634Also aliased as \fIreq\.xhr\fR, this getter checks the \fIX\-Requested\-With\fR header to see if it was issued by an \fIXMLHttpRequest\fR:635.636.IP "" 4637.638.nf639640req\.xhr641req\.isXMLHttpRequest642.643.fi644.645.IP "" 0646.More docsTj Holowaychukbefore adoptionde81ee7
line 647why, one step back647.SS "res\.header(key[, val])"More docs :)Tj Holowaychukbefore adoption0c5fe61
lines 648–666why, one step back648Get or set the response header \fIkey\fR\.649.650.IP "" 4651.652.nf653654res\.header(\'Content\-Length\');655// => undefined656657res\.header(\'Content\-Length\', 123);658// => 123659660res\.header(\'Content\-Length\');661// => 123662.663.fi664.665.IP "" 0666.More docsTj Holowaychukbefore adoptionde81ee7
line 667why, one step back667.SS "res\.contentType(type)"More docs :)Tj Holowaychukbefore adoption0c5fe61
lines 668–681why, one step back668Sets the \fIContent\-Type\fR response header to the given \fItype\fR\.669.670.IP "" 4671.672.nf673674 var filename = \'path/to/image\.png\';675 res\.contentType(filename);676 // res\.headers[\'Content\-Type\'] is now \"image/png\"677.678.fi679.680.IP "" 0681.More docsTj Holowaychukbefore adoptionde81ee7
line 682why, one step back682.SS "res\.attachment([filename])"Docs for res.attachment()Tj Holowaychukbefore adoptioneb235e1
lines 683–694why, one step back683Sets the \fIContent\-Disposition\fR response header to \"attachment\", with optional \fIfilename\fR\.684.685.IP "" 4686.687.nf688689 res\.attachment(\'path/to/my/image\.png\');690.691.fi692.693.IP "" 0694.More docsTj Holowaychukbefore adoptionde81ee7
line 695why, one step back695.SS "res\.sendfile(path)"Docs for new res methodsTj Holowaychukbefore adoptionb08dab9
lines 696–708why, one step back696Used by \fBres\.download()\fR to transfer an arbitrary file\.697.698.IP "" 4699.700.nf701702res\.sendfile(\'path/to/my\.file\');703.704.fi705.706.IP "" 0707.708.PBetter docs for res.sendfile()Tj Holowaychukbefore adoption8a337f1
line 709why, one step back709This is \fInot\fR a substitution for Connect\'s \fIstaticProvider\fR middleware, it does not support HTTP caching, and does not perform any security checks\. This method is utilized by \fIres\.download()\fR to transfer static files, and allows you do to so from outside of the public directory, so suitable security checks should be applied\.Docs for new res methodsTj Holowaychukbefore adoptionb08dab9
line 710why, one step back710.More docsTj Holowaychukbefore adoptionde81ee7
line 711why, one step back711.SS "res\.download(file[, filename])"Docs for new res methodsTj Holowaychukbefore adoptionb08dab9
lines 712–738why, one step back712Transfer the given \fIfile\fR as an attachment with optional alternative \fIfilename\fR\.713.714.IP "" 4715.716.nf717718res\.download(\'path/to/image\.png\');719res\.download(\'path/to/image\.png\', \'foo\.png\');720.721.fi722.723.IP "" 0724.725.P726This is equivalent to:727.728.IP "" 4729.730.nf731732res\.attachment(file);733res\.sendfile(file);734.735.fi736.737.IP "" 0738.More docsTj Holowaychukbefore adoptionde81ee7
line 739why, one step back739.SS "res\.send(body|status[, headers|status[, status]])"Docs for res.send()Tj Holowaychukbefore adoption5a48248
lines 740–759why, one step back740The \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:741.742.IP "" 4743.744.nf745746 res\.send(new Buffer(\'wahoo\'));747 res\.send({ some: \'json\' });748 res\.send(\'<p>some html</p>\');749 res\.send(\'Sorry, cant find that\', 404);750 res\.send(\'text\', { \'Content\-Type\': \'text/plain\' }, 201);751 res\.send(404);752.753.fi754.755.IP "" 0756.757.P758By 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\.759.More docsTj Holowaychukbefore adoptionde81ee7
line 760why, one step back760.SS "res\.redirect(url[, status])"More docsTj Holowaychukbefore adoptionbedff32
lines 761–779why, one step back761Redirect to the given \fIurl\fR with a default response \fIstatus\fR of 302\.762.763.IP "" 4764.765.nf766767res\.redirect(\'/\', 301);768res\.redirect(\'/account\');769res\.redirect(\'http://google\.com\');770res\.redirect(\'home\');771res\.redirect(\'back\');772.773.fi774.775.IP "" 0776.777.P778Express 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 \"/\"\.779.More view docsTj Holowaychukbefore adoption1c8ff48
lines 780–792why, one step back780.SS "res\.render(view[, options[, fn]])"781Render \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\.782.783.P784Most 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:785.786.IP "\(bu" 4787\fIcontext|scope\fR Template evaluation context (\fIthis\fR)788.789.IP "\(bu" 4790\fIlocals\fR Object containing local variables791.792.IP "\(bu" 4Cleaning up view docsTj Holowaychukbefore adoption25c8883
line 793why, one step back793\fIcache\fR Cache intermediate JavaScript in memory (the default in \fIproduction\fR mode)More view docsTj Holowaychukbefore adoption1c8ff48
lines 794–799why, one step back794.795.IP "\(bu" 4796\fIdebug\fR Output debugging information797.798.IP "" 0799.In depth partial docsTj Holowaychukbefore adoption793943f
lines 800–880why, one step back800.SS "res\.partial(view[, options])"801Render \fIview\fR partial with the given \fIoptions\fR\. This method is always available to the view as a local variable\.802.803.IP "\(bu" 4804\fIas\fR Variable name for each \fIcollection\fR value, defaults to the view name\.805.806.IP "\(bu" 4807as: \'something\' will add the \fIsomething\fR local variable808.809.IP "\(bu" 4810as: this will use the collection value as the template context811.812.IP "\(bu" 4813as: global will merge the collection value\'s properties with \fIlocals\fR814.815.IP "" 0816817.818.IP "\(bu" 4819\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\.820.821.IP "" 0822.823.P824The following are equivalent, and the name of collection value when passed to the partial will be \fImovie\fR as derived from the name\.825.826.IP "" 4827.828.nf829830partial(\'movie\.jade\', { collection: movies });831partial(\'movie\.jade\', movies);832partial(\'movie\', movies);833// In view: movie\.director834.835.fi836.837.IP "" 0838.839.P840To change the local from \fImovie\fR to \fIvideo\fR we can use the \"as\" option:841.842.IP "" 4843.844.nf845846partial(\'movie\', { collection: movies, as: \'video\' });847// In view: video\.director848.849.fi850.851.IP "" 0852.853.P854Also 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\.855.856.IP "" 4857.858.nf859860partial(\'movie\', { collection: movies, as: this });861// In view: this\.director862.863.fi864.865.IP "" 0866.867.P868Another 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:869.870.IP "" 4871.872.nf873874partials(\'movie\', { collection: movies, as: global });875// In view: director876.877.fi878.879.IP "" 0880.More docsTj Holowaychukbefore adoptionde81ee7
lines 881–945why, one step back881.SS "app\.set(name[, val])"882Apply an application level setting \fIname\fR to \fIval\fR, or get the value of \fIname\fR when \fIval\fR is not present:883.884.IP "" 4885.886.nf887888app\.set(\'reload views\', 200);889app\.set(\'reload views\');890// => 200891.892.fi893.894.IP "" 0895.896.SS "app\.enable(name)"897Enable the given setting \fIname\fR:898.899.IP "" 4900.901.nf902903app\.enable(\'some arbitrary setting\');904app\.set(\'some arbitrary setting\');905// => true906.907.fi908.909.IP "" 0910.911.SS "app\.disable(name)"912Disable the given setting \fIname\fR:913.914.IP "" 4915.916.nf917918app\.disable(\'some setting\');919app\.set(\'some setting\');920// => false921.922.fi923.924.IP "" 0925.926.SS "app\.configure(env|function[, function])"927Define a callback function for the given \fIenv\fR (or all environments) with callback \fIfunction\fR:928.929.IP "" 4930.931.nf932933app\.configure(function(){934 // executed for each env935});936937app\.configure(\'development\', function(){938 // executed for \'development\' only939});940.941.fi942.943.IP "" 0944.945.SS "app\.redirect(name, val)"More docsTj Holowaychukbefore adoptionbedff32
lines 946–987why, one step back946For use with \fBres\.redirect()\fR we can map redirects at the application level as shown below:947.948.IP "" 4949.950.nf951952app\.redirect(\'google\', \'http://google\.com\');953.954.fi955.956.IP "" 0957.958.P959Now in a route we may call:960.961.P962res\.redirect(\'google\');963.964.P965We may also map dynamic redirects:966.967.IP "" 4968.969.nf970971app\.redirect(\'comments\', function(req, res, params){972 return \'/post/\' + params\.id + \'/comments\';973});974.975.fi976.977.IP "" 0978.979.P980So 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\.981.982.IP "" 4983.984.nf985986app\.get(\'/post/:id\', function(req, res){987 res\.redirect(\'comments\');Error handling docsTj Holowaychukbefore adoption9fbb9fa
lines 988–994why, one step back988});989.990.fi991.992.IP "" 0993.994.SS "app\.error(function)"More error docsTj Holowaychukbefore adoption6227f5c
line 995why, one step back995Adds 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:Error handling docsTj Holowaychukbefore adoption9fbb9fa
lines 996–1002why, one step back996.997.IP "" 4998.999.nf10001001app\.error(function(err, req, res, next){1002 res\.send(err\.message, 500);More docsTj Holowaychukbefore adoptionbedff32
line 1003why, one step back1003});Added configuration sectionTj Holowaychukbefore adoption83d2555
lines 1004–1005why, one step back1004.1005.fiConfiguration docsTj Holowaychukbefore adoptioncb04364
lines 1006–1007why, one step back1006.1007.IP "" 0Server#listen() docsTj Holowaychukbefore adoptionf3803c4
lines 1008–1022why, one step back1008.1009.SS "app\.listen([port[, host]])"1010Bind 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\.1011.1012.IP "" 41013.1014.nf10151016app\.listen();1017app\.listen(3000);1018app\.listen(3000, \'n\.n\.n\.n\');1019.1020.fi1021.1022.IP "" 0Unix domain socket docsTj Holowaychukbefore adoption2fa7907
lines 1023–1055why, one step back1023.1024.P1025The \fIport\fR argument may also be a string representing the path to a unix domain socket:1026.1027.IP "" 41028.1029.nf10301031app\.listen(\'/tmp/express\.sock\');1032.1033.fi1034.1035.IP "" 01036.1037.P1038Then try it out:1039.1040.IP "" 41041.1042.nf10431044$ telnet /tmp/express\.sock1045GET / HTTP/1\.110461047HTTP/1\.1 200 OK1048Content\-Type: text/plain1049Content\-Length: 1110501051Hello World1052.1053.fi1054.1055.IP "" 0Started guideTj Holowaychukbefore adoption9e3a0c6
line 1056why, one step back1056