guide.1
This file
Path
docs/guide.1
211 lines · 4.6 KiB
Last changed by
Removed annoying padding transitionTj Holowaychukbefore adoption346b2a2
History of this fileTake it
1.\" generated with Ronn/v0.6.62.\" http://github.com/rtomayko/ronn/3.4.TH "GUIDE" "" "July 2010" "" ""5.6.SS "Creating An Application"7The \fIexpress\.Server\fR now inherits from \fIhttp\.Server\fR, however follows the same idiom by providing \fIexpress\.createServer()\fR as shown below\. This means that you can utilize Express server\'s transparently with other libraries\.8.9.IP "" 410.11.nf1213var app = require(\'express\')\.createServer();1415app\.get(\'/\', function(req, res){16 res\.send(\'hello world\');17});1819app\.listen(3000);20.21.fi22.23.IP "" 024.25.SS "Configuration"26Express supports arbitrary environments, such as \fIproduction\fR and \fIdevelopment\fR\. Developers can use the \fIconfigure()\fR method to setup needs required by the current environment\. When \fIconfigure()\fR is called without an environment name it will be run in \fIevery\fR environment prior to the environment specific callback\.27.28.P29In the example below we only \fIdumpExceptions\fR, and respond with exception stack traces in \fIdevelopment\fR mode, however for both environments we utilize \fImethodOverride\fR and \fIbodyDecoder\fR\.30.31.IP "" 432.33.nf3435app\.configure(function(){36 app\.use(\'/\', connect\.methodOverride());37 app\.use(\'/\', connect\.bodyDecoder());38});3940app\.configure(\'development\', function(){41 app\.use(\'/\', connect\.errorHandler({ dumpExceptions: true, showStack: true }));42});4344app\.configure(\'production\', function(){45 app\.use(\'/\', connect\.errorHandler());46});47.48.fi49.50.IP "" 051.52.P53For internal and arbitrary settings Express provides the \fIset(key[, val])\fR, \fIenable(key)\fR, \fIdisable(key)\fR methods:54.55.IP "" 456.57.nf5859app\.configure(function(){60 app\.set(\'views\', __dirname + \'/views\');61 app\.set(\'views\');62 // => \"\.\.\. views directory \.\.\.\"6364 app\.enable(\'some feature\');65 // same as app\.set(\'some feature\', true);6667 app\.disable(\'some feature\');68 // same as app\.set(\'some feature\', false);69});70.71.fi72.73.IP "" 074.75.SS "Settings"76Express supports the following settings out of the box:77.78.IP "\(bu" 479\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.84.IP "\(bu" 485\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.87.IP "" 088.89.SS "Routing"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\.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 /products164.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 else186});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\.redirect(),205 connect\.bodyDecoder()206);207.208.fi209.210.IP "" 0211