I am trying to setup a backend proxy so that all requests done on the client-app running on port 4200 get rewritten to port 80.
When I run the request:
getTranslation(lang: string): Observable<any> {
return this.http.get(`/translate?language=${lang}`)
.pipe(map(response => response));
}
It should be hitting localhost:80/translate?language="en" but it's instead hitting localhost:4200/translate?language="en".
I tried setting it up through angular.json adding proxy.conf.json:
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "client-app:build",
"proxyConfig": "proxy.conf.json"
},
looking like this:
{
"/api": {
"target": "http://localhost:80",
"secure": false
}
}
But this does not work. I've read it's done a little different in angular 7 and you pass the proxy with ng serve like so:
"docker-start": "ng serve --host 0.0.0.0 --port 4200 --proxy-config proxy.js"
with the proxy.js file being:
var HttpsProxyAgent = require('https-proxy-agent');
var proxyConfig = [{
context: '/api',
target: 'http://localhost:80',
secure: false
}];
function setupForCorporateProxy(proxyConfig) {
var proxyServer = process.env.http_proxy || process.env.HTTP_PROXY;
if (proxyServer) {
var agent = new HttpsProxyAgent(proxyServer);
console.log('Using corporate proxy server: ' + proxyServer);
proxyConfig.forEach(function(entry) {
entry.agent = agent;
});
}
return proxyConfig;
}
module.exports = setupForCorporateProxy(proxyConfig);
But that also doesn't seem to work and the request keeps going against localhost:4200.
The app is running inside a docker container if that matters.
Idea ideas?