• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

venables/koa-json-body: Single-purpose koa middleware to parse valid JSON reques ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

venables/koa-json-body

开源软件地址:

https://github.com/venables/koa-json-body

开源编程语言:

JavaScript 100.0%

开源软件介绍:

koa-json-body

Version Build Status Coverage Status Dependency Status Standard - JavaScript Style Guide Downloads


A single-purpose koa middleware to only parse JSON request bodies and nothing else.

By default, this libarary parses all valid JSON bodies on POST, PUT, and PATCH requests, and assigns the value to ctx.request.body.

If there is a JSON parsing error, or if the request is not of valid type, ctx.request.body is not set, and will be undefined. If the JSON request payload is too large (by default, the limit is 1mb), a 413 Payload Too Large error will be thrown.

To ensure ctx.request.body contains an empty object {} (rather than undefined) on missing/invalid payloads, you can set the fallback option to true.

Installation

yarn add koa-json-body

or via npm:

npm install koa-json-body --save

Options

  • fallback - when set to true, ctx.request.body will always contain {} upon missing or invalid payloads. (default: false)
  • limit - number or string representing the request size limit (default: 1mb)
  • strict - when set to true, koa-json-body will only accept arrays and objects. (default: true)

Additional options available via co-body.

Usage

On a every route:

const body = require('koa-json-body')

app.use(body({ limit: '10kb', fallback: true }))

app.use((ctx, next) => {
  console.log(ctx.request.body)
})

On a per-route basis (this example uses koa-router):

const body = require('koa-json-body')({ limit: '10kb' })

app.post('/users', body, (ctx, next) => {
  console.log(ctx.request.body)
})

Testing

To test, simply run

yarn test

Made for koa 2 and beyond

For koa 0.x and 1.x support, see the koa-1 branch.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap