Skip to content

Instantly share code, notes, and snippets.

@ross-u
Last active July 1, 2023 20:00
Show Gist options
  • Save ross-u/b59ea6a1febefb80bffc15ecf31ea827 to your computer and use it in GitHub Desktop.
Save ross-u/b59ea6a1febefb80bffc15ecf31ea827 to your computer and use it in GitHub Desktop.
M2 - Heroku Deployment (with Mongo Atlas)

M2 - Heroku Deployment (with Mongo Atlas)




  1. Sign Up for Mongo Atlas - enter the email, password, first name, last name

  2. Select >>> Starter Clusters (or Shared Clusters)

  3. In the drop-down Cloud Provider & Region select:

    • Europe Region
  4. in the Cluster Tier select:

  5. In the drop-down "Cluster Tier" make sure to select:

    • M0 Sandbox - with the flag Free forever.
  6. Click the button Create Cluster






3. Setup MongoDB Atlas Cluster:

  1. In the sidebar in the SECURITY part:

    • select -> Database Access

  2. Click on + ADD NEW USER to create a new user

    • Select Read and write to any database - User Privileges
    • Set the Username and Password
    • Set Database User Privileges to : Atlas admin
    • Create New User


  3. In the sidebar select SECURITY -> Network Access



  1. Click on + ADD IP ADDRESS
  • Click on ALLOW ACCESS FROM ANYWHERE
  • Confirm


  1. Connect To Your Cluster: Click on the gray button CONNECT
  • Select Connect Your Application option
  • Choose Your driver version: DRIVER: Node.js
  • Copy the Connection String Only



  1. Update the connection string and add the username and password.

4. Create a Heroku account

https://signup.heroku.com/


5. Install Heroku CLI


6. Login to the Heroku website and Create New App

  1. Go to dashboard.heroku.com/apps, and then select:

    New >>> Create new app >>> Choose region - Europe >>> Create app.

  2. After you create the app, you should be redirected to the app's dashboard.

  3. Select the Deploy section.



7. Connect Heroku app to the local repository


Follow the Deployment instructions - Deploy section


While in the Heroku app Dashboard open the Deploy section


To login to Heroku and create a new Dyno, from your server/project root directory, run the following commands (also mentioned in the Deploy section) :


In the below example the angle brackets < > indicate the identifier/parameter/argument values that are provided by the user. You should omit them when executing the commands.

# Login to heroku from the terminal
heroku login

# Add heroku remote
heroku git:remote -a <name-of-the-newly-created-app>

# Check the remotes available
# Newly added `heroku` remote repository connection should be shown
git remote -v

# Commit the most recent work on the development branch
git add .
git commit -m 'Write a clear meaningful commit message here'
git push origin <development-branch-name>


# Checkout to the master branch
git checkout master


# Merge (bring) updates from the development branch
git merge <development-branch-name>

git push heroku master

8. Set Config Vars in Heroku


  1. In the Heroku app Dashboard go to >> Settings:

  2. Click on the Reveal Config Vars

  3. Create the new keys for the environment variables:

    • SESSION_SECRET

    • MONGODB_URI - Set the value to be the previously copied Connection String from Mongo Atlas. Remember to add the username and password to the Connection String you created in Mongo Atlas.


For the sessions secret:


SESSION_SECRET=s0m3RaNd0mS7r1nG123

For the newly created MongoDB database in the Atlas Cloud.


In the below example the angle brackets < > indicate the identifier/parameter/argument values that are provided by the user. You should omit them when executing the commands.

MONGODB_URI=<Mongo Atlas Conection String with username and password>


❗ Double-check that all needed config variables (MONGODB_URI, SESSION_SECRET, etc.) were added properly during the previous step :

Heroku app Dashboard >> Settings >> Config Vars >> Reveal Config Vars


9. Update the project files


  1. Check that mongoose.connect URLs in the app.js and bin/seed.js (if you are using a seed) are updated and using process.env.MONGODB_URI.

    app.js & bin/seed.js

require('dotenv').config();               //  <--- ADD


//	...

//		...


mongoose
  .connect(
    process.env.MONGODB_URI,            //  <--- UPDATE
    {useNewUrlParser: true}
  )
  .then((x) => console.log('Connected to the DB')
  .catch(err => console.error('Error while connecting to DB', err));

        
//	...

  1. Update the package.json and specify the version of Node:

    • Run command node --version to get the version of Node.js installed:

      node --version
      
      #> v12.4.1

    • Add the engines config section with the node version to the package.json.

    For example, if your node version was v.12.4.1 you should set the following:

    package.json
    "engines": {
      "node": "12.x"
    }

  1. After updating the mongoose.connect connection strings and adding the node version make sure to commit the changes and push to remote on GitHub (origin) and Heroku (heroku)
git add .

git commit -m 'Update mongoose connection and add node version'

# Push the commit and update the version on GitHub
git push origin master

# Push the commit and update the deployed version on Heroku
git push heroku master


# Open the app in the browser (from terminal)
heroku open

10. Heroku commands

Logs

To fetch your app’s most recent logs, use the heroku logs command:

 heroku logs

The logs command retrieves 100 log lines by default.

You can specify the number of log lines to retrieve (up to a maximum of 1,500 lines) by using the --num (or -n) option.

heroku logs -n 200

If there was an error during the deployment, you will be shown the following screen prompting you to run heroku logs --tail command:


Real-time tail displays recent logs and leaves the session open for real-time logs to stream in.

This command is used to troubleshoot any deployment errors when the deployed app is not working as expected.

heroku logs --tail

Access the terminal of the Heroku container

We can open the terminal instance on the Heroku container (dyno) in order to run custom scripts or see the files included in the instance. To do this we use the command heroku run bash.


For example to run the seed sequence and populate the production database we should do the following :

# Open the terminal in the app dyno in Heroku
heroku run bash

# We may then run the seed file
node bin/seed.js

@SiimonStark
Copy link

I'm confused as to what/where the SESSION_SECRET is

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment