React create-react-app跨域解决方案

方案一:配置proxy

  1. 弹射出react详细配置 yarn eject
  2. 修改config\webpackDevServer.config.js
proxy: {
      "/api": {
        target: "http://localhost:3000",
        changeOrigin: true,
        // pathRewrite: {
        //   "^/api": ""
        // }
      }
    },

方案二:http-proxy-middleware

  1. yarn add http-proxy-middleware -S
  2. 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": ""
      // }
    })
  )
}

你可能感兴趣的:(vue,java,react,php,js)