https://github.com/trekjs/body-parser
Body parsing middleware
https://github.com/trekjs/body-parser
Last synced: 6 months ago
JSON representation
Body parsing middleware
- Host: GitHub
- URL: https://github.com/trekjs/body-parser
- Owner: trekjs
- License: mit
- Created: 2016-10-06T13:30:33.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-09-25T02:02:30.000Z (about 8 years ago)
- Last Synced: 2025-05-16T17:44:41.438Z (7 months ago)
- Language: JavaScript
- Homepage:
- Size: 182 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
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
[](https://travis-ci.org/trekjs/body-parser)
[](https://codecov.io/gh/trekjs/body-parser)

---
> [fundon.me](https://fundon.me) ·
> GitHub [@fundon](https://github.com/fundon) ·
> Twitter [@_fundon](https://twitter.com/_fundon)