You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

koa-bodyparser

Package Overview
Dependencies
Maintainers
2
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-bodyparser

a body parser for Koa

4.4.1
latest
Source
npmnpm
Version published
Weekly downloads
752K
-3.16%
Maintainers
2
Weekly downloads
 
Created

What is koa-bodyparser?

koa-bodyparser is a middleware for Koa that parses incoming request bodies in a middleware before your handlers, making it available under the `ctx.request.body` property. It supports parsing JSON, form, and text bodies.

What are koa-bodyparser's main functionalities?

JSON Body Parsing

This feature allows you to parse JSON bodies from incoming requests. The parsed data is available on `ctx.request.body`.

const Koa = require('koa');
const bodyParser = require('koa-bodyparser');
const app = new Koa();

app.use(bodyParser());

app.use(async ctx => {
  if (ctx.method === 'POST') {
    ctx.body = `Received JSON data: ${JSON.stringify(ctx.request.body)}`;
  } else {
    ctx.body = 'Send a POST request with JSON data';
  }
});

app.listen(3000);

Form Body Parsing

This feature allows you to parse URL-encoded form bodies from incoming requests. The parsed data is available on `ctx.request.body`.

const Koa = require('koa');
const bodyParser = require('koa-bodyparser');
const app = new Koa();

app.use(bodyParser());

app.use(async ctx => {
  if (ctx.method === 'POST') {
    ctx.body = `Received form data: ${JSON.stringify(ctx.request.body)}`;
  } else {
    ctx.body = 'Send a POST request with form data';
  }
});

app.listen(3000);

Text Body Parsing

This feature allows you to parse plain text bodies from incoming requests. The parsed data is available on `ctx.request.body`.

const Koa = require('koa');
const bodyParser = require('koa-bodyparser');
const app = new Koa();

app.use(bodyParser({ enableTypes: ['text'] }));

app.use(async ctx => {
  if (ctx.method === 'POST') {
    ctx.body = `Received text data: ${ctx.request.body}`;
  } else {
    ctx.body = 'Send a POST request with text data';
  }
});

app.listen(3000);

Other packages similar to koa-bodyparser

Keywords

bodyParser

FAQs

Package last updated on 22 Jun 2023

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