# trek-body-parser

> Body parsing middleware

Latest version **0.2.3** (published 2017-09-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install trek-body-parser
pnpm add trek-body-parser
yarn add trek-body-parser
bun add trek-body-parser
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.3 |
| Published | 2017-09-25 |
| First published | 2016-10-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 8.0.0 |
| Dependencies | 5 |
| Known vulnerabilities | 0 (+15 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Fangdun Cai |
| Maintainers | fundon |
| Keywords | trek, engine, middleware, body-parser, json, urlencoded, multipart, body |

## Links

- npm: https://www.npmjs.com/package/trek-body-parser
- Repository: https://github.com/trekjs/body-parser
- Homepage: https://github.com/trekjs/body-parser#readme
- Issues: https://github.com/trekjs/body-parser/issues
- npm.io page: https://npm.io/package/trek-body-parser

## Dependencies (5)

- [qs](https://npm.io/package/qs.md) 6.5.1
- [busboy](https://npm.io/package/busboy.md) 0.2.14
- [multer](https://npm.io/package/multer.md) 2.0.0-alpha.6
- [raw-body](https://npm.io/package/raw-body.md) 2.3.2
- [inflation](https://npm.io/package/inflation.md) 2.0.0

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 0.2.3 (latest) — 2017-09-25
- 0.2.2 — 2017-09-08
- 0.2.1 — 2017-09-08
- 0.2.0 — 2017-09-03
- 0.1.6 — 2017-09-02
- 0.1.5 — 2017-09-01
- 0.1.4 — 2017-02-04
- 0.1.3 — 2016-10-17
- 0.1.2 — 2016-10-10
- 0.1.1 — 2016-10-08
- 0.1.0 — 2016-10-08
- 0.0.0 — 2016-10-07

## README

# body-parser

Body parsing middleware, supports `json` `text` `raw` `urlencoded` `multipart`.

Based on [raw-body](https://github.com/stream-utils/raw-body), [busboy](https://github.com/mscdex/busboy), [multer](https://github.com/expressjs/multer/tree/explore-new-api), [qs](https://github.com/ljharb/qs).


## Installation

```
$ npm install trek-body-parser --save
```


## Examples

```js
'use strict'

const Engine = require('trek-engine')
const bodyParser = require('..')

async function start () {
  const app = new Engine()

  app.use(bodyParser())

  app.use(({req, res}) => {
    res.body = req.body
  })

  app.on('error', (err, ctx) => {
    console.log(err)
  })

  app.run(3000)
}

start().catch(err => console.log(err))
```


## API

* `bodyParser(options)`

  `json` `urlencoded` is enabled by default.

  ```js
  {
    json: true,
    urlencoded: true,
    skip: false,
    // custom
    text: {
      ...
    },
    ...
  }
  ```

* `.json(options)`

  ```js
  {
    detect: false,
    encoding: 'utf8',
    limit: '1mb',
    parse: JSON.parse,
    reviver: undefined,
    strict: true,
    type: [
      'application/json',
      'application/json-patch+json',
      'application/vnd.api+json',
      'application/csp-report'
    ],
    transform(raw, { parse, reviver, strict }) {
      const empty = raw.length === 0
      if (strict) {
        if (empty) return {}
        if (!STRICT_JSON_REG.test(raw)) {
          const err = new Error('Invalid JSON, only supports object and array')
          err.status = 400
          throw err
        }
      }
      if (empty) return raw
      return parse(raw, reviver)
    }
  }
  ```

* `.text(options)`

  ```js
  {
    detect: false,
    encoding: 'utf8',
    limit: '1mb',
    type: 'text/plain'
  }
  ```

* `.raw(options)`

  ```js
  {
    detect: false,
    encoding: null,
    limit: '1mb',
    type: 'application/octet-stream'
  }
  ```

* `.urlencoded(options)`

  ```js
  {
    detect: false,
    encoding: 'utf8',
    limit: '56k',
    parse: qs.parse,
    type: 'application/x-www-form-urlencoded',
    transform(raw, { parse }) {
      return parse(raw)
    }
  }
  ```

* `.multipart(options)`

    Creates [multer](https://github.com/expressjs/multer/tree/explore-new-api) instance.

    - `.any()`

    - `.array(fieldname[, maxCount])`

    - `.fields(fields)`

      `fields`:
      ```js
      [
        { name: 'avatar', maxCount: 1 },
        { name: 'gallery', maxCount: 8 }
      ]
      ```

    - `.none()`

    - `.single(fieldname)`

* `.busboy`


## Badges

[![Build Status](https://travis-ci.org/trekjs/body-parser.svg?branch=master)](https://travis-ci.org/trekjs/body-parser)
[![codecov](https://codecov.io/gh/trekjs/body-parser/branch/master/graph/badge.svg)](https://codecov.io/gh/trekjs/body-parser)
![](https://img.shields.io/badge/license-MIT-blue.svg)

---

> [fundon.me](https://fundon.me) &nbsp;&middot;&nbsp;
> GitHub [@fundon](https://github.com/fundon) &nbsp;&middot;&nbsp;
> Twitter [@_fundon](https://twitter.com/_fundon)

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