1.下载安装代理模块
npm install http-proxy-middleware --save
2.在src目录中新建setupProxy.js文件,在文件中放入如下代码(注意是src目录下):
const { createProxyMiddleware } = require('http-proxy-middleware')
module.exports = function (app) {
app.use(createProxyMiddleware('/api',
{
target: 'http://127.0.0.1:3000',
changeOrigin: true,
pathRewrite: {
"^/api": ""
}
})
)
}
3.使用
import axios from 'axios'
async componentDidMount() {
const res = await axios.get('/api/RoomApi/live')
}