由于本地的node是10.16.3
使用npm install http-proxy-middleware --save
的时候自动安装的是最新版本2.0,出现不兼容,需要最大版本是>=12
因此使用
npm install http-proxy-middleware@1.x.1 --save
就安装成功了,下面是配置属于多路配置
const {createProxyMiddleware} = require('http-proxy-middleware');
module.exports = function(app) {
app.use(
['/api1','/api2'], createProxyMiddleware({
target: 'http://localhost',
changeOrigin: true,
pathRewrite: {
'^/api1': '',
'^/api2': '',
},
router: {
'localhost:3000/api1':'http://localhost:5000',
'localhost:3000/api2': 'http://localhost:5001'
}
}),
)
}
备注:
- 如果本地安装了yarn
可以使用yarn add http-proxy-middleware --save
- 如果配置过程中遇到: proxy is not function