如何在HTTP端点上确认订阅并获取SNS通知? [英] How to confirm subscribe and get SNS Notifications at a HTTP endpoint?
问题描述
我正在使用Amazon Elastic Beanstalk运行nodejs网页.我只想从AWS SNS向该网页发送通知并实时捕获它们.因此,当我发布到HTTP端点时,什么也没有发生,而且我也不知道如何获取通知.
I'm using Amazon Elastic Beanstalk to run a nodejs web page. I just want to send notifications to this webpage from AWS SNS and catch them in real time. So, when i publish to the HTTP endpoint, nothing happens and i don't know how to get the notification.
作为Http终端节点,我设置了我的AWS Elastic-Beanstalk http地址.
As Http endpoint, i set my AWS Elastic-Beanstalk http address.
我正在阅读Amazon文档,但无处可找到如何在http端点一次捕获sns消息.
I'm reading the Amazon docs but nowhere i can find how to catch the sns message once at the http endpoint.
拜托,任何帮助将不胜感激.谢谢.
Please, any help will be very appreciated . Thanks.
推荐答案
尝试一下:
const express = require('express');
const router = express.Router();
const request = require('request');
var bodyParser = require('body-parser')
router.post('/',bodyParser.text(),handleSNSMessage);
module.exports = router;
var handleSubscriptionResponse = function (error, response) {
if (!error && response.statusCode == 200) {
console.log('Yess! We have accepted the confirmation from AWS');
}
else {
throw new Error(`Unable to subscribe to given URL`);
//console.error(error)
}
}
async function handleSNSMessage(req, resp, next) {
try {
let payloadStr = req.body
payload = JSON.parse(payloadStr)
console.log(JSON.stringify(payload))
if (req.header('x-amz-sns-message-type') === 'SubscriptionConfirmation') {
const url = payload.SubscribeURL;
await request(url, handleSubscriptionResponse)
} else if (req.header('x-amz-sns-message-type') === 'Notification') {
console.log(payload)
//process data here
} else {
throw new Error(`Invalid message type ${payload.Type}`);
}
} catch (err) {
console.error(err)
resp.status(500).send('Oops')
}
resp.send('Ok')
}
注意:我没有使用app.use
,因为这会影响我的所有其他端点.
Note: I didn't use app.use
as that will impact all my other endpoints.
这篇关于如何在HTTP端点上确认订阅并获取SNS通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!