.\" generated with Ronn/v0.6.6 .\" http://github.com/rtomayko/ronn/ . .TH "GUIDE" "" "July 2010" "" "" . .SS "Creating An Application" The \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\. . .IP "" 4 . .nf var app = require(\'express\')\.createServer(); app\.get(\'/\', function(req, res){ res\.send(\'hello world\'); }); app\.listen(3000); . .fi . .IP "" 0 . .SS "Configuration" Express 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\. . .P In 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\. . .IP "" 4 . .nf app\.configure(function(){ app\.use(\'/\', connect\.methodOverride()); app\.use(\'/\', connect\.bodyDecoder()); }); app\.configure(\'development\', function(){ app\.use(\'/\', connect\.errorHandler({ dumpExceptions: true, showStack: true })); }); app\.configure(\'production\', function(){ app\.use(\'/\', connect\.errorHandler()); }); . .fi . .IP "" 0 . .P For internal and arbitrary settings Express provides the \fIset(key[, val])\fR, \fIenable(key)\fR, \fIdisable(key)\fR methods: . .IP "" 4 . .nf app\.configure(function(){ app\.set(\'views\', __dirname + \'/views\'); app\.set(\'views\'); // => \"\.\.\. views directory \.\.\.\" app\.enable(\'some feature\'); // same as app\.set(\'some feature\', true); app\.disable(\'some feature\'); // same as app\.set(\'some feature\', false); }); . .fi . .IP "" 0 . .SS "Settings" Express supports the following settings out of the box: . .IP "\(bu" 4 \fIviews\fR Root views directory defaulting to \fBCWD/views\fR . .IP "\(bu" 4 \fIview engine\fR Default view engine name for views rendered without extensions . .IP "\(bu" 4 \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 });\fR . .IP "" 0 . .SS "Routing" Express 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\. . .IP "" 4 . .nf app\.get(\'/user/:id\', function(req, res, params){ res\.send(\'user \' + params\.id); }); . .fi . .IP "" 0 . .P A 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: . .IP "" 4 . .nf \\/user\\/([^\\/]+)\\/? . .fi . .IP "" 0 . .P Literal regular expressions may also be passed for complex uses: . .IP "" 4 . .nf app\.get(/^\\/foo(bar)?$/, function(){}); . .fi . .IP "" 0 . .P Below are some route examples, and the associated paths that they may consume: . .IP "" 4 . .nf \"/user/:id\" /user/12 \"/users/:id?\" /users/5 /users \"/files/*\" /files/jquery\.js /files/javascripts/jquery\.js \"/file/*\.*\" /files/jquery\.js /files/javascripts/jquery\.js \"/user/:id/:operation?\" /user/1 /user/1/edit \"/products\.:format\" /products\.json /products\.xml \"/products\.:format?\" /products\.json /products\.xml /products . .fi . .IP "" 0 . .SS "Passing Route Control" We 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\. . .IP "" 4 . .nf app\.get(\'/users/:id?\', function(req, res, params){ if (params\.id) { // do something } else { next(); } }); app\.get(\'/users\', function(req, res, params){ // do something else }); . .fi . .IP "" 0 . .SS "Middleware" The 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: . .IP "" 4 . .nf var connect = require(\'connect\'), express = require(\'express\'); var app = express\.createServer( connect\.logger(), connect\.bodyDecoder() ); . .fi . .IP "" 0