“NodeJS Stream Pipeline” Respostas de código

NodeJS Stream Pipeline

const { pipeline } = require('stream');
let readStream = fs.createReadStream("./myDataInput.txt");
let writeStream = fs.createWriteStream("./myDataOutput.txt");

pipeline(readStream, writeStream, error => {
   if (error) {
      console.error(error);
   } else {
      console.info("Pipeline Successful")
   }
});
Muhammad Abdullah

NodeJS Stream Pipeline

const fs = require('fs');
const { pipeline, Transform } = require('stream');

const readStream = fs.createReadStream("text.txt", {
    flags: 'r',
    encoding: 'utf-8',
    autoClose: true,
});

const writeStream = fs.createWriteStream("text2.txt", {
    flags: 'w',
    encoding: 'utf-8',
    autoClose: true,
});

const transformStream = new Transform({
  //Transform data to filter for even numbers
        transform(chunk, enc, cb) {
            const data = chunk.toString().split(" ").filter(value => !(parseInt(value) & 1));
            cb(null, data.join(" "));
        }
    });

pipeline(readStream, transformStream, writeStream, (err) => {
    if(err) 
        console.error(err);
    else 
        console.error("success");
  //writeStream has written only even numbers from readStream
})
Muhammad Abdullah

Respostas semelhantes a “NodeJS Stream Pipeline”

Perguntas semelhantes a “NodeJS Stream Pipeline”

Mais respostas relacionadas para “NodeJS Stream Pipeline” em JavaScript

Procure respostas de código populares por idioma

Procurar outros idiomas de código