Edit on GitHub
Report issue
Page history

Hello World app - MongoDB Atlas and App Engine flexible environment with Node.js

Author(s): @arajwade @smithkh ,   Published: 2018-06-25

Overview

In this tutorial, you will be building a "Hello World" application using Node.js with App Engine flexible environment for our frontend and a MongoDB Atlas multi-regional cluster on Google Cloud Platform as our primary database.

Technical Complexity

Beginner

Duration

45 Minutes

Objectives

  1. Create and configure MongoDB Atlas multi-regional cluster on GCP.
  2. Configuring our Google Cloud Platform Account.
  3. Configure a Node.js application on a GCE Debian VM on GCP.
  4. Push your application to App Engine Flex on GCP.
  5. Visit our new application from any web-enabled client terminal, including mobile devices.

Part 1: Configuring the MongoDB Atlas

  1. Create a free account on MongoDB Atlas on www.mongodb.com/cloud/atlas

    Click on "Login" at the top of the page:

    image

  2. If you do not already have a MongoDB Atlas account, register for new account at the bottom of the page. If you do, skip to Step 5.

    image

  3. Enter necessary details and click "Continue":

    image

  4. Once on the MongoDB Atlas Homepage, select "Build a New Cluster":

    image

  5. Create New Cluster by selecting Google Cloud Platform as a "Cloud Provider". Next select the region where you want to place your Atlas cluster. Ideally, your cluster will be located close to your end user for lower latency. Note that we can select a free tier region in your area of choice, as noted by the "Free Tier Available" icon. This is a no-cost option to get started. However, since we are assuming our end users will span across the globe, we want our Atlas cluster to be multi-regional. To enable a multi-regional cluster, we will need to select a M10 or larger cluster size. So for now, for our use case, let’s assume our primary end users for our application will be based in the US and a smaller percentage based in the UK and Australia. As such, we will select our primary region to be in North America, based in Iowa (us-central1).

    image

  6. Next, to enable multiple regions, lets configure our cluster size under "Cluster Tier". Select "M10" under "Dedicated Development Clusters" subheading since this is the minimal size to move forward for multiple-regions.

    image

  7. Now that we selected a M10 cluster, let's go back to "Cloud Provider & Region" and toggle the option to "Configure clusters across multiple regions" from "No" to "Yes". Once we enable this we will see more options. You will see your previously selected region as "Preferred" under "Node Type". Since for our use-case we will also have some users in the UK and Australia, we want to take into the considering the distance between our primary cluster in the US and enable lower latency for better read performance. As such, we will add a Read-only replica in London (europe-west2) and Sydney (australia-southeast1). Under the subheading "Deploy read-only replicas", select "Add a node" and add these two regions with 1 node each.

    image

  8. We can skip the section "Additional settings" by clicking the "NEXT: CLUSTER NAME" button since we will keep the default settings.

    image

  9. Under "'Cluster Name', provide a cluster Name." For this demo, we will keep the default of "Cluster0". Click on "Create Cluster" button at the bottom of the page.

    image

  10. Our cluster is spinning up...

    image

  11. While this spins up, let's click on "Security" tab and then click on "Add New User".

    image

  12. Leave the default user as of "admin" and select a secure password. Record your user name and password in a safe location for reference later. Under "User Privileges", select "Atlas admin" and click on the "Add User" button to complete this section.

    image

  13. Once done, we will see screen similar to this...

    image

  14. Under Security tab, select "IP Whitelist" and click on "Add IP Address".

    image

  15. Select "Allow Access from Anywhere" for the purpose of this demo and click on "Confirm". Note: When actually putting something into production, you will want to narrow the scope of where your database can be accessed and specify a specific IP address/CIDR block.

    image

  16. Go to "Overview" tab and click on "Connect" button.

    image

  17. A window will open. Select "Connect Your Application"

    image

  18. Click on "I am using driver 3.4 or earlier" and copy the connection string and keep it in a text file. We will be using it in our Node.js application to connect to MongoDB Atlas in Part 2 of this document. Close the pop-up dialogue

    image

    image

Part 2: Configuring our Google Cloud Platform Account

  1. Go to cloud.google.com and login with your Google account. If you don't have a Google account, please create a free trial account by following instructions at this link.

  2. If not already there, go to https://console.cloud.google.com/

  3. Create a new project, by selecting the following dropdown in the top left:

    image

  4. A new window will pop up. In it, select "New Project" in the top left:

    image

  5. Give your Hello World app a new project name and click the "Create" button:

    image

    image

  6. After your new project is done being created. Go back to the dropdown in Step 3, select your new project name:

    image

  7. When the right project is selected, the name will change to reflect this in the dropdown in the top left of your console:

    image

  8. Next, let's enable the specific APIs we will need:. Click on "APIs & Services" in the left toolbar:

    image

  9. Then select, "ENABLE APIS AND SERVICES":

    image

  10. Select both "Google App Engine Flexible Environment" and "Google App Engine Admin API", then select "Enable":

    image

    image

    image

  11. Next, lets enable App Engine for our specific language. Use the search bar in the console and type in "App Engine". Select "App Engine" from the list of options

    image

  12. In the blue box on the left, choose the "Select a language" dropdown:

    image

  13. Choose Node.js:

    image

  14. Select a region where the majority of your users will be, then select "Next":

    image

  15. Close out of the optional tutorial on the right side, by selecting "Cancel Tutorial" in the bottom right:

    image

Part 3: Configuring and deploying our Node.js application

  1. Create a Debian Linux GCE VM instance using the instructions given here.

    NOTE: When creating your instance, please be sure to enable "Allow full access to all Cloud APIs" under "Identity and API access":

    image

  2. After your instance is created, SSH to your instance by clicking on the SSH button of your instance.

    image

  3. You should see a Linux window similar to this open...

    image

  4. Configure your instance for Node.js and MongoDB client by executing following comments.

    sudo apt-get update
    
    curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
    
    sudo apt-get install -y nodejs
    
    sudo apt-get install -y build-essential
    
    npm install nconf
    
  5. Preparing the app - Initialize a package.json file with the following command:

    npm init
    

    image

    For "package name", enter: "test"

    image

    • For "version", enter: "1.0.0"
    • For "description", leave blank
    • For "entry point", enter: "server.js"
    • For "test command", leave blank
    • For "git repository", leave blank
    • For "keywords", leave blank
    • For "author", leave blank
    • For "license", leave blank

    image

    When done, you should see something similar to this...

    image

    Enter "yes" and press enter

  6. Install dependencies:

    npm install mongodb@2.2.33 --save
    

    image

  7. Create a server.js file with the following contents by using command.

    nano server.js
    

    Copy the given code into the Nano editor and save the file using Ctrl + X. NOTE: See the highlighted section where you need to insert your own Atlas Connection string.

    'use strict';
    
    const mongodb = require('mongodb');
    const http = require('http');
    const nconf = require('nconf');
    let uri = ` PASTE YOUR MONGODB ATLAS CONNECTION STRING HERE `;
    if (nconf.get('mongoDatabase')) {
      uri = `${uri}/${nconf.get('mongoDatabase')}`;
    }
    console.log(uri);
    
    mongodb.MongoClient.connect(uri, (err, db) => {
      if (err) {
        throw err;
      }
    
      // Create a simple little server.
      http.createServer((req, res) => {
        if (req.url === '/_ah/health') {
          res.writeHead(200, {
            'Content-Type': 'text/plain'
          });
          res.write('OK');
          res.end();
          return;
        }
    
        const collection = db.collection('Messages');
        var datetime = new Date();
        const msg = {
          msgDescription: '\nHello World received on ' + datetime
        };
    
        collection.insert(msg, (err) => {
          if (err) {
            throw err;
          }
    
          // push out a range
          let msglist = '';
          collection.find().toArray((err, data) => {
            if (err) {
              throw err;
            }
            data.forEach((msg) => {
              msglist += `${msg.msgDescription}; `;
            });
    
            res.writeHead(200, {
              'Content-Type': 'text/plain'
            });
    res.write('Messages received so far:\n');
            res.end(msglist);
          });
        });
      }).listen(process.env.PORT || 8080, () => {
        console.log('started web process');
      });
    });
    
    1. Enter "Exit" to leave
    2. On prompt to save, enter "Y"
    3. Keep same file name, Hit Enter
  8. Running our app - Run the app locally by running the following command:

    npm start
    

    image

  9. Open another instance of SSH session by repeating the steps listed in X.2 in the cloud console and run following command

    curl localhost:8080
    

    image

Part 4: Push our application to App Engine Flex on GCP

Deploying the app to App Engine Flex

  1. Create an app.yaml file by running the following command:

    nano app.yaml
    
  2. Add following content to to app.yaml file by running the following command:

    runtime: nodejs
    env: flex
    
  3. Run the following command to deploy your app by running the following command:

    gcloud app deploy
    
  4. View the deployed app by running the following command:

    gcloud app browse
    

Part 5: Visit our new application from any web-enabled client terminal; including mobile devices.

  1. Retrieve your external URL from the output of gcloud app browse command:

    image

  2. Use a web-enabled client terminal to visit your new "Hello World" application using the external URL in the previous step. You should see webpage showing screen similar to this:

    image

See more tagged App Engine Node.js MongoDB Atlas

Submit a Tutorial

Share step-by-step guides

SUBMIT A TUTORIAL

Request a Tutorial

Ask for community help

SUBMIT A REQUEST

GCP Tutorials

Tutorials published by GCP

VIEW TUTORIALS

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see our Site Policies. Java is a registered trademark of Oracle and/or its affiliates.