UNPKG

2.94 kBMarkdownView Raw
1
2# co-body
3
4[![NPM version][npm-image]][npm-url]
5[![build status][travis-image]][travis-url]
6[![Test coverage][coveralls-image]][coveralls-url]
7[![David deps][david-image]][david-url]
8[![npm download][download-image]][download-url]
9
10[npm-image]: https://img.shields.io/npm/v/co-body.svg?style=flat-square
11[npm-url]: https://npmjs.org/package/co-body
12[travis-image]: https://img.shields.io/travis/cojs/co-body.svg?style=flat-square
13[travis-url]: https://travis-ci.org/cojs/co-body
14[coveralls-image]: https://img.shields.io/coveralls/cojs/co-body.svg?style=flat-square
15[coveralls-url]: https://coveralls.io/r/cojs/co-body?branch=master
16[david-image]: https://img.shields.io/david/cojs/co-body.svg?style=flat-square
17[david-url]: https://david-dm.org/cojs/co-body
18[download-image]: https://img.shields.io/npm/dm/co-body.svg?style=flat-square
19[download-url]: https://npmjs.org/package/co-body
20
21 Parse request bodies with generators inspired by [Raynos/body](https://github.com/Raynos/body).
22
23## Installation
24
25```bash
26$ npm install co-body
27```
28
29## Options
30
31 - `limit` number or string representing the request size limit (1mb for json and 56kb for form-urlencoded)
32 - `strict` when set to `true`, JSON parser will only accept arrays and objects; when `false` will accept anything `JSON.parse` accepts. Defaults to `true`. (also `strict` mode will always return object).
33 - `queryString` an object of options when parsing query strings and form data. See [qs](https://github.com/hapijs/qs) for more information.
34 - `jsonTypes` is used to determine what media type **co-body** will parse as **json**, this option is passed directly to the [type-is](https://github.com/jshttp/type-is) library.
35 - `formTypes` is used to determine what media type **co-body** will parse as **form**, this option is passed directly to the [type-is](https://github.com/jshttp/type-is) library.
36 - `textTypes` is used to determine what media type **co-body** will parse as **text**, this option is passed directly to the [type-is](https://github.com/jshttp/type-is) library.
37
38more options available via [raw-body](https://github.com/stream-utils/raw-body#getrawbodystream-options-callback):
39
40## Example
41
42```js
43// application/json
44var body = yield parse.json(req);
45
46// explicit limit
47var body = yield parse.json(req, { limit: '10kb' });
48
49// application/x-www-form-urlencoded
50var body = yield parse.form(req);
51
52// text/plain
53var body = yield parse.text(req);
54
55// either
56var body = yield parse(req);
57
58// custom type
59var body = yield parse(req, { textTypes: ['text', 'html'] });
60```
61
62## Koa
63
64 This lib also supports `ctx.req` in Koa (or other libraries),
65 so that you may simply use `this` instead of `this.req`.
66
67```js
68// application/json
69var body = yield parse.json(this);
70
71// application/x-www-form-urlencoded
72var body = yield parse.form(this);
73
74// text/plain
75var body = yield parse.text(this);
76
77// either
78var body = yield parse(this);
79```
80
81# License
82
83 MIT