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,17 +6,15 @@ 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){
return fs.readFile('docs/' + path, 'utf8'); return fs.readFile('docs/' + path, 'utf8');
}); });
this.type = 'markdown'; this.type = 'markdown';
this.body = files.join(''); this.body = files.join('');
}
}); });
app.listen(3000); app.listen(3000);

View file

@ -1,45 +1,37 @@
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';
}
} }
} }
@ -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,13 +17,11 @@ 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);
}
} }
} }
} }
@ -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,31 +1,26 @@
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);
}
} }
}); });

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,36 +1,31 @@
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) { // some errors will have .status
// some errors will have .status // however this is not a guarantee
// however this is not a guarantee this.status = err.status || 500;
this.status = err.status || 500; this.type = 'html';
this.type = 'html'; this.body = '<p>Something <em>exploded</em>, please contact Maru.</p>';
this.body = '<p>Something <em>exploded</em>, please contact Maru.</p>';
// since we handled this manually we'll // since we handled this manually we'll
// want to delegate to the regular app // want to delegate to the regular app
// level error handling as well so that // level error handling as well so that
// 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,62 +25,56 @@ 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
this.vary('Accept'); this.vary('Accept');
this.status = 'bad request'; this.status = 'bad request';
// no body? nothing to format, early return // no body? nothing to format, early return
if (!this.body) return; if (!this.body) return;
// accepts json, koa handles this for us, // accepts json, koa handles this for us,
// so just return // so just return
if (this.accepts('json')) return; if (this.accepts('json')) return;
// accepts xml // accepts xml
if (this.accepts('xml')) { if (this.accepts('xml')) {
this.type = 'xml'; this.type = 'xml';
this.body = '<name>' + this.body.name + '</name>'; this.body = '<name>' + this.body.name + '</name>';
return; return;
}
// accepts html
if (this.accepts('html')) {
this.type = 'html';
this.body = '<h1>' + this.body.name + '</h1>';
return;
}
// default to text
this.body = this.body.name;
} }
// accepts html
if (this.accepts('html')) {
this.type = 'html';
this.body = '<h1>' + this.body.name + '</h1>';
return;
}
// default to text
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,12 +53,10 @@ 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;
}
} }
} }

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,51 +6,43 @@ 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);
if (!exists) return yield next; if (!exists) return yield 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;
var gzip = zlib.createGzip(); var gzip = zlib.createGzip();
this.set('Content-Encoding', 'gzip'); this.set('Content-Encoding', 'gzip');
this.body = gzip; this.body = gzip;
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);