chensenlai 10988628a0 语音房项目初始化 %!s(int64=5) %!d(string=hai) anos
..
lib 10988628a0 语音房项目初始化 %!s(int64=5) %!d(string=hai) anos
test 10988628a0 语音房项目初始化 %!s(int64=5) %!d(string=hai) anos
tools 10988628a0 语音房项目初始化 %!s(int64=5) %!d(string=hai) anos
.babelrc 10988628a0 语音房项目初始化 %!s(int64=5) %!d(string=hai) anos
.eslintrc 10988628a0 语音房项目初始化 %!s(int64=5) %!d(string=hai) anos
.npmignore 10988628a0 语音房项目初始化 %!s(int64=5) %!d(string=hai) anos
.travis.yml 10988628a0 语音房项目初始化 %!s(int64=5) %!d(string=hai) anos
LICENSE 10988628a0 语音房项目初始化 %!s(int64=5) %!d(string=hai) anos
index.js 10988628a0 语音房项目初始化 %!s(int64=5) %!d(string=hai) anos
package.json 10988628a0 语音房项目初始化 %!s(int64=5) %!d(string=hai) anos
readme.md 10988628a0 语音房项目初始化 %!s(int64=5) %!d(string=hai) anos

readme.md

koa-xml-body

Build Status npm version Dependency Status download times

Parse xml request body for Koa

Install

NPM

Usage

var koa = require('koa');
var xmlParser = require('koa-xml-body').default; // note the default

var app = koa();
app.use(xmlParser());

app.use(function *() {
    // the parsed body will store in this.request.body
    // if nothing was parsed, body will be undefined
    this.body = this.request.body;
});

koa-xml-body will carefully check and set this.request.body, so it can intergate well with other body parsers such as koa-bodyparser:

// ...
var bodyParser = require('koa-bodyparser');

// ...
app.use(xmlParser());
app.use(bodyParser());

Note:

The lib is written in ES6+ and transpiled with Babel@6.x. You should use the lib either the way below:

  • Traditional: var xmlParser = require('koa-xml-body').default;
  • ES6+ with Babel@6.x: import xmlParser from 'koa-xml-body';

Options

  • encoding: requested encoding. Default is utf8. If not set, the lib will retrive it from content-type(such as content-type:application/xml;charset=gb2312).
  • limit: limit of the body. If the body ends up being larger than this limit, a 413 error code is returned. Default is 1mb.
  • length: length of the body. When content-length is found, it will be overwritten automatically.
  • onerror: error handler. Default is a noop function. It means it will eat the error silently. You can config it to customize the response.
  • xmlOptions: options will be passed to xml2js. Default is {}. See xml2js Options for details.
app.use(xmlParser({
    limit: 128,
    length: 200, // '1mb'|1024... If not sure about the effect, just leave it unspecified
    encoding: 'utf8', // lib will detect it from `content-type`
    onerror: (err, ctx) => {
        ctx.throw(err.status, err.message);
    }
}));

Licences

MIT