
var http = require('http');
var koa = require('..');
var app = koa();

// x-response-time

app.use(function(next){
  return function *(){
    var start = new Date;
    yield next;
    var ms = new Date - start;
    this.set('X-Response-Time', ms + 'ms');
  }
});

// logger

app.use(function(next){
  return function *(){
    var start = new Date;
    yield next;
    var ms = new Date - start;
    console.log('%s %s - %s', this.method, this.url, ms);
  }
});

// content-length

app.use(function(next){
  return function *(){
    yield next;
    this.set('Content-Length', Buffer.byteLength(this.body));
  }
});

// response

app.use(function(next){
  return function *(){
    yield next;
    if ('/' != this.url) return;
    this.status = 200;
    this.body = 'Hello World';
  }
});

// custom 404 handler

app.use(function(next){
  return function *(){
    yield next;
    this.status = 404;
    this.body = 'Sorry cannot find that!';
    console.log(this);
  }
});

app.listen(3000);


var http = require('http');
var koa = require('..');
var app = koa();

// x-response-time

app.use(function(next){
  return function *(){
    var start = new Date;
    yield next;
    var ms = new Date - start;
    this.set('X-Response-Time', ms + 'ms');
  }
});

// logger

app.use(function(next){
  return function *(){
    var start = new Date;
    yield next;
    var ms = new Date - start;
    console.log('%s %s - %s', this.method, this.url, ms);
  }
});

// content-length

app.use(function(next){
  return function *(){
    yield next;
    if (!this.body) return;
    this.set('Content-Length', Buffer.byteLength(this.body));
  }
});

// custom 404 handler

app.use(function(next){
  return function *(){
    yield next;
    if (this.body) return;
    this.status = 404;
    this.body = 'Sorry! No luck';
  }
});

// response

app.use(function(next){
  return function *(){
    yield next;
    if ('/' != this.url) return;
    this.body = 'Hello World';
  }
});

app.listen(3000);

fix simple example