Deploying a Node app to Heroku

Deploy to the Cloud

Goals

By the end of this lesson, you will:

  • Know how to deploy a Node app to Heroku

Step 1 - Sign Up with Heroku

Once you have a username/password, you can login on your terminal:

$ heroku login

Step 2 - Add Procfile

Add a Procfile at the root of your folder with the following:

web: node server.js

(Note: If you’ve named your server file anything other than server.js, replace that line with the correct name. e.g. if your server is named index.js your procfile should say web: node index.js)

A Procfile contains a number of process type declarations, each on a new line. Each process type is a declaration of a command that is executed when a dyno of that process type is started.

For example, if a web process type is declared, then when a dyno of this type is started, the command associated with the web process type, will be executed. This could mean starting a web server, for example. The web: node server.js will override the default npm start command that Heroku tries to run. (This is helpful because your start script might be using nodemon instead of just node.)

Step 3 - Create Heroku app

$ heroku create app_name

If you 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 from the terminal. This command also added a new remote repository to our application. You can see all of your remote git details by running the following command:

$ git remote -v

Step 4 - Push to Heroku & Open Your App

$ git push heroku master
$ heroku open

The open command will open your deployed application in your web browser. And you probably have a super unhelpful ‘Application Error’ page at the moment. You will inevitably have issues in production, so it’s important to learn how to read the Heroku error logs.

Step 5 - Read the error logs

//Gets you all the logs
$ heroku logs

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

Step 6 - Configure Your Environment

We’ve previously only worked with our applications in a development environment. Now we need to set them up to work in a production environment. This involves a couple of steps:

Step 6a - Install Postgres Addon

Heroku allows you to install addons for your different projects. Under the resources tab for your project, you should see a section that allows you to search for an addon. Search for ‘Heroku Postgres’ and add the addon to your project.

This addon will create an environment variable for us to connect to our production database. Navigate to the settings page for your application and click on ‘Reveal Config Variables’.

You’ll see one has be created for us called DATABASE_URL. We don’t need or want the actual value for this variable, we’re just going to want to reference it in our database configuration.

Step 6b - Configure Knex for Production

In your knexfile.js, add a production environment. You can copy directly from your development environment and then make a handful of changes:

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.

Step 6c - Update Your Server Environment

We now need to tell our server to detect it’s environment based on the process variables. Previously in our server.js files, we simply set:

const environment = 'development';

We actually want this to be detected automatically so that it can vary based on where our application is running:

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. Learn more about environment variables and process.env.

Step 6d - Commit & Push

Commit the changes you’ve made to your knexfile.js and your server.js. Push them up to your origin remote and then push them to heroku:

$ git push origin master
$ git push heroku master

Step 6e - Migrate Your Production Database

The final step here is to migrate our production database so it has the appropriate schema. We can run knex commands through Heroku with:

heroku run 'knex migrate:latest'