使用Amazon SNS和Meteor.js
我在parsingAmazon SNS HTTP POST正文数据时遇到问题。 我正在使用Iron Router插件来运行HTTP端点。
问题是Iron Router依赖于连接npm模块,它只分析具有以下内容types的请求:
application/json application/x-www-form-urlencoded multipart/form-data
Amazon SNS发送所有以text / plain编码的数据,因此需要自定义中间件来parsing主体,如下所述: Express 3(通过连接)处理文本/纯文本? 。
我怎样才能使这个解决scheme适应meteor或铁路路由器?
如果有人仍然在为此而苦苦挣扎,现在通过使用以下方法可以更容易地解决问题:
onBeforeAction: Iron.Router.bodyParser.text()
我通过meteor访问连接处理程序来解决这个问题。 代码如下:
var connectHandlers; if (typeof __meteor_bootstrap__.app !== 'undefined') { connectHandlers = __meteor_bootstrap__.app; } else { connectHandlers = WebApp.connectHandlers; } connectHandlers.use((function(req, res, next) { if (req.headers['content-type'] === 'text/plain; charset=UTF-8') { var bodyarr = []; req.on('data', function(chunk) { bodyarr.push(chunk); }); req.on('end', function() { req.body = bodyarr.join(''); next(); }); } else { next(); } }));
这应该能够接受任何types的连接中间件,而不仅仅是一个文本/平原。