title: Catching ECONNREFUSED in node.js with http.request?
body: <p>I'm trying to catch ECONNREFUSED errors when using a HTTP client in node.js. I'm making requests like this:</p>
  
  <pre><code>var http = require('http');
  var options = { host: 'localhost', port: '3301', path: '/', method: 'GET' };
  
  http.request(options).on('response', function (res) {
    // do some stuff
  });
  </code></pre>
  
  <p>I can't figure out how to catch this error:</p>
  
  <pre><code>Error: connect ECONNREFUSED
    at errnoException (net.js:614:11)
    at Object.afterConnect [as oncomplete] (net.js:605:18)
  </code></pre>
  
  <p>If I do <code>request.on('error', function () {});</code>, it doesn't catch it. If I do it like this:</p>
  
  <pre><code>var req = request.on(etc)
  req.on('error', function blah () {});
  </code></pre>
  
  <p>Then I get <code>TypeError: Object false has no method 'on'</code>.</p>
  
  <p>Do I really have to do a top-level uncaught error thing to deal with this? At the moment whatever I do my whole process quits out.</p>
  
  <p>Edit: I found some blog posts on how to do it by creating a <code>connection</code> object, calling <code>request</code> on that, and then binding to errors on the <code>connection</code> object, but doesn't that make the entire <code>http.request()</code> shortcut useless?</p>