以react为例配置代理Proxy代理,react首先需要创建一个代理文件setupProxy.js
//引入中间键
const proxy = require('http-proxy-middleware')
module.export = function (app){
app.use(
proxy('/api1', {//碰到/api1开始的请求,就会触发代理
target: 'http://localhost:5000',//请求谁
changeOrigin: true,//控制服务器收到的请求头中Host的值
pathRewrite: {'^/api1': ''}//重新请求路径,不加此配置的话,http://localhost:5000/api1,这个地址明显不是我们要的,我们要的是这个 http://localhost:5000
}),
proxy('/api2', {
target: 'http://localhost:6000',
changeOrigin: true,
pathRewrite: {'^/api2': ''}
})
)
}