remove closure wrap in examples thanks to compose 2.0

This commit is contained in:
Jonathan Ong 2013-11-08 01:13:43 -08:00
parent 1bcf9b413f
commit 611dec10f2
12 changed files with 161 additions and 229 deletions

View file

@ -6,8 +6,7 @@ var app = koa();
// read docs/*.md in parallel
// and return a joined response
app.use(function(){
return function *(){
app.use(function *(){
var paths = yield fs.readdir('docs');
var files = yield paths.map(function(path){
@ -16,7 +15,6 @@ app.use(function(){
this.type = 'markdown';
this.body = files.join('');
}
});
app.listen(3000);

View file

@ -1,47 +1,39 @@
var compose = require('koa-compose');
var http = require('http');
var koa = require('..');
var app = koa();
var calls = [];
// x-response-time
function responseTime(){
return function responseTime(next){
return function *(){
return function *responseTime(next){
var start = new Date;
yield next;
var ms = new Date - start;
this.set('X-Response-Time', ms + 'ms');
}
}
}
// logger
function logger(){
return function logger(next){
return function *(){
return function* logger(next){
var start = new Date;
yield next;
var ms = new Date - start;
console.log('%s %s - %s', this.method, this.url, ms);
}
}
}
// response
function respond() {
return function respond(next){
return function *(){
return function* respond(next){
yield next;
if ('/' != this.url) return;
this.body = 'Hello World';
}
}
}
// composed middleware
@ -55,4 +47,4 @@ function all() {
app.use(all());
http.createServer(app.callback()).listen(3000);
app.listen(3000);

View file

@ -1,18 +1,15 @@
var http = require('http');
var koa = require('..');
var app = koa();
// logger
function logger(next){
return function *(){
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
@ -20,8 +17,7 @@ function logger(next){
// approach to conditiona-middleware.js
function ignoreAssets(mw) {
return function(next){
return function *(){
return function *(next){
if (/(\.js|\.css|\.ico)$/.test(this.path)) {
yield next;
} else {
@ -29,7 +25,6 @@ function ignoreAssets(mw) {
}
}
}
}
app.use(ignoreAssets(logger));
@ -37,10 +32,8 @@ app.use(ignoreAssets(logger));
// ad-hoc logic to enable middleware, for
// example ignoring a logger on asset requests:
app.use(function(next){
return function *(){
app.use(function *(){
this.body = 'Hello World';
}
});
app.listen(3000);

View file

@ -1,32 +1,27 @@
var http = require('http');
var koa = require('..');
var app = koa();
// logger
function logger(next){
return function *(){
function *logger(next){
var start = new Date;
yield next;
var ms = new Date - start;
console.log('%s %s - %s', this.method, this.url, ms);
}
}
// sometimes it's useful to apply some
// ad-hoc logic to enable middleware, for
// example ignoring a logger on asset requests:
app.use(function(next){
return function *(){
app.use(function *(next){
if (/(\.js|\.css|\.ico)$/.test(this.path)) {
yield next;
} else {
this.body = 'Hello World';
yield logger(next);
}
}
});
app.listen(3000);

View file

@ -1,15 +1,12 @@
var http = require('http');
var koa = require('..');
var app = koa();
app.use(function(){
return function *(){
app.use(function *(){
if ('/favicon.ico' == this.path) return;
var n = ~~this.cookies.get('view') || 1;
this.cookies.set('view', n + 1);
this.body = n + ' views';
}
});
app.listen(3000);

View file

@ -1,12 +1,10 @@
var http = require('http');
var koa = require('..');
var app = koa();
// look ma, error propagation!
app.use(function(next){
return function *(){
app.use(function *(next){
try {
yield next;
} catch (err) {
@ -22,15 +20,12 @@ app.use(function(next){
// centralized still functions correctly.
this.app.emit('error', err, this);
}
}
});
// response
app.use(function(next){
return function *(){
app.use(function *(){
throw new Error('boom boom');
}
});
// error handler

View file

@ -1,6 +1,5 @@
var koa = require('..');
var fs = require('fs');
var app = koa();
var tobi = {
@ -26,8 +25,7 @@ var users = {
// may want to check the type, as it may
// be a stream, buffer, string, etc.
app.use(function(next){
return function *(){
app.use(function *(next){
yield next;
// responses vary on accepted type
@ -57,31 +55,26 @@ app.use(function(next){
// default to text
this.body = this.body.name;
}
});
// filter responses, in this case remove ._id
// since it's private
app.use(function(next){
return function *(){
app.use(function *(next){
yield next;
if (!this.body) return;
delete this.body._id;
}
});
// try $ GET /tobi
// try $ GET /loki
app.use(function(){
return function *(){
app.use(function *(){
var name = this.path.slice(1);
var user = users[name];
this.body = user;
}
});
app.listen(3000);

View file

@ -1,5 +1,4 @@
var http = require('http');
var koa = require('..');
var app = koa();
@ -7,13 +6,11 @@ var data;
// logger
app.use(function(next){
return function *(){
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);
}
});
// response
@ -56,13 +53,11 @@ function post(path, fn) {
}
function route(method, path, fn) {
return function(next){
return function *() {
return function *(next){
var match = method == this.method && this.path == path;
if (match) return yield fn;
yield next;
}
}
}
app.listen(3000);

View file

@ -1,59 +1,48 @@
var http = require('http');
var koa = require('..');
var app = koa();
// x-response-time
app.use(function(next){
return function *responseTime(){
app.use(function *responseTime(next){
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 *logger(){
app.use(function *logger(next){
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 *contentLength(){
app.use(function *contentLength(next){
yield next;
if (!this.body) return;
this.set('Content-Length', Buffer.byteLength(this.body));
}
});
// custom 404 handler
app.use(function(next){
return function *notfound(){
app.use(function *notfound(next){
yield next;
if (this.body) return;
this.status = 404;
this.body = 'Sorry! No luck';
}
});
// response
app.use(function(next){
return function *response(){
app.use(function *response(next){
yield next;
if ('/' != this.url) return;
this.body = 'Hello World';
}
});
app.listen(3000);

View file

@ -6,26 +6,21 @@ var app = koa();
// ignore favicons
app.use(function(next){
return function *(){
app.use(function *(next){
if ('/favicon.ico' == this.path) this.status = 404;
yield next;
}
});
// logger
app.use(function(next){
return function *(){
app.use(function *(next){
console.log('%s %s', this.method, this.url);
yield next;
}
});
// stream a file
app.use(function(next){
return function *(){
app.use(function *(next){
var path = __dirname + this.path;
var exists = yield isFile(path);
@ -33,13 +28,11 @@ app.use(function(next){
this.body = fs.createReadStream(path);
yield next;
}
});
// gzip the response
app.use(function(next){
return function *(){
app.use(function *(next){
var body = this.body;
if (!body || !body.readable) return yield next;
@ -50,7 +43,6 @@ app.use(function(next){
body.pipe(gzip);
yield next;
}
});
app.listen(3000);

View file

@ -5,11 +5,9 @@ var app = koa();
// try GET /streams.js
app.use(function(){
return function *(){
app.use(function *(){
var path = __dirname + this.path;
this.body = fs.createReadStream(path);
}
});
app.listen(3000);

View file

@ -1,6 +1,5 @@
var views = require('co-views');
var http = require('http');
var koa = require('..');
var app = koa();
@ -23,21 +22,17 @@ var user = {
// logger
app.use(function(next){
return function *logger(){
app.use(function *logger(next){
var start = new Date;
yield next;
var ms = new Date - start;
console.log('%s %s - %s', this.method, this.url, ms);
}
});
// render
app.use(function(next){
return function *(){
app.use(function *(){
this.body = yield render('user', { user: user });
}
})
app.listen(4000);