# koa-body-parsers

> collection of koa body parsers

Latest version **5.0.0** (published 2024-08-31) · MIT license · 0 weekly downloads

## Install

```sh
npm install koa-body-parsers
pnpm add koa-body-parsers
yarn add koa-body-parsers
bun add koa-body-parsers
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: no vulnerabilities; high maintenance score.

Warnings: low downloads; no types; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 5.0.0 |
| Published | 2024-08-31 |
| First published | 2014-06-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 6.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 41 |
| Author | Jonathan Ong |
| Maintainers | coderhaoxin, jongleberry |
| Keywords | koa, body, parser, parsers |

## Links

- npm: https://www.npmjs.com/package/koa-body-parsers
- Repository: https://github.com/koajs/body-parsers
- Homepage: https://github.com/koajs/body-parsers#readme
- Issues: https://github.com/koajs/body-parsers/issues
- npm.io page: https://npm.io/package/koa-body-parsers

## Dependencies (1)

- [raw-body](https://npm.io/package/raw-body.md) ^3.0.0

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 5.0.0 (latest) — 2024-08-31
- 4.0.0 — 2024-02-25
- 3.1.0 — 2017-02-09
- 3.0.0 — 2016-02-27
- 2.0.0 — 2016-01-17
- 1.1.0 — 2015-06-11
- 1.0.0 — 2014-06-13

## README

# Koa Body Parsers

[![NPM version][npm-image]][npm-url]

A more functional version of body parsing.
Use this module if you want to "lazily" parse the body.
Other middleware automatically parse the body in the middleware chain, which might not be ideal as business logic like authentication, authorization, and routing are not done prior to body parsing.

Includes a `json` and `urlencoded` parsers.

## API

Initialization:

```js
import koaBodyParsers from 'koa-body-parsers'
import Koa from 'koa'

const app = new Koa()
koaBodyParsers(app)

// example usage
app.use(async (ctx) => {
  const currentUser = UserService.getCurrentUser(ctx)
  ctx.assert(currentUser, 401)

  ctx.assert(ctx.request.is('json'), 415)
  const body = await ctx.request.json('100kb')
  ctx.body = body
})
```

Because this module is a plugin for the `context`, the API signature is different.

### Expect: 100-continue and ctx.response.writeContinue()

`Expect: 100-continue` is automatically supported as long as you use `app.listen()`.
Otherwise, create your server like this:

```js
const fn = app.callback();
const server = http.createServer(); // or whatever server you use
server.on('request', fn); // regular requests
server.on('checkContinue', function (req, res) {
  // tag requests with `Expect: 100-continue`
  req.checkContinue = true;
  fn(req, res);
});
```

If `Expect: 100-continue` was sent to the client,
this will automatically response with a "100-continue".
Use this right before parsing the body.
Automatically called by all following body parsers,
but you would still have to call it if you're doing something like:

```js
app.use(async (ctx) => {
  if (ctx.request.is('image/*')) {
    ctx.response.writeContinue();
    const buffer = await ctx.request.buffer()
  }
})
```

### const body = await ctx.request.json([limit])

Get the JSON body of the request, if any.
`limit` defaults to `100kb`.

### const body = await ctx.request.urlencoded([limit])

Get the traditional form body of the request, if any,
`limit` defaults to `100kb`.

### const text = await ctx.request.text([limit])

Get the body of the request as a single `text` string.
`limit` defaults to `100kb`.
You could use this to create your own request body parser of some sort.

### const buffer = await ctx.request.buffer([limit])

Get the body of the request as a single `Buffer` instance.
`limit` defaults to `1mb`.

[npm-image]: https://img.shields.io/npm/v/koa-body-parsers.svg?style=flat-square
[npm-url]: https://npmjs.org/package/koa-body-parsers

---
_Source: https://npm.io/package/koa-body-parsers · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
