通过本地节点JS HTTP服务器端口(运行在电子)angular度服务

我在我的电子应用程序中运行本地主机上的RESTful节点JS Http服务器。 我在端口0听,这样操作系统给我一个随机的自由端口。 我怎样才能访问我的angular码中的givin端口,以便能够知道哪些URL我必须执行XHTTP请求?

在127.0.0.1.0上运行的restapi服务器(nodejs)

var listener = app.listen(port, server, function() { port = listener.address().port; console.log('Listening on port ' + port + '@' + server); }); 

XHTTP调用angular度

 private heroesUrl = 'http://localhost:1337/api/heroes'; getHeroes(): Promise<Hero[]> { return this.http.get(this.heroesUrl).toPromise() .then( ((response) => response.json() as Hero[]) ) .catch(this.handleError); } 

任何想法如何我可以取代固定端口1337端口,我打开在服务器端的侦听器时获得?

或者我应该只使用双方的固定端口,并希望它免费? 听起来对我错了。

另外我不能只用'/ api / heroes'作为URL,因为electron / angular正在http:// localhost:4200 (ng serve)上运行。

我通常在用这种设置进行开发时使用固定端口,节省了我担心configurationdynamic端口的压力。 另外,根据您的Angular应用程序的configuration方式,您可能需要设置一个代理。 例如,下面是如何使用angular-cli实现请求的function。

https://github.com/angular/angular-cli/blob/d22e9ae73763923980fc1e59cfb2a0bf255b98d8/docs/documentation/stories/proxy.md