Skip to content
Myr

guide.1

This file

Path

docs/guide.1

1,249 lines · 24.5 KiB

Last changed by

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

History of this file

Take it

The walk stopped after 200 commits: lines older than f3803c4 point at it.

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 1–3why, one step back
1.\" generated with Ronn/v0.6.6
2.\" http://github.com/rtomayko/ronn/3.

Updated docsTj Holowaychukbefore adoptionb819366

line 4why, one step back
4.TH "GUIDE" "" "August 2010" "" ""

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 5–6why, one step back
5.
6.SS "Installation"

No "or similar" for curl installTj Holowaychukbefore adoption56a1dd9

line 7why, one step back
7curl:

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 8–12why, one step back
8.
9.IP "" 410.11.nf12

Updated curl install docsTj Holowaychukbefore adoption3ceb0de

line 13why, one step back
13$ curl \-# http://expressjs\.com/install\.sh | sh

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 14–19why, one step back
14.
15.fi16.17.IP "" 018.19.P

Updated curl install docsTj Holowaychukbefore adoption3ceb0de

line 20why, one step back
20npm:

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 21–25why, one step back
21.
22.IP "" 423.24.nf25

Updated curl install docsTj Holowaychukbefore adoption3ceb0de

line 26why, one step back
26$ npm install express

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 27–32why, one step back
27.
28.fi29.30.IP "" 031.32.P

Updated install docsTj Holowaychukbefore adoption193c4a9

line 33why, one step back
33git clone, first update the submodules:

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 34–38why, one step back
34.
35.IP "" 436.37.nf38

Updated install docsTj Holowaychukbefore adoption193c4a9

line 39why, one step back
39$ git submodule update \-\-init

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

line 40why, one step back
40$ make install

Updated install docsTj Holowaychukbefore adoption193c4a9

line 41why, one step back
41$ make install\-support

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 42–69why, one step back
42.
43.fi44.45.IP "" 046.47.SS "Creating An Application"48The \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\.49.50.IP "" 451.52.nf5354var app = require(\'express\')\.createServer();5556app\.get(\'/\', function(req, res){57 res\.send(\'hello world\');58});5960app\.listen(3000);61.62.fi63.64.IP "" 065.66.SS "Configuration"67Express 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\.68.69.P

Updated configuration docsTj Holowaychukbefore adoptionf8ccaa3

line 70why, one step back
70In 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\. Note the use of \fIapp\.router\fR, which can (optionally) be used to mount the application routes, otherwise the first call to \fIapp\.{get,put,del,post}()\fR will mount the routes\.

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 71–76why, one step back
71.
72.IP "" 473.74.nf7576app\.configure(function(){

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

lines 77–78why, one step back
77 app\.use(express\.methodOverride());
78 app\.use(express\.bodyDecoder());

Updated configuration docsTj Holowaychukbefore adoptionf8ccaa3

line 79why, one step back
79 app\.use(app\.router);

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

line 80why, one step back
80 app\.use(express\.staticProvider(__dirname + \'/public\'));

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 81–83why, one step back
81});
8283app\.configure(\'development\', function(){

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

line 84why, one step back
84 app\.use(express\.errorHandler({ dumpExceptions: true, showStack: true }));

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 85–87why, one step back
85});
8687app\.configure(\'production\', function(){

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

line 88why, one step back
88 app\.use(express\.errorHandler());

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 89–134why, one step back
89});
90.91.fi92.93.IP "" 094.95.P96For internal and arbitrary settings Express provides the \fIset(key[, val])\fR, \fIenable(key)\fR, \fIdisable(key)\fR methods:97.98.IP "" 499.100.nf101102app\.configure(function(){103 app\.set(\'views\', __dirname + \'/views\');104 app\.set(\'views\');105 // => \"\.\.\. views directory \.\.\.\"106107 app\.enable(\'some feature\');108 // same as app\.set(\'some feature\', true);109110 app\.disable(\'some feature\');111 // same as app\.set(\'some feature\', false);112});113.114.fi115.116.IP "" 0117.118.P119To alter the environment we can set the \fICONNECT_ENV\fR environment variable, or more specifically \fIEXPRESS_ENV\fR, for example:120.121.IP "" 4122.123.nf124125$ EXPRESS_ENV=production node app\.js126.127.fi128.129.IP "" 0130.131.SS "Settings"132Express supports the following settings out of the box:133.134.IP "\(bu" 4

Updated docsTj Holowaychukbefore adoption4d32762

line 135why, one step back
135\fIenv\fR Application environment set internally, use \fIapp\.set(\'env\')\fR on \fIServer#listen()\fR

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 136–137why, one step back
136.
137.IP "\(bu" 4

New "home" docsTj Holowaychukbefore adoption1e7a12f

line 138why, one step back
138\fIhome\fR Application base path used for \fIres\.redirect()\fR and transparently handling mounted apps\.

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 139–145why, one step back
139.
140.IP "\(bu" 4141\fIviews\fR Root views directory defaulting to \fBCWD/views\fR142.143.IP "\(bu" 4144\fIview engine\fR Default view engine name for views rendered without extensions145.

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

lines 146–151why, one step back
146.IP "\(bu" 4
147\fIview options\fR An object specifying global view options148.149.IP "\(bu" 4150\fIpartials\fR Root view partials directory defaulting to \fIviews\fR/partials\.151.

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 152–154why, one step back
152.IP "" 0
153.154.SS "Routing"

Updated docsTj Holowaychukbefore adoptiond691ecb

line 155why, one step back
155Express 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 available as \fBreq\.params\fR\.

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 156–160why, one step back
156.
157.IP "" 4158.159.nf160

Updated docsTj Holowaychukbefore adoptiond691ecb

lines 161–162why, one step back
161app\.get(\'/user/:id\', function(req, res){
162 res\.send(\'user \' + req\.params\.id);

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 163–182why, one step back
163});
164.165.fi166.167.IP "" 0168.169.P170A 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:171.172.IP "" 4173.174.nf175176\\/user\\/([^\\/]+)\\/?177.178.fi179.180.IP "" 0181.182.P

Updated docsTj Holowaychukbefore adoptiond691ecb

line 183why, one step back
183Regular expression literals may also be passed for complex uses\. Since capture groups with literal \fIRegExp\fR\'s are anonymous we can access them directly \fBreq\.params\fR\.

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 184–188why, one step back
184.
185.IP "" 4186.187.nf188

Updated docsTj Holowaychukbefore adoptiond691ecb

lines 189–190why, one step back
189app\.get(/^\\/users?(?:\\/(\\d+)(?:\\\.\\\.(\\d+))?)?/, function(req, res){
190 res\.send(req\.params);

Docs for params.captures. Closes #358Tj Holowaychukbefore adoptiond03821b

lines 191–211why, one step back
191});
192.193.fi194.195.IP "" 0196.197.P198Curl requests against the previously defined route:199.200.IP "" 4201.202.nf203204 $ curl http://dev:3000/user205 [null,null]206 $ curl http://dev:3000/users207 [null,null]208 $ curl http://dev:3000/users/1209 [\"1\",null]210 $ curl http://dev:3000/users/1\.\.15211 [\"1\",\"15\"]

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 212–256why, one step back
212.
213.fi214.215.IP "" 0216.217.P218Below are some route examples, and the associated paths that they may consume:219.220.IP "" 4221.222.nf223224 \"/user/:id\"225 /user/12226227 \"/users/:id?\"228 /users/5229 /users230231 \"/files/*\"232 /files/jquery\.js233 /files/javascripts/jquery\.js234235 \"/file/*\.*\"236 /files/jquery\.js237 /files/javascripts/jquery\.js238239 \"/user/:id/:operation?\"240 /user/1241 /user/1/edit242243 \"/products\.:format\"244 /products\.json245 /products\.xml246247 \"/products\.:format?\"248 /products\.json249 /products\.xml250 /products251.252.fi253.254.IP "" 0255.256.SS "Passing Route Control"

Updated docsTj Holowaychukbefore adoptiond691ecb

line 257why, one step back
257We may pass control to the next \fImatching\fR route, by calling the \fIthird\fR argument, the \fInext()\fR function\. When a match cannot be made, control is passed back to Connect, and middleware continue to be invoked\.

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 258–262why, one step back
258.
259.IP "" 4260.261.nf262

Updated docsTj Holowaychukbefore adoptionb819366

line 263why, one step back
263app\.get(\'/users/:id?\', function(req, res, next){

Updated docsTj Holowaychukbefore adoptiond691ecb

lines 264–265why, one step back
264 var id = req\.params\.id;
265 if (id) {

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 266–271why, one step back
266 // do something
267 } else {268 next();269 }270});271

Updated docsTj Holowaychukbefore adoptiond691ecb

line 272why, one step back
272app\.get(\'/users\', function(req, res){

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 273–286why, one step back
273 // do something else
274});275.276.fi277.278.IP "" 0279.280.SS "Middleware"281The 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:282.283.IP "" 4284.285.nf286

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

line 287why, one step back
287var express = require(\'express\');

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 288–289why, one step back
288
289var app = express\.createServer(

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

lines 290–291why, one step back
290 express\.logger(),
291 express\.bodyDecoder()

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 292–304why, one step back
292);
293.294.fi295.296.IP "" 0297.298.P299Alternatively we can \fIuse()\fR them which is useful when adding middleware within \fIconfigure()\fR blocks:300.301.IP "" 4302.303.nf304

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

line 305why, one step back
305app\.use(express\.logger({ format: \':method :uri\' }));

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 306–310why, one step back
306.
307.fi308.309.IP "" 0310.

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

lines 311–337why, one step back
311.P
312Typically with connect middleware you would \fIrequire(\'connect\')\fR like so:313.314.IP "" 4315.316.nf317318var connect = require(\'connect\');319app\.use(connect\.logger());320.321.fi322.323.IP "" 0324.325.P326This is somewhat annoying, so express re\-exports these middleware properties, however they are \fIidentical\fR:327.328.IP "" 4329.330.nf331332app\.use(express\.logger());333.334.fi335.336.IP "" 0337.

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 338–367why, one step back
338.SS "Error Handling"
339Express 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:340.341.IP "" 4342.343.nf344345function NotFound(msg){346 this\.name = \'NotFound\';347 Error\.call(this, msg);348 Error\.captureStackTrace(this, arguments\.callee);349}350351sys\.inherits(NotFound, Error);352353app\.get(\'/404\', function(req, res){354 throw new NotFound;355});356357app\.get(\'/500\', function(req, res){358 throw new Error(\'keyboard cat!\');359});360.361.fi362.363.IP "" 0364.365.P366We 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\.367.

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

lines 368–370why, one step back
368.P
369Note that these handlers can be defined anywhere, as they will be placed below the route handlers on \fIlisten()\fR\. This allows for definition within \fIconfigure()\fR blocks so we can handle exceptions in different ways based on the environment\.370.

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 371–405why, one step back
371.IP "" 4
372.373.nf374375app\.error(function(err, req, res, next){376 if (err instanceof NotFound) {377 res\.render(\'404\.jade\');378 } else {379 next(err);380 }381});382.383.fi384.385.IP "" 0386.387.P388Here we assume all errors as 500 for the simplicity of this demo, however you can choose whatever you like389.390.IP "" 4391.392.nf393394app\.error(function(err, req, res){395 res\.render(\'500\.jade\', {396 locals: {397 error: err398 }399 });400});401.402.fi403.404.IP "" 0405.

More error handling docsTj Holowaychukbefore adoptioncea401c

lines 406–412why, one step back
406.P
407Our 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:408.409.IP "" 4410.411.nf412

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

line 413why, one step back
413app\.use(express\.errorHandler({ dumpExceptions: true }));

More error handling docsTj Holowaychukbefore adoptioncea401c

lines 414–425why, one step back
414.
415.fi416.417.IP "" 0418.419.P420Also during development we may want fancy html pages to show exceptions that are passed or thrown, so we can set \fIshowStack\fR to true:421.422.IP "" 4423.424.nf425

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

line 426why, one step back
426app\.use(express\.errorHandler({ showStack: true, dumpExceptions: true }));

More error handling docsTj Holowaychukbefore adoptioncea401c

lines 427–434why, one step back
427.
428.fi429.430.IP "" 0431.432.P433The \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\.434.

Added more view docsTj Holowaychukbefore adoption2f96506

line 435why, one step back
435.SS "View Rendering"

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

line 436why, one step back
436View 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 export the method \fIexports\.render(str, options)\fR to comply with Express, however \fIapp\.register()\fR can be used to map engines to file extensions, so that for example \"foo\.html\" can be rendered by jade\.

Added more view docsTj Holowaychukbefore adoption2f96506

lines 437–454why, one step back
437.
438.P439Below 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\.440.441.IP "" 4442.443.nf444445app\.get(\'/\', function(req, res){446 res\.render(\'index\.haml\', {447 locals: { title: \'My Site\' }448 });449});450.451.fi452.453.IP "" 0454.

Docs for "view engine" settingTj Holowaychukbefore adoption1810c9b

lines 455–506why, one step back
455.P
456The 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:457.458.IP "" 4459.460.nf461462app\.set(\'view engine\', \'jade\');463.464.fi465.466.IP "" 0467.468.P469Allowing us to render with:470.471.IP "" 4472.473.nf474475res\.render(\'index\');476.477.fi478.479.IP "" 0480.481.P482vs:483.484.IP "" 4485.486.nf487488res\.render(\'index\.jade\');489.490.fi491.492.IP "" 0493.494.P495When \fIview engine\fR is set, extensions are entirely optional, however we can still mix and match template engines:496.497.IP "" 4498.499.nf500501res\.render(\'another\-page\.ejs\');502.503.fi504.505.IP "" 0506.

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

lines 507–550why, one step back
507.P
508Express also provides the \fIview options\fR setting, which is applied each time a view is rendered, so for example if you rarely use layouts you may set:509.510.IP "" 4511.512.nf513514app\.set(\'view options\', {515 layout: false516});517.518.fi519.520.IP "" 0521.522.P523Which can then be overridden within the \fBres\.render()\fR call if need be:524.525.IP "" 4526.527.nf528529res\.render(\'myview\.ejs\', { layout: true });530.531.fi532.533.IP "" 0534.535.P536A good example of this is specifying custom \fIejs\fR opening and closing tags:537.538.IP "" 4539.540.nf541542app\.set(\'view options\', {543 open: \'{{\',544 close: \'}}\'545});546.547.fi548.549.IP "" 0550.

Added more view docsTj Holowaychukbefore adoption2f96506

lines 551–589why, one step back
551.SS "View Partials"
552The 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:553.554.IP "" 4555.556.nf557558partial(\'comment\.haml\', { collection: comments });559.560.fi561.562.IP "" 0563.564.P565To 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\.566.567.IP "" 4568.569.nf570571partial(\'comment\', { collection: comments });572.573.fi574.575.IP "" 0576.577.P578And once again even further, when rendering a collection we can simply pass an array, if no other options are desired:579.580.IP "" 4581.582.nf583584partial(\'comments\', comments);585.586.fi587.588.IP "" 0589.

Added partial magic var docs. Closes #342Tj Holowaychukbefore adoptionbe85701

lines 590–603why, one step back
590.P
591When using the partial collection support a few \"magic\" variables are provided for free:592.593.IP "\(bu" 4594\fIfirstInCollection\fR True if this is the first object595.596.IP "\(bu" 4597\fIindexInCollection\fR Index of the object in the collection598.599.IP "\(bu" 4600_lastInCollection _ True if this is the last object601.602.IP "" 0603.

Added more view docsTj Holowaychukbefore adoption2f96506

lines 604–617why, one step back
604.SS "Template Engines"
605Below are a few template engines commonly used with Express:606.607.IP "\(bu" 4608Jade \fIhttp://github\.com/visionmedia/jade\fR haml\.js successor609.610.IP "\(bu" 4611Haml \fIhttp://github\.com/visionmedia/haml\.js\fR indented templates612.613.IP "\(bu" 4614EJS \fIhttp://github\.com/visionmedia/ejs\fR Embedded JavaScript615.616.IP "" 0617.

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 618–665why, one step back
618.SS "req\.header(key[, defaultValue])"
619Get the case\-insensitive request header \fIkey\fR, with optional \fIdefaultValue\fR:620.621.IP "" 4622.623.nf624625req\.header(\'Host\');626req\.header(\'host\');627req\.header(\'Accept\', \'*/*\');628.629.fi630.631.IP "" 0632.633.SS "req\.accepts(type)"634Check if the \fIAccept\fR header is present, and includes the given \fItype\fR\.635.636.P637When 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\.638.639.IP "" 4640.641.nf642643// Accept: text/html644req\.accepts(\'html\');645// => true646647// Accept: text/*; application/json648req\.accepts(\'html\');649req\.accepts(\'text/html\');650req\.accepts(\'text/plain\');651req\.accepts(\'application/json\');652// => true653654req\.accepts(\'image/png\');655req\.accepts(\'png\');656// => false657.658.fi659.660.IP "" 0661.662.SS "req\.param(name)"663Return the value of param \fIname\fR when present\.664.665.IP "\(bu" 4

Updated docsTj Holowaychukbefore adoptiond691ecb

line 666why, one step back
666Checks route placeholders (\fIreq\.params\fR), ex: /user/:id

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 667–668why, one step back
667.
668.IP "\(bu" 4

Updated docsTj Holowaychukbefore adoptiond691ecb

line 669why, one step back
669Checks query string params (\fIreq\.query\fR), ex: ?id=12

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 670–671why, one step back
670.
671.IP "\(bu" 4

Updated docsTj Holowaychukbefore adoptiond691ecb

line 672why, one step back
672Checks urlencoded body params (\fIreq\.body\fR), ex: id=12

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 673–676why, one step back
673.
674.IP "" 0675.676.P

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

line 677why, one step back
677To utilize urlencoded request bodies, \fIreq\.body\fR should be an object\. This can be done by using the \fIexpress\.bodyDecoder\fR middleware\.

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 678–779why, one step back
678.
679.SS "req\.flash(type[, msg])"680Queue flash \fImsg\fR of the given \fItype\fR\.681.682.IP "" 4683.684.nf685686req\.flash(\'info\', \'email sent\');687req\.flash(\'error\', \'email delivery failed\');688req\.flash(\'info\', \'email re\-sent\');689// => 2690691req\.flash(\'info\');692// => [\'email sent\', \'email re\-sent\']693694req\.flash(\'info\');695// => []696697req\.flash();698// => { error: [\'email delivery failed\'], info: [] }699.700.fi701.702.IP "" 0703.704.SS "req\.isXMLHttpRequest"705Also aliased as \fIreq\.xhr\fR, this getter checks the \fIX\-Requested\-With\fR header to see if it was issued by an \fIXMLHttpRequest\fR:706.707.IP "" 4708.709.nf710711req\.xhr712req\.isXMLHttpRequest713.714.fi715.716.IP "" 0717.718.SS "res\.header(key[, val])"719Get or set the response header \fIkey\fR\.720.721.IP "" 4722.723.nf724725res\.header(\'Content\-Length\');726// => undefined727728res\.header(\'Content\-Length\', 123);729// => 123730731res\.header(\'Content\-Length\');732// => 123733.734.fi735.736.IP "" 0737.738.SS "res\.contentType(type)"739Sets the \fIContent\-Type\fR response header to the given \fItype\fR\.740.741.IP "" 4742.743.nf744745 var filename = \'path/to/image\.png\';746 res\.contentType(filename);747 // res\.headers[\'Content\-Type\'] is now \"image/png\"748.749.fi750.751.IP "" 0752.753.SS "res\.attachment([filename])"754Sets the \fIContent\-Disposition\fR response header to \"attachment\", with optional \fIfilename\fR\.755.756.IP "" 4757.758.nf759760 res\.attachment(\'path/to/my/image\.png\');761.762.fi763.764.IP "" 0765.766.SS "res\.sendfile(path)"767Used by \fBres\.download()\fR to transfer an arbitrary file\.768.769.IP "" 4770.771.nf772773res\.sendfile(\'path/to/my\.file\');774.775.fi776.777.IP "" 0778.779.P

Better docs for res.sendfile()Tj Holowaychukbefore adoption8a337f1

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

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 781–850why, one step back
781.
782.SS "res\.download(file[, filename])"783Transfer the given \fIfile\fR as an attachment with optional alternative \fIfilename\fR\.784.785.IP "" 4786.787.nf788789res\.download(\'path/to/image\.png\');790res\.download(\'path/to/image\.png\', \'foo\.png\');791.792.fi793.794.IP "" 0795.796.P797This is equivalent to:798.799.IP "" 4800.801.nf802803res\.attachment(file);804res\.sendfile(file);805.806.fi807.808.IP "" 0809.810.SS "res\.send(body|status[, headers|status[, status]])"811The \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:812.813.IP "" 4814.815.nf816817 res\.send(new Buffer(\'wahoo\'));818 res\.send({ some: \'json\' });819 res\.send(\'<p>some html</p>\');820 res\.send(\'Sorry, cant find that\', 404);821 res\.send(\'text\', { \'Content\-Type\': \'text/plain\' }, 201);822 res\.send(404);823.824.fi825.826.IP "" 0827.828.P829By 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\.830.831.SS "res\.redirect(url[, status])"832Redirect to the given \fIurl\fR with a default response \fIstatus\fR of 302\.833.834.IP "" 4835.836.nf837838res\.redirect(\'/\', 301);839res\.redirect(\'/account\');840res\.redirect(\'http://google\.com\');841res\.redirect(\'home\');842res\.redirect(\'back\');843.844.fi845.846.IP "" 0847.848.P849Express 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 \"/\"\.850.

More view docsTj Holowaychukbefore adoption1c8ff48

lines 851–867why, one step back
851.SS "res\.render(view[, options[, fn]])"
852Render \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\.853.854.P855Most 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:856.857.IP "\(bu" 4858\fIcontext|scope\fR Template evaluation context (\fIthis\fR)859.860.IP "\(bu" 4861\fIlocals\fR Object containing local variables862.863.IP "\(bu" 4864\fIdebug\fR Output debugging information865.866.IP "" 0867.

In depth partial docsTj Holowaychukbefore adoption793943f

lines 868–948why, one step back
868.SS "res\.partial(view[, options])"
869Render \fIview\fR partial with the given \fIoptions\fR\. This method is always available to the view as a local variable\.870.871.IP "\(bu" 4872\fIas\fR Variable name for each \fIcollection\fR value, defaults to the view name\.873.874.IP "\(bu" 4875as: \'something\' will add the \fIsomething\fR local variable876.877.IP "\(bu" 4878as: this will use the collection value as the template context879.880.IP "\(bu" 4881as: global will merge the collection value\'s properties with \fIlocals\fR882.883.IP "" 0884885.886.IP "\(bu" 4887\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\.888.889.IP "" 0890.891.P892The following are equivalent, and the name of collection value when passed to the partial will be \fImovie\fR as derived from the name\.893.894.IP "" 4895.896.nf897898partial(\'movie\.jade\', { collection: movies });899partial(\'movie\.jade\', movies);900partial(\'movie\', movies);901// In view: movie\.director902.903.fi904.905.IP "" 0906.907.P908To change the local from \fImovie\fR to \fIvideo\fR we can use the \"as\" option:909.910.IP "" 4911.912.nf913914partial(\'movie\', { collection: movies, as: \'video\' });915// In view: video\.director916.917.fi918.919.IP "" 0920.921.P922Also 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\.923.924.IP "" 4925.926.nf927928partial(\'movie\', { collection: movies, as: this });929// In view: this\.director930.931.fi932.933.IP "" 0934.935.P936Another 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:937.938.IP "" 4939.940.nf941942partials(\'movie\', { collection: movies, as: global });943// In view: director944.945.fi946.947.IP "" 0948.

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 949–955why, one step back
949.SS "app\.set(name[, val])"
950Apply an application level setting \fIname\fR to \fIval\fR, or get the value of \fIname\fR when \fIval\fR is not present:951.952.IP "" 4953.954.nf955

Updated docsTj Holowaychukbefore adoption3234329

lines 956–958why, one step back
956app\.set(\'views\', __dirname + \'/views\');
957app\.set(\'views\');958// => \.\.\.path\.\.\.

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 959–1038why, one step back
959.
960.fi961.962.IP "" 0963.964.SS "app\.enable(name)"965Enable the given setting \fIname\fR:966.967.IP "" 4968.969.nf970971app\.enable(\'some arbitrary setting\');972app\.set(\'some arbitrary setting\');973// => true974.975.fi976.977.IP "" 0978.979.SS "app\.disable(name)"980Disable the given setting \fIname\fR:981.982.IP "" 4983.984.nf985986app\.disable(\'some setting\');987app\.set(\'some setting\');988// => false989.990.fi991.992.IP "" 0993.994.SS "app\.configure(env|function[, function])"995Define a callback function for the given \fIenv\fR (or all environments) with callback \fIfunction\fR:996.997.IP "" 4998.999.nf10001001app\.configure(function(){1002 // executed for each env1003});10041005app\.configure(\'development\', function(){1006 // executed for \'development\' only1007});1008.1009.fi1010.1011.IP "" 01012.1013.SS "app\.redirect(name, val)"1014For use with \fBres\.redirect()\fR we can map redirects at the application level as shown below:1015.1016.IP "" 41017.1018.nf10191020app\.redirect(\'google\', \'http://google\.com\');1021.1022.fi1023.1024.IP "" 01025.1026.P1027Now in a route we may call:1028.1029.P1030res\.redirect(\'google\');1031.1032.P1033We may also map dynamic redirects:1034.1035.IP "" 41036.1037.nf1038

Updated docsTj Holowaychukbefore adoptiond691ecb

lines 1039–1040why, one step back
1039app\.redirect(\'comments\', function(req, res){
1040 return \'/post/\' + req\.params\.id + \'/comments\';

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 1041–1075why, one step back
1041});
1042.1043.fi1044.1045.IP "" 01046.1047.P1048So 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\.1049.1050.IP "" 41051.1052.nf10531054app\.get(\'/post/:id\', function(req, res){1055 res\.redirect(\'comments\');1056});1057.1058.fi1059.1060.IP "" 01061.1062.SS "app\.error(function)"1063Adds 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:1064.1065.IP "" 41066.1067.nf10681069app\.error(function(err, req, res, next){1070 res\.send(err\.message, 500);1071});1072.1073.fi1074.1075.IP "" 0

Added app.helpers() docsTj Holowaychukbefore adoption7eb088b

lines 1076–1077why, one step back
1076.
1077.SS "app\.helpers(obj)"

Docs for Server#{helpers,dynamicHelpers}()Tj Holowaychukbefore adoption78008d1

line 1078why, one step back
1078Registers static view helpers\.

Added app.helpers() docsTj Holowaychukbefore adoption7eb088b

lines 1079–1084why, one step back
1079.
1080.IP "" 41081.1082.nf10831084app\.helpers({

Docs for Server#{helpers,dynamicHelpers}()Tj Holowaychukbefore adoption78008d1

lines 1085–1107why, one step back
1085 name: function(first, last){ return first + \', \' + last },
1086 firstName: \'tj\',1087 lastName: \'holowaychuk\'1088});1089.1090.fi1091.1092.IP "" 01093.1094.P1095Our view could now utilize the \fIfirstName\fR and \fIlastName\fR variables, as well as the \fIname()\fR function exposed\.1096.1097.IP "" 41098.1099.nf11001101<%= name(firstName, lastName) %>1102.1103.fi1104.1105.IP "" 01106.1107.SS "app\.dynamicHelpers(obj)"

Updated docsTj Holowaychukbefore adoptiond691ecb

line 1108why, one step back
1108Registers dynamic view helpers\. Dynamic view helpers are simply functions which accept \fIreq\fR, \fIres\fR, and are evaluated against the \fIServer\fR instance before a view is rendered\. The \fIreturn value\fR of this function becomes the local variable it is associated with\.

Docs for Server#{helpers,dynamicHelpers}()Tj Holowaychukbefore adoption78008d1

lines 1109–1114why, one step back
1109.
1110.IP "" 41111.1112.nf11131114app\.dynamicHelpers({

Updated docsTj Holowaychukbefore adoptiond691ecb

line 1115why, one step back
1115 session: function(req, res){

Added app.helpers() docsTj Holowaychukbefore adoption7eb088b

lines 1116–1124why, one step back
1116 return req\.session;
1117 }1118});1119.1120.fi1121.1122.IP "" 01123.1124.P

Docs for Server#{helpers,dynamicHelpers}()Tj Holowaychukbefore adoption78008d1

lines 1125–1131why, one step back
1125All views would now have \fIsession\fR available so that session data can be accessed via \fIsession\.name\fR etc:
1126.1127.IP "" 41128.1129.nf11301131<%= session\.name %>

Rebuilt docsTj Holowaychukbefore adoptionba88e48

lines 1132–1152why, one step back
1132.
1133.fi1134.1135.IP "" 01136.1137.SS "app\.mounted(fn)"1138Assign a callback \fIfn\fR which is called when this \fIServer\fR is passed to \fIServer#use()\fR\.1139.1140.IP "" 41141.1142.nf11431144var app = express\.createServer(),1145 blog = express\.createServer();11461147blog\.mounted(function(parent){1148 // parent is app1149 // \"this\" is blog1150});11511152app\.use(blog);

Added app.register() docsTj Holowaychukbefore adoption77e5d8e

lines 1153–1196why, one step back
1153.
1154.fi1155.1156.IP "" 01157.1158.SS "app\.register(ext, exports)"1159Register the given template engine \fIexports\fR as \fIext\fR\. For example we may wish to map \"\.html\" files to jade:1160.1161.IP "" 41162.1163.nf11641165 app\.register(\'\.html\', require(\'jade\'));1166.1167.fi1168.1169.IP "" 01170.1171.P1172This is also useful for libraries that may not match extensions correctly\. For example my haml\.js library is installed from npm as \"hamljs\" so instead of layout\.hamljs, we can register the engine as \"\.haml\":1173.1174.IP "" 41175.1176.nf11771178 app\.register(\'\.haml\', require(\'haml\-js\'));1179.1180.fi1181.1182.IP "" 01183.1184.P1185For engines that do not comply with the Express specification, we can also wrap their api this way\.1186.1187.IP "" 41188.1189.nf11901191 app\.register(\'\.foo\', {1192 render: function(str, options) {1193 // perhaps their api is1194 // foo\.toHTML(str, options);1195 }1196 });

Docs for Server#{helpers,dynamicHelpers}()Tj Holowaychukbefore adoption78008d1

lines 1197–1200why, one step back
1197.
1198.fi1199.1200.IP "" 0

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 1201–1215why, one step back
1201.
1202.SS "app\.listen([port[, host]])"1203Bind 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\.1204.1205.IP "" 41206.1207.nf12081209app\.listen();1210app\.listen(3000);1211app\.listen(3000, \'n\.n\.n\.n\');1212.1213.fi1214.1215.IP "" 0

Unix domain socket docsTj Holowaychukbefore adoption2fa7907

lines 1216–1248why, one step back
1216.
1217.P1218The \fIport\fR argument may also be a string representing the path to a unix domain socket:1219.1220.IP "" 41221.1222.nf12231224app\.listen(\'/tmp/express\.sock\');1225.1226.fi1227.1228.IP "" 01229.1230.P1231Then try it out:1232.1233.IP "" 41234.1235.nf12361237$ telnet /tmp/express\.sock1238GET / HTTP/1\.112391240HTTP/1\.1 200 OK1241Content\-Type: text/plain1242Content\-Length: 1112431244Hello World1245.1246.fi1247.1248.IP "" 0

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

line 1249why, one step back
1249