proxy 配置
小于 1 分钟
proxy 配置
如果你需要在开发环境下将 API 请求代理到 API 服务器,你可以通过项目工程目录下的 zmip.config.js 中的 proxy 和 baseUrl 选项进行配置。
基本使用
假设现在你需要发起一个接口为 http://158.220.176.18:9060/test.do
的请求。
在 zmip.config.js 中,proxy 可以是一个指向开发环境 API 服务器的字符串,baseUrl 将作为 axios 的基础路径和 proxy 的代理路径。
module.exports = {
site: {
commonVersion: "1.0.0",
title: "Template",
assetsDir: "images",
imageLimit: 10000,
mockPath: "mock",
isMock: false,
baseUrl: "/zmip",
proxy: "http://158.220.176.18:9060/zschoauth",
},
};
这样,proxy 会将请求代理到http://158.220.176.18:9060
。
this.$http
.post("test.do", {
info: "test",
})
.then((result) => {
console.log(result);
})
.catch((error) => {
console.log(error);
});
更多配置
如果你想要更多的代理控制行为,你可以使用一个 path:options 成对的对象,如下所示:
module.exports = {
site: {
title: "Template",
assetsDir: "images",
imageLimit: 10000,
mockPath: "mock",
isMock: "true",
baseUrl: "/zmip",
proxy: {
"/mip": {
target: "",
ws: false,
secure: false,
changeOrigin: true,
pathRewrite: {
"^/mip": "",
},
},
"/web": {
target: "",
},
},
},
};