Using MongoDB in the cloud

Today, we have many options to work with MongoDB, from in-house services to hosting companies that provide Platform as a Service (PaaS) and Software as a Service (SaaS).

We will present a solution called Database as a Service (DbaaS) that provides database services for highly scalable web applications.

Here's a simple step-by-step process to start using a MongoDB instance with a cloud service:

  1. Go to https://www.compose.io/.
  2. Create your free account.
  3. On your dashboard panel, click on add Database.
  4. On the right-hand side, choose Sandbox Database.
  5. Name your database as node-api.
  6. Add a user to your database.
  7. Go back to your database title, click on admin.
  8. Copy the connection string.
  9. The string connection looks like this:
    mongodb://<user>:<password>@kahana.mongohq.com:10073/node-api.

Let's edit the server.js file using the following steps:

  1. Place your own connection string to the Mongoose.connect() function.
  2. Open your terminal and input the command:
    nodemon server.js
    
  3. Open your browser and place http://localhost:8080/api.
  4. You will see a message like this in the browser:

    {

    Hello SPA, the API is working!

    }

  5. Remember the api folder was defined on the server.js file when we registered the routes:
    app.use('/api', router);
  6. But, if you try to access http://localhost:8080/api/speakers, you must have something like this: []

    This is an empty array, because we haven't input any data into MongoDB.

Tip

We use an extension for the Chrome browser called JSONView. This way, we can view the formatted and readable JSON files. You can install this for free from the Chrome Web Store.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.186.79