“NodeJS Pipeline Stream” Kod odpowiedzi

NodeJS Pipeline Stream

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 Pipeline Stream

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

Odpowiedzi podobne do “NodeJS Pipeline Stream”

Pytania podobne do “NodeJS Pipeline Stream”

Więcej pokrewnych odpowiedzi na “NodeJS Pipeline Stream” w JavaScript

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu