【发布时间】:2018-04-07 21:19:29
【问题描述】:
我正在执行下面的代码 sn-p 但它没有重定向到实际页面。
var http = require('http');
var options = {
host: 'google.com',
port: 80,
path: '/search?q=hello+world'
};
http.get(options, function(resp){
resp.setEncoding('utf8');
resp.on('data', function(chunk){
//do something with chunk
console.log('================\n\n', chunk);
});
}).on("error", function(e){
console.log("Got error: " + e.message);
});
命令开火:
node sample.js
输出:
================
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.co.in/search?q=hello+world&gws_rd=cr&dcr=0&ei=Wd3xWd_nBITovgSl9bXwCA">here</A>.
</BODY></HTML>
我想获取整个页面的 html,但不是这个,而不是我想要 hello world 的结果。
【问题讨论】:
-
google.com 是一个相对地址。你需要把它改成Http://google.com
-
然后我得到这个错误:getaddrinfo ENOTFOUND google.com google.com:80
-
没有端口号你测试了吗?
-
是的,我也这样做了
标签: javascript html node.js http