javascript - vue-cli proxyTable怎么配置
問題描述
如何實(shí)現(xiàn)線上環(huán)境使用setting.host + ’/api/sop/’,本地dev請(qǐng)求localhost:3000呢?
const instance = axios.create({ baseURL: setting.host + ’/api/sop/’, timeout: 20000, headers: { ’Content-Type’: ’application/json’, ’Accept’: ’application/json’, },});
config
proxyTable: { ’/api’: { target: 'http://127.0.0.1:3000', changeOrigin: true, pathRewrite: { ’^/api’: '' } }},
問題解答
回答1:用的vue-resource,理論上思路是一樣的。proxyTable和nginx的反向代理是一樣的道理,攔截特定的url,轉(zhuǎn)發(fā)到其他服務(wù)器。
// configproxyTable: { ’/api’: { target: ’http://10.0.0.10:8080’, changeOrigin: true, pathRewrite: { ’^/api’: ’/api’ } }}// codethis.$http.post(’/api/login’,{ username: ’xxx’, password: ’xxx’}).then((response) => { // ...}, (response) => { // ...});# 生產(chǎn)環(huán)境 nginxlocation /api { proxy_pass http://10.0.0.10:8080/api;}回答2:
可以配置一個(gè)環(huán)境變量,通過判斷環(huán)境變量確定使用哪一種配置
process.NODE_ENV === ’LOCAL’ ? proxyTableLocal : proxyTableServer回答3:
設(shè)置后, npn run dev階段, 本地如果訪問’/get/apple, 本地服務(wù)器會(huì)幫你訪問http://api.com:6688/get/apple拿到遠(yuǎn)程的數(shù)據(jù), 變相的實(shí)現(xiàn)了跨域功能
打開config/index.js, 添加proxyTable屬性
module.exports = {
build: {...}dev: { ... proxyTable: {’/’: { target: ’http://api.com:6688’, changeOrigin: true } }, ...}
}
https://github.com/383514580/...
相關(guān)文章:
1. javascript - vue-resource中如何設(shè)置全局的timeout?2. html5和Flash對(duì)抗是什么情況?3. 小程序怎么加外鏈,語句怎么寫!求救新手,開文檔沒發(fā)現(xiàn)4. php如何獲取訪問者路由器的mac地址5. html5 - input type=’file’ 上傳獲取的fileList對(duì)象怎么存儲(chǔ)于瀏覽器?6. 多選框?qū)戇M(jìn)數(shù)據(jù)庫(kù)怎么寫7. python沒入門,請(qǐng)教一個(gè)問題8. javascript - 這兩種函數(shù)寫法各有什么好處?9. sql語句如何按or排序取出記錄10. 求教一個(gè)mysql建表分組索引問題
