From 67d9aedce60068f6d91325fd2e91bd00df78542f Mon Sep 17 00:00:00 2001 From: Jonathan Ong Date: Fri, 6 Dec 2013 14:01:10 -0800 Subject: [PATCH] simplify multipart example, add some comments --- .jshintignore | 2 ++ body-parsing/app.js | 10 ++++++++++ flash-messages/app.js | 5 +++++ multipart/app.js | 37 ++++++++++++++++++++++--------------- package.json | 1 - 5 files changed, 39 insertions(+), 16 deletions(-) create mode 100644 .jshintignore diff --git a/.jshintignore b/.jshintignore new file mode 100644 index 0000000..2a8576e --- /dev/null +++ b/.jshintignore @@ -0,0 +1,2 @@ +node_modules +*/test.js \ No newline at end of file diff --git a/body-parsing/app.js b/body-parsing/app.js index 06a68f7..cbcd791 100644 --- a/body-parsing/app.js +++ b/body-parsing/app.js @@ -1,3 +1,13 @@ +/** + * Example for handling JSON and urlencoded request bodies. + * All the throws at the beginning of the middleware are all app-dependent and may not be suited for your use-case. + * For the actual body parsing (yield rawBody and down), + * you most may be interested in modules that already do this for you: + * + * - [body](https://github.com/raynos/body) + * - [co-body](https://github.com/visionmedia/co-body) + */ + var koa = require('koa'); var qs = require('querystring'); var rawBody = require('raw-body'); diff --git a/flash-messages/app.js b/flash-messages/app.js index ff1d570..df2b606 100644 --- a/flash-messages/app.js +++ b/flash-messages/app.js @@ -1,3 +1,8 @@ +/** + * A very simple flash example. + * Only uses JSON for simplicity. + */ + var koa = require('koa'); var rawBody = require('raw-body'); var session = require('koa-session'); diff --git a/multipart/app.js b/multipart/app.js index 707cff7..5a1e80f 100644 --- a/multipart/app.js +++ b/multipart/app.js @@ -1,44 +1,51 @@ +/** + * Multipart example downloading all the files to disk using co-busboy. + * If all you want is to download the files to a temporary folder, + * just use https://github.com/cojs/multipart instead of copying this code + * as it handles file descriptor limits whereas this does not. + */ + var os = require('os'); var path = require('path'); var fs = require('fs'); var koa = require('koa'); var parse = require('co-busboy'); var saveTo = require('save-to'); -var archan = require('archan'); var app = module.exports = koa(); app.use(function *(){ - // create a go-like channel for control flow - var ch = archan({ - concurrency: 1 // save only 1 file at a time - }); - // parse the multipart body var parts = parse(this, { autoFields: true // saves the fields to parts.field(s) }); // create a temporary folder to store files - var tmpdir = path.join(os.tmpdir(), Math.random().toString(36).slice(2)); + var tmpdir = path.join(os.tmpdir(), uid()); // make the temporary directory yield fs.mkdir.bind(null, tmpdir); + // list of all the files + var files = []; + var file; + // yield each part as a stream var part; while (part = yield parts) { - // wait if there are too many file descriptors opened - yield* ch.drain(); - // save each part to a file, - // but do it in a different channel - // so we don't block this particular while loop. - saveTo(part, path.join(tmpdir, part.filename), ch.push()); + // filename for this part + files.push(file = path.join(tmpdir, part.filename)) + // save the file + yield saveTo(part, file); } // return all the filenames as an array // after all the files have finished downloading - this.body = yield* ch.flush(); + this.body = files; }) -if (!module.parent) app.listen(3000); \ No newline at end of file +if (!module.parent) app.listen(3000); + +function uid() { + return Math.random().toString(36).slice(2); +} \ No newline at end of file diff --git a/package.json b/package.json index c8e2a27..5470232 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,6 @@ "koa-session": "koajs/session", "co-busboy": "cojs/busboy", "save-to": "~1.0.0", - "archan": "~0.4.0", "raw-body": "~1.1.1" }, "devDependencies": {