Code Monkey home page Code Monkey logo

koa-body's Introduction

koa-body Build Status Dependencies Status

A full-feature koa body parser middleware. Support multipart, urlencoded and json request bodies. Provides same functionality as Express's bodyParser - multer. And all that is wrapped only around co-body and formidable.

Related module

Install

Install with npm

$ npm install koa-body

Features

  • 15 tests
  • can handle three type requests
    • multipart/form-data
    • application/x-www-urlencoded
    • application/json
  • option for patch to Koa or Node, or either
  • file uploads
  • body, fields and files limiting
  • 2 dependencies only

Usage like multer

It's very simple, because you can access the fields and files in the ctx.request.body or ctx.req.body JSON object

var app      = require('koa')(),
    koaBody   = require('koa-body');

app.use(koaBody({formidable:{uploadDir: __dirname}}));
app.use(function *(next) {
  if (this.request.method == 'POST') {
    console.log(this.request.body);
    // => POST body
    this.body = JSON.stringify(this.request.body);
  }
  yield next;
});
app.listen(3131)
console.log('curl -i http://localhost:3131/ -d "name=test"');

For a more comprehensive example, see examples/multipart.js

Usage with koa-router

It's generally better to only parse the body as needed, if using a router that supports middleware composition, we can inject it only for certain routes.

var app     = require('koa')(),
    router  = require('koa-router')(),
    koaBody = require('koa-body')();

router.post('/users', koaBody,
  function *(next) {
    console.log(this.request.body);
    // => POST body
    this.body = JSON.stringify(this.request.body);
  }
);

app.use(router.routes());

app.listen(3131);
console.log('curl -i http://localhost:3131/users -d "name=test"');

Options

Options available for koa-body. Four custom options, and others are from raw-body and formidable.

  • patchNode {Boolean} Patch request body to Node's ctx.req, default false
  • patchKoa {Boolean} Patch request body to Koa's ctx.request, default true
  • jsonLimit {String|Integer} The byte (if integer) limit of the JSON body, default 1mb
  • formLimit {String|Integer} The byte (if integer) limit of the form body, default 56kb
  • textLimit {String|Integer} The byte (if integer) limit of the text body, default 56kb
  • encoding {String} Sets encoding for incoming form fields, default utf-8
  • multipart {Boolean} Parse multipart bodies, default false
  • formidable {Object} Options to pass to the formidable multipart parser
  • strict {Boolean} If enabled, don't parse GET, HEAD, DELETE requests, default true

A note about strict mode

see http://tools.ietf.org/html/draft-ietf-httpbis-p2-semantics-19#section-6.3

  • GET, HEAD, and DELETE requests have no defined semantics for the request body, but this doesn't mean they may not be valid in certain use cases.
  • koa-body is strict by default

Some options for formidable

See node-formidable for a full list of options

  • bytesExpected {Integer} The expected number of bytes in this form, default null
  • maxFields {Integer} Limits the number of fields that the querystring parser will decode, default 10
  • maxFieldsSize {Integer} Limits the amount of memory a field (not file) can allocate in bytes, default 2mb
  • uploadDir {String} Sets the directory for placing file uploads in, default os.tmpDir()
  • keepExtensions {Boolean} Files written to uploadDir will include the extensions of the original files, default true
  • hash {String} If you want checksums calculated for incoming files, set this to either 'sha1' or 'md5', default false
  • multiples {Boolean} Multiple file uploads or no, default true
  • onFileBegin {Function} Special callback on file begin. The function is executed directly by formidable. It can be used to rename files before saving them to disk. See the docs

Note: You can patch request body to Node or Koa in same time if you want.

Tests

As usual - npm test or if you have [mocha][mocha-url] globally - mocha --harmony-generators.

$ npm test

License

The MIT License, 2014 Charlike Mike Reagent (@tunnckoCore) and Daryl Lau (@daryllau)

koa-body's People

Contributors

adamkdean avatar baoshan avatar booxood avatar dlau avatar ivan-kleshnin avatar necaris avatar tyler46 avatar v4n avatar xeodou avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.