No9 / harmon

middleware for node-http-proxy to modify the remote website response with trumpet
Other
424 stars 62 forks source link

am i doing this right? #23

Closed hijak closed 9 years ago

hijak commented 9 years ago

i want to transform the </body> tag

var https = require('https'),
    http  = require('http'),
    util  = require('util'),
    path  = require('path'),
    fs    = require('fs'),
    colors = require('colors'),
    httpProxy = require('http-proxy'),
    connect = require('connect');

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

simpleselect.query = '</body>';
simpleselect.func = function (node) {
node.createWriteStream().end('<center>MY INSERTED TEXT</center></body>');}

selects.push(simpleselect);

var app = connect();
var proxy = httpProxy.createProxyServer({target: 'https://DOMAIN.COM', agent  : https.globalAgent, headers:{ host: 'DOMAIN.COM' }}).listen(8011);

app.use(require('harmon')([], selects));
app.use(function(req, res) {
  proxy.web(req, res);
});

util.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8011'.yellow);

it serves up the proxy but there is no html transformation i suspect the simpleselect.query is wrong

No9 commented 9 years ago

Hi @RackerJack Yes your select is wrong. This example may help. https://github.com/No9/harmon/blob/master/examples/rotate.js#L9

Here it is selecting the tag in the HTML and inserting additional HTML

hijak commented 9 years ago

hi

thanks for getting back to me i have tried this but still getting no transformation

var https = require('https'),
    http  = require('http'),
    util  = require('util'),
    path  = require('path'),
    fs    = require('fs'),
    colors = require('colors'),
    httpProxy = require('http-proxy'),
    connect = require('connect');

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

simpleselect.query = 'body';
simpleselect.func = function (node) {
var out = '<center>MY TEXT</center></body>';
node.createWriteStream({ outer: true }).end(out)}

selects.push(simpleselect);

var app = connect();
var proxy = httpProxy.createProxyServer({target: 'https://DOMAIN.COM', agent  : https.globalAgent, headers:{ host: 'DOMAIN.COM' }}).listen(8011);

app.use(require('harmon')([], selects));
app.use(function(req, res) {
  proxy.web(req, res);
});

util.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8011'.yellow);
No9 commented 9 years ago

Ah I was too busy looking at the selector :)

So you left out attaching the proxy to a webserver and instead just started the proxy with .listen(8011);

I have messed around with the code a bit to get a working version for you so hopefully this will get you on your way N.B. At the web server at the end of the script.

http.createServer(app).listen(8011);

Also your https mapping solves #22 so thanks for that :)

var https = require('https'),
    http  = require('http'),
    util  = require('util'),
    path  = require('path'),
    fs    = require('fs'),
    colors = require('colors'),
    httpProxy = require('http-proxy'),
    connect = require('connect');

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

simpleselect.query = 'body';
simpleselect.func = function (node) {

   var stm = node.createStream({ outer: true });

   stm.on('data', function(data) {
      //Dropping the data
   })   
   stm.on('end', function(){
    util.puts('fired end'.red);
    var out = '<body><center>MY TEXT</center></body>';
    stm.end(out);
   })
}

selects.push(simpleselect);

var app = connect();
var proxy = httpProxy.createProxyServer({target: 'https://nodejs.org', agent  : https.globalAgent, headers:{ host: 'nodejs.org' }});

app.use(require('harmon')([], selects, true));
app.use(function(req, res) {
  proxy.web(req, res);
});

http.createServer(app).listen(8011);

util.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8011'.yellow);
hijak commented 9 years ago

@No9 still no luck your example works fine for nodejs.org but not on the site im trying it for

Does harmon have an issue if the backend proxy is gzipped??

No9 commented 9 years ago

Yes it doesn't handle decompression as that should be done by an interceptor either in the request or in the response. There is an outline of doing it in the request here https://github.com/No9/harmon/issues/19 using the proxyReq event.

I haven't seen a decompress for connect responses but I haven't really looked that hard

hijak commented 9 years ago

excellent!! thanks for the direction :D

No9 commented 9 years ago

@RackerJack Gzip support and a sample has been added to the repo https://github.com/No9/harmon/blob/master/examples/gzipped.js

hijak commented 9 years ago

wow thanks for this!!