98548f0409
just realized this since we removed the top closure. i'm not sure if there's a better way to solve this, but i'd rather have this inconvenience than the extra closures. we should add tests!!!
39 lines
807 B
JavaScript
39 lines
807 B
JavaScript
|
|
var koa = require('..');
|
|
var app = koa();
|
|
|
|
// logger
|
|
|
|
function *logger(next){
|
|
var start = new Date;
|
|
yield next;
|
|
var ms = new Date - start;
|
|
console.log('%s %s - %s', this.method, this.url, ms);
|
|
}
|
|
|
|
// passing any middleware to this middleware
|
|
// will make it conditional, and will not be used
|
|
// when an asset is requested. This is a modular
|
|
// approach to conditiona-middleware.js
|
|
|
|
function ignoreAssets(mw) {
|
|
return function *(next){
|
|
if (/(\.js|\.css|\.ico)$/.test(this.path)) {
|
|
yield next;
|
|
} else {
|
|
yield mw.call(this, next);
|
|
}
|
|
}
|
|
}
|
|
|
|
app.use(ignoreAssets(logger));
|
|
|
|
// sometimes it's useful to apply some
|
|
// ad-hoc logic to enable middleware, for
|
|
// example ignoring a logger on asset requests:
|
|
|
|
app.use(function *(){
|
|
this.body = 'Hello World';
|
|
});
|
|
|
|
app.listen(3000);
|