Koa Body Parser
JSON and urlencoded body parser for Koa.
This does not support multipart request bodies.
It supports request body limits and encoded request bodies.
Installation
$ npm install koa-body-parser
Example
var koa = require('koa')
var bodyParser = require('koa-body-parser')
var app = koa()
bodyParser(app, {
limit: '100kb'
})
app.use(function* () {
if (this.path !== '/images' || this.method !== 'POST')
return yield next
if (!this.is('image/'))
this.error(415, 'must be an image')
var imageBuffer = yield* this.request.buffer('25mb')
})
app.use(function* () {
var body = (yield* this.request.json())
|| (yield* this.request.form())
})
Philosophy
This module aims to create helpful utilities while being as unopinionated as possible.
Thus, these methods may be a little inconvenient, especially when they're only available on this.request
.
You may want to create your own helper or middleware to parse the body the way you like.
Here is an example:
function* bodyParser() {
if (this.req.checkContinue)
this.req.writeContinue()
var body = (yield* this.request.urlencoded())
|| (yield* this.request.json())
if (!body)
this.error(400, 'no body!?')
return body
}
app.use(function* (next) {
if (this.path !== '/posts' || this.method !== 'POST')
return yield next
var body = yield* bodyParser()
})
Of course, there are many ways to do this!
API
bodyParser(app, [options])
Buffer options:
limit
[1mb] - request body byte limit.
JSON parsing options:
strict
[true] - if false
, anything JSON.parse()
accepts will be parsed as JSON,
otherwise only objects and arrays will be accepted.reviver
- used as the second reviver
argument for JSON.parse()
.
Urlencoded options:
querystring
[require('querystring')] - the querystring parser. Node's by default. If you want to use qs
, set it as require('qs')
.maxKeys
[1000] - maximum number of keys. Passed to querystring.parse
yield* this.request.json([limit])
Parses the JSON body.
The function call is required.
limit
is the optional limit in bytes integer for the body that overrides the default.
yield* this.request.urlencoded([limit])
Parses the urlencoded body.
The function call is required.
limit
is the optional limit in bytes integer for the body that overrides the default.
yield* this.request.string([limit])
Buffers the response and returns a utf8 string.
The function call is required.
limit
is the optional limit in bytes integer for the body that overrides the default.
yields* this.request.buffer([limit])
Buffers the response and returns a raw Buffer
instance.
The function call is required.
limit
is the optional limit in bytes integer for the body that overrides the default.
License
The MIT License (MIT)
Copyright (c) 2013 Jonathan Ong me@jongleberry.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.