You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
Run heroku create in the command line in the root of your Express API to
create a new (blank) app on Heroku.
Commit to your local main branch
(if you do not have a branch named main, you can create one with git checkout -b main)
Push your latest code to Heroku (git push heroku main)
In terminal, run: git push heroku main (should build your site)
Due to the first line of code in the server.js file, the default
deployment environment will be production
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 (at least) two config variables set in heroku
(heroku>settings>config vars): DB_URI and CLIENT_ORIGIN
If you are using AWS, you should have two additional config variables.
Once all of these are set, run in terminal: heroku restart
Then in terminal, run: heroku open
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.
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.
Since the free-tier of MongoDB Atlas only allows one cluster
per project, create a new project and a new cluster, selecting the "Free" version
Once the cluster has been created, click "connect" on the cluster overview
Setup connection security:
For "step 1: Add a connection IP address", select Allow Access From Anywhere
Create a user, admin, with a password. Save these credentials
somewhere safe.
Choose a connection method:
Select "Connect your application"
Select "Node.js" as the driver, version 3.6 or later
Copy the connection string provided
In terminal, run heroku config:set DB_URI="<your-connection-string>",
replacing the <password> with the password you created for the admin user
and <dbname> with the name of your database.
NOTE: You don't have a database at this point yet, but MongoDB will
create it as soon as you use your application to create data. So, in the
example above, the atlas_db_test database will not exist until the first user
is created via a sign-up.
Finish up the final deployment steps, then test your app to make sure it worked.
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):
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
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.
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
these installation instructions
unequivocally.
Objectives
Deploying to Heroku
Begin inside the root directory of your application (the directory you
just renamed to
express-upload-api
fromexpress-api-template
) deploy a newapplication to Heroku:
heroku create
in the command line in the root of your Express API tocreate a new (blank) app on Heroku.
main
branch(if you do not have a branch named
main
, you can create one withgit checkout -b main
)git push heroku main
)git push heroku main
(should build your site)server.js
file, the defaultdeployment environment will be
production
accept requests from the correct domain. IF you're client is deployed on GitHub,
your ORIGIN will be:
https://<% github username %>.github.io
heroku config:set CLIENT_ORIGIN="https://<% github username %>.github.io"
(
heroku>settings>config vars
): DB_URI and CLIENT_ORIGINheroku restart
heroku open
NOTE:
heroku open
will open your deployed Express API in the browser. It will most likely be a blank white page that saysCannot 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)
response
.deployed heroku application.
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:
You may wish to refer to FAQs
related to
forking,
cloning,
and pull requests.
Creating Atlas addon
per project, create a new project and a new cluster, selecting the "Free" version
admin
, with a password. Save these credentialssomewhere safe.
heroku config:set DB_URI="<your-connection-string>"
,replacing the
<password>
with the password you created for theadmin
userand
<dbname>
with the name of your database.heroku config:set DB_URI="mongodb+srv://admin:yellowpencil@cluster0.pygcn.mongodb.net/atlas_db_test?retryWrites=true&w=majority"
create it as soon as you use your application to create data. So, in the
example above, the
atlas_db_test
database will not exist until the first useris created via a sign-up.
Finish up the final deployment steps, then test your app to make sure it worked.
return to Deploying Heroku
Add AWS credentials
and have them in your local
.env
file. Things should work locally when interactingwith AWS.
.env
file contains secret credentials and should NOT becommitted 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.heroku config:set
command from the command line (or via the Heroku web dashboard):heroku config:set SOME_KEY=some value
, just like we did when we first deployed our express appreturn 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:
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
heroku logs
toread the logs of your deployed heroku server
CLIENT_ORIGIN
is correct, ensure theDB_URI
is set successfully with the value that MongoDB Atlas provides for your cluster.Additional Resources
License
alternative licensing, please contact legal@ga.co.
The text was updated successfully, but these errors were encountered: