Node.JS App works, but then after a few hours get "Error: read ECONNRESET"

21 Views Asked by At

I’m new to hosted JavaScript / MySQL apps.

On my Render.com dashboard I’ve got a web service (starter tier) and a MySQL instance.

I wrote a simple node.js “hello world” app and it works, at first.

Here is a screenshot of my logs tab:

enter image description here

The messages with timestamp "Sept 30 11:48:13" are console logs from my app where everything is working as expected after I go to the URL for the app.

Then at the timestamp “Sept 30 12:42:43” the “Error: read ECONNRESET” shows up. Between these timestamps, I didn’t touch anything.

When the error occurs in the logs, attempting to access the URL of the app in a browser resulted in an "internal server error"

On the render.com dashboard I tried clicking the "restart web service" button and that resolve the problem but it was only temporary. As show in the log screenshot, after an hour or so the "ECONNRESET error" appeared in the logs and going to the URL of the app resulted in "502 bad gateway error".

In this case, clicking the browser refresh button a half-dozen times or so resulted in the app responding as expected. That's where I left off on troubleshooting and based on past experience I suspect this is temporary and the "ECONNRESET error" will eventually show up in the logs again.

I’ve researched the Error: read ECONNRESET error which sounds like it is usually caused my some kind of disruption in the network connection between the app and the database, but since I’m pretty new to this I am not sure if that could be caused by a mistake in my code or if it’s an environment issue.

Can anyone advise on this? The code for my simple program is below. Thanks much for any help anyone can offer!

------MY CODE----

    //IMPORT NODE PACKAGES
      import express from "express";
        const app = express();
        const port = 3000;

        import mysql from "mysql2";


    //DEFINE VARIABLES
      var sql; 

    //MySQL Database Setup
        var dbcon = mysql.createConnection({
          host: "localhost",
          user: "root",
          password: "Mwm1o2o3#"
        });

        //Create Database
        dbcon.connect(function(err) {
          if (err) throw err;
          dbcon.query("CREATE DATABASE IF NOT EXISTS mydb", function (err, result) {
            if (err) throw err;
            console.log("Database created (if it didn't already exist)");
          });
        });

        dbcon.connect(function(err) {
          if (err) throw err;
          dbcon.query("USE mydb", function (err, result) {
            if (err) throw err;
            console.log("mydb database now active");
          });
        });


        dbcon.connect(function(err) {
          if (err) throw err;
          sql = "DROP TABLE IF EXISTS customers";
          dbcon.query(sql, function (err, result) {
            if (err) throw err;
            console.log("customer table dropped (if it existed)");
          });
        });

        dbcon.connect(function(err) {
          if (err) throw err;
          sql = "CREATE TABLE IF NOT EXISTS customers (name VARCHAR(255), address VARCHAR(255))";
          dbcon.query(sql, function (err, result) {
            if (err) throw err;
            console.log("customer table created");
          });
        });






    //HTTP RESPONSE CODES
        app.get("/", (req, res) => {
          
          dbcon.connect(function(err) {
            if (err) throw err;
            sql = "INSERT INTO customers (name, address) VALUES ('Company Inc', 'Highway 37')";
            dbcon.query(sql, function (err, result) {
              if (err) throw err;
              console.log("1 record inserted");
              //res.send("1 record inserted");  
            });
          });


          dbcon.connect(function(err) {
            if (err) throw err;
            dbcon.query("SELECT * FROM customers", function (err, result, fields) {
              if (err) throw err;
              console.log(result);
              res.send(result);
            });
          });


          // console.log("Complete!");
          // res.send("complete!");
        

        });
0

There are 0 best solutions below