[英]how to properly access AWS Api Gateway from Node Web application
我遇到了一個有線問題,如果以這種方式提交,則相同的確切代碼(訪問AWS Api Gateway)可以很好地運行:node app.js,但是當從Node / Express應用程序中提交時會產生307 Redirect。
這是“獨立”單文件節點程序(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>
您需要在快速應用中將代碼http = require('http')
更改為http = require('https')
。
另外,看看request ,它是一個支持HTTPS
的HTTP
客戶端,並且還內置了許多其他功能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.