1.0.8 • Published 5 years ago

koa-parser v1.0.8

Weekly downloads
105
License
MIT
Repository
github
Last release
5 years ago

koa-parserBuild Status

a body parser for koa. support json, form(urlencoded), multipart and text type body.

Install

NPM

Usage

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}`)

Screenshot

default options

Options

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']

Licences

MIT

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

0.4.2

6 years ago

0.4.1

6 years ago

0.4.0

6 years ago

0.3.0

6 years ago

0.2.26

6 years ago

0.2.25

6 years ago

0.2.24

6 years ago

0.2.23

6 years ago

0.2.22

6 years ago

0.2.21

6 years ago

0.2.20

6 years ago

0.2.19

6 years ago

0.2.18

6 years ago

0.2.17

6 years ago

0.2.16

6 years ago

0.2.15

6 years ago

0.2.14

6 years ago

0.2.13

6 years ago

0.2.12

6 years ago

0.2.11

6 years ago

0.2.10

6 years ago

0.2.9

6 years ago

0.2.8

6 years ago

0.2.7

6 years ago

0.2.6

6 years ago

0.2.5

6 years ago

0.2.4

6 years ago

0.2.3

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.0

6 years ago

0.0.2

7 years ago

0.0.1

7 years ago

1.0.0

7 years ago