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

View file

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

View file

@ -1,17 +1,14 @@
var http = require('http');
var koa = require('..'); var koa = require('..');
var app = koa(); var app = koa();
// logger // logger
function logger(next){ function *logger(next){
return function *(){
var start = new Date; var start = new Date;
yield next; yield next;
var ms = new Date - start; var ms = new Date - start;
console.log('%s %s - %s', this.method, this.url, ms); console.log('%s %s - %s', this.method, this.url, ms);
}
} }
// passing any middleware to this middleware // passing any middleware to this middleware
@ -20,15 +17,13 @@ function logger(next){
// approach to conditiona-middleware.js // approach to conditiona-middleware.js
function ignoreAssets(mw) { function ignoreAssets(mw) {
return function(next){ return function *(next){
return function *(){
if (/(\.js|\.css|\.ico)$/.test(this.path)) { if (/(\.js|\.css|\.ico)$/.test(this.path)) {
yield next; yield next;
} else { } else {
yield mw(next); yield mw(next);
} }
} }
}
} }
app.use(ignoreAssets(logger)); app.use(ignoreAssets(logger));
@ -37,10 +32,8 @@ app.use(ignoreAssets(logger));
// ad-hoc logic to enable middleware, for // ad-hoc logic to enable middleware, for
// example ignoring a logger on asset requests: // example ignoring a logger on asset requests:
app.use(function(next){ app.use(function *(){
return function *(){
this.body = 'Hello World'; this.body = 'Hello World';
}
}); });
app.listen(3000); app.listen(3000);

View file

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

View file

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

View file

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

View file

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

View file

@ -1,5 +1,4 @@
var http = require('http');
var koa = require('..'); var koa = require('..');
var app = koa(); var app = koa();
@ -7,13 +6,11 @@ var data;
// logger // logger
app.use(function(next){ app.use(function *(next){
return function *(){
var start = new Date; var start = new Date;
yield next; yield next;
var ms = new Date - start; var ms = new Date - start;
console.log('%s %s - %s', this.method, this.url, ms); console.log('%s %s - %s', this.method, this.url, ms);
}
}); });
// response // response
@ -56,13 +53,11 @@ function post(path, fn) {
} }
function route(method, path, fn) { function route(method, path, fn) {
return function(next){ return function *(next){
return function *() {
var match = method == this.method && this.path == path; var match = method == this.method && this.path == path;
if (match) return yield fn; if (match) return yield fn;
yield next; yield next;
} }
}
} }
app.listen(3000); app.listen(3000);

View file

@ -1,59 +1,48 @@
var http = require('http');
var koa = require('..'); var koa = require('..');
var app = koa(); var app = koa();
// x-response-time // x-response-time
app.use(function(next){ app.use(function *responseTime(next){
return function *responseTime(){
var start = new Date; var start = new Date;
yield next; yield next;
var ms = new Date - start; var ms = new Date - start;
this.set('X-Response-Time', ms + 'ms'); this.set('X-Response-Time', ms + 'ms');
}
}); });
// logger // logger
app.use(function(next){ app.use(function *logger(next){
return function *logger(){
var start = new Date; var start = new Date;
yield next; yield next;
var ms = new Date - start; var ms = new Date - start;
console.log('%s %s - %s', this.method, this.url, ms); console.log('%s %s - %s', this.method, this.url, ms);
}
}); });
// content-length // content-length
app.use(function(next){ app.use(function *contentLength(next){
return function *contentLength(){
yield next; yield next;
if (!this.body) return; if (!this.body) return;
this.set('Content-Length', Buffer.byteLength(this.body)); this.set('Content-Length', Buffer.byteLength(this.body));
}
}); });
// custom 404 handler // custom 404 handler
app.use(function(next){ app.use(function *notfound(next){
return function *notfound(){
yield next; yield next;
if (this.body) return; if (this.body) return;
this.status = 404; this.status = 404;
this.body = 'Sorry! No luck'; this.body = 'Sorry! No luck';
}
}); });
// response // response
app.use(function(next){ app.use(function *response(next){
return function *response(){
yield next; yield next;
if ('/' != this.url) return; if ('/' != this.url) return;
this.body = 'Hello World'; this.body = 'Hello World';
}
}); });
app.listen(3000); app.listen(3000);

View file

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

View file

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

View file

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