简体   繁体   English

如何使用express-http-proxy修改响应头

[英]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. 我正在使用express-http-proxy代理我的SPA(单页应用程序)和CouchDB实例之间的一些请求。 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 . 这意味着我没有使用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 . 浏览完文档后,我得出的结论是,我要使用的是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. 不幸的是,我还没有完全掌握如何使用它,到目前为止,我发现的唯一 相关 问题似乎是基于httpProxy.createServer (根据我的有限理解)工作方式有所不同。

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? SUSPECT LOCATION是否有一些代码可以使我在标题上放置text以进行最终响应,而又不影响(当前以其他方式工作的)代理?

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. 我并不完全相信我的代码所在的SUSPECT LOCATION是“ SUSPECT LOCATION ,如果它需要移到其他地方,或者是否需要以其他方式解决这个问题,我会很乐意听。

It follows express.js methods on req, res objects. 它遵循req,res对象上的express.js方法。

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 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 这是express-http-proxy拦截功能的测试套件
https://github.com/villadora/express-http-proxy/blob/master/test/intercept.js 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 如果您想不了解代理的内容,最好的资源是haproxy
http://cbonte.github.io/haproxy-dconv/1.7/intro.html http://cbonte.github.io/haproxy-dconv/1.7/intro.html

But before that you need to understand http more (a constructive comment) 但在此之前,您需要进一步了解http(建设性评论)

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM