1 Star 1 Fork 0

Tenny / koa-bodyparser

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

koa-bodyparser-base

一个基于 raw-bodykoa 请求内容解析器;支持的解析类型有 textformjsonraw

安装

npm install koa-bodyparser-base

使用

let Koa = require('koa');
let bodyParser = require('koa-bodyparser-base');

let app = new Koa();
let.use(bodyParser());

let.use(async (ctx) => {
  // 解析后的请求内容将存放到 ctx.request.body 上
  // 如果没有解析到对应的请求,则 body 将是 undefined
  ctx.body = ctx.request.body;
});

配置选项(options)

bodyParser([options]);
  • json: 解析 json 请求时,使用 raw-body 解析时需要的参数,默认:{ limit: '1mb', encoding: true }
  • form: 解析 form 请求时,使用 raw-body 解析时需要的参数,默认 { limit: '56kb', encoding: true }
  • text: 解析 text 请求时,使用 raw-body 解析时需要的参数,默认 { limit: '1mb', encoding: true }
  • raw: 解析 raw 请求时,使用 raw-body 解析时需要的参数,默认 { limit: '100kb', encoding: true }
  • extendTypes: 解析相应格式需要匹配的后缀格式,支持扩展每一种请求所兼容的类型,例如:
    app.use(bodyparser({
      extendTypes: {
        // 转换请求类型(content-type) 为 `application/x-javascript` 的请求内容为 `json` 格式。
        json: ['application/x-javascript']
      }
    }));
  • disableBodyParse:可以动态的通过设置 ctx.disableBodyParse = true 来禁用解析。
    app.use(async (ctx, next) => {
      if (ctx.path === '/disable') {
        ctx.disableBodyParse = true;
      }
      await next();
    });
    app.use(bodyparser());

备注

  1. limit 参数为 bytes 所支持的参数格式;
  2. encoding 参数为 iconv-lite 所支持的编码格式。
  3. 如果格式为 form 返回的是 querystring.parse() 解析后的对象,该对象跟普通的 Object 是不同的,详情参考:querystring_parse

License

MIT

The MIT License (MIT) Copyright (c) 2018 Tenny Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

koa2 解析请求内容(body-parser), 包含基本的请求实体(raw, text, json, urlencoded) 展开 收起
NodeJS
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
NodeJS
1
https://gitee.com/towardly/koa-bodyparser.git
git@gitee.com:towardly/koa-bodyparser.git
towardly
koa-bodyparser
koa-bodyparser
master

搜索帮助