“Pipa aliran NodeJS” Kode Jawaban

Pipa aliran NodeJS

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

Pipa aliran NodeJS

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

Jawaban yang mirip dengan “Pipa aliran NodeJS”

Pertanyaan yang mirip dengan “Pipa aliran NodeJS”

Lebih banyak jawaban terkait untuk “Pipa aliran NodeJS” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya