Connecting to MongoDB Atlas

  1. Log in to Your Atlas Account

    https://www.mongodb.com/cloud/atlas/register → Link

  2. Click on New Project

    1. Give Project Name & Click next

      Untitled

    2. Click on Create Project

      Untitled

  3. Create Cluster

    1. Click Create

      Untitled

    2. Select M0 Cluster, Give Name Of your Cluster & Click on go to Advanced Configuration

      Untitled

    3. Select Shared & Click on Create Cluster

      Untitled

  4. Set Username and Password & Click Create User

    Note copy the Password for the Future usage

    Untitled

  5. Add IP address → Click add current IP address & Click Finish & Close

    Untitled

  6. Connect To Cluster


Creating Environment Variables

  1. Install dotenv using npm

    npm i dotenv
    
  2. Import it to the script

    // Import dotenv Module
    require('dotenv').config();
    
  3. Create a file named .env in the root of the dir.

  4. Create variables in the .env file.

    DBURL=mongodb+srv://pavankc005:[email protected]/?retryWrites=true&w=majority&appName=Cluster0
    PORT=4000
    
  5. Using it in the script

    // Assign Port Number to Server
    const port = process.env.PORT || 4000;
    app.listen(port, () => {
        console.log(`Server running on port <http://localhost>:${port}`);
    });
    
    // Database URL
    const dbURL = process.env.DBURL
    

Create Products API

  1. Connect to MongoDb and import the Products Collection
  2. In products-api, change the “/products” endpoint:
  3. In products-api, change the “/products/:id” endpoint: