Skip to content
Myr

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

1 file+60 added−13 removed

modified:docs/guide.md+60 added-13 removed
40 unmodified lines
41Note the use of _app.router_, which can (optionally) be used to mount the application routes,42otherwise the first call to _app.{get,put,del,post}()_ will mount the routes.4344 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 });5859For internal and arbitrary settings Express provides the _set(key[, val])_, _enable(key)_, _disable(key)_ methods:6061 app.configure(function(){
33 unmodified lines
95can be done by defining the route below. The values associated to the named placeholders 96are available as `req.params`.9798 app.get('/user/:id', function(req, res){99 res.send('user ' + req.params.id);100 });99 res.send('user ' + req.params.id);100 });101102A route is simple a string which is compiled to a _RegExp_ internally. For example103when _/user/:id_ is compiled, a simplified version of the regexp may look similar to:104
47 unmodified lines
152 /products.json153 /products.xml154 /products155156For 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_:157158 var express = require('express')159 , app = express.createServer();160161 app.use(express.bodyDecoder());162163 app.post('/', function(req, res){164 res.send(req.body);165 });166167 app.listen(3000);168169### Passing Route Control170171We may pass control to the next _matching_ route, by calling the _third_ argument,172the _next()_ function. When a match cannot be made, control is passed back to Connect,
11 unmodified lines
184 app.get('/users', function(req, res){185 // do something else186 });187188Express 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:189190 var express = require('express')191 , app = express.createServer();192193 var users = [{ name: 'tj' }];194195 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 });203204 app.get('/user/:id', function(req, res){205 res.send('viewing ' + req.user.name);206 });207208 app.get('/user/:id/edit', function(req, res){209 res.send('editing ' + req.user.name);210 });211212 app.put('/user/:id', function(req, res){213 res.send('updating ' + req.user.name);214 });215216 app.get('*', function(req, res){217 res.send('what???', 404);218 });219220 app.listen(3000); 221222### Middleware223224Middleware via [Connect](http://github.com/senchalabs/connect) can be225passed to _express.createServer()_ as you would with a regular Connect server. For example:
822 unmodified lines