javascript 使用 node-http-proxy 转发每个请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16389728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 04:27:27  来源:igfitidea点击:

Forward every request with node-http-proxy

javascriptnode.jshttp-proxynode-http-proxy

提问by greenish

I'm trying to setup a node-http-proxy that just forwards requests. In the end this proxy should inject javascript in every website I visit through the browser..

我正在尝试设置一个仅转发请求的 node-http-proxy。最后,这个代理应该在我通过浏览器访问的每个网站中注入 javascript..

Right now, most pages are forwarded and displayed correctly, but some, like posterkoenig.chor verkehrsclub.chare returning either a blank page or there is an error on the page. Both sites work well without the proxy in place. What do I have to change, or what am I missing that gets not forwarded correctly?

目前,大多数页面都已正确转发和显示,但有些页面(如posterkoenig.chverkehrsclub.ch)要么返回空白页面,要么页面上有错误。这两个站点在没有代理的情况下运行良好。我必须改变什么,或者我错过了什么没有正确转发?

Im very new to nodejs and not even completely sure if my approach should work or not.

我对 nodejs 很陌生,甚至不确定我的方法是否可行。

Here is what I've got so far:

这是我到目前为止所得到的:

var httpProxy = require('http-proxy');
var url = require('url');

httpProxy.createServer(function(req, res, proxy) {

  var urlObj = url.parse(req.url);

  proxy.proxyRequest(req, res, {
    host: urlObj.host,
    port: 80,
    changeOrigin: true,
    enable : { xforward: true }
  });
}).listen(9000, function () {
  console.log("Waiting for requests...");
});


Update

更新

As suggested by @robertklep I removed changeOriginand redefined req.headers.hostand also req.headers.url

正如@robertklep建议我删除changeOrigin和重新定义req.headers.host,也req.headers.url

posterkoenig.ch:

海报koenig.ch

Now throws:

现在抛出:

An error has occurred: 
{"code":"ENOTFOUND","errno":"ENOTFOUND","syscall":"getaddrinfo"}

verkehrsclub.ch:

verkehrsclub.ch:

The frontpage works now but subpages still throw a error on the page.

首页现在可以工作,但子页面仍然在页面上抛出错误。

var httpProxy = require('http-proxy');
var url = require('url');

httpProxy.createServer(function(req, res, proxy) {

  var urlObj = url.parse(req.url);

  req.headers['host'] = urlObj.host;
  req.headers['url'] = urlObj.href;

  proxy.proxyRequest(req, res, {
    host: urlObj.host,
    port: 80,
    enable : { xforward: true }
  });
}).listen(9000, function () {
  console.log("Waiting for requests...");
});

回答by robertklep

Your first problem is related to changeOrigin: that will send a Hostheader to the remote server which includes a port number, and both sites you mention can't handle that.

您的第一个问题与changeOrigin:这将向Host远程服务器发送一个包含端口号的标头,而您提到的两个站点都无法处理。

Instead, try this:

相反,试试这个:

req.headers.host = urlObj.host;
req.url          = urlObj.path;
proxy.proxyRequest(req, res, {
  host: urlObj.host,
  port: 80,
  enable : { xforward: true }
});

As for your other problem, I think it might be related to websites that don't serve their content as UTF-8 (which is the encoding that .toString()will use if you don't pass it an encoding). Does it happen always, or just with some sites?

至于您的另一个问题,我认为它可能与不将其内容作为 UTF-8 提供的网站有关(.toString()如果您不将其传递给它,则将使用该编码)。它总是发生,还是只是在某些网站上发生?

FWIW, harmonis a middleware for node-http-proxywhich provides a nice way of rewriting responses. It might be an overkill for your situation, but it might also solve your problem.

FWIW,harmon是一个中间件,node-http-proxy它提供了一种很好的重写响应的方式。对于您的情况,这可能是一种矫枉过正,但也可能会解决您的问题。

EDIT:here's a minimal example that seems to work just fine for both posterkoenig.chand www.verkehrsclub.ch(homepages as well as subpages):

编辑:这是一个最小的例子,它似乎适用于posterkoenig.chwww.verkehrsclub.ch(主页以及子页面):

var httpProxy = require('http-proxy');
var url       = require('url');

httpProxy.createServer(function(req, res, proxy) {
  var urlObj = url.parse(req.url);

  req.headers.host  = urlObj.host;
  req.url           = urlObj.path;

  proxy.proxyRequest(req, res, {
    host    : urlObj.host,
    port    : 80,
    enable  : { xforward: true }
  });
}).listen(9000, function () {
  console.log("Waiting for requests...");
});