简体   繁体   中英

Using Amazon SNS with Meteor.js

I'm having trouble parsing Amazon SNS HTTP POST body data. I'm using the Iron Router plugin to run an HTTP endpoint.

The problem is Iron Router depends on the Connect npm module, which only parses requests with the following Content-Types:

application/json application/x-www-form-urlencoded multipart/form-data

Amazon SNS sends all of it's data encoded in text/plain, so custom middleware is needed to parse the body, as documented here: Handling text/plain in Express 3 (via connect)? .

How can I adapt this solution to Meteor or Iron Router?

I solved this by accessing the connect handlers through meteor. Here's the code:

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();
  }
}));

This should be able to accept any kind of connect middleware, not just one for text/plain.

如果有人仍在努力解决这个问题,现在使用以下方法解决问题要容易得多:

onBeforeAction: Iron.Router.bodyParser.text()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM