Explain the difference between readFile and createReadStream in Node.js.
Instruction: Compare readFile and createReadStream in terms of their operation and use cases.
Context: This question measures the candidate's understanding of the file system in Node.js, focusing on the differences between reading files using readFile and streams.
Updated
Official answer available
Read the opening below, then unlock the full answer and practical guidance.
readFile completes with the entire file as a Buffer or decoded string. It can perform several underlying reads; “whole file returned” does not mean one operating-system read. Its asynchronous forms avoid waiting synchronously on the calling JavaScript thread, but still retain the complete result. readFileSync also blocks that thread while reading.
createReadStream exposes chunks for incremental consumption...