Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

koa-body-parser

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-body-parser

Request body parser for koa

  • 0.2.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
215
decreased by-87.15%
Maintainers
1
Weekly downloads
 
Created
Source

Koa Body Parser Build Status

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'
})

// Buffer an image upload
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')
})

// Parse a form
app.use(function* () {
  var body = (yield* this.request.json())
    || (yield* this.request.urlencoded())
  // do stuff with your body
})

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.call(this)
})

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. An optional limit overrides the default limit.

yield* this.request.urlencoded([limit])

Parses the urlencoded body. The function call is required. An optional limit overrides the default limit.

yield* this.request.string([limit])

Buffers the response and returns a utf8 string. The function call is required. An optional limit overrides the default limit.

yield* this.request.buffer([limit])

Buffers the response and returns a raw Buffer instance. The function call is required. An optional limit overrides the default limit.

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.

FAQs

Package last updated on 19 Nov 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc