Unhandled rejection Error: Non-whitespace before first tag.
来源:2-7 第二天 先撸一个自动回复出来
星辰Iron
2016-10-24
看到很多同学都有这个错误,好像都说是模块导出错误,具体怎么改还是不甚清楚,报错如下:
如下是我的代码:
//app.js 'use strict' var Koa = require('koa') var path = require('path') var wechat = require('./wechat/g') var util = require('./libs/util') var wechat_file = path.join(__dirname,'./config/wechat.txt') var config = { wechat:{ appID:'wxfeac53b7ad50b5dc', appSecret:'d7912572793e386070d20a396790f951', token: 'freecodecamplxk', getAccessToken: function(){ return util.readFileAsync(wechat_file) }, saveAccessToken: function(data){ data = JSON.stringify(data) return util.writeFileAsync(wechat_file,data) } } } var app = new Koa() app.use(wechat(config.wechat)) app.listen(1234) console.log('Listening : 1234')
//wechat目录下的g.js 'use strict' var sha1 = require('sha1') var getRawBody = require('raw-body') var Wechat = require('./wechat') var util = require('./util') module.exports = function(opts){ var wechat = new Wechat(opts) return function *(next){ // console.log('wechat1') // console.log(this.query) var that = this var token = opts.token var signature = this.query.signature var nonce = this.query.nonce var timestamp = this.query.timestamp var echostr = this.query.echostr var str = [token,timestamp,nonce].sort().join('') var sha = sha1(str) if(this.method === 'GET'){ if(sha === signature){ this.body = echostr + '' // console.log('123456'); } else{ this.body = 'wrong' } } else if(this.method === 'POST'){ if(sha !== signature){ this.body = 'wrong' return false } // console.log('123'); var data = yield getRawBody(this.req,{ length: this.length, limit: '1mb', encoding: this.charset }) console.log('test') console.log(data.toString()) // parseXMLAsync 是为了把 XML 解析为 JS 对象 var content = yield util.parseXMLAsync(data) console.log(content) console.log('123') // formatMessage 是为了把 JS 对象解析为扁平的 JS 对象 var message = util.formatMessage(content.xml) console.log(message) console.log('lxk'); if(message.MsgType === 'event'){ if(message.Event === 'subscribe'){ var now = new Date().getTime() that.status = 200 that.type = 'application/xml' that.body = '<xml>' + '<ToUserName><![CDATA[' + message.FromUserName + ']]></ToUserName>' + '<FromUserName><![CDATA[' + message.ToUserName +']]></FromUserName>' + '<CreateTime>' + now + '</CreateTime>' + '<MsgType><![CDATA[text]]></MsgType>' + '<Content><![CDATA[Hi, Imooc同学!!]]></Content>' + '</xml>' return } } } } }
//wechat目录下的util.js 'use strict' var xml2js = require('xml2js') var Promise = require('bluebird') exports.parseXMLAsync = function(xml){ return new Promise(function(resolve, reject){ xml2js.parseString(xml, {trim: true}, function(err,content){ if (err) reject(err) else resolve(content) }) }) } function formatMessage(result){ var message = {} if(typeof result === 'object'){ var keys = Object.keys(result) for(var i = 0; i < keys.length; i++){ var item = result[keys[i]] var key = key[i] if(!(item instanceof Array) || item.length === 0){ continue } if(item.length ===1){ var val = item[0] if(typeof val === 'object'){ message[key] = formatMessage(val) } else{ message[key] = (val || '').trim() } } else{ message[key] = [] for(var j = 0, k = item.length; j < k; j++){ message[key].push(formatMessage(item[j])) } } } } return message } exports.formatMessage = function(xml){ console.log('456'); return new Promise(function(resolve, reject){ xml2js.parseString(xml, {trim: true}, function(err,content){ if (err) reject(err) else resolve(content) }) }) } // exports.formatMessage = formatMessage
写回答
1回答
-
exports.formatMessage = formatMessage
为什么注释掉这个代码呢,用这个才能把 message 对象进一步格式化出来,这一段
exports.formatMessage = function(xml){ console.log('456'); return new Promise(function(resolve, reject){ xml2js.parseString(xml, {trim: true}, function(err,content){ if (err) reject(err) else resolve(content) }) }) }
可以删了
222017-08-22
相似问题