Heroku: Node, Express, PostgreSQL Setup

Step 0: Have an app

Create an empty app if you dont have one

Create a folder, cd into it and initialize your git repo.

$ mkdir <app_name>; cd <app_name>; git init

Step 1: Install Heroku

Install the heroku cli with homebrew

$ brew install heroku/brew/heroku

Check your heroku installation and login if necessary: 

$ heroku --version

Note: heroku will check for the latest version every* time you run the command

$ heroku login

or $ heroku login -i to stay in your CLI

Check what apps you have already created:

$ heroku apps

Step 2: Create a new named heroku app

$ heroku create <app_name>

If you run this command with the <app_name> heroku will choose a new random name for you.

In the picture above, https://theptrk.herokuapp.com is the new url for your blank app.

* this is the default site for your new app that sends a 502 HTTP code

Check that your app has been created

$ heroku apps

Step 3: Initialize your app to your heroku git url*

* If you ran heroku create in your app directly this step will be done automatically.

Check this by checking for a remote named heroku

$ git remote -v

If you dont see heroku, continue with this step.

“””With Github you might be used to using the word origin to mean your Github url, but the convention for heroku is to name this remote “heroku”. Note that this remote name is completely arbitrary and has nothing to do with the platform or url or anything.”””

Use git remote --verbose to check if you have any remote urls set up. There should be none

Get your apps “info” with info

$ heroku info <app_name>

With your git url add your remote:

$ git remote add heroku <git_url>

$ git push --set-upstream heroku master

* Notice that once you set this and push, you no longer need to specify the --app=<app_name>

** Note that heroku commands return different things depending on where you are.

 

Step 4: Set up node.js, install express.js

Create a package.json 

This file will be used by node/npm to indicate meta data, dependencies and other project level info

$ npm init

Install express.js

$ npm install express --save

Set the node version package.json to the one on your local machine.

You can check your local version with $ node --version

"engines": {
   // use your local version to stay consistent
   // if you want to push a newer node version, update your local node version
   // use nvm for the best way to handle local node versions
  "node": "10.6.0"
},

Create a “start” script

This is the script which is run whenever the command npm start is run.

Heroku will use this “start’ script as the default script if no Procfile is found.

"scripts": {
  "start": "node index.js"
}

curl express hello world (link)

curl -L https://bit.ly/express-hello-world > index.js

Run this server locally with 

$ heroku local web or $ npm start

Check out your website at http://localhost:5000/

At this point, you can git push heroku master

Step 5: Installing PostgreSQL instance for your app

Postgres instances are created and assigned to individual apps as “add-ons”.

Use this command to see all your existing add-ons.

$ heroku addons

** Note that heroku addons performs differently depending on where you are. If heroku recognizes that you are in a heroku app, it will only show you the add-ons for only that app. cd to a directory that is not a heroku app (say your home directory) and heroku will show you all add-ons for your account.

Create your PostgreSQL instance:

$ heroku addons:create heroku-postgresql:hobby-dev --app=<app_name>

Get more info with: $ heroku pg:info*

* if you navigated to a different directory, you may need to specify the app like this heroku pg:info --app theptrk

Congrats! You now have a heroku app instance and PostgreSQL instance.

Connecting to your PostgreSQL instance:

If you want to connect your app to your db, note that heroku automatically created a DATABASE_URL config variable for you. Type heroku config to see your current config variables. You can access this variable with process.env.DATABASE_URL in your application code.

Updated: added heroku login command; Mark Lewin suggested

2 comments

  1. Useful, but I think you are missing a “step 0.5”. You need to run `heroku login` (or `heroku login -i` if you want to stay in the CLI) to authenticate.

  2. Useful, but I think you are missing a “step 0.5”. You need to run `heroku login` (or `heroku login -i` if you want to stay in the CLI) to authenticate.

Comments are closed.