Permalink
Browse files

simplify multipart example, add some comments

  • Loading branch information...
1 parent b1e3d56 commit 67d9aedce60068f6d91325fd2e91bd00df78542f @jonathanong jonathanong committed Dec 6, 2013
Showing with 39 additions and 16 deletions.
  1. +2 −0 .jshintignore
  2. +10 −0 body-parsing/app.js
  3. +5 −0 flash-messages/app.js
  4. +22 −15 multipart/app.js
  5. +0 −1 package.json
View
@@ -0,0 +1,2 @@
+node_modules
+*/test.js
View
@@ -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');
@@ -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');
View
@@ -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);
+if (!module.parent) app.listen(3000);
+
+function uid() {
+ return Math.random().toString(36).slice(2);
+}
View
@@ -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": {

0 comments on commit 67d9aed

Please sign in to comment.