Gathering detailed insights and metrics for koa-better-body
Gathering detailed insights and metrics for koa-better-body
Gathering detailed insights and metrics for koa-better-body
Gathering detailed insights and metrics for koa-better-body
@types/koa-better-body
TypeScript definitions for koa-better-body
private-l-koa-better-body
Full-featured [koa][] body parser! Support parsing text, buffer, json, json patch, json api, csp-report, multipart, form and urlencoded bodies. Works for koa@1, koa@2 and will work for koa@3.
koa-body-clean
Koa middleware for auto cleanup files created to disk by koa-body, koa-better-body, koa-multer or any multipart middleware that populate ctx.*.files
koa-better-body-without-git
Faster download speed to install koa-better-body
Delivering delightful digital solutions. Monorepo of monorepos of Open Source packages with combined ~100M/month downloads, semantically versioned following @conventional-commits. Fully powered ES Modules, @Airbnb @ESLint + @Prettier, independent & fixed versioning. Quality with @Actions, CodeQL, & Dependabot.
npm install koa-better-body
Typescript
Module System
Min. Node Version
Node Version
NPM Version
parse-function@5.6.10
Updated on Mar 28, 2020
to-file-path@2.0.4
Updated on Mar 28, 2020
@tunnckocore/package-json@2.0.4
Updated on Mar 28, 2020
@tunnckocore/jest-runner-eslint@1.2.9
Updated on Mar 28, 2020
stringify-github-short-url@3.3.8
Updated on Mar 28, 2020
prettier-plugin-pkgjson@0.2.8
Updated on Mar 28, 2020
JavaScript (85.76%)
TypeScript (14.24%)
Total Downloads
1,495,190
Last Day
54
Last Week
4,674
Last Month
22,475
Last Year
143,971
484 Stars
601 Commits
19 Forks
10 Watchers
44 Branches
20 Contributors
Updated on Feb 23, 2025
Minified
Minified + Gzipped
Latest Version
3.3.9
Package Id
koa-better-body@3.3.9
Size
22.10 kB
NPM Version
lerna/3.20.2/node@v12.14.0+x64 (linux)
Node Version
12.14.0
Published on
Mar 28, 2020
Cumulative downloads
Total Downloads
Last Day
1.9%
54
Compared to previous day
Last Week
-22.8%
4,674
Compared to previous week
Last Month
28.1%
22,475
Compared to previous month
Last Year
3.6%
143,971
Compared to previous year
3
Full-featured koa body parser! Support parsing text, buffer, json, json patch, json api, csp-report, multipart, form and urlencoded bodies. Works for koa@1, koa@2 and will work for koa@3.
Please consider following this project's author, Charlike Mike Reagent, and :star: the project to show your :heart: and support.
If you have any how-to kind of questions, please read the Contributing Guide and Code of Conduct documents. For bugs reports and feature requests, please create an issue or ping @tunnckoCore at Twitter.
Project is semantically versioned & automatically released from GitHub Actions with Lerna.
Topic | Contact |
---|---|
Any legal or licensing questions, like private or commerical use | |
For any critical problems and security reports | |
Consulting, professional support, personal or team training | |
For any questions about Open Source, partnerships and sponsoring |
koa@1
and koa@2
(with deprecation messages), will also work in
koa@3
with koa-convertoptions
and absolutely lightweight using
lazy-cacheformidable.IncomingForm
instance, allowing awesome
customizationformidable.IncomingForm
, allowing awesome controlkoa-router
options.extendTypes
querystring
parsingstrict
mode(TOC generated by verb using markdown-toc)
This project requires Node.js >=8.11 (see
Support & Release Policy).
Install it using yarn or
npm.
We highly recommend to use Yarn when you
think to contribute to this project.
1$ yarn add koa-better-body
Generated using jest-runner-docs.
Robust body parser for koa@1, also works for
koa@2
(with deprecations). Will also work for futurekoa@3
with koa-convert.
1function(options)
options
{object} - see more on options sectionreturns
{GeneratorFunction} - plugin for Koa
1var koa = require('koa'); 2var body = require('koa-better-body'); 3var app = koa(); 4 5app 6 .use(body()) 7 .use(function* () { 8 console.log(this.request.body); // if buffer or text 9 console.log(this.request.files); // if multipart or urlencoded 10 console.log(this.request.fields); // if json 11 }) 12 .listen(8080, function () { 13 console.log('koa server start listening on port 8080'); 14 });
koa-router
using koa-router
1'use strict'; 2 3var app = require('koa')(); 4var body = require('koa-better-body'); 5var router = require('koa-router')(); 6 7router.post('/upload', body(), function* (next) { 8 console.log(this.request.files); 9 console.log(this.request.fields); 10 11 // there's no `.body` when `multipart`, 12 // `urlencoded` or `json` request 13 console.log(this.request.body); 14 15 // print it to the API requester 16 this.body = JSON.stringify( 17 { 18 fields: this.request.fields, 19 files: this.request.files, 20 body: this.request.body || null, 21 }, 22 null, 23 2, 24 ); 25 26 yield next; 27}); 28 29app.use(router.routes()); 30app.listen(4292); 31 32var format = require('util').format; 33var host = 'http://localhost:4292'; 34var cmd = 'curl -i %s/upload -F "source=@%s/.editorconfig"'; 35 36console.log('Try it out with below CURL for `koa-better-body` repository.'); 37console.log(format(cmd, host, __dirname));
Sane defaults. :sparkles:
Accepts JSON, JSON API v1, text, buffer,
csp-report, multipart and
urlencoded/form bodies. If you want to disallow accepting and parsing multipart
body you should pass multipart: false
. Most of the defaults you can see at
utils.defaultOptions and utils.defaultTypes. All options
are
also been passed to formidable.IncomingForm! Even you can pass
IncomingForm instance to be able to handle the different formidable events.
fields
{Boolean|String}: Default false
, which means it will set fields
on this.request.fields
. If you pass a string, for example 'foo'
, you will
have fields on this.request.foo
.files
{Boolean|String}: Default false
, which means it will set files
on this.request.files
. If you pass a string, for example 'bar'
, you will
have files on this.request.bar
.multipart
{Boolean}: Default true
. If you pass false
it won't
accept/parse multipart bodies.textLimit
{String}: Default '100kb'
. Passed to bytes.parse method.formLimit
{String}: Default '100kb'
. Passed to bytes.parse method.urlencodedLimit
{String}: Default '100kb'
. Alias of opts.formLimit
.jsonLimit
{String}: Default '100kb'
. Passed to bytes.parse method.bufferLimit
{String}: Default '1mb'
. Passed to bytes.parse method.jsonStrict
{Boolean}: Default true
. When set to true, JSON parser will
only accept arrays and objects.detectJSON
{Function}: Custom JSON request detect function -
detectJSON(ctx)
.strict
{Boolean}: Default true
. Pass false
if you want to allow
parsing GET, DELETE and HEAD requests.onerror
{Function}: Custom error handle, if throw an error, you can
customize the response - onerror(err, ctx)
.extendTypes
{Object}: Default accepting types can find on
utils.defaultTypes function. Allowing you to extend
what your app can accept. By default works for JSON,
JSON API v1, multipart, text, urlencoded and
csp-report.IncomingForm
{IncomingForm}: Pass an instance of
formidable.IncomingForm
to be able to handle formidable events.handler
{GeneratorFunction}: Works with options.extendTypes.custom
to
handle custom types of content-type - handler(ctx, options, next)
. More info
below.querystring
{Object}: Querystring module to be used. By default builtin
querystring
. More info below.qs
{Object}: Alias of opts.querystring
. All opts
are also passed to
qs or querystring module.delimiter
{String}: Default is &
. Delimiter of key/value pairs, passed
to querystring libsep
{String}: alias of opts.delimiter
buffer
{Boolean}: Default false
, pass true
if you want to get body
as buffer.options.extendTypes
ExandTypes option gives you a flexible way to handle different content-types and
modify the defaults which can be found
at utils.defaultTypes function. In addition you can pass
combination of options.extendTypes.custom
and options.handler
. When the
request has some of the "custom" content type, this middleware will call the
handler
generator function with ctx, options, next
. You can see more at
issue #52.
For example manually handle such content types foo/bar-x
, text/quix
:
1const app = require('koa')() 2const body = require('koa-better-body') 3 4app.use(body({ 5 textLimit: '300kb' 6 extendTypes: { 7 custom: [ 8 'foo/bar-x', 9 'text/quix' 10 ] 11 }, 12 handler: function * (ctx, opts) { 13 // `ctx` is equal to `this` and `app` 14 // `opts` is current options object 15 // passed to `koa-better-body` 16 ctx.body = yield this.request.text(opts.textLimit) 17 } 18})) 19app.use(function * showBody () { 20 // `this.body` is text 21 console.log(this.body) 22})
querystring
parsingBecause this middleware is fully based and integrated with koa-body-parsers,
by default it uses Node's built-in module for that thing
querystring. So if you have some
issues with forms, think to add custom querystring module like qs to
options.querystring
or app.querystring
. Related to this is
issue #45.
Example
1const app = require('koa')() 2const body = require('koa-better-body') 3 4app.use(body({ 5 multipart: false 6 querystring: require('qs') 7}))
It's intentional that it's not included in the deps by default. In v2
it was
also working by passing it to app.querystring
, because koa-body-parsers
works
that way (index.js#L53).
strict
modeWe are trying to follow standards. :cat2:
You can pass strict:false
, but see
IETF HTTP/1.1 Message Semantics: Section 6.1
to understand why we stay to "strict mode" by default. 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. Last two tests at
test/options.js are showing usage on non-strict and strict
mode.
Some of these projects are used here or were inspiration for this one, others are just related. So, thanks for your existance!
Please read the Contributing Guide and Code of Conduct documents for advices.
For bug reports and feature requests, please join our community forum and open a thread there with prefixing the title of the thread with the name of the project if there's no separate channel for it.
Consider reading the Support and Release Policy guide if you are interested in what are the supported Node.js versions and how we proceed. In short, we support latest two even-numbered Node.js release lines.
Become a Partner or Sponsor? :dollar: Check the OpenSource Commision (tier). :tada: You can get your company logo, link & name on this file. It's also rendered on package's page in npmjs.com and yarnpkg.com sites too! :rocket:
Not financial support? Okey! Pull requests, stars and all kind of contributions are always welcome. :sparkles:
This project follows the all-contributors specification. Contributions of any kind are welcome!
Thanks goes to these wonderful people (emoji key), consider showing your support to them:
Charlike Mike Reagent ???? ???? ???? ???? ???? ⚠️ |
Copyright (c) 2014-present, Charlike Mike Reagent
<opensource@tunnckocore.com>
& contributors.
Released under the MPL-2.0 License.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
security policy file detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/29 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
license file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-06-23
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More