Luckily both of the has other benefits too, like handling backpressure automatically so that node won't buffer chunks into memory needlessly when the remote client is on a really slow or high-latency connection. We can just let oppressor handle all that content-encoding stuff.Once you learn the stream api, you can just snap together these streaming modules like lego bricks or garden hoses instead of having to remember how to push data through wonky non-streaming custom APIs.Streams make programming in node simple, elegant, and composable.
Streams are an important component of small-program design and unix philosophy but there are many other important abstractions worth considering.
Just remember that technical debt is the enemy and to seek the best abstractions for the problem at hand.
is very large, your program could start eating a lot of memory as it serves lots of users concurrently, particularly for users on slow connections.
The user experience is poor too because users will need to wait for the whole file to be buffered into memory on your server before they can start receiving any contents. Now our file is compressed for browsers that support gzip or deflate!
All the different types of streams use to a readable stream, the chunks you push are buffered until a consumer is ready to read them.
However, it would be even better in many circumstances if we could avoid buffering data altogether and only generate the data when the consumer asks for it. These extra complications are necessary when interfacing with the external operating system pipes but are automatic when we interface directly with node streams the whole time.If you want to create a readable stream that pushes arbitrary values instead of just strings and buffers, make sure to create your readable stream with .Streams come to us from the earliest days of unix and have proven themselves over the decades as a dependable way to compose large systems out of small components that do one thing well.In unix, streams are implemented by the shell with and you get a backpressure mechanism for free to throttle writes for slow consumers.Streams can help to separate your concerns because they restrict the implementation surface area into a consistent interface that can be reused.You can then plug the output of one stream to the input of another and use libraries that operate abstractly on streams to institute higher-level flow control.