Node.js, like any back-end language, offers a simple file reading and writing module. And like most Node data modules, it uses streams abstraction to communicate between services. Streams are one of those core node ideas that are found in most data services. There are two types of Streams: readable and writeable streams. Each has its own events, behaviours and can be controlled with event listeners to your own needs. You can listen to events like connections, data, stream ending and various others (depending on the module).
However, data transfer usually come with some problems. Say you have a client to whom the server is sending data. If you happen to be reading data from an outside source, you will be doing two processes. Reading that data and sending it to the client. At certain points, you will find the client to be consuming the data slower than you are sending it. If this behaviour isn’t checked, you will end up with a slow client problem. Fortunately, Node allows you to pause streams and listen for any drained data events from client writing process.
readFile.pause(); //if we aren't writing to the client, dont read anything!
That is it; except for a simple error that I mentioned in the first paragraph. Can you spot it?
Hint #1: Try refreshing the page.
Hint #2: Your node service needs to create a new read stream for each request.
Philip Roberts: Help, I’m stuck in an event-loop. from Neo (UK) on Vimeo.