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

View file

@ -1,45 +1,37 @@
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 *(){
var start = new Date;
yield next;
var ms = new Date - start;
this.set('X-Response-Time', ms + 'ms');
}
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 *(){
var start = new Date;
yield next;
var ms = new Date - start;
console.log('%s %s - %s', this.method, this.url, ms);
}
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 *(){
yield next;
if ('/' != this.url) return;
this.body = 'Hello World';
}
return function* respond(next){
yield next;
if ('/' != this.url) return;
this.body = 'Hello World';
}
}
@ -55,4 +47,4 @@ function 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 app = koa();
// logger
function logger(next){
return function *(){
var start = new Date;
yield next;
var ms = new Date - start;
console.log('%s %s - %s', this.method, this.url, ms);
}
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
@ -20,13 +17,11 @@ function logger(next){
// approach to conditiona-middleware.js
function ignoreAssets(mw) {
return function(next){
return function *(){
if (/(\.js|\.css|\.ico)$/.test(this.path)) {
yield next;
} else {
yield mw(next);
}
return function *(next){
if (/(\.js|\.css|\.ico)$/.test(this.path)) {
yield next;
} else {
yield mw(next);
}
}
}
@ -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 *(){
this.body = 'Hello World';
}
app.use(function *(){
this.body = 'Hello World';
});
app.listen(3000);

View file

@ -1,31 +1,26 @@
var http = require('http');
var koa = require('..');
var app = koa();
// logger
function logger(next){
return function *(){
var start = new Date;
yield next;
var ms = new Date - start;
console.log('%s %s - %s', this.method, this.url, ms);
}
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 *(){
if (/(\.js|\.css|\.ico)$/.test(this.path)) {
yield next;
} else {
this.body = 'Hello World';
yield logger(next);
}
app.use(function *(next){
if (/(\.js|\.css|\.ico)$/.test(this.path)) {
yield next;
} else {
this.body = 'Hello World';
yield logger(next);
}
});

View file

@ -1,15 +1,12 @@
var http = require('http');
var koa = require('..');
var app = koa();
app.use(function(){
return 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.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,36 +1,31 @@
var http = require('http');
var koa = require('..');
var app = koa();
// look ma, error propagation!
app.use(function(next){
return function *(){
try {
yield next;
} catch (err) {
// some errors will have .status
// however this is not a guarantee
this.status = err.status || 500;
this.type = 'html';
this.body = '<p>Something <em>exploded</em>, please contact Maru.</p>';
app.use(function *(next){
try {
yield next;
} catch (err) {
// some errors will have .status
// however this is not a guarantee
this.status = err.status || 500;
this.type = 'html';
this.body = '<p>Something <em>exploded</em>, please contact Maru.</p>';
// since we handled this manually we'll
// want to delegate to the regular app
// level error handling as well so that
// centralized still functions correctly.
this.app.emit('error', err, this);
}
// since we handled this manually we'll
// want to delegate to the regular app
// level error handling as well so that
// centralized still functions correctly.
this.app.emit('error', err, this);
}
});
// response
app.use(function(next){
return function *(){
throw new Error('boom boom');
}
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,62 +25,56 @@ var users = {
// may want to check the type, as it may
// be a stream, buffer, string, etc.
app.use(function(next){
return function *(){
yield next;
app.use(function *(next){
yield next;
// responses vary on accepted type
this.vary('Accept');
this.status = 'bad request';
// responses vary on accepted type
this.vary('Accept');
this.status = 'bad request';
// no body? nothing to format, early return
if (!this.body) return;
// no body? nothing to format, early return
if (!this.body) return;
// accepts json, koa handles this for us,
// so just return
if (this.accepts('json')) return;
// accepts json, koa handles this for us,
// so just return
if (this.accepts('json')) return;
// accepts xml
if (this.accepts('xml')) {
this.type = 'xml';
this.body = '<name>' + this.body.name + '</name>';
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 xml
if (this.accepts('xml')) {
this.type = 'xml';
this.body = '<name>' + this.body.name + '</name>';
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;
});
// filter responses, in this case remove ._id
// since it's private
app.use(function(next){
return function *(){
yield next;
app.use(function *(next){
yield next;
if (!this.body) return;
if (!this.body) return;
delete this.body._id;
}
delete this.body._id;
});
// try $ GET /tobi
// try $ GET /loki
app.use(function(){
return function *(){
var name = this.path.slice(1);
var user = users[name];
this.body = user;
}
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 *(){
var start = new Date;
yield next;
var ms = new Date - start;
console.log('%s %s - %s', this.method, this.url, ms);
}
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,12 +53,10 @@ function post(path, fn) {
}
function route(method, path, fn) {
return function(next){
return function *() {
var match = method == this.method && this.path == path;
if (match) return yield fn;
yield next;
}
return function *(next){
var match = method == this.method && this.path == path;
if (match) return yield fn;
yield next;
}
}

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(){
var start = new Date;
yield next;
var ms = new Date - start;
this.set('X-Response-Time', ms + 'ms');
}
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(){
var start = new Date;
yield next;
var ms = new Date - start;
console.log('%s %s - %s', this.method, this.url, ms);
}
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(){
yield next;
if (!this.body) return;
this.set('Content-Length', Buffer.byteLength(this.body));
}
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(){
yield next;
if (this.body) return;
this.status = 404;
this.body = 'Sorry! No luck';
}
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(){
yield next;
if ('/' != this.url) return;
this.body = 'Hello World';
}
app.use(function *response(next){
yield next;
if ('/' != this.url) return;
this.body = 'Hello World';
});
app.listen(3000);

View file

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

View file

@ -5,11 +5,9 @@ var app = koa();
// try GET /streams.js
app.use(function(){
return function *(){
var path = __dirname + this.path;
this.body = fs.createReadStream(path);
}
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(){
var start = new Date;
yield next;
var ms = new Date - start;
console.log('%s %s - %s', this.method, this.url, ms);
}
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 *(){
this.body = yield render('user', { user: user });
}
app.use(function *(){
this.body = yield render('user', { user: user });
})
app.listen(4000);