Code Monkey home page Code Monkey logo

bodyparser's Introduction

koa-bodyparser

NPM version build status Coveralls David deps node version Gittip

a body parser for koa, base on co-body.

Notice: koa-bodyparser@3 support koa@2, if you want to use this module with koa@1, please use koa-bodyparser@2.

Install

NPM

Usage

var bodyParser = require('koa-bodyparser');
var Koa = require('koa');

var app = new Koa();
app.use(bodyParser());

app.use(ctx => {
  // the parsed body will store in this.request.body
  // if nothing was parsed, body will be an empty object {}
  ctx.body = ctx.request.body;
});

Options

  • encode: requested encoding. Default is utf-8 by co-body

  • formLimit: limit of the urlencoded body. If the body ends up being larger than this limit, a 413 error code is returned. Default is 56kb

  • jsonLimit: limit of the json body. Default is 1mb

  • strict: when set to true, JSON parser will only accept arrays and objects. Default is true. See strict mode in co-body

  • detectJSON: custom json request detect function. Default is null

    app.use(bodyparser({
      detectJSON: function (ctx) {
        return /\.json$/i.test(ctx.path);
      }
    }));
  • extendTypes: support extend types:

    app.use(bodyparser({
      extendTypes: {
        json: ['application/x-javascript'] // will parse application/x-javascript type body as a JSON string
      }
    }));
  • onerror: support custom error handle, if koa-bodyparser throw an error, you can customize the response like:

    app.use(bodyparser({
      onerror: function (err, ctx) {
        ctx.throw('body parse error', 422);
      }
    }));

Licences

MIT

bodyparser's People

Contributors

dead-horse avatar macalinao avatar fengmk2 avatar tejasmanohar avatar esco avatar jeromemacias avatar markherhold avatar rudijs avatar sylvainlap avatar

Watchers

James Cloos 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.