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.
- Set up MongoDB Atlas
- Add environment variables
- Share a link to your app.
Deploying to Heroku
Begin inside the root directory of your application (the directory you
just renamed to express-upload-api
from express-api-template
) deploy a new
application to Heroku:
NOTE: heroku open
will open your deployed Express API in the browser. It will most likely be a blank white page that says Cannot GET /
. This is expected since you probably did not create a route for the path /
. To verify your deployment worked, you can change the path in the URL to match a valid route you did define in your app (e.g. if I have a GET route for /books
, I should see some JSON when I add /books
to the URL).
A full list of Heroku commands can be accessed by running heroku --help
Share Your App (REQUIRED)
- Fork and clone this repository.
- Change into the new directory.
- Create and checkout a new branch, named
response
.
- Delete the contents of deployedUrl.txt with the URL of your
deployed heroku application.
- Add, commit, and push your changes.
- Open a PR on this repository here.
Your pull request description should contain a "fist to five" for comfort and
clarity. Additionally, you should mention the resources you used to help you
complete this guide. For example:
Comfort: 3
Clarity: 3
I used Google and my class notes to help with this exercise.
You may wish to refer to FAQs
related to
forking,
cloning,
and pull requests.
Creating Atlas addon
Finish up the final deployment steps, then test your app to make sure it worked.
return to Deploying Heroku
Add AWS credentials
- Make sure you have generated your AWS secrets
and have them in your local .env
file. Things should work locally when interacting
with AWS.
- Remember, your
.env
file contains secret credentials and should NOT be
committed to git or pushed to GitHub. So, to deploy your app to another server,
we have to manually set up the .env
for that server, by hand.
- On Heroku, this is done via the
heroku config:set
command from the command line (or via the Heroku web dashboard):
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
- First step upon encountering an issue should be to run
heroku logs
to
read the logs of your deployed heroku server
- Heroku Addons
- Previous Issues
- If you are getting a CORS error and the
CLIENT_ORIGIN
is correct, ensure the
DB_URI
is set successfully with the value that MongoDB Atlas provides for your cluster.
Additional Resources
- All content is licensed under a CCBYNCSA 4.0 license.
- All software code is licensed under GNU GPLv3. For commercial use or
alternative licensing, please contact [email protected].