From 17d8f6707313892be5bcc1680bea368d5e449309 Mon Sep 17 00:00:00 2001 From: TJ Holowaychuk Date: Tue, 27 Aug 2013 19:48:58 -0700 Subject: [PATCH] add pipe example. Closes #9 --- examples/streams-pipe.js | 74 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 examples/streams-pipe.js diff --git a/examples/streams-pipe.js b/examples/streams-pipe.js new file mode 100644 index 0000000..70075b8 --- /dev/null +++ b/examples/streams-pipe.js @@ -0,0 +1,74 @@ + +var zlib = require('zlib'); +var koa = require('..'); +var fs = require('fs'); +var app = koa(); + +// ignore favicons + +app.use(function(next){ + return function *(){ + 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; + } +}); + +// stream a file + +app.use(function(next){ + return function *(){ + 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){ + return function *(){ + if (!this.hasContent) return yield next; + + // TODO: fix ... + if (!this.body) return yield next; + + if (this.body.readable) { + var body = this.body; + 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