const request = require('request');
const URL = 'http://de.releases.ubuntu.com/xenial/ubuntu-16.04.3-desktop-amd64.iso';
const MAX_SIZE = 10 * 1024 * 1024 // 10MB , maximum size to download
let total_bytes_read = 0;
1 - 서버의 응답이 gzip 압축 인 경우 gzip 옵션을 사용하도록 설정해야합니다. https://github.com/request/request#examples 하위 호환성을 위해 기본값은 응답 압축을 지원하지 않습니다. gzip 압축 응답을 허용하려면 gzip 옵션 을 true로 설정하십시오.
는
request
.get({
uri: URL,
gzip: true
})
.on('error', function (error) {
//TODO: error handling
console.error('ERROR::', error);
})
.on('data', function (data) {
// decompressed data
console.log('Decompressed chunck Recived:' + data.length, ': Total downloaded:', total_bytes_read)
total_bytes_read += data.length;
if (total_bytes_read >= MAX_SIZE) {
//TODO: handle exceeds max size event
console.error("Request exceeds max size.");
throw new Error('Request exceeds max size'); //stop
}
})
.on('response', function (response) {
response.on('data', function (chunk) {
//compressed data
console.log('Compressed chunck Recived:' + chunk.length, ': Total downloaded:', total_bytes_read)
});
})
.on('end', function() {
console.log('Request completed! Total size downloaded:', total_bytes_read)
});
NB : 압축/서버가 응답을 압축하지 않지만 여전히 GZIP 옵션을 사용 경우 다음 압축 해제 청크는 & 원래 덩어리 동일합니다.당신이 압축 해제 덩어리
2의 크기 제한을 확인해야 따라서는 응답이 압축 그러나 경우합니다 ( 압축/압축 덩어리에서)으로 제한 검사에게 어느 쪽이든을 수행 할 수 있습니다 - 응답이 압축되지 않은 경우 당신은하지 않습니다
request
.get(URL)
.on('error', function (error) {
//TODO: error handling
console.error('ERROR::', error);
})
.on('response', function (response) {
response.on('data', function (chunk) {
//compressed data
console.log('Recived chunck:' + chunk.length, ': Total downloaded:', total_bytes_read)
total_bytes_read += chunk.length;
if (total_bytes_read >= MAX_SIZE) {
//TODO: handle exceeds max size event
console.error("Request as it exceds max size:")
throw new Error('Request as it exceds max size');
}
console.log("...");
});
})
.on('end', function() {
console.log('Request completed! Total size downloaded:', total_bytes_read)
});
이 도움을합니까 압축 해제 에 GZIP 옵션이 필요? - https://stackoverflow.com/questions/15636095/how-to-limit-response-length-with-http-request-in-node-js – Jackthomson
고맙습니다.하지만 요청이라는 패키지를 사용하고 있습니다. nodejs HTTP 요청 버전. 그럼에도 불구하고 응답이 없으면 요청 패키지를 노드의 HTTP 요청으로 바꾸어야하므로 사용자의 대답이 유용 할 수 있습니다. –
당신은이 패키지에 대해 다음과 같이 이야기하고 있습니까? https://www.npmjs.com/package/request - 위의 링크 된 대답과 똑같이 사용할 수 없다면이 요청 라이브러리는 여전히 동일한 응답 객체를 반환하므로 프로토 타입을 켜 놓고 똑같은 권리를 누릴 수 있습니까? 또는이 라이브러리가 해당 기능을 제거합니까? 이 라이브러리는 그냥 약간의 구문을 사용하여 프로세스를 좀 더 쉽게 만들어주는 것처럼 보입니다. 코어에서 노드 요청과 응답 객체를 반환하는 것입니다. – Jackthomson