koa-parser/README.md

40 lines
704 B
Markdown
Raw Normal View History

2017-08-18 17:16:14 +00:00
# Koa Body Parser
Koa-Middleware for parsing body parameters in the request. Lightweight and no dependencies.
##### 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"
```