Optimizing performance with streaming
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
response. 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 one piece at a time.
We are building on our code from the last example, so let's get
server.js, index.html, styles.css, and
How to do it...
We will be using
fs.createReadStream to initialize a stream, which can be piped to the
response object. In this case, implementing
fs.createReadStream within our
cacheAndDeliver function isn't ideal because the event ...