Stream Pipes in NodeJs

Rubleen Kaur
Last Updated: May 13, 2022

Introduction

Streams are used for making a handy abstraction, and we can do a lot of new inventions using them. Streams are I/O abstractions. In this article, we will be describing the Stream.pipe() method. The Stream pipe method is used to attach a readable stream to the writable Stream. We use this method to switch in flowing mode and push all the data to the attached writable file. 

Let’s learn about the stream pipe method using elaborative examples.

 

Uses of Stream Pipes

The various uses of Stream pipes are:

  • Stream.pipe() method is used to attach a readable stream to the writable Stream.
  • Stream.pipe() method is also used in file streams in NodeJs.
  • Stream.pipe() can be used for HTTP requests and response objects.
  • Stream.pipe() method can be used to send incoming requests to a file logging.

Let’s see an example of these uses,

#!/usr/bin/env node

var child = require('child_process');

var myREPL = child.spawn('node');

myREPL.stdout.pipe(process.stdout, { endfalse });

process.stdin.resume();

process.stdin.pipe(myREPL.stdin, { endfalse });

myREPL.stdin.on('end'function() {

  process.stdout.write('REPL stream ended.');

});

myREPL.on('exit'function (code) {

  process.exit(code);

});

 

In the above example, we have seen that if we need to spawn a node child process and use the pipe for stdout and stdin methods.

For the following example, we will cover the use of pipes as file streams in NodeJs.

#!/usr/bin/env node

var child = require('child_process'),

    fs = require('fs');

var myREPL = child.spawn('node'),

    myFile = fs.createWriteStream('myOutput.txt');

myREPL.stdout.pipe(process.stdout, { endfalse });

myREPL.stdout.pipe(myFile);

process.stdin.resume();

process.stdin.pipe(myREPL.stdin, { endfalse });

process.stdin.pipe(myFile);

myREPL.stdin.on("end"function() {

  process.stdout.write("REPL stream ended.");

});

myREPL.on('exit'function (code) {

  process.exit(code);

});

 

In the example below, we can observe the easiest kind of proxy,

#!/usr/bin/env node

var http = require('http');

http.createServer(function(request, response) {

  var proxy = http.createClient(9000'localhost')

  var proxyRequest = proxy.request(request.method, request.url, request.headers);

  proxyRequest.on('response'function (proxyResponse) {

    proxyResponse.pipe(response);

  });

  request.pipe(proxyRequest);

}).listen(8080);

http.createServer(function (req, res) {

  res.writeHead(200, { 'Content-Type''text/plain' });

  res.write('request successfully proxied to port 9000!''\n'JSON.stringify(req.headers, true2));

  res.end();

}).listen(9000);

 

This article has explained the basics of using the Stream.pipe() method for effortlessly passing our data streams around. 

Frequently Asked Questions

  1. What are Streams in NodeJS?
    Ans: Streams are used for making a handy abstraction, and we can do a lot of new inventions using them. Streams are I/O abstractions.
     
  2. What is Stream.pipe() method?
    Ans: The Stream pipe method is used to attach a readable stream to the writable Stream. We use this method to switch in flowing mode and push all the data to the attached writable file.
     
  3. Why are the uses of the Stream.pipe() method?
    Ans: Few uses of Stream.pipe() method in NodeJs are :
             →  Stream.pipe() method is used to attach a readable stream to the writable Stream.
             →  Stream.pipe() method is also used in file streams in NodeJs.
             → Stream.pipe() can be used for HTTP requests and response objects.
             → Stream.pipe() method can be used to send incoming requests to a file logging.

Key Takeaways

Hey everyone, so let’s brief out the article. Let’s discuss in brief whatever we have discussed here. 

  • This article has tried to explain the basics of using the Stream.pipe() method for effortlessly passing our data streams around. 
  • We have discussed the four classic uses of the Stream.pipe() method in NodeJs using descriptive examples. 

 

Isn’t Web Development engaging? Building new websites and using amazing animations and different APIs, don’t be scared if you cannot grasp the hold of this vast development. We have the perfect web development course for you to make you stand out from your fellow developers. 

Happy Learning Ninjas!
 

Was this article helpful ?
0 upvotes