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

  Рет қаралды 10,730

procademy

procademy

Күн бұрын

Пікірлер: 25
@iqronegoro1538
@iqronegoro1538 Жыл бұрын
very very very very very very very very clear explanation!
@akhilkamath1266
@akhilkamath1266 Жыл бұрын
amazing explanation!
@littlewonder8641
@littlewonder8641 Жыл бұрын
Superb explanations and content materials.
@sravanakumarpulivendula8573
@sravanakumarpulivendula8573 Жыл бұрын
Hello Sir, minor correction. above example res.end() should be call rs.on('end') event otherwise completed data will not send to the client.
@sajidalikhan5872
@sajidalikhan5872 Жыл бұрын
@@Uiosrtk like this const server = http.createServer((req, res) => { let rs = fs.createReadStream("output.txt"); rs.on("data", (chunk) => { console.log("reading data"); res.write(chunk); }); res.on("end", () => { console.log("end response"); res.end(); }); rs.on("error", (error) => { res.end(error.message); }); });
@sajidalikhan5872
@sajidalikhan5872 Жыл бұрын
instead of res.end do rs.end
@karimmuhammad7051
@karimmuhammad7051 Жыл бұрын
in this case, so what is difference between this way and `readFile`? both of them wait read completly file then send back!
@spatialnasir
@spatialnasir Жыл бұрын
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: drive.google.com/file/d/1VgzCFDkqgfrTCkcuj2MJVSQtd2mQzN1X/view?usp=sharing 2. I modified my code as follows: let part = 0; server.on('request', (req, res)=>{ let rs = fs.createReadStream('./Files/large-file.txt'); rs.on('data', (chunk)=>{ res.write(chunk); res.write(`............THIS CHUNK IS PART ${part}...............`); part += 1; }); rs.on('error', (error=>res.end(error.message))) // 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.
@mayanksinghal6794
@mayanksinghal6794 Жыл бұрын
Hello, I am using your code. And with even streaming, my page is kept on loading the content for few mins. Can you help me to think why?
@sajidalikhan5872
@sajidalikhan5872 Жыл бұрын
@@mayanksinghal6794 const fs = require("fs"); const http = require("http"); const server = http.createServer((req, res) => { let rs = fs.createReadStream("outputu.txt"); rs.on("data", (chunk) => { console.log("reading data"); res.write(chunk); }); rs.on("end", () => { console.log("end response"); res.end(); }); rs.on("error", (error) => { res.end(error.message); }); }); server.listen("4000", "localhost", () => { console.log("Received request"); });
@MayankSingh-ux9iz
@MayankSingh-ux9iz Жыл бұрын
@@mayanksinghal6794 Same query? Does anybody knows why it happens?
@dmitryfateev378
@dmitryfateev378 Жыл бұрын
This txt file with 1kk lines is around 80mb. My Chrome runs out of memory and crashes by trying to render the ton of text. Maybe that's the issue with "your page's loading for sooo long" in some way.
@吳冠賢-t1v
@吳冠賢-t1v Жыл бұрын
There is a little advise that you can print the new line by adding at head and tail of res.write(`............THIS CHUNK IS PART ${part}...............`);
@muzamilhussain6459
@muzamilhussain6459 4 ай бұрын
from where i can get the large-file.txt because it is not present in the github repo
@mohitpandya_2228
@mohitpandya_2228 8 ай бұрын
This data is practically too long to handle. I would suggest all the fellow coders to keep the data upto 50000 or 100000 to have a better result. Thank you. Happy learning :)
@TheAbiGeorge
@TheAbiGeorge 6 ай бұрын
Thank you 😍
@rahulrock5282
@rahulrock5282 27 күн бұрын
Very very nice bro i want to do file download like 1 gb data i have to query from db how to do it..
@hassannouri9796
@hassannouri9796 9 ай бұрын
❤❤❤
@mayanksinghal6794
@mayanksinghal6794 Жыл бұрын
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"); })
@didyouknow449
@didyouknow449 8 ай бұрын
that's because you are displaying ("work is done") and not the data , to fix it you need to call the end event in your case , rs.on("end", ()=>{res.end()])
@shadabkhan681
@shadabkhan681 2 жыл бұрын
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. (C:\Users\Shahdab\Documents\Shadab odejs\app.js:54:6) 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 internal/fs/streams.js:226:14 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 (internal/process/task_queues.js:81:21) { code: 'ERR_STREAM_WRITE_AFTER_END' My code is same as yours code: server.on("request", (req, res) => { let rs = fs.createReadStream('file.txt') rs.on('data',(chunk)=>{ res.write(chunk) res.end() }) rs.on('error',(err)=>{ res.end(err.message) }) })
@sajidalikhan5872
@sajidalikhan5872 Жыл бұрын
Do like this const server = http.createServer((req, res) => { let rs = fs.createReadStream("output.txt"); rs.on("data", (chunk) => { console.log("reading data"); res.write(chunk); }); res.on("end", () => { console.log("end response"); res.end(); }); rs.on("error", (error) => { res.end(error.message); }); });
@sajidalikhan5872
@sajidalikhan5872 Жыл бұрын
instead of res.end do rs.end
A Deep Dive into Node.js Streams | Masterclass
1:15:46
Platformatic
Рет қаралды 14 М.
BAYGUYSTAN | 1 СЕРИЯ | bayGUYS
36:55
bayGUYS
Рет қаралды 1,9 МЛН
VIP ACCESS
00:47
Natan por Aí
Рет қаралды 30 МЛН
黑天使只对C罗有感觉#short #angel #clown
00:39
Super Beauty team
Рет қаралды 36 МЛН
I built the same app 10 times // Which JS Framework is best?
21:58
Fireship
Рет қаралды 2,6 МЛН
Building Real-time Apps with Go | Azim Pulat
54:58
Azim Pulat
Рет қаралды 93 М.
Learn Node.js Streams in 25 minutes | NodeJS Tutorials for Beginners
25:02
All you need to know about "child_process" in Node.js
12:38
Software Developer Diaries
Рет қаралды 10 М.
Node.js Tutorial - 24 - Streams and Buffers
9:49
Codevolution
Рет қаралды 82 М.