very very very very very very very very clear explanation!
@akhilkamath1266 Жыл бұрын
amazing explanation!
@littlewonder8641 Жыл бұрын
Superb explanations and content materials.
@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 Жыл бұрын
@@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 Жыл бұрын
instead of res.end do rs.end
@karimmuhammad7051 Жыл бұрын
in this case, so what is difference between this way and `readFile`? both of them wait read completly file then send back!
@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 Жыл бұрын
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?
@@mayanksinghal6794 Same query? Does anybody knows why it happens?
@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 Жыл бұрын
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}...............`);
@muzamilhussain64594 ай бұрын
from where i can get the large-file.txt because it is not present in the github repo
@mohitpandya_22288 ай бұрын
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 :)
@TheAbiGeorge6 ай бұрын
Thank you 😍
@rahulrock528227 күн бұрын
Very very nice bro i want to do file download like 1 gb data i have to query from db how to do it..
@hassannouri97969 ай бұрын
❤❤❤
@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"); })
@didyouknow4498 ай бұрын
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()])
@shadabkhan6812 жыл бұрын
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 Жыл бұрын
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); }); });