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
srcandhrefattributes - 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!