title: Node.Js- Error: Connection lost: The server closed the connection
body: <p>Two weeks ago I deployed my first application as part of my new job (Woo Hoo! :)), but every night since I deployed it- the app crashes.
  The server-side was built with Node.js, and I'm using XAMPP and MYSQL to run the DataBase.
  So this is the error I continuously get:
  <a href="https://i.stack.imgur.com/WomeW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WomeW.png" alt="enter image description here" /></a></p>
  <p>I was searching for a solution, and found this code:</p>
  <pre><code>var connection;
  function handleDisconnect() {
      connection = mysql.createConnection(con); // Recreate the connection, since
                                                      // the old one cannot be reused.
    
      connection.connect(function(err) {              // The server is either down
        if(err) {                                     // or restarting (takes a while sometimes).
          // console.log('error when connecting to db:', err);
          setTimeout(handleDisconnect, 2000); // We introduce a delay before attempting to reconnect,
        }                                     // to avoid a hot loop, and to allow our node script to
      });                                     // process asynchronous requests in the meantime.
                                              // If you're also serving http, display a 503 error.
      connection.on('error', function(err) {
      //   console.log('db error', err);
        if(err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually
          handleDisconnect();                         // lost due to either server restart, or a
        } else {                                      // connnection idle timeout (the wait_timeout
          throw err;                                  // server variable configures this)
        }
      });
    }
    
    handleDisconnect();
  </code></pre>
  <p>I pasted this code after the createConnection function:</p>
  <pre><code>var con = mysql.createConnection({
      host: &quot;EXAMPLE FOR STUCK OVER FLOW&quot;,
      user: &quot;EXAMPLE FOR STUCK OVER FLOW&quot;,
      password: &quot;EXAMPLE FOR STUCK OVER FLOW&quot;,
      database: &quot;EXAMPLE FOR STUCK OVER FLOW&quot;
  });
  </code></pre>
  <p>I will really appreciate for your advice!</p>
  <p>Thanks!!!</p>