Table of contents
Driven by a passion for innovation and a relentless pursuit of excellence, I am excited about the challenges and opportunities that the world of full-stack development presents. Ready to contribute my skills to transformative projects, I am dedicated to creating robust and scalable solutions that elevate user experiences and drive technological advancements.Lets GooooπͺπΌπ
Fundamentals of nodeJS
Understanding streams in practice
From the code base from the previous lessons, lets comment out all the previous codes with an exception ππΌ
Lets create a new file large-files.txt
inside the files folder. Lets add some text content inside the file.ππΌ
What we want is that whenever a new request hits the server, we want to read this large-files.txt
and we want to send the content in the response. Lets try to do it using the conventional method first.
So every time a new request hits the server, the server will raise a request event. To listen to that events we use the on()
event emitter method. Here we want to listen to "request" events and when the events happens, we want to execute some logic to read the large-files.txt
and send in the response, we'll read the file asynchronously. ππΌ
Lets restart the server and checkout the response on the web page.ππΌ
The problem with the above approach is that when trying to read a much larger file, The data variable will store a very huge data, and once the data is completely read from large-files.txt
that data will be stored in the memory and from the memory that data will be sent in the response. With this solution, node will have to load the entire file into the memory because only after that can it send the data in the response. This will be a problem when the file is big and there are tons of request hitting our server.
We can use stream to fix the above problem. Lets update the code base using the readable and writable streams. We will create a readable stream using the createReadStream()
method and from there we will read the content of the large-file.txt
piece by piece. We will assign createReadStream()
method to a variable and also specify the large-file.txt
file path. When the readable stream starts reading data from large-file.txt
, it will read the data in chunks and pieces and every times it reads a new piece of data, it will emit a data event.
On the readable stream, we will listen to the data events and also execute a call back function that will perform some logic when the data events is raised. In the call back function we will receive the chunk of data which the readable stream have read. In this case we call the end()
method only after we have read all the chunks from the readable stream. so we use the write()
method instead. The write()
method can write multiple chunk of data in the response stream. When we receive the first chunk of data, the write()
method will write that data in the response stream and so on. It will keep on write the piece or chunk of data which we are receiving to this response stream, and when we have written all the chunk of data into the response stream, then we can call the end()
method just to signal that there is no more data to write in the response stream.ππΌ
Restart the server to see the response on the web pageππΌ
Just in case there is an error, we can handle the error by listening to the error events, we will use the on() method to listen to the error events as well. Lets update our code. ππΌ
That's all for this lesson.
Next: Understanding pipe() Method.
See you soonππ«‘βπΌ...