javascript Node.js http.get

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7150814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 23:06:44  来源:igfitidea点击:

Node.js http.get

javascriptnode.jsproxy

提问by Hacknightly

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

我有以下代码请求 Google.com 主页并将页面数据发送回客户端的 iframe。

 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?

但是,iframe 中的所有图像和 CSS 都已损坏?如何保留图像和 CSS?

采纳答案by Gerben

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

最简单的解决方案是将 <base href="http://google.com/"> 添加到 html。最好在头部,所以在 '<head>' 上进行字符串替换并用 '<head><base href="http://google.com/">' 替换

回答by Lumi

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

让客户获取 Google 页面不是更容易吗?

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