Docs
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent eec9d05 · Browse files · 1 file·+60 added−13 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+60 added−13 removed
- other:+60 added−13 removed
Provenance
- Commit
5234368- Tree
c3cc948- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
eec9d05
1 file changed+60 added−13 removed
modified:docs/guide.md+60 added-13 removed
40 unmodified lines41 Note the use of _app.router_, which can (optionally) be used to mount the application routes,42 otherwise the first call to _app.{get,put,del,post}()_ will mount the routes.43 44 app.configure(function(){45- app.use(express.methodOverride());46- app.use(express.bodyDecoder());47- app.use(app.router);48- app.use(express.staticProvider(__dirname + '/public'));49- });45+ app.use(express.methodOverride());46+ app.use(express.bodyDecoder());47+ app.use(app.router);48+ app.use(express.staticProvider(__dirname + '/public'));49+ });50 51- app.configure('development', function(){52- app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));53- });51+ app.configure('development', function(){52+ app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));53+ });54 55- app.configure('production', function(){56- app.use(express.errorHandler());57- });55+ app.configure('production', function(){56+ app.use(express.errorHandler());57+ });58 59 For internal and arbitrary settings Express provides the _set(key[, val])_, _enable(key)_, _disable(key)_ methods:60 61 app.configure(function(){33 unmodified lines95 can be done by defining the route below. The values associated to the named placeholders 96 are available as `req.params`.97 98 app.get('/user/:id', function(req, res){99- res.send('user ' + req.params.id);100- });99+ res.send('user ' + req.params.id);100+ });101 102 A route is simple a string which is compiled to a _RegExp_ internally. For example103 when _/user/:id_ is compiled, a simplified version of the regexp may look similar to:104 47 unmodified lines152 /products.json153 /products.xml154 /products155 156+For example we can __POST__ some json, and echo the json back using the _bodyDecoder_ middleware which will parse json request bodies (as well as others), and place the result in _req.body_:157+158+ var express = require('express')159+ , app = express.createServer();160+161+ app.use(express.bodyDecoder());162+163+ app.post('/', function(req, res){164+ res.send(req.body);165+ });166+167+ app.listen(3000);168+169 ### Passing Route Control170 171 We may pass control to the next _matching_ route, by calling the _third_ argument,172 the _next()_ function. When a match cannot be made, control is passed back to Connect,11 unmodified lines184 app.get('/users', function(req, res){185 // do something else186 });187 188+Express 1.0 also introduces the _all()_ method, which provides a route callback matching any HTTP method. This is useful in many ways, one example being the loading of resources before executing subsequent routes as shown below:189+190+ var express = require('express')191+ , app = express.createServer();192+193+ var users = [{ name: 'tj' }];194+195+ app.all('/user/:id/:op?', function(req, res, next){196+ req.user = users[req.params.id];197+ if (req.user) {198+ next();199+ } else {200+ next(new Error('cannot find user ' + req.params.id));201+ }202+ });203+204+ app.get('/user/:id', function(req, res){205+ res.send('viewing ' + req.user.name);206+ });207+208+ app.get('/user/:id/edit', function(req, res){209+ res.send('editing ' + req.user.name);210+ });211+212+ app.put('/user/:id', function(req, res){213+ res.send('updating ' + req.user.name);214+ });215+216+ app.get('*', function(req, res){217+ res.send('what???', 404);218+ });219+220+ app.listen(3000); 221+222 ### Middleware223 224 Middleware via [Connect](http://github.com/senchalabs/connect) can be225 passed to _express.createServer()_ as you would with a regular Connect server. For example:822 unmodified lines