Replacing util.pump () in Node.js

I recently upgraded to the latest version of Node.js (1.10 ~) from 0.8 ~, and I get a message at startup that says:

util.pump() is deprecated. Use readableStream.pipe() instead. 

I tried to switch my functions to say readableStream.pipe (), but I don't think it works the same.

So, I have three questions:

  • Why is util.pump out of date?
  • How to switch to readableStream.pipe ()? OR 3. How to disable this warning?

Here is the code where I use it (with a mustache)

  var stream = mu.compileAndRender(template_file, json_object_from_db); util.pump(stream, res); 

When I replace util.pump with readableStream.pipe, I get this error:

 ReferenceError: readableStream is not defined 

Can anyone point me in the right direction?

+4
source share
3 answers

Ok, so this question was a fairly easy answer after several experiments (although the documentation was null).

Basically, readableStream is just a variable that you should replace with your stream. So in my case the answer is:

 stream.pipe(res); 

You just replace the utility, mainly with a thread. Easy peezy.

+11
source

You can see this link http://nodejs.cn/api/stream.html
This is emitted whenever the stream.pipe () method is called on a readable stream, adding it for writing to its destination set.

 var writer = getWritableStreamSomehow(); var reader = getReadableStreamSomehow(); writer.on('pipe', (src) => { console.error('something is piping into the writer'); assert.equal(src, reader); }); reader.pipe(writer); 
+1
source

I think the following link will help you in your work! https://groups.google.com/forum/#!msg/nodejs/YWQ1sRoXOdI/3vDqoTazbQQJ


 var readS = fs.createReadStream("fileA.txt"); var writeS = fs.createWriteStream("fileB.txt"); util.pump(readS, writeS, function(error) // Operation done }); 

=====>

 var readS = fs.createReadStream("fileA.txt"); var writeS = fs.createWriteStream("fileB.txt"); readS.pipe(writeS); readS.on("end", function() { // Operation done }); 
0
source

Source: https://habr.com/ru/post/1479206/


All Articles