Skip to content

Instantly share code, notes, and snippets.

@N-Gibson
Forked from qfarenwald/heroku.md
Created November 25, 2019 01:17
Show Gist options
  • Save N-Gibson/54b6d123d93c679bb6828451b754639b to your computer and use it in GitHub Desktop.
Save N-Gibson/54b6d123d93c679bb6828451b754639b to your computer and use it in GitHub Desktop.

Deploy to Heroku

https://frontend.turing.io/lessons/module-4/deploy-to-heroku.html

Do this once globally

brew tap heroku/brew && brew install heroku

Login to Heroku in the terminal

$ heroku login

Touch Procfile in root of project folder (no file ext!)

Paste this code in the Procfile

web: node server.js

Create Heroku app in terminal (Name must start with a letter, end with a letter or digit and can only contain lowercase letters, digits, and dashes.)

heroku create <app-name>

Go back to Heroku in your browser, you should now see that you have an app listed under your personal apps that corresponds to the one you just created

Check git remote details with git remote -v ex.

heroku	https://git.heroku.com/legos-api.git (fetch)
heroku	https://git.heroku.com/legos-api.git (push)
origin	https://github.com/qfarenwald/BYOB.git (fetch)
origin	https://github.com/qfarenwald/BYOB.git (push)

To interact with heroku, use keyword like this git push heroku master

To interact with origin, use keyword like this git push origin master

Now push to Heroku master

git push heroku master

Open your deployed application in your web browser

heroku open

Application error WILL occur, READ error message and check logs

//Gets you all the logs
heroku logs

//Gets you the latest log
heroku logs --tail

Error fix: Add this code to the end of the current list in the knexfile.js

production: {
  client: 'pg',
  connection: process.env.DATABASE_URL + `?ssl=true`,
  migrations: {
    directory: './db/migrations'
  },
  useNullAsDefault: true
}

Save > git add > git commit > git push heroku master > heroku open

Success?! This code below is in my server.js files and dislays now on the deployed brower

app.get('/', (request, response) => {
  response.send('BYOB ROOT');
});

Install Postgres Addon

Under the resources tab for your project on Heroku site, search for an addon Heroku Postgres and add

Navigate to the settings page for your application and click on ‘Reveal Config Variables’... we’re just going to want to reference it in our database configuration.

Configure Knex for Production

We did this above to fix the error, but be sure code is correct here

In the knexfile.js

production: {
  client: 'pg',
  connection: process.env.DATABASE_URL + `?ssl=true`,
  migrations: {
    directory: './db/migrations'
  },
  useNullAsDefault: true
}

Notice here we are using that DATABASE_URL variable that was created for us. This configuration will now tell Heroku to connect to postgres through the addon we installed. *Note: we’ve appended ?ssl=true to the end of our connection string because the Heroku Postgres addon requires it.

Tell server to detect it’s environment based on the process variables

In server.js file, make sure code looks likes this

const environment = process.env.NODE_ENV || 'development';

Now when our application is running in Heroku, it will recognize that it’s in a production environment and use all of the appropriate configurations.

Commit & Push

Save > git add > git commit > git push origin master > git push heroku master

Migrate Production Database

Migrate production database so it has the appropriate schema.

heroku run 'knex migrate:latest'

TEST: use deployed url and test in Postman

GET https://legos-api.herokuapp.com/api/<table>

Will see an empty array

Seed Production Database

Add seed path to production in knexfile.js

module.exports = {
  development: {
    client: 'pg',
    connection: 'postgres://localhost/legos',
    migrations: {
      directory: './db/migrations'
    },
    seeds: {
      directory: './db/seeds/dev'
    },
    useNullAsDefault: true
  },
  production: {
    client: 'pg',
    connection: process.env.DATABASE_URL + `?ssl=true`,
    migrations: {
      directory: './db/migrations'
    },
    seeds: {
      directory: './db/seeds/dev'
    },
    useNullAsDefault: true
  }
};

Save > git add > git commit > git push origin master > git push heroku master

Then run

heroku run 'knex seed:run'

TEST: use deployed url and test in Postman

GET https://legos-api.herokuapp.com/api/<table>

Will see all data

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment