create-react-app webpack反向代理
1、npm install http-proxy-middleware --save
#or
yarn add http-proxy-middleware
2、创建src/setupProxy.js
const { createProxyMiddleware } = require('http-proxy-middleware');
module.exports = function(app) {
app.use('/api', createProxyMiddleware({
target: 'http://www.baidu.com',
changeOrigin: true,
}));
};
3、组件调用
axios.post('/api/xxxxx', {id: 'xxxx'}).then((res) => {
console.log(res)
})