Caching content certainly improves upon reading a file from disk for every request. However, with
fs.readFile, we are reading the whole file into memory before sending it out in a
response object. For better performance, we can stream a file from disk and pipe it directly to the
response object, sending data straight to the network socket a piece at a time.
We are building on our code from the last example, so let's get
We will be using
fs.createReadStream to initialize a stream, which can be piped to the
If streaming and piping are new concepts, don't worry! We'll be covering streams in depth in Chapter 5 ...