最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node.js write after end error zlib - Stack Overflow

programmeradmin2浏览0评论

I have the following code where I'm piping the request of a URL that's gzipped. This works just fine, however if I try to execute the code a few times, I get the below error. Any suggestions how to work around this?

Thank you!

http.get(url, function(req) {

   req.pipe(gunzip);

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

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

});

Error:

  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)' ] }

I have the following code where I'm piping the request of a URL that's gzipped. This works just fine, however if I try to execute the code a few times, I get the below error. Any suggestions how to work around this?

Thank you!

http.get(url, function(req) {

   req.pipe(gunzip);

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

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

});

Error:

  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)' ] }
Share Improve this question asked Oct 25, 2013 at 21:17 dzmdzm 23.5k50 gold badges152 silver badges229 bronze badges 1
  • req should be res – Dan Ross Commented Dec 12, 2013 at 4:06
Add a comment  | 

1 Answer 1

Reset to default 21

Once a writable stream is closed, it cannot accept anymore data (see the documentation): this is why on the first execution your code will work, and on the second you'll have the write after end error.

Just create a new gunzip stream for each request:

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

});
发布评论

评论列表(0)

  1. 暂无评论