看看AWS Node.js SDK - 它可以访问所有AWS服务端点。
var sns = new AWS.SNS();
// subscribe
sns.subscribe({topic: "topic", Protocol: "https"}, function (err, data) {
if (err) {
console.log(err); // an error occurred
} else {
console.log(data); // successful response - the body should be in the data
}
});
// publish example
sns.publish({topic: "topic", message: "my message"}, function (err, data) {
if (err) {
console.log(err); // an error occurred
} else {
console.log(data); // successful response - the body should be in the data
}
});
编辑:问题是标准身材分析器不处理纯/文这就是SNS发送的内容类型。这里是提取原始身体的代码。你的身体解析器之前将其放置:
app.use(function(req, res, next) {
var d= '';
req.setEncoding('utf8');
req.on('d', function(chunk) {
d+= chunk;
});
req.on('end', function() {
req.rawBody = d;
next();
});
});
然后,您可以使用:
JSON.stringify(req.rawBody));
路线内创建一个JavaScript对象,并在SNS运营后适当。
您也可以修改body parser来处理text/plain,但修改中间件不是一个好主意。只需使用上面的代码。
作为上下文:这种(合理的,天真的)方法失败的原因是SNS HTTP设置。 “Content-Type:text/plain; charset = UTF-8” 请参阅:http://docs.aws.amazon.com/sns/latest/dg/SendMessageToHttp.html (SNS并不真正讨好JSON) – Jason