koa-short-body
v1.1.0
Published
More short API for koa-better-body.
Downloads
32
Maintainers
Readme
More short API for koa-better-body.
A koa body parser middleware with support for
multipart
,json
,csp-report
andurlencoded
request bodies. Viaformidable
andco-better-body
. It's fork ofkoa-better-body
Install
npm i --save koa-short-body
npm test
Usage
examples/multer
- usage like Express's bodyParser - multernpm run examples-multer
examples/koa-router
- usage with Alex's koa-routernpm run examples-koa-router
Extending default types
Every new defined type, will be appended to defaults, see defaults
app.use(koaBody({
extendTypes: {
// will parse application/x-javascript type body as a JSON string
json: ['application/x-javascript'],
multipart: ['multipart/mixed']
}
}));
will expects request bodies
application/json
application/json-patch+json
application/vnd.api+json
application/csp-report
application/x-javascript
application/x-www-form-urlencoded
multipart/form-data
multipart/mixed
get field or file via this
app.use(function * (next) {
// Get specify fields
console.log(this.field("field_name"));
// Get received all fields.
console.log(this.field());
// Get specify fields file (it's must be returns Array<File>)
console.log(this.file("file"));
// Get all received files
console.log(this.file());
})
.koaBetterBody
However,
koa-short-body
have few custom options, see also co-better-body, raw-body and formidable
[options]
{Object}jsonLimit
{String|Number} The byte limit of the JSON body, default1mb
formLimit
{String|Number} The byte limit of the form body, default56kb
encoding
{String} Sets encoding for incoming form fields, defaultutf-8
encode
{String} alias ofopts.encoding
multipart
{Boolean} Supportmultipart/form-data
request bodies, defaultfalse
extendTypes
{Object} extending request types, see defaultsmultipart
{Array} array with multipart types, default['multipart/form-data']
json
{Array} array with json types, default['application/x-www-form-urlencoded']
form
{Array} array with form types
qs
{Object} Options that are passing to qsformidable
{Object} Options that are passing toformidable
formidable.maxFields
{Number} See formidable-options. our default10
formidable.multiples
{Boolean} See formidable-options, our defaulttrue
formidable.keepExtensions
{Boolean} See formidable-options, our defaulttrue
return
{GeneratorFunction} That you can use with koa or co
formidable options
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 a field can allocate in bytes, default2mb
uploadDir
{String} Sets the directory for placing file uploads in, defaultos.tmpDir()
hash
{String} If you want checksums calculated for incoming files -'sha1'
or'md5'
, defaultfalse
multiples
{Boolean} Multiple file uploads or no, defaultfalse
qs options
See qs for a full list of options
depth
{Integer} The parsed object maximum hierarchy depth, default5
delimiter
{String|Regex} The query string delimeter, default&
arrayLimit
{Integer} Maximum array size or -1 to disable arrays, default20
Author
Charlike Mike Reagent
koa-short-body
maked by twitter/_ragg_
License
Copyright (c) 2014-2015 Charlike Mike Reagent, contributors.
Released under the MIT
license.
Powered and automated by kdf, February 6, 2015