我有一个调用 node-express API 的反应应用程序,它代理到不同主机上的另一台服务器。
它给了我 http 302
我的反应应用程序在https://localhost:3000 我的 node-express api 在http://localhost:8080
反应应用程序中的代码:
React.useEffect(
() => {
fetch("http://localhost:8080/cloudshell/api/v1/UserProductsInfo",)
.then(function(res) {
console.log(res);
})
.catch(function() {
console.log("error");
});
},
[]
);
快递应用程序中的代码:
const express = require("express");
const app = express();
const proxy = require("http-proxy-middleware");
const port = process.env.PORT || 8080;
const IICS_POD = "https://qa-pod1.com";
app.use(function(req, res, next) {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Methods",
"GET, POST, OPTIONS, PUT, DELETE"
);
res.setHeader(
"Access-Control-Allow-Headers",
"Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With"
);
next();
});
app.use(
proxy("/**/api/**", {
target: IICS_POD,
secure: false,
changeOrigin: true,
cookiePathRewrite: "/",
cookieDomainRewrite: "",
logLevel: "debug",
onProxyRes: function(proxyRes, req, res) {
proxyRes.headers["Access-Control-Allow-Origin"] = "*";
}
})
);
//error handling
app.use(function(req, res, next) {
res.status(404).send("Sorry can't find that!");
});
app.listen(port, () => console.log(`server is running on port ${port}!`));