koa-get-body
v1.1.2
Published
A body parser for koa2, support `urlencoded`, `multipart`, `json`, `xml` and `text`
Downloads
31
Maintainers
Readme
koa-get-body
A body parser for koa2, support urlencoded
, multipart
, json
,text
and xml
(need option xmlParser
configured).
(return raw body for non-supported mime types)
Installation
npm install koa-get-body
Usage
const Koa = require('koa')
const getBody = require('koa-get-body')
let app = new Koa()
app.use(getBody())
app.use(async ctx => {
const body = await ctx.request.getBody()
// or get a single field
const name = await ctx.request.getBody('name')
})
Custom Options
app.use(getBody({
alias: 'getPost', // alias func name on `ctx',
uploadDir: '/path/to/your/dir/',
xmlParser: utils.paserXML, // see https://github.com/yangg/koa-get-body/blob/master/spec/utils.js
getFileName: (fieldName) => {
return shortid.generate()
},
limits: {
fileSize: Infinity,
// ...
}
}))
app.use(async ctx => {
const body = await ctx.getPost() // get body via alias `getPost'
})
Options
- alias - {string}, append an alias to koa
ctx
, then you can usectx.aliasName()
- jsonTypes - {array|string}, types to detect as
json
, see type-is, default:['json', '+json']
, the following mime types will matched:application/json
,application/vnd.api+json
,application/json-patch+json
- textTypes - {array|string}, types to detect as
text
, default:text/*
- xmlTypes - {array|string}, types to detect as
xml
, default:xml
- xmlParser - {function}, a function to parse xml if you needed
- uploadDir - {string}, custom upload dir, default to
os.tmpdir()
- getFileName - {function}, generate upload temp filename in you own way
- more options: see Busboy options
TODO
- [ ] add support for array or object fieldNames (
[]
or[name]
)
License
MIT