- Heroku is a cloud application platform โ a new way of building and deploying web apps.
- Heroku allows app developers spend their time on their application code, not managing servers, deployment, ongoing operations, or scaling. Heroku makes it easy!
- We can deploy full stack apps to Heroku for FREE!
- Heroku is also PSQL & Node friendly! Awesome!
Warning: Never git add, commit, or push sensitive information to a remote repository. Sensitive information can include, but is not limited to:
- Passwords
- SSH keys
- AWS access keys
- API keys
- Credit card numbers
- PIN numbers
Go to heroku.com and sign up for a free account. Remember your name and password - You will need it in a minute!!
- Open a terminal window and
brew install heroku/brew/heroku
heroku autocomplete
- Once installed, you'll have access to the heroku app from the terminal by running
heroku login
. Enter the email address and password you used when creating your Heroku account.
sudo apt-get install heroku
got to [heroku-cli]
(https://devcenter.heroku.com/articles/heroku-cli)
At this point you should have a personal Heroku account and the Heroku CLI toolbelt should be installed
When we build on our computers, we've been working locally in a development environment and all our variables and configurations reflect this. We need to prepare our files so our app can switch to production mode and declare our dependencies before we launch.
-
Create the Procfile: Make sure you are inside the root of your App's directory.
-
From the command line run
touch Procfile
exactly like that, no extension, and a capital 'P'. -
From your text editor go into your Procfile and type:
web: node myentrypoint.js
.HEY! YOU! The file name (here
myentrypoint.js
) should be the entry point for YOUR APP! (index.js, for example) -
If no Procfile is present in the root directory of your app during the build process, your web process will be started by running the npm start script from your package.json.
Heroku says it is best practice to include a Procfile, so just do it!
-
-
We need to update our package.json:
- add a start script where myentrypoint.js is the entry file for YOUR APP:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node myentrypoint.js" // this should be whatever YOUR entrypoint file is named
},
- add your engines. First, from terminal, type
node -v
and copy just the numbers. Go back to your package.json and include engines:
"engines": {
"node": "7.0.0" // put YOUR version of node here, in this format
},
- Make sure all the NPM packages you are using are in your
package.json
.
- Example:
{
"name": "heroku_node",
"version": "1.0.0",
"description": "",
"main": "index.js",
"engines": {
"node": "6.2.1"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^0.16.2",
"body-parser": "^1.17.2",
"color": "^1.0.3",
"express": "^4.15.3",
"morgan": "^1.8.2",
"mustache-express": "^1.2.4",
"pg-promise": "^6.1.0"
}
}
-
Okay, now we need to make sure both our database and our port are accessible both in Dev and Production mode. Go to your
myentrypoint.js
file (the entry file for YOUR APP)(again, probablyapp.js
orindex.js
) and add these lines:- Replace your port variable with:
const PORT = process.env.PORT || 3000;
- This line tells our Node app to first check environment variables for a port number (for Heroku), otherwise default to port = 3000. Make sure your app is listening on this new PORT variable.
- Replace your database variable with:
const db = pgp(process.env.DATABASE_URL || 'postgres://myname@localhost:5432/mydatabasename');
or, if you're using a configuration object instead, such as
const cn = { host: 'localhost', port: 5432, database: 'mydatabasename', user: 'myname' };
you can just say
const db = pgp(process.env.DATABASE_URL || cn); // cn is the configuration object
-
HEY! YOU! make sure the postgres address and database name are YOUR APP's information
- Replace your port variable with:
-
Finally, make sure any sensitive information, like API keys, are saved as variables and exported from your bash profile or a .env file (while cover dotenv soon, like in the next couple of minutes).
- In the terminal, type
heroku create
. The heroku create command creates a new application on Heroku โ along with a git remote that must be used to receive your application source. Heroku will provide you a random name for your app. If you want to provide a custom name, typeheroku create mycustomname
. Make sure to use all lowercases and no spaces (but dashes are okay). It is totally possible to change the name of your app at a later date, so don't get too wrapped up in this step. - Now let's make sure our remotes are set, run
git remote -v
, you should see your heroku remote here. - Sweet, we have our app. Love it. Now we need to communicate with Heroku. Run a
git add
,git commit
, thengit push heroku master
. Heroku will now rebuild your app on it's servers. If all goes well, when Heroku is done you'll seeremote: Verifying deploy... done.
. If deployment fails, read the errors in your terminal. - Heroku gives us 2 easy command to check out our logs, let's run it now to see what happened:
- See the last 100 lines of your log:
heroku logs
- Real time continous logging:
heroku logs --tail
(This one is probably best utilized in it's own terminal window)
- See the last 100 lines of your log:
- Run
heroku open
, this will open a browser page with your app. Don't be surprised to see Application Error - we still have stuff to do. Let's head to step 4!
Your database has been living a tidy but sheltered life on your local machine. It's time to give it wings so the world can interact with it by hosting it on Heroku.
Run heroku addons:create heroku-postgresql:hobby-dev
. This tells heroku to add a postgres database under the free hobby-dev plan.
Regardless if you used Heroku or terminal to set up you DB, we need to run our files from the command line:
-
Run
cat db/myseedfile.sql | heroku pg:psql
HEY! YOU! make sure that you are running this command from the root of your directory, that your seed file is located in a db directory, and you insert the name of YOUR APP's seed/schema file!
-
You enter Heroku's database shell by typing the command
heroku pg:psql
.- You can test a query here if you need to just like you do locally inside the psql shell.
-
You can use the handy
heroku pg:info
command to access information about your db on heroku.