h2non/toxy

http OPTIONS req returns 502 bad gateway from rocky server

sergueyarellano opened this issue · 1 comments

Hi @h2non I'm making an OPTIONS request and rocky returns a 502 to the client.

I'm just forwarding:

  proxy
    .forward('http://127.0.0.1:9990');

  proxy
    .all('/*')

localhost 9990 is not getting any request, rocky server is complaining and responding 502.

Any idea?

h2non commented

toxy does not support CORS by default and therefore does not handle OPTIONS pre-flight requests. Instead, it invites you to plug in a third-party custom middleware that provides the required capabilities in your proxy.

I wrote a simple representative working toxy proxy server that enables CORS (via cors package):

const cors = require('cors')
const toxy = require('toxy')
const { poisons, rules } = toxy

// Create toxy proxy
const proxy = toxy({
  forwardHost: true,
  timeout: 6e4,
  proxyTimeout: 6e4
})

// Enable CORS via middleware
proxy.use(cors())

// Default global traffic forwaring
proxy.forward('http://httpbin.org')

// Route all traffic with custom poisoning
proxy.all('/*')
  .poison(poisons.bandwidth({ bps: 1024 }))
  .withRule(rules.method('GET'))
  .withRule(rules.probability(50))

// Listen on the network
proxy.listen(3000)
console.log('Server listening on port:', 3000)

You can test it by fetching:
http://localhost:3000/image/jpeg