Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 快递代理+;axios+;cors:仍然是cors问题_Node.js_Express_Proxy_Cors - Fatal编程技术网

Node.js 快递代理+;axios+;cors:仍然是cors问题

Node.js 快递代理+;axios+;cors:仍然是cors问题,node.js,express,proxy,cors,Node.js,Express,Proxy,Cors,我已使用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("

我已使用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");
});
从前端开始,我使用axios:

axios.defaults.withCredentials = true;
const res = await axios.get("http://localhost:3000", {
    auth: {
      username: "xxxxx",
      password: "xxxxx",
    },
    headers: {
      "Content-Type": "application/json",
    },
});
但我仍然有以下cors问题:

在“”处访问XMLHttpRequest(从 “来源”已被删除 被CORS策略阻止:对飞行前请求的响应未通过 访问控制检查:未显示“访问控制允许原点”标题 在请求的资源上显示

在面对这个问题很多小时之后。。。我来了。 有人能解释一下我做错了什么吗?

最后,它通过使用“请求”来工作,类似于:

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);
    }
  );
});