koa-json-body
Very simple koa middleware wrapper around co-body for parsing JSON request bodies. Largely inspired by koa-body.
This will not parse anythig but valid JSON request bodies. If there is a JSON error, the middleware will set ctx.request.body
to {}
and continue.
Installation
npm install koa-json-body --save
Options
Available via co-body:
limit
- number or string representing the request size limit (default: 1mb
)
Usage
On a every route:
var jsonBody = require('koa-json-body');
app.use(jsonBody({ limit: '10kb' }));
app.use(function *() {
console.log(this.request.body);
});
On a per-route basis (using koa-router):
var jsonBody = require('koa-json-body')({ limit: '10kb' });
app.post('/users', jsonBody, function *() {
console.log(this.request.body);
});