title: How do I return 304 Unmodified status with Express.js?
body: <p>I'm using node and express for the back end of an iOS application. Data is stored in a SQL Server database, so iOS apps query the server, the server queries the database, server receives the db response, and then forwards the response to the iOS application. I'm trying to figure out how caching works though. I'm serving a lot of static content - blog articles for example. So I planned to use etags, but I'm not sure how it's supposed to work. I make a request, get content, and cache the response on the client side. Ok. Then I make the same request later with the etag of the previous response stored in the 'If-None-Match' header. Then what? </p>
  
  <p>Does Express.js handle this automatically? It doesn't seem to - I couldn't get it to generate a 304 response. If I try to check the response headers before I send the response, I get null, so I can't get the etag of a response before I send it. So how am I supposed to compare the request etag with the etag of the content that the server would send back? Am I supposed to use custom generated etags and cache these on the server, then compare request etags with this cache?</p>
  
  <p>Below is a very simple route I set up to test this, no database involved. I just send a number to the server, and it returns the square. If I send a request with an etag to the same url, I will get the same response. I can check the 'If-None-Match' header of the request, but to what do I compare it in order to determine if I should send a 304 instead of a 200 status?</p>
  
  <pre><code>router.use("/square/:testId", function(req, res) {
  
    var obj = {};
    obj["testId"] = req.params.testId;
    obj["result"] = req.params.testId * req.params.testId;
  
    res.setHeader('Cache-Control', 'public, max-age=5');
  
    var h2 = JSON.stringify(res.headers,null,2);
    console.log("The response headers: " + h2);
    //Prints null
  
    res.status(200).send(obj);
  });
  </code></pre>