Connecting Eat Da Burger to JawsDB on Heroku

Steps to setting up the Node/Express/MySQL app EatDaBurger on Heroku with a working database.

My code for this app is located at Github
I’ve deployed this app using these steps to Heroku

Screenshot of Eat the burger MySQL application

Step 1.

Add a .env file, which you will use to get the process.env variable from for your MySQL database. Also add the .env file to your .gitignore. You don’t want to add your database credentials to GitHub or anywhere else on the internet where it can be grabbed.
echo .env >> .gitignore
There’s more information on getting JawsDB set up at https://devcenter.heroku.com/articles/jawsdb

Inside your .env file set the JAWSDB_URL equal to your MySQL string you get from JawsDB, you can get this information from https://dashboard.jawsdb.com/mysql/dashboard or from the running the command heroku config:get JAWSDB_URL

JAWSDB_URL='mysql://username:password@hostvalue.amazonaws.com:3306/nameOfYour_db'

Step 2.

Inside your connection file add this to connect with JawsDB if it’s online or localhost if you’re on your machine

var mysql = require('mysql');

var connection;
if (process.env.JAWSDB_URL) {
    // Database is JawsDB on Heroku
    connection = mysql.createConnection(process.env.JAWSDB_URL);
} else {
    // Database is local
    connection = mysql.createConnection({
        port: 3306,
        host: 'localhost',
        user: 'root',
        password: '',
        database: 'nameOfYour_db'
    })
};

var mysql = require('mysql');

Step 3.

Set up db connection to your JawsDB in MySQL workbench.
Use your JawsDB credentials to sign into MySQL workbench, then open up a SQL editior.
Inside the SQL editor, make sure you’re using your JawsDB database, by running the USE command.

USE jawsDBname;

Once you’re inside your database, using your JawsDB databse, then run your SQL commands in MySQL workbench.
Now your app should be working within a couple of minutes.

Extra Step. If error.

If you’re getting error H13.
Whenever your app experiences an error, Heroku will return a standard error page with the HTTP status code 503. To help you debug the underlying error, however, the platform will also add custom error information to your logs. Each type of error gets its own error code, with all HTTP errors starting with the letter H and all runtime errors starting with R. Logging errors start with L.

I looked up the heroku error code, which you can read yourself, which wasn’t that helpful in solving the problem.
https://devcenter.heroku.com/articles/error-codes#h13-connection-closed-without-response

These two articles were helpful.
https://github.com/request/request#requestoptions-callback
https://devcenter.heroku.com/articles/request-timeout

Here’s what you need to do. Inside your server.js file, add a timeout. I grabbed a timed out function, then it has to be called after your app.use statements.

// Timeout
app.use(timeout(15000));
app.use(haltOnTimedout);

function haltOnTimedout(req, res, next) {
    if (!req.timedout) next();
}

I hope this has been helpful, if you have questions, leave me a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *