简体   繁体   English

使用harmon在node-http-proxy中修改HTML响应-无输出运算

[英]Modifying HTML response in node-http-proxy using harmon - no output maniputlation

Probably a n00b question :-). 可能是一个n00b问题:-)。 I'm looking at node-http-proxy to create a filtering proxy. 我正在查看node-http-proxy来创建过滤代理。 Looking for the string manipulation example I found the pointer to harmon and ran their example successfully. 在查找字符串操作示例时,我找到了指向harmon的指针,并成功运行了该示例

Then I tried running my own example against an Apache HTTP listening on localhost:80. 然后,我尝试针对在localhost:80上侦听的Apache HTTP运行自己的示例。 Here is my code: 这是我的代码:

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

    // Create an array of selects that harmon will process. 
    var actions = [];

    var simpleaction = {};
    simpleaction.query = 'head';
    simpleaction.func = function (node) {
                             var out = '<style type="text/css"> h1 {color : red; border-bottom : 5px solid green} </style>';
                            node.createWriteStream({ outer: true }).end(out);
                            console.log("head function called:" + out);
                            };
    var simpleaction2 = { 'query' : 'body',
                         'func' : function (node) {
                             var out = '<h1>You have been proxied</h1>';
                            node.createWriteStream({ outer: false }).end(out);
                            console.log("body function called" + out);
                            }
                        };                  

    // Add the action to the action array
    actions.push(simpleaction);
    actions.push(simpleaction2);

    var proxy = httpProxy.createServer(
      require('harmon')([], actions),
      80, 'localhost'
    );

    proxy.listen(8899);

    console.log("Up and running on port 8899");

Initially I got an error since I was using a newer version of http-proxy. 最初,由于使用的是http-proxy的较新版本,因此出现错误。 Using 0.8.7 fixed that. 使用0.8.7修复了该问题。 The console output when loading a page now is: 现在,加载页面时的控制台输出为:

stw@devmachine:~/tests$ nodejs ptest.js 
Up and running on port 8899
head function called:<style type="text/css"> h1 {color : red; border-bottom : 5px solid green} </style>
body function called<h1>You have been proxied
head function called:<style type="text/css"> h1 {color : red; border-bottom : 5px solid green} </style>
body function called<h1>You have been proxied
head function called:<style type="text/css"> h1 {color : red; border-bottom : 5px solid green} </style>
body function called<h1>You have been proxied
head function called:<style type="text/css"> h1 {color : red; border-bottom : 5px solid green} </style>
body function called<h1>You have been proxied</h1>

So it looks good, but the output isn't changed at all. 这样看起来不错,但是输出完全没有改变。 What did I miss out? 我错过了什么?

Ultimately I need to: 最终,我需要:

  • add a stylesheet to the <head> section 将样式表添加到<head>部分
  • replace all src and href attributes 替换所有srchref属性
  • add some DOM elements at specific places (eg a <h1> as first element of the body) 在特定位置添加一些DOM元素(例如<h1>作为正文的第一个元素)
  • add some headers 添加一些标题
  • GZIP the result before sending out 寄出前将结果压缩
  • work on http and https URLs 在http和https网址上工作
  • leave images resources alone 不理会图片资源

Pointers appreciated! 指针表示赞赏!

Seems strange Not having the full environment for your test means I can't give a definitive answer. 似乎很奇怪没有完整的测试环境意味着我无法给出确切的答案。 But I have put together a POC here that has two actions similar to the ones you are creating. 但是我在这里汇总了一个POC,它具有与您正在创建的动作类似的两个动作。

https://gist.github.com/No9/10874082 https://gist.github.com/No9/10874082

That works fine if you clone harmon from git and drop it into the test folder. 如果您从git克隆harmon并将其放到test文件夹中,则效果很好。

$ git clone https://github.com/No9/harmon.git $ cd harmon/test $ curl https://gist.githubusercontent.com/No9/10874082/raw/38a26d15b7ecbd875eee0988c94af0333927b98a/host-multiaction.js > host-multiaction.js $ node host-multiaction.js

For the overall functionality with GZIP etc I would say the new version of node-proxy would be better positioned to provide a complete solution. 对于GZIP等的整体功能,我想说新版本的node-proxy可以更好地提供完整的解决方案。

I suggest watching https://github.com/No9/harmon/issues/8 for the next couple of weeks ;) 我建议在接下来的几周内观看https://github.com/No9/harmon/issues/8 ;)

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

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