Webpack Dev Server 代理 API
我们的开发项目一般运行localhost:8080 但后端的接口部署在同源地址下,生产环境没问题,但是本地就不行了
不是任何情况后端都支持跨域
proxy是一个对象,每个属性都是代理规则
上面的changeOrigin,可以看看http
此时就被代理到了 git的用户接口了
devServer: {
contentBase:'./public',
proxy: {
'/api': {
// http://localhost:8080/api/users -> https://api.github.com/api/users
target: 'https://api.github.com',
// http://localhost:8080/api/users -> https://api.github.com/users
pathRewrite: {
'^/api': ''
},
// 不能使用 localhost:8080 作为请求 GitHub 的主机名
changeOrigin: true
}
}
},