Jun 28, 2022Backend Project Tutorial 5 - DeploymentThe final part to the PPC backend series, covering deployment of the API.
Jasper Di Francesco3 min read

Now we are going to talk about the all important final step of deployment. This is the when you get to send a link to your mum of what you made and she's proud of you.

We will be going with the service called Heroku for our deployment. Head over to signup.heroku.com/login to create an account. Once you have logged in you will be greeted by a friendly dashboard showing you that you have no current projects.

We use Heroku because it's free and easy to use. You will hear a lot of talk about AWS and GCP and other fancy hosting sites but, through my years of expertise, I have found Heroku has the most generous free tier and shortest setup time. As long as your site traffic stays under a certain level, Heroku is happy for you to host any hobby projects for free.

Creating a Heroku project

From your dashboard, click the "new" button. Then click "create new app" and give it a fun name, leave the region as United States.

Heroku CLI

After that exhausting step, we will want to send our code to Heroku. As though it was made to be simple, if you navigate to the Deploy tab of your project you will see deployment instructions. Your code will need to be in a Github repository for this deployment to work.

If you are not sure what that means, that's fine, just go to this link and follow the instructions. Git is a very useful skill to learn that you will use all throughout your development career! Atalassian has very good tutorials covering git in depth.

I would recommend using the Heroku CLI, but using the "Connect to Github" option is just as easy.

Secrets

Remember in the Sign up section of tutorial 4, we set our SUPER_SECRET_KEY in our .env file to some random string? Make sure that whenever you push a repo to Github, you NEVER push this .env file. This file should be highly guarded on your computer as it contains all of our secrets for the app.

This poses a problem for Heroku as it has no way of accessing our key, so now none of our encryption will work and our users will have a hard time logging in.

Open up your Heroku project dashboard and navigate to the "Settings" tab. Scrolling down a little bit you will see a section titled "Config Vars". It is here we tell Heroku our secrets.

Click "Reveal Config Vars", then fill in the boxes with SUPER_SECRET_KEY as your key and the random string from earlier as the value. Click add and we should be good. You may or may not need to redeploy after doing this.

Excellent! Now there should be a URL specifying where your API has ended up (mine is at https://ppc-backend.herokuapp.com/).

Now you should be able to make requests to your API at the URL specified and get some kind of response back!

That's about it for our backend tutorial. To recap, we have set up an Express backend server which takes all types of HTTP requests, connecte that to a MongoDB database with a collection to manage our users and a collection to manage our venues, then we have deployed it using Heroku. Hopefully this has given you a bit of direction to now create your own project for the Personal Project Competition, or just a hobby project. Good luck!