| Start | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Read Data from TextFile with Streams I/O in node is asynchronous, so interacting with the disk and network involves passing callbacks to functions. You might be tempted to write code that serves up a file from disk like this: This code works but it's bulky and buffers up the entire data.txt file into memory for every request before writing the result back to clients. If data.txt 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. Luckily both of the (req, res) arguments are streams, which means we can write this in a much better way using fs.createReadStream() instead of fs.readFile(): Here .pipe() takes care of listening for 'data' and 'end' events from the fs.createReadStream(). This code is not only cleaner, but now the data.txt file will be written to clients one chunk at a time immediately as they are received from the disk. |
||||||||||
| Piping streams Readable streams can be "piped," or connected, to writable streams. This makes data flow from the source stream to the destination stream without much effort. When writable streams are also readable streams, i.e. when they're duplex streams, you can continue piping it to other writable streams. Readable streams can also be piped into multiple streams. Note that you must pipe to the output streams synchronously (at the same time) before any data 'flows'. Failure to do so might lead to incomplete data being streamed. Also note that stream objects can emit |
||||||||||
| Creating your own readable/writable stream We will see stream objects being returned by modules like fs etc but what if we want to create our own streamable object. To create Stream object we need to use the stream module provided by NodeJs This will give us our own custom writable stream. we can implement anything within the _write function. Above method works in NodeJs 4.x.x version but in NodeJs 6.x ES6 introduced classes therefore syntax have changed. Below is the code for 6.x version of NodeJs |
||||||||||
| Why Streams? Lets examine the following two examples for reading a file's contents: The first one, which uses an async method for reading a file, and providing a callback function which is called once the file is fully read into the memory: And the second, which uses It's worth mentioning that both examples do the exact same thing. What's the difference then?
When the files you deal with are small then there is no real effect when using Without With The above example does not illustrate how I would like to download a
Here's a [small file][1], which is stored in my This is how it looks using Yep, it's not faster when dealing with small files - the tested file weights
Parameters
|
Read Article
https://develop.horje.com/learn/1434/reference