This is my proxy setup:
var express = require('express');
var vhost = require('vhost');
var proxy = require('http-proxy').createProxyServer();
var app = express();
var server = http.createServer(app);
// route api subdomain to port 2000
app.use(vhost('api.*.*', function(req, res) {
proxy.web(req, res, {target: 'http://localhost:2000'});
}));
// I need to get websocket requests to ws://api.example.com proxied port 4000
server.on('upgrade', function(req, socket, head) {
// proxy.ws seems like the right way to go, but how do I get this under the vhost?
proxy.ws(req, socket, head);
});
// everything else goes to port 3000
app.get('*', function(req, res) {
proxy.web(req, res, {target: 'http://localhost:3000'});
});
server.listen(80, function() {
console.log('Proxy listening on port 80.');
});
Do I need to dive into the req, res
objects inside the vhost handler to route the socket request? What is the right approach?
Update
Not pretty, but I'm going to give this a try.
server.on('upgrade', function(req, socket, head) {
var host = req.headers.host;
var parts = host.split('.');
if (parts.length === 3 && parts[0] === 'api') {
proxy.ws(req, socket, head, {target: {target: 'http://localhost:2000'});
}
});
Hopeful for a more elegant vhost-like solution.