proxyTable是解决开发环境中的跨域问题,正式环境的跨域需要使用nginx反向代理或者是后端解决
proxyTable: {
'/api': {
target: 'https://cnodejs.org',
changeOrigin: true,
secure: false, //target默认情况下,不接受运行在HTTPS上,且使用了无效证书的后端服务器。如果你想要接受, 则需设置该项为false
pathRewrite: {
'^/api': ''
}
}
}
- '/api' = target
- '^/api': '' 的作用是对/api改变其path,至于前面的^符号,是属于正则判断
// rewrite path
pathRewrite: {'^/old/api' : '/new/api'}
// remove path
pathRewrite: {'^/remove/api' : ''}
// add base path
pathRewrite: {'^/' : '/basepath/'}
// custom rewriting
pathRewrite: function (path, req) { return path.replace('/api', '/base/api') }
举例:对于接口 https://cnodejs.org/api/v1/topics
axios.get('/api/v1/topics')
.then((res)=> {console.log(res)})
.catch((err) => {console.log(err)})
报404
proxyTable: {
'/api': {
target: 'https://cnodejs.org',
secure: false,
changeOrigin: true,
pathRewrite: {
'^/api': ' '
}
}
}
proxyTable: {
'/api': {
target: 'https://cnodejs.org',
secure: false,
}
}
网友评论