javascript - How to configure vue-cli proxyTable
高洛峰
高洛峰 2017-05-16 13:39:25
0
3
879

How to use setting.host + '/api/sop/' in the online environment, and the local dev requests 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': ""
    }
  }
},
高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(3)
世界只因有你

Using vue-resource, the theoretical idea is the same. proxyTablenginx的反向代理是一样的道理,拦截特定的url, forward to other servers.

// 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;
}
迷茫

You can configure an environment variable and determine which configuration to use by judging the environment variable

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

After setting up, in the npn run dev stage, if you access '/get/apple locally, the local server will help you access http://api.com:6688/get/apple to get the remote data, realizing the cross-domain function in disguise

Open config/index.js and add proxyTable attribute

module.exports = {

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

}

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!