webpack 4+ create-react-app如何配置请求跨域
一、安装http-proxy-middleware管理包,npm i --save http-proxy-middleware
二、在项目目录src/下新建setupProxy.js文件,然后写入如下代码:
const proxy = require(‘http-proxy-middleware‘);
module.exports = function(app) {
app.use(proxy(‘/api‘, {
target: ‘http://localhost:8090‘ ,
secure: false,
changeOrigin: true,
pathRewrite: {
"^/api": "/"
},
// cookieDomainRewrite: "http://localhost:3000"
}));
};
三、然后设置axios默认前缀 axios.defaults.baseURL = ‘/api’;
发送axios请求
axios('/getDailyAims').then(res => {
console.log(res);
})

本文介绍如何在webpack4和create-react-app环境下配置跨域请求。通过安装http-proxy-middleware管理包,创建并编辑setupProxy.js文件,设置axios默认前缀,实现本地开发环境下的跨域请求。

1277

被折叠的 条评论
为什么被折叠?



