Angular Proxy.Conf.Json无法使用多个API [英] Angular Proxy.Conf.Json not working against multiple apis
问题描述
我有以下proxy.conf.json,日志行和api调用.
I have the following proxy.conf.json, log lines, and api calls.
{
"/first/api/": {
"target": "/first/api/",
"secure": false,
"logLevel": "debug"
},
"/second/api/": {
"target": "/second/api/",
"secure": false,
"logLevel": "debug"
}
[HPM] GET /first/api/values-> /first/api/
[HPM] GET /second/api/dummy -> /second/api/
return this.http.get<any>(this.firstApi + 'values')
return this.http.get<any>(this.secondApi + 'dummy')
鉴于我可以看到日志行,所以我相信proxy.conf.json正确接收了api调用,但是当调用消失时却得到了404.日志仅输出目标,因此我不清楚如何编写所需的url,例如: localhost/first/api/values
Given I can see log lines, I believe the proxy.conf.json is correctly picked up the api calls, but I'm getting a 404 when the call goes out. The logs only output the target, so it's unclear to me how to compose the url I need, for example: localhost/first/api/values
只有一个api时,此方法可以正常工作:
This works correctly when there's only one api:
{
"/api/": {
"target": "/first/",
"secure": false
}
有人可以建议我进一步调试吗?
已解决
yanky_cranky的回答是正确的.为了帮助理解他的答案与我所看到的内容有何关系,我还需要查看IIS日志.在这里,我可以看到正在调用的网址.
yanky_cranky's answer was correct. As an aide in understanding how his answer related to what I was seeing, I also needed to look at my IIS logs. Here I could see what urls were being called.
推荐答案
首先,这里您没有正确利用Angular的代理概念.
Firstly, here you are not correctly leveraging the proxy concept of Angular.
1)关于代理:代理可用于将"/first/api"之类的任何请求映射到您无法访问的特定域". 如果api不公开,则如果api指向不同的主机,则将导致cors问题(这是浏览器的属性):{即,主机名或端口或两者都不同} 使用Angular,在我们的开发阶段,我们可以利用Nginix提供的相同反向代理概念并将其定位到正确的域.
1) About Proxy : Proxy can be used to map any request like '/first/api' to target specific "domain" which is out of your reach. If the apis are not public they will result in cors issue ( which is the property of browser) if the api is pointing to different host : {i.e, either hostname or port or both are different} With Angular,during our development phase we can leverage the same reverse proxy concept what Nginix provides and target to the right domain.
2)您的Nginix conf将导致: 以下路径:
2) your Nginix conf will result in : following paths :
{
"/first/api/": {
"target": "/first/api/",
"secure": false,
"logLevel": "debug"
},
/first/api/first/api/,因此您得到404
/first/api/first/api/ , thus you are getting 404
"/second/api/": {
"target": "/second/api/",
"secure": false,
"logLevel": "debug"
}
/second/api/second/api/,相同的404
/second/api/second/api/ , same 404
3)更正配置:
{
"/first/api/": {
"target": "http://localhost:{portNo}",
"secure": false,
"logLevel": "debug"
},
"/second/api/": {
"target": "http://localhost:{portNo}",
"secure": false,
"logLevel": "debug"
}
这些api然后将定位到:
These api will then target to :
http://localhost :{portNo}/first/api
http://localhost:{portNo}/first/api
http://localhost :{portNo}/second/api
http://localhost:{portNo}/second/api
欢呼(y)
这篇关于Angular Proxy.Conf.Json无法使用多个API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!