Skip to content

Instantly share code, notes, and snippets.

@jessebrite
Forked from ross-u/README.md
Last active February 5, 2020 23:53
Show Gist options
  • Save jessebrite/5e1b26e4c8a6556415f03ce25f84feee to your computer and use it in GitHub Desktop.
Save jessebrite/5e1b26e4c8a6556415f03ce25f84feee to your computer and use it in GitHub Desktop.
Heroku Deployment (with Mongo Atlas) - ExpressJS + Handlebars

Heroku Deployment - with Mongo Atlas


Benefit of deploying a Heroku free dyno and using Mongo Atlas for your MongoDB is that you are not required to use a credit card during the sign up. The process of setting up a DB cluster with Mongo Atlas takes a bit longer than it would be if you are using the Heroku's MLab add-on (free sandbox).

This is a life saver as 'd tried a countless times to no avail


Create a Heroku account

https://signup.heroku.com/


Install Heroku CLI

If unable to install the CLI via the Standalone installation (using a file) :

npm install -g heroku


heroku --version

# heroku/7.0.0 (darwin-x64) node-v8.0.0


heroku login

Login to the Heroku website and Create New App

  1. 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.

Follow the instructions from the app dashboard "Deploy" section

While in the server root directory, in the terminal run :

# Commit the most recent work and merge it to the master branch
git add .
git commit -m 'Update xyz.js with new middleware'
git push origin <branch-name>


# Checkout to the master branch
git checkout master


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


# Check the remotes available
git remote -v


# Make some changes to the code and deploy them to Heroku using Git.
git add .

git commit -m 'Make it better'

git push heroku master

Set the Config Var for sessions


In the Dashboard >>> Settings section, add the following Config Var:
SESSION_SECRET=cookies-and-milk

  • Open your project in the VSCode, and open the terminal.

  • We will work from the root directory of your project.

Check that Heroku is installed.

heroku --version

Run the heroku login command.

heroku login

Connecting the repository to Heroku

Login to the Heroku website and click on >>> Create New App

Inside the server directory run :

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


# Check the remotes available
git remote -v


# Make some changes to the code and deploy them to Heroku using Git.
git add .

git commit -m 'Make it better'

git push heroku master

MongoDB Atlas configuration - (without credit card)


https://www.mongodb.com/atlas-signup-from-mlab


Sign Up & Create a Free Cluster

  1. Sign Up for Mongo Atlas - enter the email, password, first name, last nae
  2. Select >>> Starter Clusters
  3. In the drop-down Cloud Provider & Region select:
    • Europe Region with "FREE TIER AVAILABLE" flag
  4. In the drop-down "Cluster Tier" make sure to select:
    • M2 Sandbox
  5. Click the button Create Cluster

Setup MongoDB Atlas Cluster:

  1. In the sidebar select SECURITY -> 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
    • Create New User

  3. In the sidebar select SECURITY -> Network Access


  4. Click on + ADD IP ADDRESS

    • Click on ALLOW ACCESS FROM ANYWHERE
    • Confirm

  5. 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


Set the "Config Vars" update the project files

  1. In the Heroku app Dashboard >> Settings:

    • Create tne new Config Var KEY - MONGODB_URI
    • Set the value to be the previously copied Connection String from Mongo Atlas.
    • Remember to add the <password> to the Connection String you copy/pasted.

  2. Update also the .env file in your project, which is used for development:

    • Change the name from DB_NAME (or whatever you called it) to MONGODB_URI

    • MONGODB_URI value should include the complete URI including the database name, like:

      .env
      MONGODB_URI=mongodb://localhost:27017/deployment-db-name

  1. Update the app.js and bin/seed.js.

    app.js & bin/seed.js

// 			app.js   	AND    bin/seed.js

//	...

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


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

//	...
//		...
//			...

  1. Commit the changes and push to remote on GitHub (origin) and Heroku (heroku)
git add .

git commit -m 'Update .env variables and add MONGODB_URI'

git push origin master

git push heroku master

Double-check that the config variable process.env.MONGODB_URI is available (set by the same name) in your Heroku app **Settings **:

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

img


Set the "engines": { "node": 'x.xx.xx'} in the package.json

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

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

Create a commit and deploy to Heroku

# Make some changes to the code and deploy them to Heroku using Git.
git add .

git commit -m 'Production deployment v1'

git push heroku master

heroku open

Heroku commands

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

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

heroku logs -t

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