title: Detecting successful read stream open
body: <p>I'm implementing cache for static serving middleware for Express.js, which works as follows — when request comes, middleware first tries to serve file from filesystem, and if there is none, file is fetched from upstream and stored in file system.</p>
  
  <p>Problem is I don't know how to properly detect “cache hit” event.</p>
  
  <pre><code>staticMiddleware = function(req, res, next) {
      // try to read file from fs
      filename = urlToFilename(req.url);
      stream = fs.createReadStream(filename);
  
      // cache miss - file not found
      stream.on('error', function() {
          console.log('miss ' + req.url);
  
          // get file from upstream, store it into fs and serve as response
          stream = fetchFromUpstream(url);
          stream.pipe(fs.createWriteStream(filename));
          stream.pipe(res);
      });
  
      // cache hit - file is being read
      I_DONT_KNOW_WHAT_TO_PUT_HERE(function() {
          console.log('hit ' + req.url);
  
          stream.pipe(res);
      });
  }
  </code></pre>
  
  <p>So, basically, how can I detect succesful file reading? If I listen to 'data' event, I guess I miss first chunk of data. If I just <code>pipe()</code> it to response, response stream gets closed on error, and I can't serve it with fetched data, and this approach really lacks flexibility. I wonder if there is way to listen for event like <code>fdcreated</code> or <code>opened</code> or similar, or way to push back data I've got in <code>data</code> event, so it will be resent in next <code>data</code> event.</p>