搜尋

首頁  >  問答  >  主體

javascript - vue-cli proxyTable怎麼配置

如何實現線上環境使用setting.host + '/api/sop/',本地dev請求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': ""
    }
  }
},
高洛峰高洛峰2827 天前967

全部回覆(3)我來回復

  • 世界只因有你

    世界只因有你2017-05-16 13:41:25

    用的vue-resource,理論上思路是一樣的。 proxyTablenginx的反向代理是一样的道理,拦截特定的url,轉送到其他伺服器。

    // config
    proxyTable: {
      '/api': {
        target: 'http://10.0.0.10:8080',
        changeOrigin: true,
        pathRewrite: {
          '^/api': '/api'
        }
      }
    }
    
    // code
    this.$http.post('/api/login',{
      username: 'xxx',
      password: 'xxx'
    }).then((response) => {
      // ...
    }, (response) => {
      // ...
    });
    
    # 生产环境 nginx
    location /api {
      proxy_pass http://10.0.0.10:8080/api;
    }

    回覆
    0
  • 迷茫

    迷茫2017-05-16 13:41:25

    可以配置一個環境變量,透過判斷環境變數來決定使用哪一種配置

    process.NODE_ENV === 'LOCAL' ? proxyTableLocal : proxyTableServer

    回覆
    0
  • phpcn_u1582

    phpcn_u15822017-05-16 13:41:25

    設定後, npn run dev階段, 本地如果訪問'/get/apple, 本地伺服器會幫你訪問http://api.com:6688/get/apple拿到遠端的資料, 變相的實現了跨域功能

    開啟config/index.js, 新增proxyTable屬性

    module.exports = {

    build: {...}
    dev: {
           ...
        proxyTable: {
            '/': {
                     target: 'http://api.com:6688',
                     changeOrigin: true
                }
        },
           ...
    }

    }

    https://github.com/383514580/...

    回覆
    0
  • 取消回覆