Search code examples
node.jsreactjsexpressheroku

Heroku error: "connect ECONNREFUSED 127.0.0.1:3306" when trying to deploy Express + React application


I was trying to deploy my Express + React application to Heroku. Heroku connected successfully with my Github account, then clicking "Deploy Branch" led to "Your app was successfully deployed". But when I went to view my website, it showed:

"Application error An error occurred in the application and your page could not be served. If you are the application owner, check your logs for details".

Here are my logs:

Starting process with command `npm start`
> myproject@ start /app
> node backend/index.js
My project SQL server listening on PORT 4000
/app/backend/index.js:22
if (err) throw err;
^
Error: connect ECONNREFUSED 127.0.0.1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)

And the index.js which connects to MySQL:

const express = require('express');
const cors = require('cors');
const mysql = require('mysql');
const app = express();

app.use(cors());

app.get('/', (req, res) => {
    res.send('go to /my-project to see my project')
});

const pool = mysql.createPool({
    connectionLimit: 10,
    host: 'localhost',
    user: 'root',
    password: 'myjs123@',
    database: 'my-project',
    debug: false
});

pool.getConnection((err, connection) => {
    if (err) throw err;
    app.get('/my-project', (req, res) => {
        connection.query(SELECT_ALL_FACTS_QUERY, (err, results) => {
            if (err) {
                return res.send(err)
            }
            else {
                return res.json({
                    data: results
                })
            };
        });
    });

});

const SELECT_ALL_FACTS_QUERY = 'SELECT * FROM `my-project`.`my-table`;';

app.listen(4000, () => {
    console.log('My project SQL server listening on PORT 4000');
});

What did I do wrong and how could I deploy it?


Solution

  • It passed this stage after I removed if (err) throw err;, still not sure why this happened.

    Nithin's answer was taken into account too.