I'm using a simple proxy for web and socket and for some reason some of my request throw errors.
instead of having an object with { target: .., forward: ... } I got an empty withouth these two key. However I do have the "secure" key that I set while creating proxy object and I do have "prependPath".
Ex:
For most of my sockets request I have usually:
{ target:
Url {
protocol: 'https:',
slashes: true,
auth: null,
host: 'localhost:3001',
port: '3001',
hostname: 'localhost',
hash: null,
search: null,
query: null,
pathname: '/',
path: '/',
href: 'https://localhost:3001/' },
forward:
Url {
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: '?EIO=3&transport=polling&t=1483089414416-48&sid=CyuT-vFAV_iKoec5AAAC',
query: 'EIO=3&transport=polling&t=1483089414416-48&sid=CyuT-vFAV_iKoec5AAAC',
pathname: '/socket.io/',
path: '/socket.io/?EIO=3&transport=polling&t=1483089414416-48&sid=CyuT-vFAV_iKoec5AAAC',
href: '/socket.io/?EIO=3&transport=polling&t=1483089414416-48&sid=CyuT-vFAV_iKoec5AAAC' },
secure: false,
prependPath: true }
For some other I only got
{ secure: false, prependPath: true }
which is why the module throw error because of this check
if (!options.target && !options.forward) {
return this.emit('error', new Error('Must provide a proper URL as target'));
}
Here is what I do on my server side
app.locals.proxy = httpProxy.createProxyServer({
secure: false
});
app.all("/remote-api/*", function(req, res) {
req.app.locals.proxy.web(req, res, { target: req.app.locals.system.config.sharedApiUrl, forward: req.url });
});
server.on('upgrade', function (req, socket, head) {
app.locals.proxy.ws(req, socket, head);
});
My stack is pretty simple and as I said, all my web request are ok, pretty much all sockets too. Only some of them fails.
After some digging, only the web socket request from wss:// are in cause. What is strange is that my web page is working, connected and receiving socket connection. Also when I use a dedicated proxy server (instead of my actual server) it works without any problem. So I think I'm missing some configuration with the socket proxying part ?
@mbret its because for your web proxy requests, you specify your target, you do not do the same for the ws proxy requests on the upgrade event. Add a 4th argument to the function that is an object that specifies { target: req.app.locals.system.config.sharedApiUrl })
Your example should look like:
app.locals.proxy = httpProxy.createProxyServer({
secure: false
});
app.all("/remote-api/*", function(req, res) {
req.app.locals.proxy.web(req, res, { target: req.app.locals.system.config.sharedApiUrl });
});
server.on('upgrade', function (req, socket, head) {
app.locals.proxy.ws(req, socket, head, { target: req.app.locals.system.config.sharedApiUrl });
});
Of course I did not -_-
Thanks a lot @jcrugzz. To be honest I'm a bit stupid because some of my socket request pass through http and so inside the req.app.locals.proxy.web. So I naively thought that my app.locals.proxy.ws was okay like this. Don't know if you get what I mean but whatever, thank you :)
yea if it was using the socket.io longpolling those would have worked, but if it upgraded, then the issues began. No problem and good luck
@jcrugzz A great thank you for your efforts on this module!
Just a comment - is it worth changing the README.md file to reflect the above advice?
I had exactly the same code as @mbret as I'd copied it from the example in the readme, encountered the same error and after a quick googling, ended up here...
Most helpful comment
@jcrugzz A great thank you for your efforts on this module!
Just a comment - is it worth changing the README.md file to reflect the above advice?
I had exactly the same code as @mbret as I'd copied it from the example in the readme, encountered the same error and after a quick googling, ended up here...