2013-10-25 2 views
9

gzip으로 압축 된 URL 요청을 파이핑하는 코드는 다음과 같습니다. 이 코드는 몇 번 실행하려고하면 잘 작동하지만 아래 오류가 발생합니다. 이 문제를 해결하는 방법은 무엇입니까?Node.js end error after write zlib

감사합니다.

http.get(url, function(req) { 

    req.pipe(gunzip); 

    gunzip.on('data', function (data) { 
    decoder.decode(data); 
    }); 

    gunzip.on('end', function() { 
    decoder.result(); 
    }); 

}); 

오류 : 쓰기 가능한 스트림이 닫혀되면

stack: 
    [ 'Error: write after end', 
    ' at writeAfterEnd (_stream_writable.js:125:12)', 
    ' at Gunzip.Writable.write (_stream_writable.js:170:5)', 
    ' at write (_stream_readable.js:547:24)', 
    ' at flow (_stream_readable.js:556:7)', 
    ' at _stream_readable.js:524:7', 
    ' at process._tickCallback (node.js:415:13)' ] } 
+0

'req'가 있어야한다'res' –

답변

18

, 그것은 더 이상 받아 들일 수있는 데이터 (see the documentation는) : 첫 번째 실행에 코드가 작동하는 이유이고, 두 번째에 당신 ' write after end 오류가 있습니다.

그냥 각 요청에 대해 새로운 풀어서 스트림을 생성합니다

http.get(url, function(req) { 
    var gunzip = zlib.createGzip(); 
    req.pipe(gunzip); 

    gunzip.on('data', function (data) { 
    decoder.decode(data); 
    }); 

    gunzip.on('end', function() { 
    decoder.result(); 
    }); 

});