a body parser for koa. support json, form(urlencoded), multipart and text type body.
const Koa = require('koa')
const parser = require('koa-parser')
const port = 3000
const app = new Koa()
app.use(parser())
app.use(async (ctx, next) => {
// if nothing was parsed, body will be undefined
if (ctx.request.body !== undefined) {
ctx.body = ctx.request.body
}
await next()
})
app.listen(port)
console.error(`listening on port ${port}`)
app.use(parser({
encoding?: string, // requested encoding
error?: (err: any, ctx: Koa.Context) => any, // support custom parser error handle
json?: string | string[], // support json parser types
multipart?: string | string[], // support multipart(form-data) parser types
text?: string | string[], // support text parser types
urlencoded?: string | string[] // support urlencoded(form) parser types
}))
-
encoding: requested encoding. Default is
utf-8
-
error: support custom error handle, Default is
false
. if koa-bodyparser throw an error, you can customize the response like:app.use(parser({ error (err, ctx) { console.log(err) ctx.throw(err, 422) } }))
-
json: Extended support for json parsing options, The default supported types are
['application/json', 'application/json-patch+json', 'application/vnd.api+json', 'application/csp-report']
. you can customize the type like:// json will be support default types and application/x-javascript app.use(parser({ json: 'application/x-javascript' // You can also pass parameters in an array }))
-
multipart: Extended support for multipart(form-data) parsing options, The default supported types are
['multipart/form-data']
-
text: Extended support for text parsing options, The default supported types are
['text/plain']
-
urlencoded: Extended support for urlencoded(form) parsing options, The default supported types are
['application/x-www-form-urlencoded']