Skip to content

Commit

Permalink
more powerful onParseEnd event handler
Browse files Browse the repository at this point in the history
  • Loading branch information
hacksparrow committed Jul 13, 2014
1 parent bcea12f commit e737aee
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ The following are the options that can be passed to Multer.
* `onFileUploadData(file, data)`
* `onFileUploadComplete(file)`
* `onParseStart()`
* `onParseEnd()`
* `onParseEnd(req, next)`
* `onError()`
* `onFilesLimit()`
* `onFieldsLimit()`
Expand Down Expand Up @@ -157,19 +157,24 @@ onParseStart: function () {
}
```

### onParseEnd(req)
### onParseEnd(req, next)

Event handler triggered when the form parsing completes. The request is available to the function.
Event handler triggered when the form parsing completes. The `request` object and the `next` objects are are passed to the function.

```js
onParseEnd: function (req) {
console.log('Form parsing completed at: ', new Date())
onParseEnd: function (req, next) {
console.log('Form parsing completed at: ', new Date());

// usage example: custom body parse
req.body = require('qs').parse(req.body)
req.body = require('qs').parse(req.body);

// call the next middleware
next();
}
```

**Note**: If you have created a `onParseEnd` event listener, you must manually call the `next()` function, else the request will be left hanging.

### onError()

Event handler for any errors encountering while processing the form. The `error` object and the `next` object is available to the function. If you are handling errors yourself, make sure to terminate the request or call the `next()` function, else the request will be left hanging.
Expand Down
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ module.exports = function(options) {
req.files[fieldname].push(file);
// trigger "file end" event
if (options.onFileUploadComplete) { options.onFileUploadComplete(file); }

// defines has completed processing one more file
fileCount--;
onFinish();
Expand Down Expand Up @@ -166,8 +166,8 @@ module.exports = function(options) {
req.body = qs.parse(req.body);

// when done parsing the form, pass the control to the next middleware in stack
if (options.onParseEnd) { options.onParseEnd(req); }
next();
if (options.onParseEnd) { options.onParseEnd(req, next); }
else { next(); }
};

req.pipe(busboy);
Expand Down

0 comments on commit e737aee

Please sign in to comment.