guide.md
This file
Path
docs/guide.md
146 lines · 3.9 KiB · Markdown
Last changed by
Removed annoying padding transitionTj Holowaychukbefore adoption346b2a2
History of this fileTake it
Started guideTj Holowaychukbefore adoption9e3a0c6
line 1why, one step back1Removed annoying padding transitionTj Holowaychukbefore adoption346b2a2
line 2why, one step back2### Creating An ApplicationAdded configuration sectionTj Holowaychukbefore adoption83d2555
lines 3–4why, one step back34The _express.Server_ now inherits from _http.Server_, howeverMoved config docs upTj Holowaychukbefore adoption6e8413f
lines 5–6why, one step back5follows the same idiom by providing _express.createServer()_ as shown below. This means6that you can utilize Express server's transparently with other libraries.Added configuration sectionTj Holowaychukbefore adoption83d2555
lines 7–15why, one step back78 var app = require('express').createServer();9 10 app.get('/', function(req, res){11 res.send('hello world');12 });13 14 app.listen(3000);15Moved config docs upTj Holowaychukbefore adoption6e8413f
lines 16–58why, one step back16### Configuration1718Express supports arbitrary environments, such as _production_ and _development_. Developers19can use the _configure()_ method to setup needs required by the current environment. When20_configure()_ is called without an environment name it will be run in _every_ environment 21prior to the environment specific callback.2223In the example below we only _dumpExceptions_, and respond with exception stack traces24in _development_ mode, however for both environments we utilize _methodOverride_ and _bodyDecoder_.2526 app.configure(function(){27 app.use('/', connect.methodOverride());28 app.use('/', connect.bodyDecoder());29 });30 31 app.configure('development', function(){32 app.use('/', connect.errorHandler({ dumpExceptions: true, showStack: true }));33 });34 35 app.configure('production', function(){36 app.use('/', connect.errorHandler());37 });3839For internal and arbitrary settings Express provides the _set(key[, val])_, _enable(key)_, _disable(key)_ methods:4041 app.configure(function(){42 app.set('views', __dirname + '/views');43 app.set('views');44 // => "... views directory ..."45 46 app.enable('some feature');47 // same as app.set('some feature', true);48 49 app.disable('some feature');50 // same as app.set('some feature', false);51 });5253### Settings5455Express supports the following settings out of the box:5657 * _views_ Root views directory defaulting to **CWD/views**58 * _view engine_ Default view engine name for views rendered without extensionsAdded more setting docsTj Holowaychukbefore adoption38281e6
lines 59–60why, one step back59 * _reload views_ Reloads altered views, by default watches for _mtime_ changes with60 with a 5 minute interval. Example: `app.set('reload views', { interval: 60000 });`Moved config docs upTj Holowaychukbefore adoption6e8413f
line 61why, one step back61Started guideTj Holowaychukbefore adoption9e3a0c6
lines 62–65why, one step back62### Routing6364Express utilizes the HTTP verbs to provide a meaningful, expressive routing API.65For example we may want to render a user's account for the path _/user/12_, thisRoute passing docsTj Holowaychukbefore adoption0ace97c
lines 66–67why, one step back66can be done by defining the route below. The values associated to the named placeholders,67are passed as the _third_ argument, which here we name _params_.Started guideTj Holowaychukbefore adoption9e3a0c6
lines 68–111why, one step back6869 app.get('/user/:id', function(req, res, params){70 res.send('user ' + params.id);71 });7273A route is simple a string which is compiled to a _RegExp_ internally. For example74when _/user/:id_ is compiled, a simplified version of the regexp may look similar to:7576 \/user\/([^\/]+)\/?7778Literal regular expressions may also be passed for complex uses:7980 app.get(/^\/foo(bar)?$/, function(){});8182Below are some route examples, and the associated paths that they83may consume:8485 "/user/:id"86 /user/128788 "/users/:id?"89 /users/590 /users9192 "/files/*"93 /files/jquery.js94 /files/javascripts/jquery.js9596 "/file/*.*"97 /files/jquery.js98 /files/javascripts/jquery.js99 100 "/user/:id/:operation?"101 /user/1102 /user/1/edit103 104 "/products.:format"105 /products.json106 /products.xml107108 "/products.:format?"109 /products.json110 /products.xml111 /productsRoute passing docsTj Holowaychukbefore adoption0ace97c
lines 112–127why, one step back112113### Passing Route Control114115We may pass control to the next _matching_ route, by calling the _fourth_ parameter,116the _next()_ function. When a match cannot be made, control is passed back to Connect.117118 app.get('/users/:id?', function(req, res, params){119 if (params.id) {120 // do something121 } else {122 next();123 }124 });125 126 app.get('/users', function(req, res, params){127 // do something elseAdded configuration sectionTj Holowaychukbefore adoption83d2555
lines 128–144why, one step back128 });129130### Middleware131132The Express _Plugin_ is no more! middleware via [Connect](http://github.com/extjs/Connect) can be133passed to _express.createServer()_ as you would with a regular Connect server. For example:134135 var connect = require('connect'),136 express = require('express');137138 var app = express.createServer(139 connect.logger(),140 connect.redirect(),141 connect.bodyDecoder()142 );143144Configuration docsTj Holowaychukbefore adoptioncb04364
lines 145–146why, one step back145146