Home

Awesome

@koa/bodyparser

NPM version build status Coveralls node version

Koa body parsing middleware, based on co-body. support json, form and text type body.

Parse incoming request bodies in a middleware before your handlers, available under the ctx.request.body property.

⚠ Notice: This module doesn't support parsing multipart format data, please use @koa/multer to parse multipart format data.

Install

NPM

$ npm i @koa/bodyparser --save

Usage

const Koa = require("koa");
const { bodyParser } = require("@koa/bodyparser");

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

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

Options

Raw Body

You can access raw request body by ctx.request.rawBody after koa-bodyparser when:

  1. koa-bodyparser parsed the request body.
  2. ctx.request.rawBody is not present before koa-bodyparser.

Koa v1.x.x Support

To use koa-bodyparser with koa@1.x.x, please use bodyparser 2.x.

$ npm install koa-bodyparser@2 --save

usage

const Koa = require("koa");
const bodyParser = require("@koa/bodyparser");

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

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

Licences

MIT