How to modify response headers with express-http-proxy

Background

I'm using express-http-proxy to proxy a handful of requests between my SPA (single page application) and a CouchDB instance. I'm doing this proxy on a per call basis, NOT creating a proxy server (this will be important in a moment).

example of current use

app.use(`some/url`, proxy(dburl, {
  forwardPath: req => {return 'some/url'+require('url').parse(req.url).path;}
 }) );

Which means I am NOT using httpProxy.createServer. I want to send some snippet of text data along with my responses as a header. After looking through the documentation I've come to the conclusion that what I want will be using intercept. Unfortunately I've not quite managed to grasp how to use it, and the only related questions I've found so far appear to be based on httpProxy.createServer which appears (from my limited understanding) to work differently.

We are using individual request proxying because we wish to proxy different requests to different micro-services, and found this to be the most concise way (that we knew of & at the time) of doing that.

The Question

Given the code

const text = 'asdf';
app.use(`some/url`, proxy(dburl, {
  forwardPath: req => {return 'some/url'+require('url').parse(req.url).path;},
  intercept: function(rsp, data, req, res, callback) {
    //SUSPECT LOCATION
  }
}) );

Is there some code at SUSPECT LOCATION which would allow me to place text on the header for the final response without further affects to the (currently otherwise working) proxy?

Additional Notes

Headers and network requests in general are not very familiar to me, my apologies if the answer seems self evident.

Bonus points for a link to a resource that helps explain either the finer points of using this library for proxying, a similar library for proxying, or the underlying technologies which would make it clear how to use this library for proxying. AKA I'd rather spend some of my own time looking further into this and not come back for further questions.

I am not entirely confident that the place for my code will be SUSPECT LOCATION and I will happily listen if it needs to go somewhere else, or if we need to approach this problem in a different way.

Answers:

Answer

The accepted answer is now outdated. Intercept does not exist anymore.

Instead, use your own middleware before the proxy function

router.route('/my-route').get((req, res, next) => {
  res.set('My-Header', 'my-header-value');
  next();
}, proxyFunction);
Answer

It follows express.js methods on req, res objects.

Within the intercept function body, set the response headers using the following express format.

res.set('hola', 'amigos!!!');

Refer below link:
http://expressjs.com/en/4x/api.html#res.set

The best way to understand a library when there is no documentation is to follow its test suite. If there is no test suite don't use that library.

This is the test suite for the express-http-proxy intercept function
https://github.com/villadora/express-http-proxy/blob/master/test/intercept.js

This is the test case

it('can modify the response headers', function(done) {
  var app = express();
  app.use(proxy('httpbin.org', {
    intercept: function(rsp, data, req, res, cb) {
      res.set('x-wombat-alliance', 'mammels');
      res.set('content-type', 'wiki/wiki');
      cb(null, data);
    }
  }));

  request(app)
  .get('/ip')
  .end(function(err, res) {
    if (err) { return done(err); }
    assert(res.headers['content-type'] === 'wiki/wiki');
    assert(res.headers['x-wombat-alliance'] === 'mammels');
    done();
  });
});

If you want to undetstand in and out of proxying, the best resource is haproxy
http://cbonte.github.io/haproxy-dconv/1.7/intro.html

But before that you need to understand http more (a constructive comment)

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.