Need some hand holding...
jdart opened this issue · 2 comments
jdart commented
I'm trying to use toxy to always allow OPTIONS requests for CORS, and intermittently fail everything else. I've tried the below but it seems only one poison ever takes effect, hopefully I'm making an obvious mistake.
https://gist.github.com/jdart/28aefe58ebb2a58d80ccca11c3d699d3
Care to help me out?
h2non commented
You're declaring the same route twice. Don't use poisons for other purposes than infect your traffic, instead you can use the raw middleware layer (as you can do with express/connect).
I did some changes to your code, and it works as expected:
var toxy = require('toxy')
var poisons = toxy.poisons
var rules = toxy.rules
// Create a new toxy proxy
var proxy = toxy()
// Default server to forward incoming traffic
proxy
.forward('http://httpbin.org')
.options({ secure: false })
.use(function (req, res, next) {
if (req.method === 'OPTIONS') {
res.headers['Access-Control-Allow-Origin'] = 'http://localhost:8100'
res.headers['Access-Control-Allow-Credentials'] = 'true'
}
next()
})
// Create route to handle all the traffic
var route = proxy.all('/*')
route.poison(poisons.inject({ code: 500 }))
.withRule(rules.probability(30))
.withRule(rules.method(['GET', 'POST', 'PUT', 'DELETE']))
route.poison(poisons.abort(1000))
.withRule(rules.probability(30))
.withRule(rules.method(['GET', 'POST', 'PUT', 'DELETE']))
proxy.listen(3000)
console.log('Server listening on port:', 3000)
console.log('Test it:', 'http://localhost:3000/image/jpeg')
jdart commented
One minor change to your improvements, I had to use res.setHeader('foo', 'bar')
.