以下のコードスニペットを実行していますが、実際のページにリダイレクトしていません。ノード 'http'を使用してターゲットURLにリダイレクトする方法
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 http://www.google.com http://www.google.com:80 – VVB
ポートなしでテストしましたか数? –