[英]Express proxy + axios + cors: still cors issue
I've created the following proxy with express:我用 express 创建了以下代理:
import express from "express";
import cors from "cors";
import proxy from "express-http-proxy";
const app = express();
app.use(
cors({
origin: "http://localhost:8081",
credentials: true,
})
);
app.use("/", proxy("http://my-website:8810"));
app.listen(3000, () => {
console.log("server listening on port 3000");
});
From the frontend I'm using axios:从前端我使用 axios:
axios.defaults.withCredentials = true;
const res = await axios.get("http://localhost:3000", {
auth: {
username: "xxxxx",
password: "xxxxx",
},
headers: {
"Content-Type": "application/json",
},
});
But I still have the following cors issue:但我仍然有以下 cors 问题:
Access to XMLHttpRequest at ' http://my-website:8810 ' (redirected from ' http://localhost:3000/ ') from origin ' http://localhost:8081 ' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Access to XMLHttpRequest at ' http://my-website:8810 ' (redirected from ' http://localhost:3000/ ') from origin ' http://localhost:8081 ' has been blocked by CORS policy: Response to preflight请求未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”header。
And after many hours facing this issue... here I am.在面对这个问题数小时后……我来了。 Could anyone explain to me what I'm doing wrong here?谁能向我解释我在这里做错了什么?
Finally it works by using "request", something like:最后它通过使用“请求”来工作,例如:
app.use(
cors({
origin: "http://localhost:8081",
credentials: true,
})
);
router.get("/", (req, res) => {
request(
"http://my-website:8810",
{
auth: {
username: "node_proxy",
password: "password",
},
headers: { Accept: "application/json" },
},
(error, response, body) => {
res.send(body);
}
);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.