nodejs:node-http-proxy and harmon:从终点重写html响应,而不是302重定向响应。

时间:2015-03-30 18:18:21

标签: javascript node.js redirect proxy node-http-proxy

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

以下是我从harmon示例文件夹运行的脚本示例:

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

1 个答案:

答案 0 :(得分:0)

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

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

如果您仍然遇到其他任意重定向问题,请在闹钟上记录问题。

谢谢