简体   繁体   English

通过代理配置从Electron-Angular项目调用API

[英]Call API from Electron-Angular project by proxy config

On angular, the only thing to do is to create a proxy.conf.json file, to configure the proxy with the command ng serve --proxy-config proxy.conf.json and to write a text like 在angular上,唯一要做的就是创建proxy.conf.json文件,使用命令ng serve --proxy-config proxy.conf.json配置代理,并编写类似

{
  "/api/v1/": {
    "target": "http://localhost:3000",
    "secure": false
  }
}

Then the app will automatically call http://localhost:3000 if I call in my app http://localhost:4200/api/v1/... 然后,如果我在应用程序http://localhost:4200/api/v1/...调用,则该应用程序将自动调用http://localhost:3000 http://localhost:4200/api/v1/...

My issue is that my angular app is running like an Electron app, so the command I execute to run it is electron . 我的问题是我的角度应用程序像电子应用程序一样运行,因此我执行的运行命令是electron . and I don't even know how to tell Electron that I want to call the locahost 3000 if I call api/v1 in my app. 而且我什至不知道如何告诉Electron如果我在应用程序中调用api / v1,我想调用locahost 3000。

I've read the doc of Electron but I don't understand how to set the proxy with setProxy() . 我已经阅读了Electron的文档,但不了解如何使用setProxy()设置代理。

The error I got when I try to call my API from the app is 尝试从应用程序调用API时遇到的错误是

ERR_CONNECTION_REFUSED

Finally, if you had this issue too, there is no need to call the localhost of the angular project, you just need to call directly the API and it works. 最后,如果您也遇到此问题,则无需调用angular项目的本地主机,只需直接调用API即可。

For example, 例如,

this.httpClient.get('http://localhost:4200/api/v1/...').subscribe(...);

becomes

this.httpClient.get('http://localhost:3000/api/v1/...').subscribe(...);

Simply as this. 就这么简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM