圈内,使用Koa2、express比较多,而我hapi使用比较多。目前在做微信公众号开发,要求返回数据是xml格式。
1、之前的返回,直接return
Json2Xml: async function (request, h) {const data = `<xml><ToUserName>< ![CDATA[oFVpQ1qGVmf4Vf0pCkLdEWsQiM2k] ]></ToUserName><FromUserName>< ![CDATA[gh_ba5cd257765a] ]></FromUserName><CreateTime>1534334883</CreateTime><MsgType>< ![CDATA[text] ]></MsgType><Content>< ![CDATA[你好,hapi] ]></Content></xml>`;return data;
},
2、返回xml需要设置response.type('application/xml')即可
Json2Xml: async function (request, h) {const data = `<xml><ToUserName>< ![CDATA[oFVpQ1qGVmf4Vf0pCkLdEWsQiM2k] ]></ToUserName><FromUserName>< ![CDATA[gh_ba5cd257765a] ]></FromUserName><CreateTime>1534334883</CreateTime><MsgType>< ![CDATA[text] ]></MsgType><Content>< ![CDATA[你好,hapi] ]></Content></xml>`;const response = h.response(replyXml);response.type('application/xml');return response;
}