title: ExpressJS: How to know when a request has finished?
body: <p>In an ExpressJS set up on top of NodeJS, I have a bit of code like this:</p>
  
  <pre><code>app.get('/keys/readresults/:keyname', basic_auth, function(req,res){
      res.writeHead(200, {'content-type': 'text/json'});
      setInterval(
          go_to_database_and_get('stuff', function (database_replies) {
          res.write(database_replies)
          })
      ,5000);
  });
  </code></pre>
  
  <p>The code is wrote like that for simplicity (if anyone wants the real code I'm happy to post it in some pastebin).</p>
  
  <p>What happens when I <code>curl -u username:password <a href="http://localhost:4000/keys/readresults/key" rel="noreferrer">http://localhost:4000/keys/readresults/key</a></code> is exactly what I wanted to happen: I get a 200 status header, it checks the database for any results I want and writes the response back to me, then waits 5 seconds and searches for new results in the database and writes the response again, and so on every interval of 5 seconds.</p>
  
  <p>The problem is that when I exit curl, the loop keeps on going forever. <strong>How do I tell express or nodejs that it's supposed to end that loop</strong> (with clearInterval I guess) <strong>as soon as the request has ended ?</strong></p>