我正在使用带有node-http-proxy的harmon和harmon。我正在使用harmon重写代理响应,以包括javascript文件和css文件。当我将代理的目标设置为http://nodejs.org或除localhost以外的任何其他地址时,我会收到301或302重定向。该脚本将重写301响应,而不是完全代理的响应。如何使用和声重写结束响应而不是302响应?

这是我从harmon example文件夹运行的脚本示例:

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

var selects = [];
var simpleselect = {};

//<img id="logo" src="/images/logo.svg" alt="node.js">
simpleselect.query = 'img';
simpleselect.func = function (node) {

//Create a read/write stream wit the outer option
//so we get the full tag and we can replace it
var stm = node.createStream({ "outer" : true });

//variable to hold all the info from the data events
var tag = '';

//collect all the data in the stream
stm.on('data', function(data) {
   tag += data;
});

//When the read side of the stream has ended..
stm.on('end', function() {

  //Print out the tag you can also parse it or regex if you want
  process.stdout.write('tag:   ' + tag + '\n');
  process.stdout.write('end:   ' + node.name + '\n');

  //Now on the write side of the stream write some data using .end()
  //N.B. if end isn't called it will just hang.
  stm.end('<img id="logo" src="http://i.imgur.com/LKShxfc.gif"         alt="node.js">');

  });
 }

  selects.push(simpleselect);

 //
 // Basic Connect App
 //
 var app = connect();

 var proxy = httpProxy.createProxyServer({
   target: 'http://nodejs.org'
 })


 app.use(require('../')([], selects, true));

 app.use(
   function (req, res) {
    proxy.web(req, res);
   }
 );

最佳答案

问题在于,现在许多站点都将HTTP重定向到HTTPS。
nodejs.org就是其中之一。

我已经更新了示例https://github.com/No9/harmon/blob/master/examples/doge.js,以显示如何将http-proxy配置为处理HTTPS。

如果您仍然对其他任意重定向有疑问,请在harmon上记录问题。

谢谢

10-08 18:05