Git Product home page Git Product logo

mongodb_atlas_deployment's Introduction

MERN App Deployment

MERN

Objectives

  • Deploy server to Heroku
  • Deploy database to MongoDB Atlas
  • Deploy React app to either Netlify or Heroku

Disclaimer

This repo is set up assuming that your apps are stuctured the same way we've done them in class.

MongoDB Atlas

Once you've signed in to MongoDB Atlas, let's set up your account:

setup

  • Give Your Organization a name.

  • Create a project name.

  • Select your preferred language, javascript should be your preferred.

When choosing a path, select the free tier.

Select Tier

Creating A Cluster

Once you've selected a tier, let's set up our cluster/database.

Cluster

You can leave most of these settings as default, double check that your Cluster Tier is M0 Sandbox. Give your cluster a name, ideally this should be the name of your database.

You cluster should now be provisioning.

Provisioning

Creating A Database User

Click on Database Access on the left sidebar. Create a new database user.

Creating A User

Don't lose this password!

Database Access Control

Select the Network Access option from the left sidebar. And click on Add IP Address.

ACL

Select Allow Access From Anyhwere. Confirm your changes.

Connecting Your Database

Select the Clusters option on the left sidebar and click on CONNECT underneath your cluster name.

Select connect your application:

Copy the connection URL, do not lose this!

Wiring Up Our Code

We now need to prep our code for deployment.

Let's install the dotenv package to read environment variables:

Note: This should be done along side your server code, or your base directory for your project.

npm install dotenv

NOTE: Only do this step if you have environment variables

touch .env

In your server.js:

  • Telling Express to serve our react app:

    NOTE: This should be added with your other middleware

    app.use(express.static(`${__dirname}/client/build`))

    NOTE: This should be added just before app.listen

    app.get('/*', (req, res) => {
     res.sendFile(`${__dirname}/client/build/index.html`)
    })

Modify your db/index.js to the following:

const mongoose = require('mongoose')
require('dotenv').config() // Add this line

let dbUrl = process.env.NODE_ENV === 'production' ? process.env.MONGODB_URI : 'mongodb://127.0.0.1:27017/<database_name>'

mongoose
  .connect(dbUrl)
  .then(() => {
    console.log('Successfully connected to MongoDB!')
  })
  .catch((e) => {
    console.error('Connection error', e.message)
  })
mongoose.set('debug', true)
const db = mongoose.connection

module.exports = db

Next, in your package.json for your server, add a new script in your scripts section:

    "build": "cd client && rm -rf build && npm install && npm run build"

Proxy requests in development to server

In client/package.json add just above "scripts": {

"proxy": "http://localhost:3001",

Initializing A Heroku App

To install Heroku, enter this in your terminal

brew tap heroku/brew && brew install heroku

In your project folder type in the following command:

  heroku create

This will create a Heroku app for you.

Next, we'll add our MongoDB connection string to Heroku for our Atlas Database:

Replace <your password> and <dbname> with the user password for your database and database name, respectively. Remember, these must be an exact match.

Enter this in your terminal command line

heroku config:set MONGODB_URI='mongodb+srv://<username>:<database_password>@<cluster>.i57hr.mongodb.net/<database_name>?retryWrites=true&w=majority'

Finally we'll add, commit, and push our changes to Heroku:

git add .
git commit -m <some message>
git push heroku main

The git push heroku main will only push the changes to Heroku. To push them to GitHub enter the following:

git push

We can monitor what our app is doing with the following command:

heroku logs --tail

Recap

In this lesson, we successfully deployed our MERN app to Heroku. The server will render the built React app and handle the clients API requests. We used MongoDB Atlas to host our Mongo database.

Resources

mongodb_atlas_deployment's People

Contributors

jasonmar08 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.