ウェブサイトからすべての画像を取得し、その画像を文字列としてブラウザに送信するコードで作業していますが、機能しません!
http モジュールを使用してサーバーを作成し、ピンタレストの主要ページを取得し、すべての画像タグを照合し、各一致を配列に格納して、最後に送信しようとしています。
これはコードです:
var http = require('http')
, options = {
host: 'www.pinterest.com'
, port: 80
, path: '/'
, method: 'GET'
}
, images = [ ]
;
http.createServer( function ( request, response ) {
http.request( options, function ( res ) {
res.setEncoding( 'utf8' );
res.on( 'data', function ( chunk ) {
matches.push( chunk.match(/<img[^>]+src="([^">]+)/g) );
});
}).on('error', function(e) {
console.log('problem with request: ' + e.message);
});
response.writeHead( 200, { 'Content-Type' : 'text/html' } );
response.end( images.toString() );
}).listen(8888);
コンソールにエラーはありませんが、1 分後にコンソールに次のように出力されます。
problem with request: socket hang up