Heroku is a Cloud Application Platform that will allow you to publish your apps to the web. We'll be primarily using Heroku in this class but it's important to note that there are many other companies out there that offer similar services.
Best of all, Heroku is free for development use!
- Sign up for an account on Heroku.com
- Install Heroku CLI by typing
brew tap heroku/brew && brew install heroku
in Terminal
- Authenticate by typing
heroku login
in Terminal
Note: Your project also needs to have a git repository.
Before you deploy, make sure your server PORT
is configured correctly as:
const PORT = process.env.PORT || 5000;
Run the following commands from within your project folder.
- In terminal, navigate to your project folder and type
heroku create
- Login in if prompted
- Type
git remote -v
to ensure it added successfully - In terminal, type
git push heroku master
- Our website is now live! However... we also have a database
- In terminal, type
heroku addons:create heroku-postgresql:hobby-dev
to set up Postgresql on your Heroku project - Next, type
heroku pg:push your_database DATABASE_URL
to copy your database contents up to Heroku.your_database
is the actual name of your database (e.g.koala_holla_
).DATABASE_URL
is a heroku config variable created by the Add On. Do not replace it with something else, just type:DATABASE_URL
. For example, if you were deploying thekoala_holla
database, you should typeheroku pg:push koala_holla DATABASE_URL
- Update or create a module for your pg-pool configuration to the following code that will convert the heroku
DATABASE_URL
into a pool config object. The only line you should have to change isdatabase: process.env.DATABASE_NAME || 'your_database'
. Changeyour_database
to the actual name of your database. (e.g.database: process.env.DATABASE_NAME || 'koala_holla'
:
modules/pool.js
/**
Update your pool.js as shown below
**/
const pg = require('pg');
const url = require('url');
let config = {};
// We need a different pg configuration if we're running
// on Heroku, vs if we're running locally.
//
// Heroku gives us a process.env.DATABASE_URL variable,
// so if that's set, we know we're on heroku.
if (process.env.DATABASE_URL) {
config = {
// We use the DATABASE_URL from Heroku to connect to our DB
connectionString: process.env.DATABASE_URL,
// Heroku also requires this special `ssl` config
ssl: { rejectUnauthorized: false },
};
} else {
// If we're not on heroku, configure PG to use our local database
config = {
host: 'localhost',
port: 5432,
database: 'prime_app', // CHANGE THIS LINE to match your local database name!
};
}
// this creates the pool that will be shared by all other modules
const pool = new pg.Pool(config);
// the pool will log when it connects to the database
pool.on('connect', () => {
console.log('Postgesql connected');
});
// the pool with emit an error on behalf of any idle clients
// it contains if a backend error or network partition happens
pool.on('error', (err) => {
console.log('Unexpected error on idle client', err);
process.exit(-1);
});
module.exports = pool;
When you need a pool, use the following code:
var pool = require('../modules/pool.js');
Next, commit your changes and push them to Heroku:
git add .
git commit -m "MESSAGE"
git push heroku master
Note: You'll need to commit and push each time you make a change that you want to deploy to Heroku. Automatic deployments are covered in a later section Keep in mind you CAN NOT pull from Heroku. This is not a replacement for GitHub!
Lastly, open terminal and type heroku open
, which should show you your deployed site!
Note: It is best to fully test your code locally before deploying to Heroku. Bugs are much harder to troubleshoot on a live website.
heroku logs
- Display error logsheroku config
- Show basic app infoheroku restart
- Sometimes it helps to turn things off an on againheroku open
- Opens the website for you project in the browser
The Heroku website GUI can simplify several of the steps taken above especially for projects where you intend to make future changes.
- In your list of Heroku apps, select your application.
- Under the
Deploy
tab, in theDeployment Method
section, selectGithub
. Connect to theGithub
repository with your application by searching for the name of your repository. - In the
Manual Deploy
section, clickDeploy Branch
to deploy for the first time.
If you would like to edit your database, you can connect to your Heroku database directly from Postico.
- In your list of Heroku apps, select your application.
- Under
Resources
or in theConfigure Add-Ons
section, selectHeroku Postgres
. - Select the
Settings
tab and clickView Credentials
- Open Postico and select
New Favorite
. - In the new Postico favorite, update the following to match Heroku:
- Host
- User
- Database
- Password
- Port
- Click
Connect
and you should have access to your database directly from Postico!
More detailed instructions can be found here: