React 代理

47 阅读1分钟

React中用的代理包http-proxy-middleware在各个版本中使用方式不同

在@1版本中

const proxy = require('http-proxy-middleware');
const resProxy = proxy(
  "/res",
  {
    target: "http://www.zhangdaxu.cn",
    changeOrigin: true,
  }
);
module.exports = function (app) {
  app.use(resProxy);
}

在@2版本中

const { createProxyMiddleware } = require('http-proxy-middleware');
const resProxy = createProxyMiddleware(
  "/res",
  {
    target: "http://www.zhangdaxu.cn",
    changeOrigin: true,
  }
);
module.exports = function (app) {
  app.use(resProxy);
}

在使用代理时要注意版本问题 npm i http-proxy-middleware@2