Angular 未读取代理配置文件

Angular 未读取代理配置文件,angular,rest,Angular,Rest,我的开发环境angular server在localhost:4200上运行,spring引导服务器在localhost:8080上运行。Angular服务使用 this.http.get(window.location.protocol + '//' + window.location.hostname + ':8080/' +'<context-path>/api/<endpoint>'); 我正在使用启动服务器 ng serve --proxy-config pr

我的开发环境angular server在localhost:4200上运行,spring引导服务器在localhost:8080上运行。Angular服务使用

this.http.get(window.location.protocol + '//' + window.location.hostname + ':8080/' +'<context-path>/api/<endpoint>');
我正在使用启动服务器

ng serve --proxy-config proxy.conf.json
我故意提供端口号为800,但仍然可以从服务器获得结果。这确保了我没有读取代理配置文件


当我们可以直接从angular服务访问url时,为什么我们需要代理配置文件?

代理文件的使用基本上是告诉angular检查请求的url,并在匹配的情况下将其替换为目标。
在您的情况下,尝试请求
this.http.get('api/something')
,您将看到有一个指向
http://localhost:800

回答最后一个问题:

直接从angular服务访问url对于本地开发似乎很方便。但是,一旦您将后端设置在与前端应用程序不同的服务器上,您就可以节省大量时间来尝试找出出现CORS错误的原因。

我可以看到,在angular控制台上,url重定向到会在控制台上抛出错误。将代理配置文件中的url更改为后,我可以看到
[HPM]/api/->http://localhost:8080/
安装在角度控制台上。请求未命中服务器,在chrome开发工具上,我可以看到错误:
Http失败响应http://localhost:4200/api/: 404未找到”名称:“HttpErrorResponse”确定:错误状态:404状态文本:“未找到”url:http://localhost:4200/api/“
ng serve --proxy-config proxy.conf.json