toa-body
v3.0.0
Published
Request body parser for toa.
Downloads
8
Readme
toa-body
Request body parser for toa.
toa
Demo
const Toa = require('toa')
const toaBody = require('toa-body')
const app = new Toa()
toaBody(app)
app.use(function * () {
this.body = yield this.parseBody()
})
app.listen(3000)
const Toa = require('toa')
const toaBody = require('toa-body')
const app = new Toa()
app.use(toaBody()) // It will try to parse body for every request.
app.use(function () {
this.body = this.request.body
})
app.listen(3000)
Installation
npm install toa-body
API
const toaBody = require('toa-body')
toaBody(app[, options])
It will add parseBody
method to context
.
options.encoding
: requested encoding. Default isutf8
.options.formLimit
: limit of theurlencoded
body. If the body ends up being larger than this limit, a 413 error code is returned. Default is56kb
options.jsonLimit
: limit of thejson
body. Default is1mb
options.extendTypes
: support extend types:
toaBody(app, {
extendTypes: {
json: ['application/x-javascript'] // will parse application/x-javascript type body as a JSON string
}
}))
options.parse
: support custom parse:
const parseXml = require('xml2js').parseString
toaBody(app, {
parse: function (buf) {
var str = buf.toString('utf8')
if (!this.is('text/xml')) return str
// return promise or thunk function for async task
return function (done) { parseXml(str, done) }
}
}))
context.parseBody()
return thunk function.
this.body = yield this.parseBody()
this.parseBody()(function (err, body) {
// this.request.body === body
this.body = body
})
app.use(toaBody([options]))
Toa >= v2.x required.
Licences
(The MIT License)