Skip to content

Instantly share code, notes, and snippets.

@ManishPoduval
Last active August 16, 2021 21:26
Show Gist options
  • Save ManishPoduval/aa31886bc4af7ee8432a3f2704d6ea58 to your computer and use it in GitHub Desktop.
Save ManishPoduval/aa31886bc4af7ee8432a3f2704d6ea58 to your computer and use it in GitHub Desktop.

Deployment with Heroku-cli

To check how to deploy in another way with Github read here

  • install heroku CLI

  • prepare app inside the project in your command line terminal (iterm/ powershell/vsCode terminal)

    • $ heroku login
    • $ heroku git:remote -a app-name
    • make sure package.json is in the root of the project
    • make sure package.json contains a start script
"scripts": {
  "start": "node server.js"
},

Go the mongoDB atlas and sign in.

  • When you see these screens, choose the free cluster version

  • Create a new cluster on mongoDB atlas (Please remember your username, password when you create them the first time for your account)
    • Click on connect
    • Allow access from everywhere are create a user with a password (Please make sure you remember these)

  • Choose Connect your application

  • Copy that url that you see

  • That url will look something like this mongodb+srv://manish:<password>@cluster0-st12d.mongodb.net/<dbname>?retryWrites=true&w=majority

  • Change <password> with your real password and <dbname> with the database name you need for your project

  • It will then look like this mongodb+srv://manish:Test@123@cluster0-st12d.mongodb.net/secondProject?retryWrites=true&w=majority

  • copy this mongodb url and save it somewhere

  • configure app

    • REMOVE ALL API KEYS (e.g. secret, clientIDs) FROM YOUR CODE
    • Use the dotenv package if you've not used to be able to access environment variables via process.env
    • $ npm install --save dotenv
    • add require('dotenv').config(); to app.js
    • create .env file (adds environment variables to process.env)
    • add .env to .gitignore (To ensure we never commit out .env file to github)
    • replace hardcoded mongodb URI with process env variable in app.js

#These .env variables are used to work locally, to make them work in your deployed app you need to set them up in heroku as well.

  • Go to your heroku dashboard app page and go to the Settings Tab

  • Add all the envs variable that connect to your Database or any external API's there.

  • To deploy

    • $ git add && $ git commit
    • $ git push heroku master
  • If your application isn't deployed successfully do

    • $ heroku logs
    • $ heroku logs -t

You can also share your cluster with other team members so that they can check the data in MongoDB Compass

  • Wile connecting, choose mongoDB Compass
  • Share that url with your password

Ensure you've allowed any website to connect to your database

  • Go to the Network Access tab . Edit the IP whitelist and allow all sites to connect.

Checking for errors

To view error, we'll need to check the logs from heroku and see why it might be crashing.

  • Go to your project directory, open the terminal and type heroku logs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment