Node.js http.get

17,205

Solution 1

Simplest solution is to add <base href="http://google.com/"> to the html. Preferably in the head, so do string replace on '<head>' and replace in with '<head><base href="http://google.com/">'

Solution 2

Wouldn't it be easier to have the client fetch the Google page?

<html>
<head>
<script>
window.onload = function () {
  var nif = document.createElement("iframe");
  nif.width = 850;
  nif.height = 500;
  nif.src = "http://www.google.de";
  nif.appendChild( document.createTextNode("no iframe support") );
  document.body.appendChild(nif);
};
</script>
</head>
<body>
<h1>IFRAME</h1>
</body>
</html>
Share:
17,205

Related videos on Youtube

Hacknightly
Author by

Hacknightly

I work daily. I hack nightly.

Updated on June 20, 2022

Comments

  • Hacknightly
    Hacknightly almost 2 years

    I have the following code that requests the Google.com homepage and sends the page data back to an Iframe on the client side.

     var options = {
        host: 'www.google.com',
        port: 80,
        path: '/',
        method: 'GET'
      };
    
      var req = http.get(options, function(res) {
        var pageData = "";
        res.setEncoding('utf8');
        res.on('data', function (chunk) {
          pageData += chunk;
        });
    
        res.on('end', function(){
          response.send(pageData)
        });
      });
    

    However, all images and CSS are broken in the iframe? How can I preserve the images and CSS?

  • Gerben
    Gerben over 12 years
    PS. you probably also have to strip the JS, because AJAX won't work cross origin.
  • Hacknightly
    Hacknightly over 12 years
    Hm, I've never seen that before.... Sounds reasonable, I'm going to give it a shot, thank you ; )
  • Hacknightly
    Hacknightly over 12 years
    Dude, you saved me another week of wanting to bang my head against my monitor. Thank you, a thousand times thank you.
  • Hacknightly
    Hacknightly over 12 years
    Ahh, sorry, I failed to mention that I'm on the server side making the request via Node js.
  • Gerben
    Gerben over 12 years
    You're a thousand times welcome. I think your monitor will be pleased :-)
  • Lumi
    Lumi over 12 years
    Sure, I noticed; I just didn't understand why when it is easier on the client side. Well, there may be a reason ... just couldn't figure out what it could be.
  • Hacknightly
    Hacknightly over 12 years
    Actually, I now see how your solution is viable. I'm going to give this a shot also.
  • Lumi
    Lumi over 12 years
    Good. :) And of course you can hardcode the IFRAME, making it even easier - if that's allowed by the problem's constraints.