9951 explained code solutions for 126 technologies


nodejsHow to pipeline streams


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

let input = fs.createReadStream('/var/www/examples/test.txt');
let output = fs.createWriteStream('/tmp/out.txt');

pipeline(input, output, (err) => console.log(err));ctrl + c
require('fs')

library to work with file system

fs.createReadStream(

create stream to read data from (file in our case)

fs.createWriteStream

open file and create writing stream from it

/var/www/examples/test.txt

path to file to stream read

/tmp/out.txt

path to file to write stream to

pipeline

pipelines given streams (left to right) and properly destroys all objects after processing

(err) => console.log(err)

last argument of pipeline() is always error callback


Usage example

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

let input = fs.createReadStream('/var/www/examples/test.txt');
let output = fs.createWriteStream('/tmp/out.txt');
pipeline(input, output, (err) => console.log(err));
output
undefined