In this post, we will see how pipe works and how we can use it.
First of all, what is a pipe?
The pipe() function reads data from a readable stream as it becomes available and writes it to a destination writable stream.
In the previous post Node.js – Streams, we have read a file and we have written the content in a new one, using this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | var fs = require( 'fs' ); var pathDir = __dirname; var finalReadPath = pathDir + '/README.md' ; var finalWritePath = pathDir + '/NewFile.txt' var fileReadStream = fs.createReadStream(finalReadPath, 'utf8' ); var fileWriteStream = fs.createWriteStream(finalWritePath, 'utf8' ); fileReadStream.on( 'data' , function (chunk) { fileWriteStream.write(chunk); }); fileReadStream.on( 'end' , function () { console.log( "End Read data from file." ) }); fileReadStream.on( 'error' , function (error) { console.log( "Attention! There is an error: " + error.stack); }); fileWriteStream.on( 'finish' , function () { console.log( "Write completed." ); }); fileWriteStream.on( 'error' , function (err) { console.log( "Attention! There is an error during the creation of the new file: " + error.stack); }); |
With the pipe function, we can simplify the code in this way:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | var fs = require( 'fs' ); var pathDir = __dirname; var finalReadPath = pathDir + '/README.md' ; var finalWritePath = pathDir + '/NewFile.txt' var fileReadStream = fs.createReadStream(finalReadPath, 'utf8' ); var fileWriteStream = fs.createWriteStream(finalWritePath, 'utf8' ); // when fileReadStream has some data, it pushes them into fileWriteStream fileReadStream.pipe(fileWriteStream); fileReadStream.on( 'end' , function () { console.log( "End Read data from file." ) }); fileReadStream.on( 'error' , function (error) { console.log( "Attention! There is an error: " + error.stack); }); |
Another script where we could use pipe(), is the script for creating a HTTP server, that we saw in the post How to create an HTTP server with Node.js.
We can modify the code in order to read the file README.md, using a Stream, and then, with the pipe() function, we can push the content into the response object, in order to display data.
We open the file called serverweb.js, and we modify the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | // import the module http var http = require( 'http' ); // inport the module fs var fs = require( 'fs' ); var pathDir = __dirname; var finalReadPath = pathDir + '/README.md' ; var finalWritePath = pathDir + '/NewFile.txt' // creation of the Server var serverweb = http.createServer( function (request, response){ // definition of the status code to send at the header var codhead= 200; // definition of the output message var message = "The call was done from " ; // with request.url it is possible to know the part of the url after domain switch (request.url) { case "/api" : message = message + "API" ; break ; case "/test" : message = message + "TEST" ; break ; case "/" : message = message + "ROOT" ; break ; default : message = "This position isn't known" ; codhead = 404; break ; } // it sends a status code and information to the client response.writeHead(codhead, { "Content-Type" : "text/html" }); // text shown in the page response.write( "<b>" + message + "</b>" ); response.write( "<br/>" ); // read the file README.md var fileReadStream = fs.createReadStream(finalReadPath, "utf8" ); // push data into response fileReadStream.pipe(response); }).listen(3366, '127.0.0.1'); // the server will listen on the port 3366 console.log( "server started" ) |
If we run the application, this will be the result: