title: Get status code in express middleware
body: <p>I tried to cache some request to static files which can be served by nginx directly by middleware.</p>
  
  <p>Core codes:</p>
  
  <pre><code>function PageCache(config) {
      config = config || {};
      root = config.path || os.tmpdir() + "/_viewcache__";
      return function (req, res, next) {
          var key = req.originalUrl || req.url;
          var shouldCache = key.indexOf("search") &lt; 0;
          if (shouldCache) {
              var extension = path.extname(key).substring(1);
              if (extension) {
  
              } else {
                  if (key.match(/\/$/)) {
                      key = key + "index.html"
                  } else {
                      key = key + ".html";
                  }
              }
  
              var cacheFilePath = path.resolve(root + key)
              try {
  
                  res.sendResponse = res.send;
                  res.send = function (body) {
                      res.sendResponse(body);
  
                      // cache file only if response status code is 200
                      cacheFile(cacheFilePath, body);
                  }
              }
              catch (e) {
                  console.error(e);
              }
          }
          next()
      }
  }
  </code></pre>
  
  <p>However I found that all the response are cached no matter the status code, while respone with code 404,410,500 or something else should not be cached.</p>
  
  <p>But I can not find any api like <code>res.status</code> or <code>res.get('status')</code> which can be used to get the status code of the current request.</p>
  
  <p>Any alternative solution?</p>