<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Rushikesh</title>
    <description>The latest articles on DEV Community by Rushikesh (@rmhetre).</description>
    <link>https://dev.to/rmhetre</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F274921%2F10c2652c-4f52-4e8b-99af-bc7a9e53073d.jpg</url>
      <title>DEV Community: Rushikesh</title>
      <link>https://dev.to/rmhetre</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rmhetre"/>
    <language>en</language>
    <item>
      <title>How to build a microservice with NodeJs, Express and MongoDB</title>
      <dc:creator>Rushikesh</dc:creator>
      <pubDate>Thu, 13 May 2021 08:21:09 +0000</pubDate>
      <link>https://dev.to/rmhetre/how-to-build-a-microservice-with-nodejs-express-and-mongodb-3l5o</link>
      <guid>https://dev.to/rmhetre/how-to-build-a-microservice-with-nodejs-express-and-mongodb-3l5o</guid>
      <description>&lt;p&gt;Most of us have gone through tough times where you have a large scale&lt;br&gt;
application which has a bunch of features, API integrations, database&lt;br&gt;
connections, etc. New features and updates get released, and of course you need&lt;br&gt;
to fix the bugs and maintain the code.&lt;/p&gt;

&lt;p&gt;Building this real-world application requires dynamic programming, where the&lt;br&gt;
size of the application grows uncontrollably.&lt;/p&gt;

&lt;p&gt;This monolith pattern becomes very hard to downsize or simplify the application.&lt;br&gt;
So to run the app smoothly, it is essential to convert the large, homogeneous&lt;br&gt;
structure into small, independent pieces of programs. Similar complexities can&lt;br&gt;
be resolved effortlessly when the NodeJs applications are build on&lt;br&gt;
**microservices, **more so with the Node.js ecosystem.&lt;/p&gt;

&lt;p&gt;So I am going to show you how to build the microservices…&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This blog is only for an overview of how to build the services, please visit&lt;br&gt;
&lt;a href="https://github.com/Nomercy10/ecommerce-micro-services" rel="noopener noreferrer"&gt;https://github.com/Nomercy10/ecommerce-micro-services&lt;/a&gt;&lt;br&gt;
for a full functional working microservices demo.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In our example we will be building two microservices, **Users &amp;amp; Orders. **The&lt;br&gt;
idea is very simple, users will place an order. We create the order with all the&lt;br&gt;
required details, and so on.&lt;/p&gt;

&lt;p&gt;Here is the list of the features each of these microservice will have&lt;/p&gt;

&lt;h4&gt;
  
  
  Users
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;GET all users&lt;/li&gt;
&lt;li&gt;GET user&lt;/li&gt;
&lt;li&gt;Create new user&lt;/li&gt;
&lt;li&gt;DEL user by userId&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Orders
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;GET all orders&lt;/li&gt;
&lt;li&gt;GET order&lt;/li&gt;
&lt;li&gt;GET orders for a user&lt;/li&gt;
&lt;li&gt;Create new order for a user&lt;/li&gt;
&lt;li&gt;DEL an order&lt;/li&gt;
&lt;li&gt;DEL orders for an user&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Tech
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://nodejs.org/" rel="noopener noreferrer"&gt;Node.js&lt;/a&gt; : evented I/O for the backend&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://expressjs.com/" rel="noopener noreferrer"&gt;Express&lt;/a&gt; : Fast node.js network app framework&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.mongodb.com/" rel="noopener noreferrer"&gt;MongoDb&lt;/a&gt; : A NoSql database for modern applications&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As we build these microservices we will create separate folders for each&lt;br&gt;
microservice. Each folder will have its own package.json and node modules&lt;br&gt;
installed.&lt;/p&gt;

&lt;p&gt;Let’s get started…&lt;/p&gt;

&lt;h3&gt;
  
  
  Installation
&lt;/h3&gt;

&lt;p&gt;Create &lt;code&gt;Users&lt;/code&gt; folder and install below dependencies&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2AwDmPGwtWmkeDf41LCZve3w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2AwDmPGwtWmkeDf41LCZve3w.png"&gt;&lt;/a&gt;&lt;br&gt;
&lt;span&gt;Users installed dependencies&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Info about the dependencies&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;body-parser&lt;/strong&gt;&lt;em&gt;:&lt;/em&gt; &lt;em&gt;It is a Node.js body parsing middleware, it parses incoming
request bodies in a middleware before your handlers and is available under
*&lt;code&gt;req.body&lt;/code&gt;&lt;/em&gt; property.*&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;axios&lt;/strong&gt;&lt;em&gt;: A promise based HTTP client for browser and Node Js.&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;nodemon&lt;/strong&gt;&lt;em&gt;: A tool used for Node Js development as it provides hot-reloading
for any file change.&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;mongoose&lt;/strong&gt;&lt;em&gt;: A MongoDB Object modelling tool designed to work in an
asynchronous environment.&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Development
&lt;/h3&gt;

&lt;p&gt;Create &lt;code&gt;Users.js&lt;/code&gt;in the &lt;code&gt;/Users&lt;/code&gt; folder, this file will be our entry point to&lt;br&gt;
Users microservice.&lt;/p&gt;

&lt;p&gt;Load the dependencies&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2Apuf0ITJf2bHxqzOVesgmmA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2Apuf0ITJf2bHxqzOVesgmmA.png"&gt;&lt;/a&gt;&lt;br&gt;
&lt;span&gt;Load dependencies&lt;/span&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Install the MongoDB Node.js Driver
&lt;/h3&gt;

&lt;p&gt;The MongoDB Node.js Driver allows you to easily interact with MongoDB databases&lt;br&gt;
from within Node.js applications. You’ll need the driver in order to connect to&lt;br&gt;
your database and execute the queries described in this Quick Start series.&lt;/p&gt;

&lt;p&gt;If you don’t have the MongoDB Node.js Driver installed, you can install it with&lt;br&gt;
the following command.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2AhfUl7T8uYc-p60FK_D4LBw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2AhfUl7T8uYc-p60FK_D4LBw.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Create a free MongoDB Atlas cluster and load the sample data
&lt;/h3&gt;

&lt;p&gt;Next, you’ll need a MongoDB database. The easiest way to get started with&lt;br&gt;
MongoDB is to use Atlas, MongoDB’s fully-managed database-as-a-service.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.mongodb.com/cloud/atlas" rel="noopener noreferrer"&gt;Head over to Atlas&lt;/a&gt; and create a new&lt;br&gt;
cluster in the free tier. At a high level, a cluster is a set of nodes where&lt;br&gt;
copies of your database will be stored. Once your tier is created, &lt;a href="https://docs.atlas.mongodb.com/sample-data/" rel="noopener noreferrer"&gt;load the&lt;br&gt;
sample data&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Get your cluster’s connection info
&lt;/h3&gt;

&lt;p&gt;The final step is to prep your cluster for connection.&lt;/p&gt;

&lt;p&gt;In &lt;a href="https://www.mongodb.com/cloud/atlas" rel="noopener noreferrer"&gt;Atlas&lt;/a&gt;, navigate to your cluster and&lt;br&gt;
click &lt;strong&gt;CONNECT&lt;/strong&gt;. The Cluster Connection Wizard will appear.&lt;/p&gt;

&lt;p&gt;The Wizard will prompt you to add your current IP address to the IP Access List&lt;br&gt;
and create a MongoDB user if you haven’t already done so. Be sure to note the&lt;br&gt;
username and password you use for the new MongoDB user as you’ll need them in a&lt;br&gt;
later step.&lt;/p&gt;

&lt;p&gt;Next, the Wizard will prompt you to choose a connection method. Select Connect&lt;br&gt;
Your Application. When the Wizard prompts you to select your driver version,&lt;br&gt;
select Node.js and 3.6 or later. Copy the provided connection string as you will&lt;br&gt;
use in the following section.&lt;/p&gt;

&lt;p&gt;For more details on how to access the Connection Wizard and complete the steps&lt;br&gt;
described above, see the &lt;a href="https://docs.atlas.mongodb.com/connect-to-cluster/" rel="noopener noreferrer"&gt;official&lt;br&gt;
documentation&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Connect to your database from a Node.js application
&lt;/h3&gt;

&lt;p&gt;Now that everything is set up, it’s time to code! Let’s write a Node.js script&lt;br&gt;
that connects to your database and lists the databases in your cluster.&lt;/p&gt;

&lt;h3&gt;
  
  
  Import mongoose client
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2AElg2AMZsZ-n6-oNEgtNt7w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2AElg2AMZsZ-n6-oNEgtNt7w.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;mongoose&lt;/code&gt; client will be used to connect to a MongoDB database. This is an&lt;br&gt;
instance of MongoDb to connect to a cluster, access the database in that&lt;br&gt;
cluster, and close the connection to that cluster.&lt;/p&gt;

&lt;p&gt;The first thing after this we need to do is create a constant for our connection&lt;br&gt;
URI. The connection URI is the connection string you copied in Atlas in the&lt;br&gt;
previous section. When you paste the string don’t forget to update &lt;code&gt;&amp;lt;username&amp;gt;&lt;/code&gt;&lt;br&gt;
and &lt;code&gt;&amp;lt;password&amp;gt;&lt;/code&gt; to be the credentials for the user you created in the previous&lt;br&gt;
section. The connection string includes a &lt;code&gt;&amp;lt;dbname&amp;gt;&lt;/code&gt; placeholder.&lt;/p&gt;

&lt;p&gt;**Note: **The username and password you provide in the connection string are NOT&lt;br&gt;
the same as your Atlas credentials.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2AImrgGjVZcODWYaOE7n-bhw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2AImrgGjVZcODWYaOE7n-bhw.png"&gt;&lt;/a&gt;&lt;br&gt;
&lt;span&gt;Connection string URI&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Now that we have the URI, we can create an instance of the MongoDB&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2ABZB_wZDkkyOEyv_eqbO3BA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2ABZB_wZDkkyOEyv_eqbO3BA.png"&gt;&lt;/a&gt;&lt;br&gt;
&lt;span&gt;inItialization of MongoDB client and loading the global instance&lt;/span&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Database Models
&lt;/h3&gt;

&lt;p&gt;Now that we have our connection setup, let’s go ahead and create MongoDB data&lt;br&gt;
models. We will be using &lt;code&gt;mongoose.model&lt;/code&gt; &lt;br&gt;&lt;br&gt;
&lt;a href="https://mongoosejs.com/docs/api.html#model-js" rel="noopener noreferrer"&gt;Models&lt;/a&gt; are fancy constructors&lt;br&gt;
compiled from &lt;code&gt;Schema&lt;/code&gt; definitions. An instance of a model is called a&lt;br&gt;
&lt;a href="https://mongoosejs.com/docs/documents.html" rel="noopener noreferrer"&gt;document&lt;/a&gt;. Models are responsible&lt;br&gt;
for creating and reading documents from the underlying MongoDB database.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2AxtBSq2cMu-8kkHN8XF9Ctg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2AxtBSq2cMu-8kkHN8XF9Ctg.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  APIs
&lt;/h3&gt;

&lt;p&gt;For the sake of this blog not becoming too long to read I am going to show only&lt;br&gt;
two APIs. You may create the others I referenced or as many features you want.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;GET all orders for a user API&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2AnMAaGOPyy65eRaRt2Sw96Q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2AnMAaGOPyy65eRaRt2Sw96Q.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see above, when we trigger “/users/:uid/” orders we are making an&lt;br&gt;
external API call to Orders microservice and a call the GET API for&lt;br&gt;
“/orders?uid= ”. This way we have communicated between the microservices and&lt;br&gt;
have shared data.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Create new user API&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2ADlrhSBuY6nQTuHVhs9t6DQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2ADlrhSBuY6nQTuHVhs9t6DQ.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we are creating a new user. First we create a newUser&lt;br&gt;
object with the response from the request body.&lt;br&gt; We then create a new “User”&lt;br&gt;
instance, a MongoDB data model User instance &amp;amp; pass the “newUser” to it.&lt;/p&gt;

&lt;p&gt;We can now use .save() property to save the data to MongoDB. As its a promise&lt;br&gt;
based property we can use &lt;code&gt;.then&lt;/code&gt; and perform the operation we want to.&lt;/p&gt;

&lt;p&gt;**Note: **Note: I am not showing here how to create the microservice for Orders&lt;br&gt;
as it is pretty straightforward and similar to what we have done for Users&lt;br&gt;
microservice. If you still need to see how to implement that, please visit my&lt;br&gt;
github repo link mentioned above at the start of the blog.&lt;/p&gt;

&lt;p&gt;Thankyou!&lt;/p&gt;

&lt;p&gt;Happy Coding!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
