guide.1
This file
Path
docs/guide.1
1,048 lines · 21.0 KiB
Last changed by
Added app.helpers() docsTj Holowaychukbefore adoption7eb088b
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 more view docsTj Holowaychukbefore adoption2f96506
lines 504–517why, one step back504.SS "Template Engines"505Below are a few template engines commonly used with Express:506.507.IP "\(bu" 4508Jade \fIhttp://github\.com/visionmedia/jade\fR haml\.js successor509.510.IP "\(bu" 4511Haml \fIhttp://github\.com/visionmedia/haml\.js\fR indented templates512.513.IP "\(bu" 4514EJS \fIhttp://github\.com/visionmedia/ejs\fR Embedded JavaScript515.516.IP "" 0517.More docsTj Holowaychukbefore adoptionde81ee7
line 518why, one step back518.SS "req\.header(key[, defaultValue])"More docsTj Holowaychukbefore adoptiondfecbe5
lines 519–532why, one step back519Get the case\-insensitive request header \fIkey\fR, with optional \fIdefaultValue\fR:520.521.IP "" 4522.523.nf524525req\.header(\'Host\');526req\.header(\'host\');527req\.header(\'Accept\', \'*/*\');528.529.fi530.531.IP "" 0532.More docsTj Holowaychukbefore adoptionde81ee7
line 533why, one step back533.SS "req\.accepts(type)"More docsTj Holowaychukbefore adoptiondfecbe5
lines 534–561why, one step back534Check if the \fIAccept\fR header is present, and includes the given \fItype\fR\.535.536.P537When 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\.538.539.IP "" 4540.541.nf542543// Accept: text/html544req\.accepts(\'html\');545// => true546547// Accept: text/*; application/json548req\.accepts(\'html\');549req\.accepts(\'text/html\');550req\.accepts(\'text/plain\');551req\.accepts(\'application/json\');552// => true553554req\.accepts(\'image/png\');555req\.accepts(\'png\');556// => false557.558.fi559.560.IP "" 0561.More docsTj Holowaychukbefore adoptionde81ee7
line 562why, one step back562.SS "req\.param(name)"More docsTj Holowaychukbefore adoptiondfecbe5
lines 563–578why, one step back563Return the value of param \fIname\fR when present\.564.565.IP "\(bu" 4566Checks route placeholders, ex: /user/:id567.568.IP "\(bu" 4569Checks query string params, ex: ?id=12570.571.IP "\(bu" 4572Checks urlencoded body params, ex: id=12573.574.IP "" 0575.576.P577To utilize urlencoded request bodies, \fIreq\.body\fR should be an object\. This can be done by using the \fIconnect\.bodyDecoder\fR middleware\.578.More docsTj Holowaychukbefore adoptionde81ee7
line 579why, one step back579.SS "req\.flash(type[, msg])"More docsTj Holowaychukbefore adoptiondfecbe5
lines 580–603why, one step back580Queue flash \fImsg\fR of the given \fItype\fR\.581.582.IP "" 4583.584.nf585586req\.flash(\'info\', \'email sent\');587req\.flash(\'error\', \'email delivery failed\');588req\.flash(\'info\', \'email re\-sent\');589// => 2590591req\.flash(\'info\');592// => [\'email sent\', \'email re\-sent\']593594req\.flash(\'info\');595// => []596597req\.flash();598// => { error: [\'email delivery failed\'], info: [] }599.600.fi601.602.IP "" 0603.More docsTj Holowaychukbefore adoptionde81ee7
line 604why, one step back604.SS "req\.isXMLHttpRequest"More docsTj Holowaychukbefore adoptiondfecbe5
lines 605–617why, one step back605Also aliased as \fIreq\.xhr\fR, this getter checks the \fIX\-Requested\-With\fR header to see if it was issued by an \fIXMLHttpRequest\fR:606.607.IP "" 4608.609.nf610611req\.xhr612req\.isXMLHttpRequest613.614.fi615.616.IP "" 0617.More docsTj Holowaychukbefore adoptionde81ee7
line 618why, one step back618.SS "res\.header(key[, val])"More docs :)Tj Holowaychukbefore adoption0c5fe61
lines 619–637why, one step back619Get or set the response header \fIkey\fR\.620.621.IP "" 4622.623.nf624625res\.header(\'Content\-Length\');626// => undefined627628res\.header(\'Content\-Length\', 123);629// => 123630631res\.header(\'Content\-Length\');632// => 123633.634.fi635.636.IP "" 0637.More docsTj Holowaychukbefore adoptionde81ee7
line 638why, one step back638.SS "res\.contentType(type)"More docs :)Tj Holowaychukbefore adoption0c5fe61
lines 639–652why, one step back639Sets the \fIContent\-Type\fR response header to the given \fItype\fR\.640.641.IP "" 4642.643.nf644645 var filename = \'path/to/image\.png\';646 res\.contentType(filename);647 // res\.headers[\'Content\-Type\'] is now \"image/png\"648.649.fi650.651.IP "" 0652.More docsTj Holowaychukbefore adoptionde81ee7
line 653why, one step back653.SS "res\.attachment([filename])"Docs for res.attachment()Tj Holowaychukbefore adoptioneb235e1
lines 654–665why, one step back654Sets the \fIContent\-Disposition\fR response header to \"attachment\", with optional \fIfilename\fR\.655.656.IP "" 4657.658.nf659660 res\.attachment(\'path/to/my/image\.png\');661.662.fi663.664.IP "" 0665.More docsTj Holowaychukbefore adoptionde81ee7
line 666why, one step back666.SS "res\.sendfile(path)"Docs for new res methodsTj Holowaychukbefore adoptionb08dab9
lines 667–679why, one step back667Used by \fBres\.download()\fR to transfer an arbitrary file\.668.669.IP "" 4670.671.nf672673res\.sendfile(\'path/to/my\.file\');674.675.fi676.677.IP "" 0678.679.PBetter docs for res.sendfile()Tj Holowaychukbefore adoption8a337f1
line 680why, one step back680This 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 681why, one step back681.More docsTj Holowaychukbefore adoptionde81ee7
line 682why, one step back682.SS "res\.download(file[, filename])"Docs for new res methodsTj Holowaychukbefore adoptionb08dab9
lines 683–709why, one step back683Transfer the given \fIfile\fR as an attachment with optional alternative \fIfilename\fR\.684.685.IP "" 4686.687.nf688689res\.download(\'path/to/image\.png\');690res\.download(\'path/to/image\.png\', \'foo\.png\');691.692.fi693.694.IP "" 0695.696.P697This is equivalent to:698.699.IP "" 4700.701.nf702703res\.attachment(file);704res\.sendfile(file);705.706.fi707.708.IP "" 0709.More docsTj Holowaychukbefore adoptionde81ee7
line 710why, one step back710.SS "res\.send(body|status[, headers|status[, status]])"Docs for res.send()Tj Holowaychukbefore adoption5a48248
lines 711–730why, one step back711The \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:712.713.IP "" 4714.715.nf716717 res\.send(new Buffer(\'wahoo\'));718 res\.send({ some: \'json\' });719 res\.send(\'<p>some html</p>\');720 res\.send(\'Sorry, cant find that\', 404);721 res\.send(\'text\', { \'Content\-Type\': \'text/plain\' }, 201);722 res\.send(404);723.724.fi725.726.IP "" 0727.728.P729By 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\.730.More docsTj Holowaychukbefore adoptionde81ee7
line 731why, one step back731.SS "res\.redirect(url[, status])"More docsTj Holowaychukbefore adoptionbedff32
lines 732–750why, one step back732Redirect to the given \fIurl\fR with a default response \fIstatus\fR of 302\.733.734.IP "" 4735.736.nf737738res\.redirect(\'/\', 301);739res\.redirect(\'/account\');740res\.redirect(\'http://google\.com\');741res\.redirect(\'home\');742res\.redirect(\'back\');743.744.fi745.746.IP "" 0747.748.P749Express 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 \"/\"\.750.More view docsTj Holowaychukbefore adoption1c8ff48
lines 751–763why, one step back751.SS "res\.render(view[, options[, fn]])"752Render \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\.753.754.P755Most 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:756.757.IP "\(bu" 4758\fIcontext|scope\fR Template evaluation context (\fIthis\fR)759.760.IP "\(bu" 4761\fIlocals\fR Object containing local variables762.763.IP "\(bu" 4Cleaning up view docsTj Holowaychukbefore adoption25c8883
line 764why, one step back764\fIcache\fR Cache intermediate JavaScript in memory (the default in \fIproduction\fR mode)More view docsTj Holowaychukbefore adoption1c8ff48
lines 765–770why, one step back765.766.IP "\(bu" 4767\fIdebug\fR Output debugging information768.769.IP "" 0770.In depth partial docsTj Holowaychukbefore adoption793943f
lines 771–851why, one step back771.SS "res\.partial(view[, options])"772Render \fIview\fR partial with the given \fIoptions\fR\. This method is always available to the view as a local variable\.773.774.IP "\(bu" 4775\fIas\fR Variable name for each \fIcollection\fR value, defaults to the view name\.776.777.IP "\(bu" 4778as: \'something\' will add the \fIsomething\fR local variable779.780.IP "\(bu" 4781as: this will use the collection value as the template context782.783.IP "\(bu" 4784as: global will merge the collection value\'s properties with \fIlocals\fR785.786.IP "" 0787788.789.IP "\(bu" 4790\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\.791.792.IP "" 0793.794.P795The following are equivalent, and the name of collection value when passed to the partial will be \fImovie\fR as derived from the name\.796.797.IP "" 4798.799.nf800801partial(\'movie\.jade\', { collection: movies });802partial(\'movie\.jade\', movies);803partial(\'movie\', movies);804// In view: movie\.director805.806.fi807.808.IP "" 0809.810.P811To change the local from \fImovie\fR to \fIvideo\fR we can use the \"as\" option:812.813.IP "" 4814.815.nf816817partial(\'movie\', { collection: movies, as: \'video\' });818// In view: video\.director819.820.fi821.822.IP "" 0823.824.P825Also 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\.826.827.IP "" 4828.829.nf830831partial(\'movie\', { collection: movies, as: this });832// In view: this\.director833.834.fi835.836.IP "" 0837.838.P839Another 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:840.841.IP "" 4842.843.nf844845partials(\'movie\', { collection: movies, as: global });846// In view: director847.848.fi849.850.IP "" 0851.More docsTj Holowaychukbefore adoptionde81ee7
lines 852–916why, one step back852.SS "app\.set(name[, val])"853Apply an application level setting \fIname\fR to \fIval\fR, or get the value of \fIname\fR when \fIval\fR is not present:854.855.IP "" 4856.857.nf858859app\.set(\'reload views\', 200);860app\.set(\'reload views\');861// => 200862.863.fi864.865.IP "" 0866.867.SS "app\.enable(name)"868Enable the given setting \fIname\fR:869.870.IP "" 4871.872.nf873874app\.enable(\'some arbitrary setting\');875app\.set(\'some arbitrary setting\');876// => true877.878.fi879.880.IP "" 0881.882.SS "app\.disable(name)"883Disable the given setting \fIname\fR:884.885.IP "" 4886.887.nf888889app\.disable(\'some setting\');890app\.set(\'some setting\');891// => false892.893.fi894.895.IP "" 0896.897.SS "app\.configure(env|function[, function])"898Define a callback function for the given \fIenv\fR (or all environments) with callback \fIfunction\fR:899.900.IP "" 4901.902.nf903904app\.configure(function(){905 // executed for each env906});907908app\.configure(\'development\', function(){909 // executed for \'development\' only910});911.912.fi913.914.IP "" 0915.916.SS "app\.redirect(name, val)"More docsTj Holowaychukbefore adoptionbedff32
lines 917–958why, one step back917For use with \fBres\.redirect()\fR we can map redirects at the application level as shown below:918.919.IP "" 4920.921.nf922923app\.redirect(\'google\', \'http://google\.com\');924.925.fi926.927.IP "" 0928.929.P930Now in a route we may call:931.932.P933res\.redirect(\'google\');934.935.P936We may also map dynamic redirects:937.938.IP "" 4939.940.nf941942app\.redirect(\'comments\', function(req, res, params){943 return \'/post/\' + params\.id + \'/comments\';944});945.946.fi947.948.IP "" 0949.950.P951So 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\.952.953.IP "" 4954.955.nf956957app\.get(\'/post/:id\', function(req, res){958 res\.redirect(\'comments\');Error handling docsTj Holowaychukbefore adoption9fbb9fa
lines 959–965why, one step back959});960.961.fi962.963.IP "" 0964.965.SS "app\.error(function)"More error docsTj Holowaychukbefore adoption6227f5c
line 966why, one step back966Adds 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 967–973why, one step back967.968.IP "" 4969.970.nf971972app\.error(function(err, req, res, next){973 res\.send(err\.message, 500);More docsTj Holowaychukbefore adoptionbedff32
line 974why, one step back974});Added configuration sectionTj Holowaychukbefore adoption83d2555
lines 975–976why, one step back975.976.fiConfiguration docsTj Holowaychukbefore adoptioncb04364
lines 977–978why, one step back977.978.IP "" 0Added app.helpers() docsTj Holowaychukbefore adoption7eb088b
lines 979–999why, one step back979.980.SS "app\.helpers(obj)"981Registers both static and dynamic view helpers\. Dynamic view helpers are simply functions which accept \fIreq\fR, \fIres\fR, and \fIparams\fR, and are evaluated against the \fIServer\fR instance\. The \fIreturn value\fR of this function becomes the local variable it is associated with\.982.983.IP "" 4984.985.nf986987app\.helpers({988 siteTitle: \'My Site\',989 session: function(req, res, params){990 return req\.session;991 }992});993.994.fi995.996.IP "" 0997.998.P999All views would now have \fIsiteTitle\fR available, and \fIsession\fR available so that session data could be accessed via \fIsession\.name\fR etc\.Server#listen() docsTj Holowaychukbefore adoptionf3803c4
lines 1000–1014why, one step back1000.1001.SS "app\.listen([port[, host]])"1002Bind 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\.1003.1004.IP "" 41005.1006.nf10071008app\.listen();1009app\.listen(3000);1010app\.listen(3000, \'n\.n\.n\.n\');1011.1012.fi1013.1014.IP "" 0Unix domain socket docsTj Holowaychukbefore adoption2fa7907
lines 1015–1047why, one step back1015.1016.P1017The \fIport\fR argument may also be a string representing the path to a unix domain socket:1018.1019.IP "" 41020.1021.nf10221023app\.listen(\'/tmp/express\.sock\');1024.1025.fi1026.1027.IP "" 01028.1029.P1030Then try it out:1031.1032.IP "" 41033.1034.nf10351036$ telnet /tmp/express\.sock1037GET / HTTP/1\.110381039HTTP/1\.1 200 OK1040Content\-Type: text/plain1041Content\-Length: 1110421043Hello World1044.1045.fi1046.1047.IP "" 0Started guideTj Holowaychukbefore adoption9e3a0c6
line 1048why, one step back1048