Question

Probably a n00b question :-). I'm looking at node-http-proxy to create a filtering proxy. Looking for the string manipulation example I found the pointer to harmon and ran their example successfully.

Then I tried running my own example against an Apache HTTP listening on localhost:80. 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. Using 0.8.7 fixed that. 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
  • replace all src and href attributes
  • add some DOM elements at specific places (e.g. a <h1> as first element of the body)
  • add some headers
  • GZIP the result before sending out
  • work on http and https URLs
  • leave images resources alone

Pointers appreciated!

Was it helpful?

Solution

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.

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

That works fine if you clone harmon from git and drop it into the test folder.

$ 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.

I suggest watching https://github.com/No9/harmon/issues/8 for the next couple of weeks ;)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top