New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

koa-parser

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-parser

a body parser for koa

latest
Source
npmnpm
Version
1.0.8
Version published
Maintainers
1
Created
Source

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

Keywords

koa

FAQs

Package last updated on 08 Aug 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts