July 2012
Beginner to intermediate
342 pages
7h 27m
English
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 script.js ready.
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 ...
Read now
Unlock full access