chensenlai 10988628a0 语音房项目初始化 | 5 anos atrás | |
---|---|---|
.. | ||
lib | 5 anos atrás | |
node_modules | 5 anos atrás | |
.npmignore | 5 anos atrás | |
LICENSE | 5 anos atrás | |
README.md | 5 anos atrás | |
index.js | 5 anos atrás | |
jsconfig.json | 5 anos atrás | |
package.json | 5 anos atrás |
Parse wechat xml request body for Koa
const koa = require('koa');
const wechatBodyParser = require('co-wechat-body');
const app = koa();
app.use(wechatBodyParser(<options>));
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;
});
co-wechat-body
will carefully check and set this.request.body
, so it can intergate well with other body parsers such as koa-bodyparser
:
// ...
const bodyParser = require('koa-bodyparser');
// ...
app.use(wechatBodyParser(<options>));
app.use(bodyParser());
http request raw body:
<xml>
<return_code><![CDATA[SUCCESS]]></return_code>
<return_msg><![CDATA[OK]]></return_msg>
</xml>
parse result, this.request.body:
{
"return_code": "SUCCESS",
"return_msg": "OK"
}
Please refer to (https://www.npmjs.com/package/koa-xml-body#options)
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.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);
}
}));