Skip to content

Instantly share code, notes, and snippets.

@ironhack-edu
Forked from ross-u/README.md
Last active March 16, 2021 21:14
Show Gist options
  • Save ironhack-edu/8bd98534498c35710541f4ea8fe92c11 to your computer and use it in GitHub Desktop.
Save ironhack-edu/8bd98534498c35710541f4ea8fe92c11 to your computer and use it in GitHub Desktop.
Heroku Deployment (with Mongo Atlas) - M2

M2 - Heroku Deployment (with Mongo Atlas)


welcome-mongo-atlas


get-started-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


free-cluster


cluster-settings-1


In case you are in the US, Asia or Australia, chose the closest region.

cluster-settings-2


cluster-creating


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

    db-access


  3. In the sidebar select SECURITY -> Network Access


network-access


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

ip-whitelist


  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

db-connect


connect-settings


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

4. Create a Heroku account

To create a new Heroku account, follow the link: 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.


new-heroku-app


Create Heroku Dyno and connect it to the local repository

Follow the Deployment instructions

Go to app Dashboard >> "Deploy" section

While in the server root directory, to login to heroku and create a new Dyno, run the following commands (also mentioned in the Deploy section) :

# 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

Set the Config Vars in Heroku


In the Dashboard >>> Settings section, add the following Config Var:

For the sessions secret:

SESSION_SECRET=cookies-and-milk

For the newly created MongoDB database in the Atlas Cloud.

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


Double-check that the config variable MONGODB_URI was added properly in your Heroku app **Settings **:

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

img

Set the "Config Vars" update the project files

  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.



  1. Make sure that mongoose.connect URLs are update both in the app.js and bin/seed.js (if you are using a seed).

    app.js & bin/seed.js

// 			app.js   	AND    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
    • Add the engines config with the node version to the package.json:

      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

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:

heroku-app-error


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

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.

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

# We may then run the seed file
node bin/seed.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment