create-react-app设置代理

Others 2019-04-25 02:36:00 2019-04-25 02:36:00 2641 次浏览

在src目录新建setupProxy.js文件

v1:


const proxy = require('http-proxy-middleware');

module.exports = function(app) { app.use(proxy('/api', { target: 'http://localhost:4000/',changeOrigin:true, pathRewrite:{ "^/api":"/" } })); };

v2:
// const proxy = require("http-proxy-middleware");
const { createProxyMiddleware } = require("http-proxy-middleware");
module.exports = function (app) {
  app.use(
    "/api",
    createProxyMiddleware({
      target: "http://127.0.0.1:9000",
      changeOrigin: true,
    })
  );
};