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)
  })
 
 
posted @ 2020-04-30 09:50  凹润之之之  阅读(602)  评论(0编辑  收藏  举报