React
react 16 版本之后的代理
// 单个代理// "proxy": "https://yapi.baidu.com/mock/44743"// 16+ 之后不允许这个写// proxy -> 'string' -> 'object'// "proxy": {// '/api': {// target: "https://yapi.baidu.com/mock/44743"// },// '/aps': {// target: "https://yapi.baidu.com/mock/44743"// }// }// 1. 去 src 目录 新建一个 setuoProxy.js// 2. 下载 http-proxy-middleware, 分为两个版本 1.0 | 2.0 // 2.1 const proxy = require('http-proxy-middleware'); // 1.0 // 2.2 const { createProxyMiddleware } = require('http-proxy-middleware');// 3. setupProxy// 引入 proxyconst { createProxyMiddleware } = require('http-proxy-middleware');// 抛出一个函数module.exports = function(app) { // 使用 app.use( // createProxyMiddleware 插件 createProxyMiddleware( '/api', // 代理之后的名称 { target: 'https://yapi.baidu.com/mock/44743', // 代理的地址 secure: false, // 安全 changeOrigin: true, // 是否设置跨域 pathRewrite: { // 替换 "^/api": "" }, } ), );};## 代理多个// 抛出一个函数module.exports = function(app) { // 使用 app.use( // createProxyMiddleware 插件 createProxyMiddleware( '/api', // 代理之后的名称 { target: 'https://yapi.baidu.com/mock/44743', // 代理的地址 secure: false, // 安全 changeOrigin: true, // 是否设置跨域 pathRewrite: { // 替换 "^/api": "" }, } ), createProxyMiddleware( '/aps', // 代理之后的名称 { target: 'https://yapi.baidu.com/mock/44757/banner/to', // 代理的地址 secure: false, // 安全 changeOrigin: true, // 是否设置跨域 pathRewrite: { // 替换 "^/aps": "" }, } ), );};