title: Async https requests in node.js
body: <p>I've got an app that fires off https requests to a 3rd party service to perform logging. For some reason or another I noticed a few hours ago that the 3rd party is refusing the connections. That's a different topic though.</p>
  
  <p>What I'm noticing in my node.js app is that despite the requests being created dynamically, and therefore shouldn't be hanging the entire app, they are.</p>
  
  <p>After a restart of the process, the https request is hanging and then dies with a connection refused error. Sample code is below. </p>
  
  <pre><code>var logger = function(data){
  
      var req = https.request({
          host: 'logs.loggly.com',
          port: 443,
          path: '/inputs/&lt;my real key is here, removed obviously&gt;',
          method: 'POST',
          headers:{
              'content-type': 'application/json'
          }
      }, function(res){
          var body = [];
          res.on('chunk', function(data){
              body.push(data);
          });
          res.on('end', function(){
              console.log(body.join(''));
          })
      });
      req.write(JSON.stringify(data));
      req.end();
  
  }
  </code></pre>
  
  <p>And that's called simply by:</p>
  
  <pre><code>logger({ 'test': 'datafoo' });
  </code></pre>
  
  <p>So I'm curious why a connection refused/timeout from this outbound https request should be hanging and then crashing the entire app. </p>
  
  <p>Thanks!</p>