#23 Understanding Streams in Practice | Fundamentals of NODE JS | A Complete NODE JS Course

preview_player
Показать описание
In the last lecture we theoretically learned what is streams. In this lecture, let's understand how to create and use streams with some practical examples.

Рекомендации по теме
Комментарии
Автор

Superb explanations and content materials.

littlewonder
Автор

very very very very very very very very clear explanation!

iqronegoro
Автор

Hello Sir, minor correction. above example res.end() should be call rs.on('end') event otherwise completed data will not send to the client.

sravanakumarpulivendula
Автор

Hey sir, My content of the input.txt file is not displaying on the local host screen. I have tried to change the file, and also my location of the file is correct. res.end is displayed but res.write is not displayed on the local host screen.

HERE'S MY CODE:

server.on("request", (req, res)=>{
let rs= fs.createReadStream("./files/start.txt", "utf-8")

rs.on('data', (chunk)=>{
res.write(chunk)
})

res.end("work is done");
})

mayanksinghal
Автор

This data is practically too long to handle. I would suggest all the fellow coders to keep the data upto 50000 or to have a better result. Thank you. Happy learning :)

mohitpandya_
Автор

This is a wonderful explanation.

However, it's a bit difficult to appreciate the streaming if you're not seeing the beginning and end of each stream chunk. So, here's a little improvement I tried:

1. I created a txt file that contains the exact text used by the author but each line is numbered. Here's the link to download it:

2. I modified my code as follows:
let part = 0;
server.on('request', (req, res)=>{
let rs =

rs.on('data', (chunk)=>{
res.write(chunk);
CHUNK IS PART
part += 1;
});

rs.on('error',

// res.end('All done!');

})

Here, I added a variable called 'part' to keep track of how many chunks have been streamed.
After writing each chunk, I write the current "part" to display the line where the chunk stopped.
Then I increment the part "variable"

For me, each chunk was about 680 lines long.


I hope you also find it helpful.

geoafrikana
Автор

will you please tell me why I'm getting this error:
events.js:292
throw er; // Unhandled 'error' event
^

Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at writeAfterEnd (_http_outgoing.js:668:15)
at write_ (_http_outgoing.js:680:5)
at ServerResponse.write (_http_outgoing.js:661:15)
at ReadStream.<anonymous>
at ReadStream.emit (events.js:315:20)
at addChunk (_stream_readable.js:309:12)
at readableAddChunk (_stream_readable.js:284:9)
at ReadStream.Readable.push (_stream_readable.js:223:10)
at
at FSReqCallback.wrapper [as oncomplete] (fs.js:539:5)
Emitted 'error' event on ServerResponse instance at:
at writeAfterEndNT (_http_outgoing.js:727:7)
at processTicksAndRejections {
code: 'ERR_STREAM_WRITE_AFTER_END'
My code is same as yours code:

server.on("request", (req, res) => {
let rs =
rs.on('data', (chunk)=>{
res.write(chunk)
res.end()
})

rs.on('error', (err)=>{
res.end(err.message)
})
})

shadabkhan