自己在写课程作业中的遇到的跨域问题 原打算用package.json中写代理服务器的方式解决 但好像react-scripts版本过高会失去效果,所以采用以下的方式进行前端跨域配置
安装插件
npm install http-proxy-middleware
编写setUpProxy.js
const { createProxyMiddleware } = require("http-proxy-middleware")
module.exports = function (app) {
app.use(
createProxyMiddleware("/api",{
target: "http://localhost:xxxx", //配置转发目标地址(能返回数据的服务器地址)
changeOrigin: true, //控制服务器接收到的请求头中host字段的值
pathRewrite: { "^/api": "" },
})
)
}