![](/img/trans.png)
[英]How do I return a Base64 encoded zip file through AWS api-gateway for download
[英]CORS error: even i enable it at API-Gateway and in my index.js file when uploading to Lamda function AWS
我希望使用Lamda function
部署backend -login functionality
我已經部署了frontend(S3-bucket)
和backend(RDS-mysql)
。 當我提交帶有登錄詳細信息的表單時出現錯誤,我還在 AWS API gateway
中啟用了 CORS。 我對 AWS 有點陌生,所以我不確定我是否遺漏了什么。 任何建議請在這里。
Error in console
- index.js
我有以下代碼在后端代碼中啟用CORS
app.use(
cors({
credentials: true, // for cookies
origin: "*",
optionsSuccessStatus: 200,
})
);
- API-Gateway AWS
我也在 AWS 中啟用了 CORS
- 我上傳到Lamda function
Snippet of code from backend index.js
,這是第一次,所以我不確定我是否在下面編寫了正確的代碼( before tweaking in below i used it locally and it worked fine the code)
// bring in express
const express = require("express");
const mysqlx = require("mysql");
const jwt = require("jsonwebtoken");
const auth = require("./verifyTokenExisting");
const authNew = require("./verifyTokenNew");
const cors = require("cors"); // also using cors library here
const cookieParser = require("cookie-parser");
const pdf = require("html-pdf");
const pdfTemplate = require("./documents/pdfTemplate");
const fs = require("fs");
const { isContext } = require("vm");
const app = express();
app.use(express.json());
app.use(cookieParser());
app.use(
cors({
credentials: true, // for cookies
origin: "*",
optionsSuccessStatus: 200,
})
);
// mysql connection
let connection = mysqlx.createConnection({
host: "database-x.xxxxxxxxxxx.eu-west-2.rds.amazonaws.com",
user: "xxxx",
password: "xxxxxxxxxxxx",
database: "join_us",
insecureAuth: true,
});
////! LOGIN & LOGOUT
exports.handler = (event, contect, callback) => {
Context.callbackWaitsForEmptyEventLoop = false;
//app.post("/api/newuser", (req, res) => {
let x1 = event.body;
console.log("144", x1);
if (event.body.logout === false) {
connection.query("SELECT * FROM users WHERE email=?;", [x1.email], function (err, results) {
if ((results[0].password == x1.password && results[0].userloginStatus == false) || (results[0].password == x1.password && results[0].userloginStatus == null)) {
const payload = { email: results[0].email };
// generate new token
const token = jwt.sign(payload, "lllfasdgfdadsfasdfdasfcadsf");
//below are the cookies sent to user first time when he log in
callback(
null,
cookie("yogaoutlet_access_token", token, {
maxAge: 25 * 24 * 60 * 60 * 1000,
httpOnly: true, // it will enable on frotend-javascript to not have access to cokkies
})
);
} else callback(null, redirect("http://localhost:3000/about"));
}
}
// if event.body.logout === true then logout the user
else {
const payload = { email: event.body.email };
console.log("339x", payload);
const token = jwt.sign(payload, "lllfasdgfdadsfasdfdasfcadsf");
callback(null, clearCookie("yogaoutlet_access_token"));
}
//});
};
您在 API 網關中設置的 CORs(來自您的屏幕截圖)僅適用於 OPTIONS 方法。 您還需要在 GET、POST、PUT、DELETE 方法中返回“Access-Control-Allow-Origin”。 此外,您絕對不能使用通配符“*”並將憑據設置為 true。 https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin
如果您想通過代碼返回 CORS,您需要將方法設置為“使用 Lambda 代理集成”。 否則你需要 map HTTP Header 到 stageVariable(通常)例如 stageVariable.ORIGIN。 我想您可以將 map 轉換為請求中的值,但我認為將 map 轉換為 StageVariable 更安全,因此只有您已知的站點可以調用您的 api。
關於 Lambda 代理集成的另一件事需要注意,您的代碼必須處理整個響應。 我在 Python 中執行我的 lambda,我的返回值如下所示:
return {
'isBase64Encoded': 'false',
'statusCode': status_code,
'headers': {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': origin,
'Access-Control-Allow-Headers': 'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token',
'Access-Control-Allow-Methods': 'GET,OPTIONS',
},
'body': json.dumps(resp)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.