title: nodejs server recovering from crash
body: <p>I'm attempting to build resilient and fault-tolerant services, As a test, I'm intentionally causing it to crash with an undefined variable outside the try-catch block.</p>
  
  <p>I'm calling this service locally via nodejs http client, first I intentionally cause the error and then I continue to call the service.</p>
  
  <p><strong>First time the error is</strong> <code>ECONNRESET</code></p>
  
  <pre><code>{
      "code": "ECONNRESET",
      "message": "socket hang up",
      "stack": "Error: socket hang up\n    at createHangUpError (_http_client.js:323:15)\n    at Socket.socketOnEnd (_http_client.js:426:23)\n    at Socket.emit (events.js:203:15)\n    at endReadableNT (_stream_readable.js:1145:12)\n    at process._tickCallback (internal/process/next_tick.js:63:19)"
  }
  </code></pre>
  
  <p>Calling the same service second-time returns <code>ECONNREFUSED</code></p>
  
  <pre><code>{
      "code": "ECONNREFUSED",
      "message": "connect ECONNREFUSED 172.20.0.4:3000",
      "stack": "Error: connect ECONNREFUSED 172.20.0.4:3000\n    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14)"
  }
  </code></pre>
  
  <p>Microservice - Code with intentional error</p>
  
  <pre><code>function getEmployee() {
      return new Promise(function (resolve, reject) {
          let sql = "SELECT * FROM employee";
          db.query(sql, function(error, result){
              console.log(a); // undefined var
              try {
                  if(error) {
                      reject({status: 500, result: error});
                  } else {
                      resolve({status: 200, result: result});
                  }
              } catch(err) {
                  reject({status: 500, result: err});
              }
          });
      });
  };
  </code></pre>
  
  <p>Code that's calling the Microservice endpoint</p>
  
  <pre><code>function getEmployee() {
      return new Promise(function (resolve, reject) {
          http.request({
              method: "GET",
              hostname: "localhost",
              port: 3000,
              path: "/employees"
          }, function(response){
              let buffers = [];
              response.on("data", buffers.push.bind(buffers));
              response.on("end", function(){
                  let data = JSON.parse(Buffer.concat(buffers).toString());
                  response.statusCode == 200 ? resolve(data) : reject(data);
              });
          }).on("error", reject).end();
      });
  };
  </code></pre>
  
  <p>Question is, how do I approach it that nodejs server recovers from errors and subsequent calls to this endpoint or other endpoints go through?</p>