A full-feature
koabody parser middleware. Supportmultipart,urlencodedandjsonrequest bodies. Provides same functionality as Express's bodyParser -multer. And all that is wrapped only aroundco-bodyandformidable.
Install with npm
$ npm install koa-body
- 15 tests
- can handle three type requests
- multipart/form-data
- application/x-www-urlencoded
- application/json
- option for patch to Koa or Node, or either
- file uploads
- body, fields and files limiting
- 2 dependencies only
Usage like multer
It's very simple, because you can access the fields and files in the
ctx.request.bodyorctx.req.bodyJSON object
var app = require('koa')(),
koaBody = require('koa-body');
app.use(koaBody({formidable:{uploadDir: __dirname}}));
app.use(function *(next) {
if (this.request.method == 'POST') {
console.log(this.request.body);
// => POST body
this.body = JSON.stringify(this.request.body);
}
yield next;
});
app.listen(3131)
console.log('curl -i http://localhost:3131/ -d "name=test"');For a more comprehensive example, see examples/multipart.js
Usage with koa-router
It's generally better to only parse the body as needed, if using a router that supports middleware composition, we can inject it only for certain routes.
var app = require('koa')(),
router = require('koa-router'),
koaBody = require('koa-body')();
app.use(router());
app.post('/users', koaBody,
function *(next) {
console.log(this.request.body);
// => POST body
this.body = JSON.stringify(this.request.body);
}
);
app.listen(3131)
console.log('curl -i http://localhost:3131/ -d "name=test"');Options available for
koa-body. Four custom options, and others are fromraw-bodyandformidable.
patchNode{Boolean} Patch request body to Node'sctx.req, defaultfalsepatchKoa{Boolean} Patch request body to Koa'sctx.request, defaulttruejsonLimit{String|Integer} The byte limit of the JSON body, default1mbformLimit{String|Integer} The byte limit of the form body, default56kbencoding{String} Sets encoding for incoming form fields, defaultutf-8multipart{Boolean} Parse multipart bodies, defaultfalseformidable{Object} Options to pass to the formidable multipart parser
See node-formidable for a full list of options
bytesExpected{Integer} The expected number of bytes in this form, defaultnullmaxFields{Integer} Limits the number of fields that the querystring parser will decode, default10maxFieldsSize{Integer} Limits the amount of memory a field (not file) can allocate in bytes, default2mbuploadDir{String} Sets the directory for placing file uploads in, defaultos.tmpDir()keepExtensions{Boolean} Files written touploadDirwill include the extensions of the original files, defaulttruehash{String} If you want checksums calculated for incoming files, set this to either'sha1'or'md5', defaultfalsemultiples{Boolean} Multiple file uploads or no, defaulttrue
Note: You can patch request body to Node or Koa in same time if you want.
As usual -
npm testor if you have [mocha][mocha-url] globally -mocha --harmony-generators.
$ npm test
The MIT License, 2014 Charlike Mike Reagent (@tunnckoCore) and Daryl Lau (@daryllau)
