BrowserSync / browser-sync

Keep multiple browsers & devices in sync when building websites. https://browsersync.io
https://discord.gg/2d2xUThp
Apache License 2.0
12.18k stars 756 forks source link

Feature requst - Read proxy target from options rather than closure variable #1143

Open mike-suggitt opened 8 years ago

mike-suggitt commented 8 years ago

While I am aware that there has been work done for V3 to support dynamic proxying (https://github.com/BrowserSync/UI/issues/6) it would be great to have this supported in V2 asap.

As such I've seen a quite, relatively low impact solution that will allow devs to dynamically set the proxy target.

In lib/server/proxy-server.js simply make the following update:

var middlewareAdded = bs.options
        .update("middleware", function (mw) {
            return mw.concat({
                id: "Browsersync Proxy",
                route: opt.get("route"),
                handle: function (req, res) {
                    proxy.web(req, res, {
                        target: bs.options.get("proxy").get("target")
                    });
                }
            })
        });

This allows a dev to dynamically change the proxy target using middleware specified in their configration

constantin-melniciuc commented 7 years ago

Hey could elaborate a bit on this, please? Where do you set the target at runtime?

Either how are you overriding the config file at runtime. Thanks!

mike-suggitt commented 7 years ago

Wow its been over 6 months since I raised this but i think what i meant is that the proxy was read in and held in a closure rather than continually checking the config/options object. With a small change that means it re-evaluates the object each time it would allow a dev to dynamically overwrite that value.