koa-better-body

A koa body parser middleware with support for multipart, json, csp-report or urlencoded request bodies. Via formidable and co-body.

A koa body parser middleware with support for multipart, json, csp-report and urlencoded request bodies. Via formidable and co-body.

npm i --save koa-better-body
npm test

For more use-cases see the tests or examples folder.

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

.koaBetterBody

However, koa-better-body have few custom options, see also co-body, raw-body, formidable

  • [options] {Object}
    • patchNode {Boolean} Patch request body to Node's ctx.req object, default false
    • patchKoa {Boolean} Patch request body to Koa's ctx.request object, default true
    • jsonLimit {String|Number} The byte limit of the JSON body, default 1mb
    • formLimit {String|Number} The byte limit of the form body, default 56kb
    • encoding {String} Sets encoding for incoming form fields, default utf-8
    • encode {String} alias of opts.encoding
    • multipart {Boolean} Support multipart/form-data request bodies, default false
    • fieldsKey {String|Boolean} Name of the key for fields in the body object or false
    • filesKey {String|Boolean} Name of the key for files in the body object or false
    • extendTypes {Object} extending request types, see defaults
      • multipart {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
    • formidable {Object} Options that are passing to formidable
  • return {GeneratorFunction} That you can use with koa or co

See node-formidable for a full list of options

  • bytesExpected {Integer} The expected number of bytes in this form, default null
  • maxFields {Integer} Limits the number of fields that the querystring parser will decode, default 1000
  • maxFieldsSize {Integer} Limits the amount of memory a field can allocate in bytes, default 2mb
  • uploadDir {String} Sets the directory for placing file uploads in, default os.tmpDir()
  • hash {String} If you want checksums calculated for incoming files - 'sha1' or 'md5', default false
  • multiples {Boolean} Multiple file uploads or no, default false

Charlike Mike Reagent

License

Copyright (c) 2014-2015 Charlike Mike Reagent, contributors.
Released under the MIT license.


Powered and automated by kdf, February 6, 2015