本地搭建demo的时候,angular项目默认部署在4200端口,而后台服务器部署在8080端口; 当用angular发送请求到服务器的时候会报跨域问题
解决方案是在中间加一层nginx
配置如下 nginx:
location /api {
proxy_pass http://localhost:8080/angular/demo;
}
angular增加配置文件 proxy.conf.json
{
"/api": {
"target": "http://localhost",
"secure": false
}
}
启动angular项目的时候 使用 ng serve --proxy-config proxy.conf.json
这样 angular 请求 http://localhost:4200/api/getList会被解析为 http://localhost/api/getList
nginx接收到请求,转发到 http://localhost:8080/angular/demo/getList