Coder Social home page Coder Social logo

Comments (6)

schmkr avatar schmkr commented on August 20, 2024

Excuse me, I just noticed it is possible to omit the status code, resulting to a default 500.

from toxy.

h2non avatar h2non commented on August 20, 2024

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.

schmkr avatar schmkr commented on August 20, 2024

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.

h2non avatar h2non commented on August 20, 2024

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.

h2non avatar h2non commented on August 20, 2024

@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.

h2non avatar h2non commented on August 20, 2024

Now headers are merged in the inject poison. You should just overwrite them.

from toxy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.