Day 14 of 100days of code.

Day 14 of 100days of code.

Β·

2 min read

My goal is to seamlessly integrate this newfound backend expertise with my frontend skills, ultimately realizing my vision of becoming a well-rounded MERN stack developer. Lets GooooπŸ’ͺπŸΌπŸš€

Fundamentals of nodeJS

Understanding pipe() Method

From the previous lesson we had a bug when executing the end() method. Remember the end event is raised when there is no more data available in the readable stream to read. Lets update our code base to fix the bug. using the rs variable, we can also listen to the end() method. We are using the end() method on the response object to signify that we don't want to write any more data in the response stream. πŸ‘‡πŸΌ

Lets restart the server and run our code to see the response on the web page. still working πŸ‘‡πŸΌ

There is still some problem with our code. Back Pressure happens when the response cannot send the data nearly as fast as it is receiving it from the file. Remember, the response is the writable stream, what if we have a situation where by the readable stream is faster than the writable stream, this will overwhelm the writable stream which cannot handle the incoming data so fast. This problem is called back pressure. To fix this problem, we use the pipe() method.

The pipe() method allows us to pipe the output of a readable stream, write into the input of the writable stream. pipe() method is only available on the readable stream. The pipe() method helps us fix the problem of back pressure because it will automatically handle the speed of data coming in and the speed of data going out. We will receive the data in chunks.

Lets update the code base, restart the server and display the response on our web page πŸ‘‡πŸΌ

Still working πŸ‘‡πŸΌ

We are still streaming the data from our server and receiving the response in chunks but now, the speed of data going in and out is matching. The pipe() method provides two advantages, it fixes the problem of back pressure and secondly we only have to write few lines of code.

That's all for this lesson.

Next: Whats is NPM

See you soon😊🫑✌🏼...

Β