[英]how to properly access AWS Api Gateway from Node Web application
I'm experiencing a wired issue, where the same exact code (which accesses AWS Api Gateway) runs just fine if submitted this way: node app.js, but produces 307 Redirect, when submitted from within Node/Express app. 我遇到了一个有线问题,如果以这种方式提交,则相同的确切代码(访问AWS Api Gateway)可以很好地运行:node app.js,但是当从Node / Express应用程序中提交时会产生307 Redirect。
Here is the "stand-alone" single file node program (app.js) : 这是“独立”单文件节点程序(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();
And here is the same code, as a part of a Node/Express Web app: 这是相同的代码,作为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();
});
};
The first one works fine when submitted: node app.js The second one returns: 第一个提交时工作正常: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>
You need to change the code http = require('http')
to http = require('https')
inside your express app. 您需要在快速应用中将代码http = require('http')
更改为http = require('https')
。
Also, take a look at request , it is an HTTP
client with support for HTTPS
and also has lots of other features built in. 另外,看看request ,它是一个支持HTTPS
的HTTP
客户端,并且还内置了许多其他功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.