DirtDiver DirtDiver - 20 days ago 11
Node.js Question

Angular 2 Electron - Consuming REST-API

I have an Electron App, which uses Angular 2 - in order to work i needed to modify

<base href="/">
to
<base href="./">
, which is a relative path in the file system. But now the API doesn't work anymore (Webclient on localhost works fine, but not Electron Client).

I access the API via proxy.conf.json

{
"/api": {
"target": "http://127.0.0.1:4747/Ticketsale",
"secure": false
}
}


And the error message in the JavaScript Console of Chromium / Electron is:

file:///Users/myusername/folder/apps/officeclient_electron/office-client-darwin-x64/office-client.app/Contents/Resources/app/api/1/initialData
Failed to load resource: net::ERR_FILE_NOT_FOUND


How can i tell Electron to access the local resources via relative Path and consume the API via HTTP?

Answer

We enabled CORS on the Webserver and this setting allowed us to consume the REST-API from a different URL.

See https://en.wikipedia.org/wiki/Cross-origin_resource_sharing for details.