title: Node.js https.request with keep-alive header
body: <p>I want to pull posts from a users Facebook wall.  </p>
  
  <p>The following code snippet works, but it never terminates:</p>
  
  <pre><code>var https = require('https');
  
  facebookWall = function(user) {
    var options = {
      host: 'graph.facebook.com',
      port: 443,
      path: '/me/home?access_token=' + user.facebook_token + '&amp;since=' + encodeURIComponent(user.facebook_timestamp),
      method: 'GET',
      headers: {
        'Connection':'keep-alive'
      }
    };
  
    var req = https.request(options)
    .on('response', function(response) {
      var body = '';
      response.on('data', function(data) {
        body += data;
        try {
          var wallPosts = JSON.parse(body);
          console.log("user " + user.id + " has " + wallPosts.data.length + " new items on their wall");
        }
        catch (e) {
          //console.log("waiting for more data chunks...");
        }
      })
    });
    req.end();
  
    req.on('error', function(e) {
      console.error(e);
    });
  }
  </code></pre>
  
  <p>I think it is caused by the <code>'Connection':'keep-alive'</code> header.  When I replace it with <code>'Connection':'close'</code> the script will terminate when all data has been retrieved from facebook.    </p>
  
  <p>I'm hoping to be able to use the keep-alive header to prevent having to create a new SSL connection for each request.    I have thousands of requests and with the keep-alive header, it completes in just a few seconds, as opposed to a few minutes without the keep-alive  header.</p>
  
  <p>Does anyone know how to accomplish this?  I'm fairly new to Node.JS, so if I'm missing something obvious, I apologize.</p>