我在Electron中嵌套了一个Angular 4应用程序,其中express.js从MongoDB中获取数据。但是,我现在停留在如何在http请求中与Express进行“对话” 的问题。
在这种 Angular 服务中,我有一种获取所有玩家数据的方法
dataUrl: string = "/api/player/all";
getPlayers(): Observable<any> {
return this.dataService.getCookedData(this.dataUrl, this.finalizePlayerData); //the function pointing to data. It works in regular web browser.
}
然后在express.js中,设置了监听API,
router.get('/all', (req: Request, res: Response) => {
player.find().then((result) => {
res.send(result);
});
});
现在,该应用程序遇到了404错误
zone.js:1980 GET file:///api/player/all net::ERR_FILE_NOT_FOUND
我知道问题出在
file:///
上,它应该像http://
一样,但我不知道解决此问题的方法。有什么建议吗?
最佳答案
从本地文件而不是Express服务器加载窗口时:
browserWindow.loadUrl('file:///index.html')
预期所有XHR请求(包括
Http
)将使用相同的基本URL。无论API是远程服务器还是本地服务器,其基本URL都应进行硬编码:
export const API_BASE_URL = 'http://localhost:3456';
...
http.get(`${API_BASE_URL}/api/player/all`)...
关于angular - Electron中的Angular服务从Express错误获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43962145/