2013-08-28 04:06:47 +00:00
|
|
|
|
|
|
|
var koa = require('..');
|
|
|
|
var app = koa();
|
|
|
|
|
|
|
|
// logger
|
|
|
|
|
2013-11-08 09:13:43 +00:00
|
|
|
function *logger(next){
|
|
|
|
var start = new Date;
|
|
|
|
yield next;
|
|
|
|
var ms = new Date - start;
|
|
|
|
console.log('%s %s - %s', this.method, this.url, ms);
|
2013-08-28 04:06:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// sometimes it's useful to apply some
|
|
|
|
// ad-hoc logic to enable middleware, for
|
|
|
|
// example ignoring a logger on asset requests:
|
|
|
|
|
2013-11-08 09:13:43 +00:00
|
|
|
app.use(function *(next){
|
|
|
|
if (/(\.js|\.css|\.ico)$/.test(this.path)) {
|
|
|
|
yield next;
|
|
|
|
} else {
|
|
|
|
this.body = 'Hello World';
|
2013-11-15 05:20:51 +00:00
|
|
|
yield logger.call(this, next);
|
2013-08-28 04:06:47 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
app.listen(3000);
|