chensenlai 10988628a0 语音房项目初始化 | 5 yıl önce | |
---|---|---|
.. | ||
lib | 5 yıl önce | |
test | 5 yıl önce | |
tools | 5 yıl önce | |
.babelrc | 5 yıl önce | |
.eslintrc | 5 yıl önce | |
.npmignore | 5 yıl önce | |
.travis.yml | 5 yıl önce | |
LICENSE | 5 yıl önce | |
index.js | 5 yıl önce | |
package.json | 5 yıl önce | |
readme.md | 5 yıl önce |
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);
}
}));