Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • PDF Invoice Generator
Menu

Node.js Express Tutorial to Read & Write Stream of Data From Buffer to Server Using Pipes in Browser

Posted on March 20, 2023

 

 

Welcome folks today in this blog post we will be looking at how to read and write stream of data from buffer to server using pipes in browser in node.js and express. All the full source code of the application is shown below.

 

 

Get Started

 

 

In order to get started you need to make a new node.js project and then we need to copy paste the below code inside the index.js file as shown below

 

 

index.js

 

 

JavaScript
1
2
3
4
5
let fs = require('fs')
 
let data = fs.readFileSync('file.txt','utf-8')
 
console.log(data)

 

 

So as you can see in the above code we are importing the fs module and then we are using the readFileSync() method to read the entire content of the text file at once and also we are passing the utf-8 encoding type for the plain text. This is not the recommended way to read data as the size of data increases then it will take more time for data fetching.

 

 

Using Buffers & Streams

 

 

Now to tackle the above problem we can use the concept of buffers to push data inside the memory and create a readable stream from the file.txt and push that data to the writable stream as shown below

 

 

JavaScript
1
2
3
4
5
6
7
8
let fs = require('fs')
 
let readStream = fs.createReadStream('file.txt','utf-8')
 
readStream.on('data',(chunk) => {
    console.log("data chunk")
    console.log(chunk)
})

 

 

As you can see we have modified the code to use the createReadStream() method inside the fs module to create the readable stream of the plain text file where small chunks of data will be stored in the form of buffer in the memory and then we have the on() method where we can listen for various events such as data event where this chunk will be returned in the callback function

 

Similarly we can other events too for error and end event as shown below

 

 

JavaScript
1
2
3
4
5
6
7
readStream.on('end',() => {
    console.log("all content of file is read")
})
 
readStream.on('error',(err) => {
    console.log("some error takes place " + err)
})

 

 

Write Streams of Data to File

 

 

Now guys we can create the writeStream similarly and push the entire content of the file.txt chunk by chunk to a new output.txt file using the write() method as shown below

 

 

JavaScript
1
2
3
4
5
6
7
8
let readStream = fs.createReadStream('file.txt','utf-8')
let writeStream = fs.createWriteStream('output.txt')
 
readStream.on('data',(chunk) => {
    console.log("data chunk")
    console.log(chunk)
    writeStream.write(chunk)
})

 

 

Using Pipes to Read & Write Streams

 

 

We can use pipes to eliminate the above code to do the task of reading and writing in one line of code as shown below

 

 

JavaScript
1
2
3
4
let readStream = fs.createReadStream('file.txt','utf-8')
let writeStream = fs.createWriteStream('output.txt')
 
readStream.pipe(writeStream)

 

 

 

Pushing Stream of Data to Browser

 

 

Now we can create a simple express server and push the content of the file.txt file inside the browser using pipes as shown below

 

 

JavaScript
1
2
3
4
5
6
7
8
9
10
11
const express = require('express')
const app = express()
let fs = require('fs')
 
let readStream = fs.createReadStream('file.txt','utf-8')
 
app.get('/',(req,res) => {
    readStream.pipe(res)
})
 
app.listen(5000)

 

 

node index.js

 

 

 

Recent Posts

  • Node.js Express Project to Remove Background of Images Using Rembg & Formidable Library in Browser
  • Node.js Tutorial to Remove Background From Image Using Rembg & Sharp Library in Command Line
  • Python 3 Flask Project to Remove Background of Multiple Images Using Rembg Library in Browser
  • Python 3 Rembg Library Script to Bulk Process Multiple Images and Remove Background in Command Line
  • Python 3 Rembg Library Script to Remove Background From Image in Command Line
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme