2013-10-25 9 views
9

Ho il seguente codice dove sto piping la richiesta di un URL che è gzip. Funziona bene, tuttavia se provo a eseguire il codice alcune volte, ottengo l'errore seguente. Qualche suggerimento su come aggirare questo?Node.js scrivere dopo l'errore di fine zlib

Grazie!

http.get(url, function(req) { 

    req.pipe(gunzip); 

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

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

}); 

errore:

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' dovrebbe essere' res' –

risposta

18

Una volta che un flusso scrivibile è chiuso, non può più accettare dati (see the documentation): questo è il motivo per cui sulla prima esecuzione funziona il codice, e il secondo si' Avremo l'errore write after end.

basta creare un nuovo flusso di gunzip per ogni richiesta:

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(); 
    }); 

});