Toby
Toby

Reputation: 13385

http-proxy-middleware to modify port only

I'm using an advanced proxy in create-react-app which uses http-proxy-middleware. I'd like to pass through the domain and modify the port only for all requests.

I believe in create-react-app I must use the shorthand:

app.use(
  '/api',
  proxy({ target: 'http://www.example.org:8000' })
)

Is there a way I can pass through all requests to API so that the domain is unchanged but the port is 8000?

Upvotes: 1

Views: 2458

Answers (1)

Toby
Toby

Reputation: 13385

I found that this approach works:

const proxy = require('http-proxy-middleware');

module.exports = function(app) {
  app.use(
    proxy('/api', {
      target: 'https://[::1]:8000',
      secure: false,
    })
  )
}

As a way to proxy https://<any-domain>:3000/api to https://<any-domain>:8000, as mentioned in a few issues on the http-proxy-middleware. However, this seems a little hacky, and I can find no reference to this in the docs.

If anyone has any additional feedback on this, I'd be very interested to read more.

Note that ::1 is just the IPv6 loopback address.

Upvotes: 2

Related Questions