方案一:配置proxy
- 弹射出react详细配置
yarn eject
- 修改
config\webpackDevServer.config.js
proxy: {
"/api": {
target: "http://localhost:3000",
changeOrigin: true,
// pathRewrite: {
// "^/api": ""
// }
}
},
方案二:http-proxy-middleware
- yarn add http-proxy-middleware -S
- src目录下新建文件setupProxy.js
const { createProxyMiddleware } = require("http-proxy-middleware");
module.exports = function (app) {
app.use(
createProxyMiddleware("/api", {
target: "http://localhost:3000",
changeOrigin: true,
// pathRewrite: {
// "^/api": ""
// }
})
)
}
React create-react-app跨域解决方案