koa-lite/examples/route.js

64 lines
1 KiB
JavaScript
Raw Permalink Normal View History

2013-08-17 07:15:57 +00:00
var koa = require('..');
var app = koa();
var data;
// logger
app.use(function *(next){
var start = new Date;
yield next;
var ms = new Date - start;
console.log('%s %s - %s', this.method, this.url, ms);
2013-08-17 07:15:57 +00:00
});
// response
app.use(get('/', function *(){
if (data) {
this.body = data;
} else {
this.body = 'POST to /';
}
}));
app.use(post('/', function *(){
data = yield buffer(this);
this.status = 201;
this.body = 'created';
}));
function buffer(ctx) {
return function(done){
var buf = '';
ctx.req.setEncoding('utf8');
ctx.req.on('data', function(chunk){
buf += chunk;
});
ctx.req.on('end', function(){
done(null, buf);
});
};
}
function get(path, fn) {
return route('GET', path, fn);
}
function post(path, fn) {
return route('POST', path, fn);
}
function route(method, path, fn) {
return function *(next){
var match = method == this.method && this.path == path;
if (match) return yield fn;
yield next;
2013-08-17 07:15:57 +00:00
}
}
2013-08-28 02:57:16 +00:00
app.listen(3000);