3rd Feb 2020

Building a RESTful API with Node.js and MongoDB Atlas

Mean

Written By, Yuvraj Upadhyay

Overview

In the domains of JavaScript and Node.js, MongoDB has set up itself as the go-to database. MongoDB Atlas takes the responsibility of hosting, patching, maintaining and guarding your MongoDB cluster, and leaves you free for putting it to smart use.

Atlas makes connecting MongoDB as simple as clicking a button and answering four queries. Once that’s complete you’ll have a MongoDB cluster running some moments later. Creating clients and designating restricted consents is easy and done through a nice UI.

In this tutorial, We’re planning to see how to create a restful API using Node.js and Express Framework that communicates to a cloud instance of MongoDB known as Atlas.

Create Cluster on MongoDB Atlas

If you haven’t registered, register yourself here.
restfulapi-node-mongodb-cluser-signup

Once you have logged in successfully, click ‘Build a Cluster’.

restfulapi-node-mongodb-build-cluster

Next, You should see a screen like below and select the Starter Clusters:

restfulapi-node-mongodb-starter-clusters

Now make sure to choose a region which is marked as “Free Tier Available”. Then choose the “MO Sandbox” Cluster tier. This is completely free. We’ll keep the cluster name as RAW.

restfulapi-node-mongodb-create-cluster

After a few minutes, you should see a screen like this:
restfulapi-node-mongodb-cluster-dashboard

Setup Network Permissions:

To Connect Cluster it requires an IP address, you can use your current IP address or there is one more option you can connect with any IP address.

restfulapi-node-mongodb-network-permissions

Set User Permissions:

Now we need to define users and set permissions according to their roles. For this tutorial, we will create a user called “Admin” with password “admin@123”.(For your main project, I would recommend using more secured credentials)

restfulapi-node-mongodb-user-permissions

Get URI:

restfulapi-node-mongodb-get-uri

From the options, choose ‘Connect Your Application’ and you will get a connection URI like this:

restfulapi-node-mongodb-connection-method

Copy the URI. Now we’re ready to start coding.

Connect Cluster using Node.js and Mongoose

Make sure you have Node installed, then open new directory and open terminal and type

Next, we will install all node modules we’ll be using in this tutorial:

Create a file called “index.js” in the root directory.

restfulapi-node-mongodb-indexjs

Create a file called “keys.js” for config our mongoURI which copies while we connect the cluster, and Replace with the password for the Admin User.

restfulapi-node-mongodb-keysjs

To test the connection that your server is working fine or not, go into the root directory in the terminal and type:

If you follow the steps correctly you can see output like this:

restfulapi-node-mongodb-output

Create Models for Data using Mongoose

One of MongoDB’s benefits is that it’s no schema: there’s no need to create tables in advance, therefore you’ll be able to send information without the fear of filling in each field properly.

But what if you would like to define objects with a strongly-typed schema? That’s where mongoose comes in. Mongoose is common as it provides a simple way to bring structure to MongoDB and a host of related benefits.

In this tutorial, we’ll make an ‘Agent’ model.

restfulapi-node-mongodb-usersjs

Expose API endpoints for Database operations

For Testing API endpoints we will be using POSTMAN. If you haven’t installed POSTMAN you can install it from here.

Once Postman is loaded, Create a new POST request and URL should be like
http://localhost:8858/register.

Set the one header with the key Content-Type with value application/x-www-form-urlencoded.

restfulapi-node-mongodb-api-name

In Body section select x-www-form-urlencoded with the key name and value “your name”.

restfulapi-node-mongodb-api-name

After sending Request you can get a response like:

restfulapi-node-mongodb-api-response

You can also see in your MongoDB cluster.

restfulapi-node-mongodb-clusters

Conclusion

If you followed this article till the end, you’ll now have experience of integrating your MongoDB as part of your Back-end. If you like the article and want to explore similar concepts in detail, let us know.

Written By,

Blockchain Developer at Yudiz Solutions Pvt. Ltd