Package Exports
- koa-body
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (koa-body) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
koa-body

A full-featured
koa
body parser middleware. Supportmultipart
,urlencoded
andjson
request bodies. Provides same functionality as Express's bodyParser -multer
. And all that is wrapped only aroundco-body
andformidable
.
Install
Install with npm
$ npm install koa-body
Legacy Koa1 support
$ npm install koa-body@1
Features
- 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
Usage like multer
It's very simple, because you can access the fields and files in the
ctx.request.body
orctx.req.body
JSON object
var app = require('koa')(),
koaBody = require('koa-body');
app.use(koaBody({formidable:{uploadDir: __dirname}}));
app.use((ctx, next) => {
if (ctx.request.method == 'POST') {
console.log(ctx.request.body);
// => POST body
ctx.body = JSON.stringify(ctx.request.body);
}
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')();
router.post('/users', koaBody,
(ctx, next) => {
console.log(ctx.request.body);
// => POST body
ctx.body = JSON.stringify(ctx.request.body);
}
);
app.use(router.routes());
app.listen(3131);
console.log('curl -i http://localhost:3131/users -d "name=test"');
Options
Options available for
koa-body
. Four custom options, and others are fromraw-body
andformidable
.
patchNode
{Boolean} Patch request body to Node'sctx.req
, defaultfalse
patchKoa
{Boolean} Patch request body to Koa'sctx.request
, defaulttrue
jsonLimit
{String|Integer} The byte (if integer) limit of the JSON body, default1mb
formLimit
{String|Integer} The byte (if integer) limit of the form body, default56kb
textLimit
{String|Integer} The byte (if integer) limit of the text body, default56kb
encoding
{String} Sets encoding for incoming form fields, defaultutf-8
multipart
{Boolean} Parse multipart bodies, defaultfalse
urlencoded
{Boolean} Parse urlencoded bodies, defaulttrue
text
{Boolean} Parse text bodies, defaulttrue
json
{Boolean} Parse json bodies, defaulttrue
formidable
{Object} Options to pass to the formidable multipart parseronError
{Function} Custom error handle, if throw an error, you can customize the response - onError(error, context), default will throwstrict
{Boolean} If enabled, don't parse GET, HEAD, DELETE requests, defaulttrue
A note about strict mode
see http://tools.ietf.org/html/draft-ietf-httpbis-p2-semantics-19#section-6.3
- GET, HEAD, and DELETE requests have no defined semantics for the request body, but this doesn't mean they may not be valid in certain use cases.
- koa-body is strict by default
Some options for formidable
See node-formidable for a full list of options
bytesExpected
{Integer} The expected number of bytes in this form, defaultnull
maxFields
{Integer} Limits the number of fields that the querystring parser will decode, default1000
maxFieldsSize
{Integer} Limits the amount of memory all fields together (except files) can allocate in bytes. If this value is exceeded, an 'error' event is emitted, default2mb (2 * 2 * 1024)
uploadDir
{String} Sets the directory for placing file uploads in, defaultos.tmpDir()
keepExtensions
{Boolean} Files written touploadDir
will include the extensions of the original files, defaultfalse
hash
{String} If you want checksums calculated for incoming files, set this to either'sha1'
or'md5'
, defaultfalse
multiples
{Boolean} Multiple file uploads or no, defaulttrue
onFileBegin
{Function} Special callback on file begin. The function is executed directly by formidable. It can be used to rename files before saving them to disk. See the docs
Note: You can patch request body to Node or Koa in same time if you want.
Tests
$ npm test
License
The MIT License, 2014 Charlike Mike Reagent (@tunnckoCore) and Daryl Lau (@daryllau)