{"id":13798845,"url":"https://github.com/venables/koa-json-body","last_synced_at":"2025-04-15T07:31:08.794Z","repository":{"id":15303354,"uuid":"18033145","full_name":"venables/koa-json-body","owner":"venables","description":"Single-purpose koa middleware to parse valid JSON request bodies and nothing else.","archived":false,"fork":false,"pushed_at":"2017-04-22T14:52:26.000Z","size":46,"stargazers_count":15,"open_issues_count":1,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-14T13:12:51.586Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/venables.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-03-23T12:45:57.000Z","updated_at":"2020-08-25T20:50:53.000Z","dependencies_parsed_at":"2022-08-25T15:21:45.037Z","dependency_job_id":null,"html_url":"https://github.com/venables/koa-json-body","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/venables%2Fkoa-json-body","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/venables%2Fkoa-json-body/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/venables%2Fkoa-json-body/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/venables%2Fkoa-json-body/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/venables","download_url":"https://codeload.github.com/venables/koa-json-body/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249026727,"owners_count":21200499,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-04T00:00:55.202Z","updated_at":"2025-04-15T07:31:08.452Z","avatar_url":"https://github.com/venables.png","language":"JavaScript","funding_links":[],"categories":["仓库"],"sub_categories":["中间件"],"readme":"koa-json-body\n=============\n\n[![Version](https://img.shields.io/npm/v/koa-json-body.svg)](https://www.npmjs.com/package/koa-json-body)\n[![Build Status](https://img.shields.io/travis/venables/koa-json-body/master.svg)](https://travis-ci.org/venables/koa-json-body)\n[![Coverage Status](https://img.shields.io/coveralls/venables/koa-json-body.svg)](https://coveralls.io/github/venables/koa-json-body)\n[![Dependency Status](https://img.shields.io/david/venables/koa-json-body/master.svg)](https://david-dm.org/venables/koa-json-body)\n[![Standard - JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)\n[![Downloads](https://img.shields.io/npm/dm/koa-json-body.svg)](https://www.npmjs.com/package/koa-json-body)\n\n----------\n\nA single-purpose [koa](https://github.com/koajs/koa) middleware to only parse JSON request bodies and nothing else.\n\nBy default, this libarary parses all **valid** JSON bodies on `POST`, `PUT`, and `PATCH` requests, and assigns the value to `ctx.request.body`.\n\nIf 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](#options), the limit is `1mb`), a `413 Payload Too Large` error will be thrown.\n\nTo ensure `ctx.request.body` contains an empty object `{}` (rather than `undefined`) on missing/invalid payloads, you can set the [`fallback` option](#options) to `true`.\n\nInstallation\n------------\n\n```bash\nyarn add koa-json-body\n```\n\nor via npm:\n\n```bash\nnpm install koa-json-body --save\n```\n\nOptions\n-------\n\n* `fallback` - when set to `true`, `ctx.request.body` will always contain `{}` upon missing or invalid payloads. (default: `false`)\n* `limit` - number or string representing the request size limit (default: `1mb`)\n* `strict` - when set to `true`, koa-json-body will only accept arrays and objects. (default: `true`)\n\nAdditional options available via [co-body](https://github.com/cojs/co-body).\n\nUsage\n-----\n\nOn a every route:\n\n```javascript\nconst body = require('koa-json-body')\n\napp.use(body({ limit: '10kb', fallback: true }))\n\napp.use((ctx, next) =\u003e {\n  console.log(ctx.request.body)\n})\n```\n\nOn a per-route basis (this example uses [koa-router](https://github.com/alexmingoia/koa-router)):\n\n```javascript\nconst body = require('koa-json-body')({ limit: '10kb' })\n\napp.post('/users', body, (ctx, next) =\u003e {\n  console.log(ctx.request.body)\n})\n```\n\n\nTesting\n-------\n\nTo test, simply run\n\n```\nyarn test\n```\n\n\nMade for koa 2 and beyond\n-------------------------\n\nFor koa 0.x and 1.x support, see the [koa-1](https://github.com/venables/koa-json-body/tree/koa-1) branch.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvenables%2Fkoa-json-body","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvenables%2Fkoa-json-body","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvenables%2Fkoa-json-body/lists"}