• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

React 使用 Proxy 代理create-react-app

武飞扬头像
深沉影子灰
帮助24

在create-react-app 中配置proxy代理

proxy,默认为NULL,类型为URL,一个为了发送http请求的代理 在平时开发时,尤其前后端分离时,需要假数据来进行模拟请求,这个时候就需要proxy代理来处理

create-react-app < 2.0

package.json 中配置

  1.  
    "proxy":{
  2.  
    "/api/**":{
  3.  
    "target":"https://easymock.spiritling.pub/",
  4.  
    "changeOrigin": true
  5.  
    }
  6.  
    }

create-react-app > 2.0

package.json 中配置(不推荐)

"proxy": "https://easymock.spiritling.pub/",

配置文件 /src/setupProxy.js (推荐)

create-react-app 解包后,可以在 config 文件夹下找到配置

config/path.js 中存在 proxySetup: resolveApp('src/setupProxy.js'),

proxySetup 是只在 webpackDevServer.config.js 文件中使用,也就是说只在开发时使用

所以,可以在 /src/setupProxy.js 中配置

首先安装 http-proxy-middleware

npm install http-proxy-middleware -D

然后文件配置

  1.  
    const proxy = require('http-proxy-middleware');
  2.  
    module.exports = function (app) {
  3.  
    app.use(
  4.  
    '/api/v1/',
  5.  
    proxy({
  6.  
    target : 'https://easymock.spiritling.pub/',
  7.  
    changeOrigin : true, // 设置跨域请求
  8.  
    PathRewrite : {
  9.  
    '^/api/v1' : '' ///api/v1 变为 ''
  10.  
    }
  11.  
    })
  12.  
    );
  13.  
    };

使用例子

01

  1.  
    const proxy = require('http-proxy-middleware');
  2.  
    module.exports = function (app) {
  3.  
    app.use(
  4.  
    '/api/v1/',
  5.  
    proxy({
  6.  
    target : 'https://easymock.spiritling.pub/',
  7.  
    changeOrigin : true
  8.  
    })
  9.  
    );
  10.  
    };

游览器中请求为 https://example.com/api/v1/login

则经过代理后可以转为 https://easymock.spiritling.pub/api/v1/login

02

  1.  
    const proxy = require('http-proxy-middleware');
  2.  
    module.exports = function (app) {
  3.  
    app.use(
  4.  
    '/api/v1/',
  5.  
    proxy({
  6.  
    target : 'https://easymock.spiritling.pub/',
  7.  
    changeOrigin : true,
  8.  
    PathRewrite : {
  9.  
    '^/api/v1' : ''
  10.  
    }
  11.  
    })
  12.  
    );
  13.  
    };

游览器中请求为 https://example.com/api/v1/login

则经过代理后可以转为 https://easymock.spiritling.pub/login

create-react-app官方-Proxying API Requests in Development

http-proxy-middleware 新版本 ≧ 1.0.0

在新版本大于等于 1.0.0 时使用会出现如下问题

proxy is not a function

也就是说 http-proxy-middleware 或者 setupProxy 会出现报错 proxy is not a function

所以需要使用新版本的写法才可以

  1.  
    const { createProxyMiddleware } = require('http-proxy-middleware');
  2.  
     
  3.  
    module.exports = function (app) {
  4.  
    app.use(createProxyMiddleware('/api/v1', {
  5.  
    target : 'https://easymock.spiritling.pub',
  6.  
    changeOrigin : true,
  7.  
    ws: true,
  8.  
    pathRewrite : {
  9.  
    '^/api/v1' : ''
  10.  
    },
  11.  
    }));
  12.  
    };

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhciabgf
系列文章
更多 icon
同类精品
更多 icon
继续加载