Skip to content

Commit 67d9aed

Browse files
committed
simplify multipart example, add some comments
1 parent b1e3d56 commit 67d9aed

File tree

5 files changed

+39
-16
lines changed

5 files changed

+39
-16
lines changed

.jshintignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
*/test.js

body-parsing/app.js

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
/**
2+
* Example for handling JSON and urlencoded request bodies.
3+
* All the throws at the beginning of the middleware are all app-dependent and may not be suited for your use-case.
4+
* For the actual body parsing (yield rawBody and down),
5+
* you most may be interested in modules that already do this for you:
6+
*
7+
* - [body](https://github.com/raynos/body)
8+
* - [co-body](https://github.com/visionmedia/co-body)
9+
*/
10+
111
var koa = require('koa');
212
var qs = require('querystring');
313
var rawBody = require('raw-body');

flash-messages/app.js

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* A very simple flash example.
3+
* Only uses JSON for simplicity.
4+
*/
5+
16
var koa = require('koa');
27
var rawBody = require('raw-body');
38
var session = require('koa-session');

multipart/app.js

+22-15
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,51 @@
1+
/**
2+
* Multipart example downloading all the files to disk using co-busboy.
3+
* If all you want is to download the files to a temporary folder,
4+
* just use https://github.com/cojs/multipart instead of copying this code
5+
* as it handles file descriptor limits whereas this does not.
6+
*/
7+
18
var os = require('os');
29
var path = require('path');
310
var fs = require('fs');
411
var koa = require('koa');
512
var parse = require('co-busboy');
613
var saveTo = require('save-to');
7-
var archan = require('archan');
814

915
var app = module.exports = koa();
1016

1117
app.use(function *(){
12-
// create a go-like channel for control flow
13-
var ch = archan({
14-
concurrency: 1 // save only 1 file at a time
15-
});
16-
1718
// parse the multipart body
1819
var parts = parse(this, {
1920
autoFields: true // saves the fields to parts.field(s)
2021
});
2122

2223
// create a temporary folder to store files
23-
var tmpdir = path.join(os.tmpdir(), Math.random().toString(36).slice(2));
24+
var tmpdir = path.join(os.tmpdir(), uid());
2425

2526
// make the temporary directory
2627
yield fs.mkdir.bind(null, tmpdir);
2728

29+
// list of all the files
30+
var files = [];
31+
var file;
32+
2833
// yield each part as a stream
2934
var part;
3035
while (part = yield parts) {
31-
// wait if there are too many file descriptors opened
32-
yield* ch.drain();
33-
// save each part to a file,
34-
// but do it in a different channel
35-
// so we don't block this particular while loop.
36-
saveTo(part, path.join(tmpdir, part.filename), ch.push());
36+
// filename for this part
37+
files.push(file = path.join(tmpdir, part.filename))
38+
// save the file
39+
yield saveTo(part, file);
3740
}
3841

3942
// return all the filenames as an array
4043
// after all the files have finished downloading
41-
this.body = yield* ch.flush();
44+
this.body = files;
4245
})
4346

44-
if (!module.parent) app.listen(3000);
47+
if (!module.parent) app.listen(3000);
48+
49+
function uid() {
50+
return Math.random().toString(36).slice(2);
51+
}

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
"koa-session": "koajs/session",
1010
"co-busboy": "cojs/busboy",
1111
"save-to": "~1.0.0",
12-
"archan": "~0.4.0",
1312
"raw-body": "~1.1.1"
1413
},
1514
"devDependencies": {

0 commit comments

Comments
 (0)