I’m recently starting to learn about HTTP encoding types, and I was looking at the chunked encoding type. I noticed that it is often used to send files over HTTP, in a way like so
let http = require('http')
let fs = require('fs')
server = http.createServer(function(req, res){
console.log(`req made ${req.url}`)
let readStream = fs.createReadStream(__dirname + '/file.txt')
readStream.pipe(res);
})
server.listen(3000, '127.0.0.1')
My question is, why do we need/use chunked encoding to perform this? Could we not simply just stat the file to get the size, and then stream it over the wire as bytes, and then the server receiving it can do what it wants with the data as it comes in?
Additionally could someone give a little bit of insight between HTTP/1.1 and HTTP/2 and how streaming/chunking data differ? My understanding is that HTTP sends the data in data frames so that the data is chunked regardless, which seems like a solution to the flaw I am describing with the logic in #2. Any extra help is appreciated!