Updating angular proxy config URL without restarting the application
Asked Answered
D

1

7

I have the following configuration for API proxy in my Angular application.

const proxyConfig = [
  {
    context: ['**', '!'],
    target: 'https://example.com',
    secure: false,
    changeOrigin: true
  }
];

I was looking for an option to update the target URL without restarting the application. Wondering if it's possible or not.

Please let me know if anyone has any solution or workaround?

Dunlin answered 20/12, 2019 at 7:6 Comment(4)
The workaround is to restart the application. The cli server is not meant to be used in production. It's a development server. What's the problem with restarting your development server?Cosby
does the config changes frequently. Configs are something predefined . The configs can be used to bring data dynamically. Please clarify more whats the reason behind doing this.Unspotted
One reason would be shorter debug cycle when trying to get the right setup for the proxy.conf.json file ..Narcotism
I faced a similar problem. In my case, the target needs to be changed more frequently for testing different versions of services hosted at different ports. And it's a pain to restart the application (it's huge).You
I
0

I ended up deploying a haproxy container that forwards to where I want things to go, and then I point my proxy configuration to that container.

My proxy would point at http://localhost:4444 and my container command is:

docker run -d --name haproxy -p 4444:80 -v /Path/To/Folder/hap-conf:/usr/local/etc/haproxy:ro --sysctl net.ipv4.ip_unprivileged_port_start=0 haproxy:2.3

This requires you to create a hap-conf folder somewhere on your computer, with a haproxy.cfg file inside of it with the following:

global
    ssl-default-bind-options ssl-min-ver TLSv1.2

defaults
    mode http
    maxconn          10000
    timeout connect  5000
    timeout check    5000
    timeout client   300000
    timeout server   300000

frontend http-in
    bind *:80
    mode http
    log global
    log 127.0.0.1 local0
    option httplog

    default_backend main

backend main
    default-server inter 3s fall 3 rise 2
    server server-name server-name:8443 check ssl verify none

Then I just change the server-name to repoint at things, and if I need to point at something on my local computer I just use a line like:

server localhost host.docker.internal:8443 check ssl verify none

Then its just a matter of updating the haproxy.cfg, restarting the container and refreshing your browser.

Not as convenient as a hot-reloadable proxy settings from Angular but for myself where I have to work on an angular app that takes 10 minutes to start, it saves a lot of time.

Infatuation answered 5/12, 2023 at 16:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.