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