nodejs proxyfies requests with dynamic subdomains

301 Views Asked by At

i want my server to act as a middleware between the client and another server, the problem is that the target server have a dynamic subdomains that changes everytime randomly

i've tried http-proxy-middleware already, it works well with non dynamic targets, but i could't do any better

var options = {
  target: 'https://123.cdn.me', // target host
  changeOrigin: true,
  ws: true,
  pathRewrite: {
   '^/download/': '/'
  }
};

// create the proxy (without context)
var exampleProxy = proxy(options);
app.use('/download', exampleProxy);
`

i need a way to proxify requests from x1.myserver.com, to x1.traget_server.com and x2.myserver.com to x2.traget_server.com with x1,x2 a random values that should work at runtime and be added dynamically

1

There are 1 best solutions below

1
justabithope On

The question is a bit older but i had similar problems and maybe my solution can help somebody.

var optionsLogin = {
logLevel: 'debug',
changeOrigin: true,
target: 'https://standardTarget.com',
router: async function (proxyRequest, path, req) {
    const sub= proxyRequest.headers.sub
    return 'https://' + sub + env.targetAdress
},

};

with the router parameter i was able to rewrite the targetpoint.