0
대량 다운로드 파일에 노드 스크립트를 쓰고 있습니다. 예제에서 파일의 이미지는 각 행의 파일 이름과 URL을가집니다. 이 스크립트를 다운로드 할 수있는 수백만 개의 URL까지 확장 할 수 있기를 바랍니다.Node.js에서 동시에 파이프 된 HTTP 요청 제한
노드 JS 스트림은 URL 목록을 파이프하고, http 요청 URL을 작성하고, 응답을 파일에 쓸 수 있으므로이 작업을 수행하는 좋은 방법 인 것 같습니다.
이 스크립트는 내 컴퓨터를 크래킹하고 빈 jpg 파일을 잃어 버렸습니다. pipe()
방법은 역 압력을 처리하지 않는 것 같습니다. 스크립트가 모든 URL을 즉시 병렬로 요청하고있는 것 같습니다. URL을 많이 다운로드 할 때 크기를 조정할 때 일관되게 실행할 수 있도록이 제한을 동시 HTTP 요청 수로 어떻게 설정할 수 있습니까? 감사. through
에서
'use strict';
var fs = require('fs'),
request = require('request'),
through = require('through'),
split = require('split'),
urlList = 'https://gist.githubusercontent.com/phelma/e1558aeb181c0cfe47b8/raw/cc5e667277308fda408f6af1404bc2d322b5186c/images.txt';
// 10000 images
var splitByTab = through(function(buf) {
var item = buf.toString().split('\t');
this.queue(item);
});
var downloadStream = through(function(item) {
// item is array [ filename , URL ]
if (item[1]) {
console.log('Requesting ' + item[1]);
request
.get(item[1])
.on('error', function(err) {
console.log('\nError: ' + err.message + '\n' + item[1]);
})
.pipe(fs.createWriteStream(__dirname + '/out/' + item[0] + '.jpg'));
}
});
request
.get(urlList) // Request the
.pipe(split()) // Split file into rows
.pipe(splitByTab) // Split each row into a array items
.pipe(downloadStream); // Download each item