Skip to content
Myr

guide.1

This file

Path

docs/guide.1

210 lines · 4.6 KiB

Last changed by

Merge branch 'redirect' into connectTj Holowaychukbefore adoption87cfca7

History of this file

Take it

Started guideTj Holowaychukbefore adoption9e3a0c6

lines 1–5why, one step back
1.\" generated with Ronn/v0.6.6
2.\" http://github.com/rtomayko/ronn/3.4.TH "GUIDE" "" "July 2010" "" ""5.

Removed annoying padding transitionTj Holowaychukbefore adoption346b2a2

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

Moved config docs upTj Holowaychukbefore adoption6e8413f

line 7why, one step back
7The \fIexpress\.Server\fR now inherits from \fIhttp\.Server\fR, however follows the same idiom by providing \fIexpress\.createServer()\fR as shown below\. This means that you can utilize Express server\'s transparently with other libraries\.

Added configuration sectionTj Holowaychukbefore adoption83d2555

lines 8–24why, one step back
8.
9.IP "" 410.11.nf1213var app = require(\'express\')\.createServer();1415app\.get(\'/\', function(req, res){16 res\.send(\'hello world\');17});1819app\.listen(3000);20.21.fi22.23.IP "" 024.

Moved config docs upTj Holowaychukbefore adoption6e8413f

lines 25–83why, one step back
25.SS "Configuration"
26Express supports arbitrary environments, such as \fIproduction\fR and \fIdevelopment\fR\. Developers can use the \fIconfigure()\fR method to setup needs required by the current environment\. When \fIconfigure()\fR is called without an environment name it will be run in \fIevery\fR environment prior to the environment specific callback\.27.28.P29In the example below we only \fIdumpExceptions\fR, and respond with exception stack traces in \fIdevelopment\fR mode, however for both environments we utilize \fImethodOverride\fR and \fIbodyDecoder\fR\.30.31.IP "" 432.33.nf3435app\.configure(function(){36 app\.use(\'/\', connect\.methodOverride());37 app\.use(\'/\', connect\.bodyDecoder());38});3940app\.configure(\'development\', function(){41 app\.use(\'/\', connect\.errorHandler({ dumpExceptions: true, showStack: true }));42});4344app\.configure(\'production\', function(){45 app\.use(\'/\', connect\.errorHandler());46});47.48.fi49.50.IP "" 051.52.P53For internal and arbitrary settings Express provides the \fIset(key[, val])\fR, \fIenable(key)\fR, \fIdisable(key)\fR methods:54.55.IP "" 456.57.nf5859app\.configure(function(){60 app\.set(\'views\', __dirname + \'/views\');61 app\.set(\'views\');62 // => \"\.\.\. views directory \.\.\.\"6364 app\.enable(\'some feature\');65 // same as app\.set(\'some feature\', true);6667 app\.disable(\'some feature\');68 // same as app\.set(\'some feature\', false);69});70.71.fi72.73.IP "" 074.75.SS "Settings"76Express supports the following settings out of the box:77.78.IP "\(bu" 479\fIviews\fR Root views directory defaulting to \fBCWD/views\fR80.81.IP "\(bu" 482\fIview engine\fR Default view engine name for views rendered without extensions83.

Added more setting docsTj Holowaychukbefore adoption38281e6

lines 84–86why, one step back
84.IP "\(bu" 4
85\fIreload views\fR Reloads altered views, by default watches for \fImtime\fR changes with with a 5 minute interval\. Example: \fBapp\.set(\'reload views\', { interval: 60000 });\fR86.

Moved config docs upTj Holowaychukbefore adoption6e8413f

lines 87–88why, one step back
87.IP "" 0
88.

Started guideTj Holowaychukbefore adoption9e3a0c6

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

Route passing docsTj Holowaychukbefore adoption0ace97c

line 90why, one step back
90Express 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 91–163why, one step back
91.
92.IP "" 493.94.nf9596app\.get(\'/user/:id\', function(req, res, params){97 res\.send(\'user \' + params\.id);98});99.100.fi101.102.IP "" 0103.104.P105A 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:106.107.IP "" 4108.109.nf110111\\/user\\/([^\\/]+)\\/?112.113.fi114.115.IP "" 0116.117.P118Literal regular expressions may also be passed for complex uses:119.120.IP "" 4121.122.nf123124app\.get(/^\\/foo(bar)?$/, function(){});125.126.fi127.128.IP "" 0129.130.P131Below are some route examples, and the associated paths that they may consume:132.133.IP "" 4134.135.nf136137 \"/user/:id\"138 /user/12139140 \"/users/:id?\"141 /users/5142 /users143144 \"/files/*\"145 /files/jquery\.js146 /files/javascripts/jquery\.js147148 \"/file/*\.*\"149 /files/jquery\.js150 /files/javascripts/jquery\.js151152 \"/user/:id/:operation?\"153 /user/1154 /user/1/edit155156 \"/products\.:format\"157 /products\.json158 /products\.xml159160 \"/products\.:format?\"161 /products\.json162 /products\.xml163 /products

Route passing docsTj Holowaychukbefore adoption0ace97c

lines 164–185why, one step back
164.
165.fi166.167.IP "" 0168.169.SS "Passing Route Control"170We 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\.171.172.IP "" 4173.174.nf175176app\.get(\'/users/:id?\', function(req, res, params){177 if (params\.id) {178 // do something179 } else {180 next();181 }182});183184app\.get(\'/users\', function(req, res, params){185 // do something else

Added configuration sectionTj Holowaychukbefore adoption83d2555

lines 186–207why, one step back
186});
187.188.fi189.190.IP "" 0191.192.SS "Middleware"193The 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:194.195.IP "" 4196.197.nf198199var connect = require(\'connect\'),200 express = require(\'express\');201202var app = express\.createServer(203 connect\.logger(),204 connect\.bodyDecoder()205);206.207.fi

Configuration docsTj Holowaychukbefore adoptioncb04364

lines 208–209why, one step back
208.
209.IP "" 0

Started guideTj Holowaychukbefore adoption9e3a0c6

line 210why, one step back
210