Installations
npm install koa-bodyparser
Developer
koajs
Developer Guide
Module System
CommonJS
Min. Node Version
>=8.0.0
Typescript Support
No
Node Version
18.16.0
NPM Version
6.14.18
Statistics
1,313 Stars
110 Commits
118 Forks
19 Watching
5 Branches
33 Contributors
Updated on 26 Nov 2024
Bundle Size
458.69 kB
Minified
191.22 kB
Minified + Gzipped
Languages
TypeScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
140,685,747
Last day
-13.6%
111,063
Compared to previous day
Last week
-6%
628,558
Compared to previous week
Last month
5.7%
2,761,980
Compared to previous month
Last year
-2.4%
29,393,958
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
@koa/bodyparser
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
1$ npm i @koa/bodyparser --save
Usage
1const Koa = require("koa"); 2const { bodyParser } = require("@koa/bodyparser"); 3 4const app = new Koa(); 5app.use(bodyParser()); 6 7app.use((ctx) => { 8 // the parsed body will store in ctx.request.body 9 // if nothing was parsed, body will be an empty object {} 10 ctx.body = ctx.request.body; 11});
Options
-
patchNode: patch request body to Node's
ctx.req
, default isfalse
. -
enableTypes: parser will only parse when request type hits enableTypes, support
json/form/text/xml
, default is['json', 'form']
. -
encoding: requested encoding. Default is
utf-8
byco-body
. -
formLimit: limit of the
urlencoded
body. If the body ends up being larger than this limit, a 413 error code is returned. Default is56kb
. -
jsonLimit: limit of the
json
body. Default is1mb
. -
textLimit: limit of the
text
body. Default is1mb
. -
xmlLimit: limit of the
xml
body. Default is1mb
. -
jsonStrict: when set to true, JSON parser will only accept arrays and objects. Default is
true
. See strict mode inco-body
. In strict mode,ctx.request.body
will always be an object(or array), this avoid lots of type judging. But text body will always return string type. -
detectJSON: custom json request detect function. Default is
null
.1app.use( 2 bodyParser({ 3 detectJSON(ctx) { 4 return /\.json$/i.test(ctx.path); 5 }, 6 }) 7);
-
extendTypes: support extend types:
1app.use( 2 bodyParser({ 3 extendTypes: { 4 // will parse application/x-javascript type body as a JSON string 5 json: ["application/x-javascript"], 6 }, 7 }) 8);
-
onError: support custom error handle, if
koa-bodyparser
throw an error, you can customize the response like:1app.use( 2 bodyParser({ 3 onError(err, ctx) { 4 ctx.throw(422, "body parse error"); 5 }, 6 }) 7);
-
enableRawChecking: support the already parsed body on the raw request by override and prioritize the parsed value over the sended payload. (default is
false
) -
parsedMethods: declares the HTTP methods where bodies will be parsed, default
['POST', 'PUT', 'PATCH']
. -
disableBodyParser: you can dynamic disable body parser by set
ctx.disableBodyParser = true
.1app.use((ctx, next) => { 2 if (ctx.path === "/disable") ctx.disableBodyParser = true; 3 return next(); 4}); 5app.use(bodyParser());
Raw Body
You can access raw request body by ctx.request.rawBody
after koa-bodyparser
when:
koa-bodyparser
parsed the request body.ctx.request.rawBody
is not present beforekoa-bodyparser
.
Koa v1.x.x Support
To use koa-bodyparser
with koa@1.x.x, please use bodyparser 2.x.
1$ npm install koa-bodyparser@2 --save
usage
1const Koa = require("koa"); 2const bodyParser = require("@koa/bodyparser"); 3 4const app = new Koa(); 5app.use(bodyParser()); 6 7app.use((ctx) => { 8 // the parsed body will store in ctx.request.body 9 // if nothing was parsed, body will be an empty object {} 10 ctx.body = ctx.request.body; 11});
Licences
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
0 existing vulnerabilities detected
Reason
security policy file detected
Details
- Info: security policy file detected: github.com/koajs/.github/SECURITY.md:1
- Info: Found linked content: github.com/koajs/.github/SECURITY.md:1
- Info: Found disclosure, vulnerability, and/or timelines in security policy: github.com/koajs/.github/SECURITY.md:1
- Info: Found text in security policy: github.com/koajs/.github/SECURITY.md:1
Reason
Found 15/30 approved changesets -- score normalized to 5
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:17: update your workflow using https://app.stepsecurity.io/secureworkflow/koajs/bodyparser/ci.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:19: update your workflow using https://app.stepsecurity.io/secureworkflow/koajs/bodyparser/ci.yml/master?enable=pin
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/ci.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 16 are checked with a SAST tool
Score
5
/10
Last Scanned on 2024-11-18
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