【发布时间】:2017-09-06 23:08:09
【问题描述】:
我在电子应用程序的本地主机上运行 RESTful 节点 JS Http 服务器。我在端口 0 上监听,以便操作系统给我一个随机的空闲端口。如何访问我的 Angular 代码中的 givin 端口,以便能够知道我必须在哪个 URL 上执行 XHTTP 请求?
restapi 服务器 (nodejs) 在 127.0.0.1:0 上运行
var listener = app.listen(port, server, function() {
port = listener.address().port;
console.log('Listening on port ' + port + '@' + server);
});
XHTTP 角度调用
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)上运行。
【问题讨论】:
标签: angularjs node.js angular xmlhttprequest electron