简体   繁体   中英

Node.js http.get

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?

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/">'

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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