2017-08-18 17:16:14 +00:00
|
|
|
# Koa Body Parser
|
2017-08-24 16:35:34 +00:00
|
|
|
[![npm](https://img.shields.io/npm/v/cca-koa-parser.svg)](https://www.npmjs.com/package/cca-koa-parser)
|
|
|
|
[![npm](https://img.shields.io/npm/dt/cca-koa-parser.svg)]()
|
|
|
|
[![npm](https://img.shields.io/npm/l/cca-koa-parser.svg)]()
|
|
|
|
|
2017-08-18 17:16:14 +00:00
|
|
|
Koa-Middleware for parsing body parameters in the request. Lightweight and no dependencies.
|
2017-08-24 16:35:34 +00:00
|
|
|
|
|
|
|
### Install
|
|
|
|
```bash
|
|
|
|
npm install cca-koa-parser --save
|
|
|
|
```
|
|
|
|
|
2017-08-18 17:16:14 +00:00
|
|
|
##### Supported encodings:
|
|
|
|
- form-data
|
|
|
|
- x-www-form-urlencoded
|
|
|
|
- json
|
|
|
|
|
|
|
|
### Complete Example
|
|
|
|
```javascript
|
|
|
|
const
|
|
|
|
Koa = require('koa'),
|
2017-08-20 22:23:57 +00:00
|
|
|
parser = require('cca-koa-parser'),
|
|
|
|
router = require('cca-koa-router')
|
2017-08-18 17:16:14 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
app = new Koa(),
|
2017-08-20 22:23:57 +00:00
|
|
|
port = 3000
|
2017-08-18 17:16:14 +00:00
|
|
|
|
|
|
|
app.use(parser)
|
|
|
|
|
2017-08-20 22:23:57 +00:00
|
|
|
app.use(router(_ => {
|
2017-08-18 17:16:14 +00:00
|
|
|
_.post('/', (c, n) => {
|
2017-08-20 22:23:57 +00:00
|
|
|
c.body = c.request.body
|
2017-08-18 17:16:14 +00:00
|
|
|
})
|
|
|
|
}))
|
|
|
|
|
|
|
|
app.listen(port)
|
|
|
|
```
|
|
|
|
|
|
|
|
## Documentation
|
|
|
|
|
|
|
|
##### ~ `body` Element
|
|
|
|
The Parser defines an object `ctx.request.body` where all the sent parameters are stored
|
|
|
|
|
|
|
|
###### Example
|
|
|
|
```javascript
|
|
|
|
// Request: HTTP POST /
|
|
|
|
ctx.request.body['username'] => "joe"
|
|
|
|
```
|