Skip to content

Instantly share code, notes, and snippets.

@secretfader
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save secretfader/54a0d749ef98d9a38607 to your computer and use it in GitHub Desktop.
Save secretfader/54a0d749ef98d9a38607 to your computer and use it in GitHub Desktop.
koa multipart
router.post('/', multipart, function *() {
if (this.state.files && this.state.files.media) {
this.state.files.media.pipe(fs.createWriteStream(/* output */));
// stream error handling omitted
}
});
module.exports = function *(next) {
var parts = parse(this, { autoFields: true })
, part;
this.state.files = {};
while (part = yield parts) {
this.state.files[part.fieldname] = part;
part.resume();
}
this.body = this.body || parts.field || {};
yield next;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment