node.js - Cannot pipe after data has been emitted from the response nodejs -


i've been experiencing problem require library of node js. when try pipe file , stream on response, error: you cannot pipe after data has been emitted response. because calculations before piping data.

example:

var request = require('request') var fs = require('fs') var through2 = require('through2')  options = {     url: 'url-to-fetch-a-file' };  var req = request(options) req.on('response',function(res){     //some computations remove files potentially     //these computations take quite somme time.     //function creates path recursively     createpath(path,function(){         var file = fs.createwritestream(path+fname)         var stream = through2.obj(function (chunk, enc, callback) {             this.push(chunk)             callback()         })          req.pipe(file)         req.pipe(stream)     }) }) 

if pipe stream without calculations, it's fine. how can pipe both file , stream using request module in nodejs?

i found this:node.js piping same readable stream multiple (writable) targets not same thing. there, piping happens 2 times in different tick. example pipes answer in question , still receives error.

instead of piping directly file can add listener stream defined. can replace req.pipe(file) with

stream.on('data',function(data){     file.write(data) })  stream.on('end',function(){     file.end() }) 

or

stream.pipe(file) 

this pause stream untill read, doesn't happen request module.

more info: https://github.com/request/request/issues/887


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -