Skip to content

Commit

Permalink
add options for which body parameter to modify
Browse files Browse the repository at this point in the history
  • Loading branch information
Daryl Lau committed Jan 31, 2014
1 parent 5579217 commit 020f3dd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ $ npm install koa-body

## Options

TODO

- patchNode
Set the body parameter in the node request object `this.req.body`
*defauls to false*
- patchKoa
Set the body parameter in the koa request object `this.request.body`
*defaults to true*

## Example

```js
Expand Down
18 changes: 16 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,26 @@ var co_body = require('co-body');


module.exports = function(opts){
var patchNode = (opts && opts.patchNode) || false;
var patchKoa = (opts && opts.patchKoa) || true;
return function *(next){
if(this.is('application/json')){
this.request.body = yield co_body.json(this);
var body = yield co_body.json(this);
if(patchNode){
this.req.body = body;
}
if(patchKoa){
this.request.body = body;
}
}
else if(this.is('application/x-www-form-urlencoded')){
this.request.body = yield co_body.form(this);
var body = yield co_body.form(this);
if(patchNode){
this.req.body = body;
}
if(patchKoa){
this.request.body = body;
}
}
yield next;
};
Expand Down

0 comments on commit 020f3dd

Please sign in to comment.