node.js 使用SQS需要功能示例

node.js using SQS need functional example

var params = {
    QueueUrl: sqsEndPoint + compInfoQueName,
    AttributeNames: [All],
    MaxNumberOfMessages: '5',
    MessageAttributeNames: [],
    ReceiveRequestAttemptId: "",
    VisibilityTimeout: '15',
    WaitTimeSeconds: '10'
};

sqs.receiveMessage(params, function (err, data) {
    if (err) console.log(err, err.stack);
    else return data;
});

从 AWS 网站获取

我设置了所有参数,并且可以使用以下方法在 html 页面上获得响应:

https://sqs.us-west-2.amazonaws.com/ACCOUNT/QUEUENAME";

但我想将它整合到一个 node.js 项目中

我是 javascript 的新手,在 java 我只是导入 AWS 对象并在函数中调用一个方法,即

函数... {json 响应 = sqs.receive... }

如何在JS中使用顶部的代码?

如何使用代码示例将字符串对象设置为接收结果?

我只想在一个html页面中设置一个字段到接收到的sqs消息

textarea.value = myMessageFromAWS

由于您来自 Java,回调不是很直观。

我建议您使用 async/await 方法,这样代码看起来更线性,如下所示:

async function main () {
  var params = {
    QueueUrl: sqsEndPoint + compInfoQueName,
    AttributeNames: [All],
    MaxNumberOfMessages: '5',
    MessageAttributeNames: [],
    ReceiveRequestAttemptId: "",
    VisibilityTimeout: '15',
    WaitTimeSeconds: '10'
  };

  try {
    // this will be your data received from sqs
    const data = await sqs.receiveMessage(params).promise()
    // prints to stdout
    console.log(data)

  } catch(err) {
    // prints to stderr
    console.error(err, err.stack);
  };
}

// Because this is not Java you have to call this function explicitly
main()