Node.js · Middle · Question 11 of 75
Why use streams to send a 2 GB file instead of readFile?
- 1
readFilecannot read files larger than 1 MB - ✓Correct answer: Streams process the data in small chunks, so memory stays low and sending starts immediately
- 3Streams compress data automatically
Explanation
readFile loads the whole file into memory first. With fs.createReadStream(path).pipe(res) only a small buffer is in memory at any time.