update guide docs for new middleware style

This commit is contained in:
TJ Holowaychuk 2013-11-10 15:40:59 -07:00
parent ee6dce83af
commit 9ee6ada2d5

View file

@ -8,14 +8,12 @@
`X-Response-Time` header field the middleware would look like the following:
```js
function responseTime(next){
return function *(){
function *responseTime(next){
var start = new Date;
yield next;
var ms = new Date - start;
this.set('X-Response-Time', ms + 'ms');
}
}
app.use(responseTime);
```
@ -23,13 +21,11 @@ app.use(responseTime);
Here's another way to write the same thing, inline:
```js
app.use(function(next){
return function *(){
app.use(function *(next){
var start = new Date;
yield next;
var ms = new Date - start;
this.set('X-Response-Time', ms + 'ms');
}
});
```
@ -63,20 +59,9 @@ to this behaviour.
For example this would be fine:
```js
app.use(function(){
return function *response(){
app.use(function *response(){
if ('/' != this.url) return;
this.body = 'Hello World';
}
});
```
It's important to note that not all middleware will need to conform, the best example of this
is a router, which are typically always acting as end-points, or downstream middleware:
````js
app.use(get('/', function *(){
this.body = 'Hello World';
});
```
@ -101,8 +86,7 @@ app.use(get('/', function *(){
function logger(format){
format = format || ':method ":url"';
return function(next){
return function *(){
return function *(next){
var str = format
.replace(':method', this.method)
.replace(':url', this.url);
@ -112,7 +96,6 @@ function logger(format){
yield next;
}
}
}
app.use(logger());
app.use(logger(':method :url'));
@ -125,10 +108,8 @@ app.use(logger(':method :url'));
```js
function logger(format){
return function(next){
return function *logger(){
// ^-- name this guy
}
return function *logger(next){
}
}
```
@ -143,8 +124,7 @@ function logger(format){
```js
var fs = require('co-fs');
app.use(function(){
return function *(){
app.use(function *(){
var paths = yield fs.readdir('docs');
var files = yield paths.map(function(path){
@ -153,7 +133,6 @@ app.use(function(){
this.type = 'markdown';
this.body = files.join('');
}
});
```