如何从Node Web应用程序正确访问AWS Api Gateway

时间:2017-06-05 18:25:06

标签: node.js amazon-web-services amazon-cloudfront aws-api-gateway

我遇到了有线问题,如果以这种方式提交,相同的确切代码(访问AWS Api Gateway)运行得很好:node app.js,但是当从Node / Express中提交时生成307重定向应用

这是"独立"单文件节点程序(app.js):

var http = require("https");

var options = {
      "method": "POST",
        "hostname": "ipzjnsvxnd.execute-api.us-west-2.amazonaws.com",
          "port": null,
            "path": "/DEV/execution",
              "headers": {
                      "cache-control": "no-cache",
                          "postman-token": "b929e970-fe22-0f4f-e659-117890fda955"
                                }
};

var req = http.request(options, function (res) {
      var chunks = [];

        res.on("data", function (chunk) {
                chunks.push(chunk);
                  });

          res.on("end", function () {
                  var body = Buffer.concat(chunks);
                      console.log(body.toString());
                        });
});

req.write("{\n  \"input\": \"{ \\\"account_key\\\": \\\"9990\\\", \\\"acc1\\\": \\\"1235813\\\", \\\"acc2\\\": \\\"13711\\\",\\\"amount\\\": \\\"1000.00\\\", \\\"city\\\": \\\"BrandonTown\\\" }\",\n  \"name\": \"RequiredUniqueValueGoesHere19090\",\n  \"stateMachineArn\": \"arn:aws:states:us-west-2:217465658899:stateMachine:FICO_StateMachine3\"\n}");
req.end();

以下是相同的代码,作为Node / Express Web应用程序的一部分:

module.exports = function(app) {

    var querystring = require('querystring');
    var http = require('http');
    http.post = require('http-post');

    app.get('*', function(req, res) {
        res.sendfile('./public/index.html');
    });

    app.post("/customerinfo", function(req, res) {

        var options = {
            "method": "POST",
            "hostname": "ipzjnsvxnd.execute-api.us-west-2.amazonaws.com",
            "path": "/DEV/execution",
            "headers": {
                "cache-control": "no-cache",
                "postman-token": "b929e970-fe22-0f4f-e659-117890fda955"
            }
        };

          var req1 = http.request(options, function (res1) {
            var chunks = [];

            res1.on("data", function (chunk) {
                chunks.push(chunk);
            });

            res1.on("end", function () {
                var body = Buffer.concat(chunks);
                console.log(body.toString());
            });
        });

        req1.write("{\n  \"input\": \"{ \\\"account_key\\\": \\\"9990\\\", \\\"acc1\\\": \\\"1235813\\\", \\\"acc2\\\": \\\"13711\\\",\\\"amount\\\": \\\"1000.00\\\", \\\"city\\\": \\\"BrandonTown\\\" }\",\n  \"name\": \"RequiredUniqueValueGoesHere1239091\",\n  \"stateMachineArn\": \"arn:aws:states:us-west-2:217465658899:stateMachine:FICO_StateMachine3\"\n}");
        req1.end();
    });

};

第一个在提交时工作正常:node app.js 第二个回归:

<html>
<head><title>307 Temporary Redirect</title></head>
<body bgcolor="white">
<center><h1>307 Temporary Redirect</h1></center>
<hr><center>CloudFront</center>
</body>
</html>

<html>
<head><title>307 Temporary Redirect</title></head>
<body bgcolor="white">
<center><h1>307 Temporary Redirect</h1></center>
<hr><center>CloudFront</center>
</body>
</html>

<html>
<head><title>307 Temporary Redirect</title></head>

1 个答案:

答案 0 :(得分:0)

您需要在快递应用中将代码http = require('http')更改为http = require('https')

此外,请查看request,它是HTTP客户端,支持HTTPS,并且还内置了许多其他功能。