remove remaining examples

This commit is contained in:
TJ Holowaychuk 2013-12-31 10:33:26 -08:00
parent f28dee3423
commit 94efc85b7a
5 changed files with 0 additions and 211 deletions

View file

@ -1,20 +0,0 @@
var koa = require('..');
var fs = require('co-fs');
var app = koa();
// read docs/*.md in parallel
// and return a joined response
app.use(function *(){
var paths = yield fs.readdir('docs');
var files = yield paths.map(function(path){
return fs.readFile('docs/' + path, 'utf8');
});
this.type = 'markdown';
this.body = files.join('');
});
app.listen(3000);

View file

@ -1,39 +0,0 @@
var koa = require('..');
var app = koa();
// logger
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
// when an asset is requested. This is a modular
// approach to conditiona-middleware.js
function ignoreAssets(mw) {
return function *(next){
if (/(\.js|\.css|\.ico)$/.test(this.path)) {
yield next;
} else {
yield mw.call(this, next);
}
}
}
app.use(ignoreAssets(logger));
// sometimes it's useful to apply some
// ad-hoc logic to enable middleware, for
// example ignoring a logger on asset requests:
app.use(function *(){
this.body = 'Hello World';
});
app.listen(3000);

View file

@ -1,27 +0,0 @@
var koa = require('..');
var app = koa();
// logger
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){
if (/(\.js|\.css|\.ico)$/.test(this.path)) {
yield next;
} else {
this.body = 'Hello World';
yield logger.call(this, next);
}
});
app.listen(3000);

View file

@ -1,63 +0,0 @@
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);
});
// 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;
}
}
app.listen(3000);

View file

@ -1,62 +0,0 @@
var zlib = require('zlib');
var koa = require('..');
var fs = require('fs');
var app = koa();
// ignore favicons
app.use(function *(next){
if ('/favicon.ico' == this.path) this.status = 404;
yield next;
});
// logger
app.use(function *(next){
console.log('%s %s', this.method, this.url);
yield next;
});
// stream a file
app.use(function *(next){
var path = __dirname + this.path;
var exists = yield isFile(path);
if (!exists) return yield next;
this.body = fs.createReadStream(path);
yield next;
});
// gzip the response
app.use(function *(next){
var body = this.body;
if (!body || !body.readable) return yield next;
var gzip = zlib.createGzip();
this.set('Content-Encoding', 'gzip');
this.body = gzip;
body.pipe(gzip);
yield next;
});
app.listen(3000);
/**
* Stat regular file helper.
*/
function isFile(file) {
return function(done){
fs.stat(file, function(err, stat){
if (err && 'ENOENT' == err.code) return done(null, false);
if (err) return done(err);
done(null, stat.isFile());
});
}
}