Skip to content

Latest commit

 

History

History
112 lines (78 loc) · 5.23 KB

README.md

File metadata and controls

112 lines (78 loc) · 5.23 KB

General Assembly Logo

Express Deployment with Heroku

You've learned a lot about how to build a Node application over the last few weeks. Now let's 'go public' and share our apps with the world!

Prerequisites

Objectives

  • Create a Heroku app from the command line
  • Push the latest code to Heroku
  • Migrate the production database

Deploying to Heroku

Begin inside the root directory of your application (the directory you just renamed to from express-api-template) deploy a new application to Heroku:

  • Run heroku create in the command line in the root of you Express API to create a new (blank) app on Heroku.
  • commit to your local master branch
  • Push your latest code to Heroku (git push heroku master)
  • Add any addons e.g. mlab to create a deployed database
  • mlab add on steps
  • in terminal, run : git push heroku master (should build your site)
  • due to the first line of code in the server.js file, the default deployment environment will be production
  • in terminal, run : echo SECRET_KEY=$(/usr/local/opt/openssl/bin/openssl rand -base64 66 | tr -d '\n') this should generate a secret_key
  • in terminal run: heroku config:set <copy and paste secret_key generated from last command> . should start with “SECRET_KEY and span about 40 randomized characters”
  • you need to set your client ORIGIN so that your deployed API will ONLY accept requests from the correct domain. IF you're client is deployed on Github, your ORIGIN will be: https://<% github username %>.github.io
  • Set your client ORIGIN by: heroku config:set CLIENT_ORIGIN="https://<% github username %>.github.io"
  • You should have three config variables set in heroku (heroku>settings>config vars): MONGODB_URI, SECRET_KEY, CLIENT_ORIGIN
  • Once all three of these are set, run in terminal: heroku restart
  • Then in terminal, run: heroku open

A full list of Heroku commands can be accessed by running heroku --help

Creating Mlab addon

  • Run heroku addons:create with mongolab:sandbox $ heroku addons:create mongolab:sandbox

The first time you run the above command you'll see a message like this:

Creating mongolab:sandbox on ⬢ pacific-cliffs-91276... !
 ▸    Please verify your account to install this add-on plan (please enter a credit card) For more information, see
 ▸    https://devcenter.heroku.com/categories/billing Verify now at https://heroku.com/verify
  • You need to go to that URL, enter in your credit card information and then re-run the command again. This time you should see something like:
~/wdi/projects/group-project/TimeCapsule-Backend (master)$ heroku addons:create mongolab:sandbox
Creating mongolab:sandbox on ⬢ pacific-cliffs-91276... free
Welcome to mLab.  Your new subscription is being created and will be available shortly.  Please consult the mLab Add-on Admin UI to check on its progress.
Created mongolab-cubed-11237 as MONGODB_URI
Use heroku addons:docs mongolab to view documentation

Now you can log into your heroku dashboard, go to add-ons and click the mlab link. This will bring you to your mlab database.

return to Deploying Heroku

WARNING: Ephemeral Filesystem.

One serious limitation of Heroku is that it provides an 'ephemeral filesystem'; in other words, if you try to save a new file inside the repo (e.g. an uploaded image file), it will disappear when your app is restarted or redeployed.

As an example, try running the following commands:

heroku run bash
touch happy.txt; echo 'is happy' > happy.txt
cat happy.txt

Then, hit Ctrl-D to get out of heroku bash shell. If you re-open the shell and run ls -l, happy.txt will be missing!

The typical workaround is to save files in cloud storage such as Amazon S3.

Troubleshooting

Additional Resources

  1. All content is licensed under a CC­BY­NC­SA 4.0 license.
  2. All software code is licensed under GNU GPLv3. For commercial use or alternative licensing, please contact legal@ga.co.