Permalink
Browse files

Added `defer` property to multipart() middleware

The newly added `defer` property allows one to defer mulitpart parsing and
immediately execute the next middleware. In this case, Formidable's form
object is exposed via `req.form`, allowing middleware to bind to "progress",
"error", and/or "end" events.
  • Loading branch information...
1 parent 8760300 commit 762654959d7d3295e9077e8e6726a775499fa846 @bminer bminer committed Oct 2, 2012
Showing with 17 additions and 7 deletions.
  1. +17 −7 lib/middleware/multipart.js
@@ -40,6 +40,9 @@ function noop(req, res, next) {
* Options:
*
* - `limit` byte limit defaulting to none
+ * - `defer` defers processing and exposes the Formidable form object as `req.form`.
+ * `next()` is called without waiting for the form's "end" event.
+ * This option is useful if you need to bind to the "progress" event, for example.
*
* @param {Object} options
* @return {Function}
@@ -98,24 +101,31 @@ exports = module.exports = function(options){
ondata(name, val, files);
});
- form.on('error', function(err){
- err.status = 400;
- next(err);
- done = true;
- });
+ if(!options.defer) {
+ form.on('error', function(err){
+ err.status = 400;
+ next(err);
+ done = true;
+ });
+ }
form.on('end', function(){
if (done) return;
try {
req.body = qs.parse(data);
req.files = qs.parse(files);
- next();
+ if(!options.defer) next();
} catch (err) {
- next(err);
+ form.emit('error', err);
}
});
form.parse(req);
+
+ if(options.defer) {
+ req.form = form;
+ next();
+ }
});
}
};

0 comments on commit 7626549

Please sign in to comment.