chensenlai 10988628a0 语音房项目初始化 | %!s(int64=5) %!d(string=hai) anos | |
---|---|---|
.. | ||
lib | %!s(int64=5) %!d(string=hai) anos | |
test | %!s(int64=5) %!d(string=hai) anos | |
tools | %!s(int64=5) %!d(string=hai) anos | |
.babelrc | %!s(int64=5) %!d(string=hai) anos | |
.eslintrc | %!s(int64=5) %!d(string=hai) anos | |
.npmignore | %!s(int64=5) %!d(string=hai) anos | |
.travis.yml | %!s(int64=5) %!d(string=hai) anos | |
LICENSE | %!s(int64=5) %!d(string=hai) anos | |
index.js | %!s(int64=5) %!d(string=hai) anos | |
package.json | %!s(int64=5) %!d(string=hai) anos | |
readme.md | %!s(int64=5) %!d(string=hai) anos |
Parse xml request body for Koa
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:
var xmlParser = require('koa-xml-body').default;
ES6+
with Babel@6.x
: import xmlParser from 'koa-xml-body';
utf8
. If not set, the lib will retrive it from content-type
(such as content-type:application/xml;charset=gb2312
).1mb
.content-length
is found, it will be overwritten automatically.noop
function. It means it will eat the error silently. You can config it to customize the response.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);
}
}));