第一方式:使用angular-cli代理来实现,适用于开发
API:https://localhost:8443
工程:http://localhost:4200
假如需要调用https://localhost:8443/user这个接口,那么只须这样调用:
this.http.get("/api/user",{}).subscribe(
data=>{
console.log(data);
}
);
具体操作如下:
1.在工程根目录新建proxy.config.json
{
"/api":{
"target":"https://localhost:8443",
"secure": false,
"changeOrigin": true,
"pathRewrite":{
"^/api":""
}
}
}
2.
可直接使用命令:
ng serve --proxy-config proxy.config.json
或者在package.json中修改start的值
"start": "ng serve --proxy-config proxy.config.json",
然后使用npm run start来启动
第二种方式:使用nginx代理
location /api {
rewrite ^.+api/?(.*)$ /$1 break;
include uwsgi_params;
proxy_pass https://192.168.1.10:8443;
}