From 94efc85b7a4fdec200dbaa62180a6cf5048315b2 Mon Sep 17 00:00:00 2001 From: TJ Holowaychuk Date: Tue, 31 Dec 2013 10:33:26 -0800 Subject: [PATCH] remove remaining examples --- examples/co.js | 20 ------ examples/conditional-middleware-middleware.js | 39 ------------ examples/conditional-middleware.js | 27 -------- examples/route.js | 63 ------------------- examples/streams-pipe.js | 62 ------------------ 5 files changed, 211 deletions(-) delete mode 100644 examples/co.js delete mode 100644 examples/conditional-middleware-middleware.js delete mode 100644 examples/conditional-middleware.js delete mode 100644 examples/route.js delete mode 100644 examples/streams-pipe.js diff --git a/examples/co.js b/examples/co.js deleted file mode 100644 index 0e3ee0e..0000000 --- a/examples/co.js +++ /dev/null @@ -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); diff --git a/examples/conditional-middleware-middleware.js b/examples/conditional-middleware-middleware.js deleted file mode 100644 index 92eaaeb..0000000 --- a/examples/conditional-middleware-middleware.js +++ /dev/null @@ -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); diff --git a/examples/conditional-middleware.js b/examples/conditional-middleware.js deleted file mode 100644 index aa085b6..0000000 --- a/examples/conditional-middleware.js +++ /dev/null @@ -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); diff --git a/examples/route.js b/examples/route.js deleted file mode 100644 index cc2d561..0000000 --- a/examples/route.js +++ /dev/null @@ -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); diff --git a/examples/streams-pipe.js b/examples/streams-pipe.js deleted file mode 100644 index f95972b..0000000 --- a/examples/streams-pipe.js +++ /dev/null @@ -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()); - }); - } -} \ No newline at end of file