gre*_*inn 2 node.js amazon-sns express
我正在尝试设置对 SNS 主题的订阅,我已在 SNS UI 中添加了 HTTP 订阅,并将其标记为“待确认”。我已经设置了端点来查找正确的标头(如下所示)并且正在工作。我的问题是端点永远不会获得“令牌”,请参见下面的我的代码和日志的输出。
NodeJS(w/express 4.15)测试代码:
router.post('/', (req, res, next) => {
    console.log('-----------------')
    console.log(req.body)
    console.log(req.headers)
    console.log('-----------------')
    if (req.get('x-amz-sns-message-type') == "SubscriptionConfirmation") {
        SNS.confirmSubscription({ Token: req.body.Token, TopicArn: req.get('x-amz-sns-topic-arn') }, (err, finished) => {
            if (err) console.log(err)
        })
    } else {
        console.log(req.body)
    }
})
来自 AWS 的 POST 请求
-----------------
{}
{ host: 'myapp.us-west-2.elasticbeanstalk.com',
  'x-real-ip': '172.XXX.40.XXX',
  'x-forwarded-for': '54.240.XXX.XXX, 172.XXX.40.XXX',
  'content-length': '1604',
  'accept-encoding': 'gzip,deflate',
  'content-type': 'text/plain; charset=UTF-8',
  'user-agent': 'Amazon Simple Notification Service Agent',
  'x-amz-sns-message-id': '493b2a00-a99f-4cf3-ac9a-aad8ad6ee115',
  'x-amz-sns-message-type': 'SubscriptionConfirmation',
  'x-amz-sns-topic-arn': 'arn:aws:sns:us-west-2:4868XXXXXXXX:mysnstopic',
  'x-forwarded-port': '80',
  'x-forwarded-proto': 'http' }
-----------------
{ InvalidParameter: Invalid token
    at Request.extractError (/var/app/current/node_modules/aws-sdk/lib/protocol/query.js:47:29)
    at Request.callListeners (/var/app/current/node_modules/aws-sdk/lib/sequential_executor.js:105:20)
    at Request.emit (/var/app/current/node_modules/aws-sdk/lib/sequential_executor.js:77:10)
    at Request.emit (/var/app/current/node_modules/aws-sdk/lib/request.js:683:14)
    at Request.transition (/var/app/current/node_modules/aws-sdk/lib/request.js:22:10)
    at AcceptorStateMachine.runTo (/var/app/current/node_modules/aws-sdk/lib/state_machine.js:14:12)
    at /var/app/current/node_modules/aws-sdk/lib/state_machine.js:26:10
    at Request.<anonymous> (/var/app/current/node_modules/aws-sdk/lib/request.js:38:9)
    at Request.<anonymous> (/var/app/current/node_modules/aws-sdk/lib/request.js:685:12)
    at Request.callListeners (/var/app/current/node_modules/aws-sdk/lib/sequential_executor.js:115:18)
  message: 'Invalid token',
  code: 'InvalidParameter',
  time: 2018-08-13T17:32:15.516Z,
  requestId: '6945a62f-c746-5785-8497-f5c4059943ab',
  statusCode: 400,
  retryable: false,
  retryDelay: 50.366310913093 }
在上面,您将看到它req.body是空的,并且设置了标题,但不Token包含任何内容。有人可以指出我做错了什么吗?
您必须明确通知express为此端点使用文本解析。
尝试这个:
    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')
    }
编辑:从express v4.17.0开始,您可以使用express.text()中间件本身,而不是bodyParser.text()。看看这个官方文档
注意:我没有使用app.use,因为这会影响我的所有其他端点。
| 归档时间: | 
 | 
| 查看次数: | 2983 次 | 
| 最近记录: |