1

我遇到了一個有線問題,如果以這種方式提交,那麼相同的確切代碼(訪問AWS Api Gateway)運行得很好:node app.js,但產生307從Node/Express應用程序提交時,重定向。如何從節點Web應用程序正確訪問AWS Api網關

這裏是「獨立」的單個文件節點程序(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(); 

這裏是相同的代碼,作爲節點/快速的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(); 
    }); 

}; 

第一個正常工作:節點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

您需要更改代碼'HTTP =需要(「HTTP」)'到'http = require('https')'在快速應用程序內。 –

+0

這個效果很好 - 謝謝。如果您將此作爲解決方案發布,我很樂意接受。 –

+0

很高興工作。添加了我的答案! –

回答

0

你的東東d在快速應用程序內將代碼http = require('http')更改爲http = require('https')

而且,看看request,它是一個HTTP客戶端與HTTPS支持,也有很多內置的其他功能。

相關問題