Monday 12 June 2017

NodeJS express proxy - Getting a port number from params and supplying it in the proxy target?

Pretty strange one here.

I have url's with changing random endpoint ports e.g:

  • 0.0.0.0:1111
  • 0.0.0.0:2222
  • 0.0.0.0:3333

Now I'm contacting an API on these endpoints from a different endpoint (say 0.0.0.0:4444). Therefore I inevitably hit CORS issues from my JS client-side code.

Now, my client-side code actually knows the port. So I've tried to start implementing a NodeJS proxy which lets me get around this. Therefore I've decided to try and pass the port in as a URL parameter. So I can call 0.0.0.0:4444/api/[port].

My current attempt to do this is by using the npm package 'express-request-proxy'

const requestProxy = require('express-request-proxy');

app.all('/api/:port/*', requestProxy({
  url: 'http://0.0.0.0:<<PORT NEEDED HERE>>/*',
}));

I have tried the following:

url: 'http://0.0.0.0::port/*'

But this just returns the error:

Error: connect ECONNREFUSED 0.0.0.0:80<br> &nbsp; &nbsp;at Object.exports._errnoException (util.js:1014:11)<br> &nbsp; &nbsp;at exports._exceptionWithHostPort (util.js:1037:20)<br> &nbsp; &nbsp;at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1138:14)

I've successfully done this using NGINX with some config tweaks but I was hoping to move over to Node/Express.

Hoping someone can make sense of what I require and give me some help.

Thanks.



via Peza

No comments:

Post a Comment