Comments (6)
Excuse me, I just noticed it is possible to omit the status code, resulting to a default 500
.
from toxy.
All the params to the poison are actually optional. The main reason to encapsulate the arguments in a function of one arity is mainly to avoid passing multiple argument when they are optional.
Regarding to the headers feature you're proposing, I can easily merge already defined headers in the response, but not that if you're using this poison, the target server never will be reached, as this poison intercepts the incoming traffic in a blocking way.
Probably I should add some flag in the docs to clarify if the poison is blocking or not.
Not that you can plug in your custom middleware to define CORS headers, for instance:
var toxy = require('toxy')
var proxy = toxy()
proxy.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*')
next()
})
proxy
.all('/*')
.poison(toxy.poisons.inject({ code: 503 })
.withRule(toxy.rules.method('GET'))
from toxy.
Thanks for the quick reply. That makes sense, that the target server is never reached. Never thought about that. Maybe there should be a new poison (e.g. transform
) that does go through to the backend server, but after that, transforms the responses.?
I already worked around the issue of the cors headers just like you're describing. thanks.
from toxy.
I would consider adding a new poison to achieve that in a simple way, however it worth to say that you can actually achieve that using the response middleware phase, which is provided natively by rocky
. Also you might be interested in toxy.transformResponse()
built-in helper, which it's also an inherited feature from rocky
.
Via the response
middleware layer you can consistently intercept the response from your target server, and behave accordingly as you need. Here is pretty simple example replacing the headers and replying with a custom error, as you can do with the inject
poison:
var toxy = require('toxy')
var proxy = toxy()
// Intercept the response from the target server
proxy.useResponse(function (req, res, next) {
if (res.getHeader('server') === 'nginx') {
// Actually, this is your poison now :D
res.setHeader('server', 'toxy')
res.writeHead(500, res.headers)
return res.end('Oh no!') // and we stop here!
}
next() // otherwise, continue with the next middleware
})
// Handle all the traffic
proxy
.forward('http://server.net')
.all('/*')
proxy.listen(3000)
from toxy.
@schmkr FYI I've just released toxy 0.3.0
. A couple of notes below that you probably worth to know.
That release includes two phases for poisoning, one applied to the incoming traffic (like you can do in previous versions), and a new phase to poison the outgoing traffic (when request is received from the server in the proxy, and before it's sent to the client).
Now you can plugin poisons in one or other stage according to your specific scenario.
For instance, you can plug in inject
as outgoing poison, enabling it based on the response status code, response headers or even response body.
Additionally, I've created new rules specifically for outgoing poisons.
Take a look to the API docs for more info.
from toxy.
Now headers are merged in the inject
poison. You should just overwrite them.
from toxy.
Related Issues (20)
- slowRead problems when submitting files larger then 30KB HOT 2
- Need some hand holding... HOT 2
- Having issues with Node 4.5.0 after updating from 0.12.5 HOT 9
- Forward to IP no to a host and preserve host header HOT 1
- Odd self signed certificate JSON payload
- http OPTIONS req returns 502 bad gateway from rocky server HOT 1
- Forward all HOT 4
- MaxListenersExceededWarning when trying to update poison on the proxy HOT 6
- HTTP API is unable to handle CORS preflight requests HOT 2
- Strict Forwarding and Websockets
- Unable to delete outgoing poisons via HTTP API HOT 1
- Toxy is awesome, but obscure. Needs a kick ass example. HOT 1
- Network throttling in iOS app programatically for automation. HOT 11
- How to allow the entire network traffic via proxy port by not filtering via certain urls?
- Relative forward
- Relative forward? HOT 1
- How to use behind a corporate proxy?
- Can toxy be used as a web browser proxy? HOT 1
- Nothing works on Node 16 HOT 4
- How to use timeout poison?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from toxy.