title: How to find the source of a connect ECONNREFUSED error in node.js?
body: <p>I've inherited a nodejs application with reasonable complexity. It interacts with 5 other applications through various network interfaces, mostly HTTP REST APIs. Every now and then I run into an issue where an error is thrown, and while its handled to prevent the application from crashing, I can't tell where the error is coming from. Here's a typical amount of information I get from this error:</p>
  
  <ul>
  <li>connect ECONNREFUSED</li>
  <li><a href="https://github.com/joyent/node/blob/v0.12.2/lib/util.js#L746" rel="nofollow noreferrer">util.js:L746</a></li>
  <li><a href="https://github.com/joyent/node/blob/v0.12.2/lib/net.js#L1000" rel="nofollow noreferrer">net.js:1000</a></li>
  </ul>
  
  <p>And that's it. That's the entire message and stacktrace (message is actually duplicated in <code>errno=ECONNREFUSED</code> and <code>syscall=connect</code>, not helping). I'm using nodejs 0.12.2 and linked the stacktrace lines to the source accordingly. I've read the source but didn't get anywhere.</p>
  
  <p>I've also looked through the many questions here on SO related to <code>ECONNREFUSED</code>, but those always come with a code example. If I knew what part of my applicatino is response for the network request, I could fix it.</p>
  
  <p>So my question is, how can I instrument a node application to find out where failed requests like this come from?</p>
  
  <p>PS: I've also looked at <a href="https://stackoverflow.com/questions/1911015/how-to-debug-node-js-applications?rq=1">the recommendations for debugging nodejs applications</a>, but didn't find anything answering my question. </p>