Piping Readable Stream to Writable Stream

In this post you’ll learn how to pipe readable stream into writable stream.

Piping readable stream to standard output

var request = require('request');
var strm = request('https://techstackjournal.com/');
strm.pipe(process.stdout);
  • Line 1: Importing ‘request’ module which simplifies reading from http request. Make sure to download this module using npm install request
  • Line 2: Requesting response from a web address
  • Line 3: Piping the readable stream to standard output that is console

Piping readable stream to file

var request = require('request');
var fs = require('fs');
request('https://techstackjournal.com/').pipe(fs.createWriteStream('webpage.html'));
  • Line 1 & 2: Importing ‘request’ and ‘fs’ modules. The ‘fs’ module provides methods to interact with file system
    Line 3: Requesting response from a web address and piping the returned readable stream to writable stream of a file

Leave a Comment