express-http-proxy icon indicating copy to clipboard operation
express-http-proxy copied to clipboard

The interface design may make the reverse proxy hard to implement.

Open whitebob opened this issue 5 years ago • 2 comments

Suppose i want to use the request path as arguments to find out the matched one of the proxy addresses to localhost with different port numbers. There are lots of ports and they follow some rules, thus we do not want to write each proxy as app.use('/xxx/yyy',proxy('localhost:nnn'))

In this situation, the host resolving process should be after the request parsing, and the host address is unknown until we find out what it should be. Thus the proxy('host',...) format will not suit in such case.

I don't know how to finish such a job with current interface, could you give me an example?

And if it is not easy currently, I suggest to add a certain hostname type like unspecified and provide interfaces in the request processing part to change it.

whitebob avatar Sep 30 '18 17:09 whitebob

I haven't tested it out yet, but I might have a solution to your issue.

If you managed to find a solution on your end I'd love to hear it.

What I want to achieve is that people get assigned a subdomain name and depending on this subdomain, they are getting redirected to a specific port on the machine.

I use the vhost npm package for the vhost part.

app.use(vhost(`*.${conf.KMProxy.Host}`), getKMRoom, proxy(redirectKMRoom, {
		memoizeHost: false
	}));

function getKMRoom(req, res, next) {
	const instance = getInstanceRoom(req.vhost[0]);
	if (!instance) {
		res.status(404).send('No room exists by this name');		
	} else {
		req.KMAppPort = instance.port;
		next();
	}
}

function redirectKMRoom(req) {
	return `http://localhost:${req.KMAppPort}`;
}

I have yet to test this out, my only doubt is that I don't know if http-proxy passes the req object to the redirectKMRoom function.

Hope it helps (and hope it works, I'll test it out this weekend)

AxelTerizaki avatar Nov 23 '18 11:11 AxelTerizaki

Thanks for your reply. Currently I use http-proxy directly, because it makes me able to write like this: apiProxy.web(req, res, {target: 'http://127.0.0.1:'+port}) . and I can figure out the port i want by using port=calculate_port(req, policy)

whitebob avatar Dec 05 '18 14:12 whitebob