npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

koa-body-plus

v4.2.0-4

Published

A Koa body parser middleware. Supports multipart, urlencoded JSON and any other format request bodies.

Downloads

2

Readme

koa-body-plus Build Status Dependencies Status KoaJs Slack

This object forked from https://github.com/dlau/koa-body on 2020-08-15

本项目是koa-body项目的分支版本,2020-08-15从https://github.com/dlau/koa-body取得的源代码。

Keep most of the original code, only make a small change to the content type.

保持绝大部分源代码,只是对支持的文档类型做了小修改。

Tribute to the original author.

向原作者致敬。

The release name in NPM is koa-body-plus

在npm的发布名称koa-body-plus

A full-featured koa body parser middleware. Supports multipart, urlencoded, json any other format request bodies. Provides the same functionality as Express's bodyParser - multer.

Install

Install with npm

npm install koa-body-plus

Features

  • can handle requests such as:
    • multipart/form-data

    • application/x-www-urlencoded

    • application/json

    • application/json-patch+json

    • application/vnd.api+json

    • application/csp-report

    • text/xml

    • image/*

And any other requests

  • option for patch to Koa or Node, or either
  • file uploads
  • body, fields and files size limiting

Hello World - Quickstart

npm install koa koa-body-plus # Note that Koa requires Node.js 7.6.0+ for async/await support

index.js:

const Koa = require('koa');
const koaBody = require('koa-body-plus');

const app = new Koa();

app.use(koaBody());
app.use(ctx => {
  ctx.body = `Request Body: ${JSON.stringify(ctx.request.body)}`;
});

app.listen(3000);
node index.js
curl -i http://localhost:3000/users -d "name=test"

Output:

HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
Content-Length: 29
Date: Wed, 03 May 2017 02:09:44 GMT
Connection: keep-alive

Request Body: {"name":"test"}%

index.js:

const Koa = require('koa');
const koaBody = require('koa-body-plus');

const app = new Koa();

app.use(koaBody());
app.use(ctx => {
  ctx.body = ctx.request.file;
});

app.listen(3001);
curl -v http://localhost:3001/ -H"content-type: application/x-gzip" -X PUT -d "@redis-5.0.8.tar.gz"

Output:

HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 119
Date: Sun, 16 Aug 2020 01:21:49 GMT
Connection: keep-alive

{"name":"","path":"C:\\Users\\Mustang\\AppData\\Local\\TempqQ0oyOInGOOGt4Ss","size":950107,"type":"application/x-gzip"}

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.

const Koa = require('koa');
const app = new Koa();
const router = require('koa-router')();
const koaBody = require('koa-body-plus');

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

app.use(router.routes());

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

Usage with unsupported text body type

For unsupported text body type, for example, text/xml, you can use the unparsed request body at ctx.request.body. For the text content type, the includeUnparsed setting is not required.

// xml-parse.js:
const Koa = require('koa');
const koaBody = require('koa-body-plus');
const convert = require('xml-js');

const app = new Koa();

app.use(koaBody());
app.use(ctx => {
  const obj = convert.xml2js(ctx.request.body)
  ctx.body = `Request Body: ${JSON.stringify(obj)}`;
});

app.listen(3000);
node xml-parse.js
curl -i http://localhost:3000/users -H "Content-Type: text/xml" -d '<?xml version="1.0"?><catalog id="1"></catalog>'

Output:

HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
Content-Length: 135
Date: Tue, 09 Jun 2020 11:17:38 GMT
Connection: keep-alive

Request Body: {"declaration":{"attributes":{"version":"1.0"}},"elements":[{"type":"element","name":"catalog","attributes":{"id":"1"}}]}%

Options

Options available for koa-body-plus. 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
  • binLimit {String|Integer} The byte (if integer) limit of the binary body, default 1mb
  • encoding {String} Sets encoding for incoming form fields, default utf-8
  • multipart {Boolean} Parse multipart bodies, default false
  • urlencoded {Boolean} Parse urlencoded bodies, default true
  • text {Boolean} Parse text bodies, such as XML, default true
  • json {Boolean} Parse JSON bodies, default true
  • bin {Boolean} Parse binary bodies, default true
  • jsonStrict {Boolean} Toggles co-body strict mode; if set to true - only parses arrays or objects, default true
  • includeUnparsed {Boolean} Toggles co-body returnRawBody option; if set to true, for form encodedand and JSON requests the raw, unparsed requesty body will be attached to ctx.request.body using a Symbol, default false
  • formidable {Object} Options to pass to the formidable multipart parser
  • onError {Function} Custom error handle, if throw an error, you can customize the response - onError(error, context), default will throw
  • strict {Boolean} DEPRECATED If enabled, don't parse GET, HEAD, DELETE requests, default true
  • parsedMethods {String[]} Declares the HTTP methods where bodies will be parsed, default ['POST', 'PUT', 'PATCH']. Replaces strict option.

A note about parsedMethods

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-plus is strict by default, parsing only POST, PUT, and PATCH requests

File Support

Uploaded files are accessible via ctx.request.files. Requests other than multipart, urlencoded, and JSON can be accessed through the ctx.request.file visit

A note about unparsed request bodies

Some applications require crytopgraphic verification of request bodies, for example webhooks from slack or stripe. The unparsed body can be accessed if includeUnparsed is true in koa-body-plus's options. When enabled, import the symbol for accessing the request body from unparsed = require('koa-body-plus/unparsed.js'), or define your own accessor using unparsed = Symbol.for('unparsedBody'). Then the unparsed body is available using ctx.request.body[unparsed].

Some options for formidable

See node-formidable for a full list of options

  • maxFields {Integer} Limits the number of fields that the querystring parser will decode, default 1000
  • maxFieldsSize {Integer} Limits the amount of memory all fields together (except files) can allocate in bytes. If this value is exceeded, an 'error' event is emitted, default 2mb (2 * 1024 * 1024)
  • 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 false
  • 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

Changelog

Please see the Changelog for a summary of changes.

Tests

$ npm test

License

The MIT License, 2020 Charlike Mike Reagent (@tunnckoCore) Daryl Lau (@daryllau) and Mustang Yu(@于恩水)