<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Arjun K S on Medium]]></title>
        <description><![CDATA[Stories by Arjun K S on Medium]]></description>
        <link>https://medium.com/@arjunks1991?source=rss-c341fd0e40a9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*DM9m_9FzPXSNqiYdsaJ12g.jpeg</url>
            <title>Stories by Arjun K S on Medium</title>
            <link>https://medium.com/@arjunks1991?source=rss-c341fd0e40a9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 13:16:12 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@arjunks1991/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[API Gateway, Service Discovery and Load Balancing in Microservices applications using Zuul, Eureka…]]></title>
            <link>https://medium.com/swlh/service-registry-service-discovery-and-load-balancing-in-microservices-applications-83c937c16d48?source=rss-c341fd0e40a9------2</link>
            <guid isPermaLink="false">https://medium.com/p/83c937c16d48</guid>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[load-balancing]]></category>
            <category><![CDATA[microservices-application]]></category>
            <category><![CDATA[service-discovery]]></category>
            <category><![CDATA[spring-boot]]></category>
            <dc:creator><![CDATA[Arjun K S]]></dc:creator>
            <pubDate>Fri, 02 Jul 2021 07:56:09 GMT</pubDate>
            <atom:updated>2021-07-05T06:08:39.985Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>API Gateway, Service Discovery and Load Balancing in Microservices applications using Zuul, Eureka and Ribbon</strong></h3><p>Microservices architecture has been a buzzword in the technology space for some time now and my curiosity got the better of me. When I ventured into creating web applications using microservices architecture, I figured out why this has become so popular. In this era of containerization and cloud computing, it provides a long list of benefits that will make the life of every developer easy.</p><p>Microservices is an architectural style that structures an application as a collection of services that are easily maintainable and testable, loosely coupled, independently deployable and could be maintained by entirely different teams. Each service could be developed and deployed on a different platform using different programming languages and developer tools.</p><p>Here I am with another blog which takes you through some important concepts and tools that could be useful while working with microservices. The following are the concepts that I will be covering in this blog — Service Registry, Service Discovery and Load Balancing. I have also included some code snippets which can help you configure certain related tools directly into a spring boot application.</p><p><strong>Prerequisites</strong></p><p>As first steps let me introduce to you certain tools like Eureka, Ribbon and Zuul which are created and developed by Team Netflix. Eureka and Ribbon are tools which will take care of service discovery and load balancing. Zuul is another such tool used for implementing API gateway in a microservice based application. I will take you through how these can be seamlessly integrated into a Spring-Boot application. In this article I have used the above mentioned tools just to demonstrate the concepts. There are many other better solutions provided by various cloud vendors to implement these concepts.</p><p>Basic understanding of Java and Spring-boot is recommended to follow this article</p><p><strong>The concepts</strong></p><p>Let’s say you are working on a microservice architecture and you have multiple services which communicate with each other. In order to make a request to a service, the network location must be identified. You might wonder why we can’t make the request using the hardcoded network location of the service instance. Yes, that is possible in a traditional way of running applications on a physical hardware, where the network locations of the services would be static. But in a modern cloud-based microservices application, this method won’t suffice. There could be multiple instances of the same service in different network locations. The service instances would have dynamically allocated network locations and also, the set of service instances could also change because of auto-scaling, failures, upgrades etc. So the client needs to have a more reliable or more elaborate service discovery mechanism. Service Discovery is nothing but a way of locating a service and Load Balancing is about deciding which service instance to be invoked in case of multiple instances.</p><p><strong>Understanding the concepts</strong></p><p>We will go step by step to understand these concepts completely . First, I will introduce the Service Registry mechanism.</p><p>Service registry is nothing but another microservice where all the independent services of the application will have to be registered. During the startup, all other services will get registered in the registry using an alias. Each service could be identified using the respective alias registered. Suppose a microservice named MicroserviceA is registered in the registry with an alias as ‘microserviceA’. Now this service could be identified using the specified alias. i.e <a href="http://microservicea/">http://microserviceA</a> instead of <a href="http://[HOST]:[PORT]/.">http://[HOST]:[PORT]/.</a> Which means the client making the request need not know the ip address and port number of the service instance. For this to work there should be someone in there who translates the alias into specific actual URLs. The mapping in the registry would do this job. This would be explained in detail later. Doesn’t this mechanism look familiar to you? Yes, it is pretty similar to dynamic DNS: we assign an alias to a service so it can move around locations without us needing to care about the particular IP in which it is deployed.</p><p>This is how a Service Registry works.</p><p>You might be thinking what if we have multiple instances for the same service? Which instance would be selected if a service is accessed with its alias? For this we need the Load Balancing mechanism. It could be done either from the client side or from the server side. Suppose we have two instances for the MicroserviceA which are accessible at ports 8081 and 8082 respectively. Both these instances will be registered in the service registry during the start up and will be mapped to the same alias ‘microserviceA’. So when a request comes with this alias, both the instances will be returned by the registry. A Load Balancer comes into action here. It will decide on which instance to be chosen. So a service registry along with a Load Balancer will do the service discovery process. This would allow us to scale up our services without any tight coupling with the infrastructure.</p><p><strong>Problem not fully solved</strong></p><p>Our web client will be running on a browser. It won’t be able to take care of the service discovery or load balancing. Also we have tasks like authentication or request filtering which have to be implemented in our application for which we need a central point of control for our APIs. For this purpose we will implement an API Gateway which would act as a common entry point for all the requests. Any authentication logic or request filtering could be introduced here. The API Gateway will be another microservice running in our application that contains a routing table which points to the microservice aliases registered in the service registry. The API gateway would accept all the requests and would use the service discovery and load balancing to redirect the request to the respective service instance.</p><p><strong>Introducing the tools.</strong></p><p>Now I will introduce various tools that could be used for these purposes. Netflix Zuul could be used as an API Gateway which would act as a front door for all requests in our application. Any client that wants to access our microservices has to access or will be directed to the API gateway first.</p><p>For the service registry and discovery we will use another tool from Netflix — Eureka. All services running in the architecture need to register in the Eureka server.</p><p>And finally for the load balancing purpose, we will be using another Netflix product named Ribbon. It takes care of the client side load balancing.</p><p><strong>How do they integrate?</strong></p><p>The API gateway microservice implemented using Zuul will contain a routing table that points to microservice aliases registered in Eureka instead of the physical addresses. This is where all the above mentioned tools integrate and combine together to give us a full solution based on an API Gateway, service discovery and load balancing. When Zuul receives a request from any of the web clients, it decomposes the request URL and locates the pattern in the routing table. Every pattern in the table is mapped to a microservice alias. Now, Zuul gets help from Eureka to go to the registry and find the available instances corresponding to the alias. It is here where Ribbon comes into action and picks one of the instances returned by Eureka based on the load balancing strategy defined. We could add a custom load balancing strategy if required. And finally Zuul redirects the original request to the corresponding microservice instance.</p><p><strong>Time for some hands-on experience</strong></p><p>Finally! Now that you understand the basic concepts, we can apply these patterns by including Zuul, Eureka and Ribbon in an existing spring boot microservices based application. We won’t be creating a microservice application from scratch. Instead we will be just integrating these tools to an existing application.</p><p><strong>Implementing the API Gateway with Zuul</strong></p><p>Zuul will be introduced as a new spring boot application and will run as an independent service. To create a spring boot application you could make use of the Spring Initialzr (http://start.spring.io).</p><p>Provide the name as <strong>gateway. </strong>Provide the package name as per your project structure. Select Zuul as a dependency. Click on the ‘Generate Project’ button. This will generate a zip file. Extract and import it to your IDE.</p><p>Navigate to the application.properties file and rename it to application.yml. I prefer the yml format since it is more readable.</p><p>To make any spring boot application behave as a Zuul API gateway, we just need to add an annotation to the main class. In our case the main method would be in a class named GatewayApplication.java. Add the annotation</p><p><strong><em>@EnableZuulProxy</em></strong></p><p>This annotation is used to make a spring boot application act as a Zuul server. Add the annotation in the GatewayApplication.java file as below.</p><pre>@EnableZuulProxy<br>@SpringBootApplication<br>public class GatewayApplication {<br>   public static void main(String[] args) {<br>      SpringApplication.run(GatewayApplication.class, args);<br>   }<br>}</pre><p>In Zuul we could configure the routing directly in the yml file. Your application.yml file should be like below:</p><pre>server:<br>  port: 8000</pre><pre>zuul:<br>  prefix: /api<br>  routes:<br>    route1:<br>      path: /route1/**<br>      url: <a href="http://localhost:8080/route1">http://localhost:8080/route1</a><br>    <br>    route2:<br>      path: /route2/**<br>      url: <a href="http://localhost:8080/route2">http://localhost:8080/route2</a></pre><pre>    route3:<br>      path: /route3/**<br>      url: <a href="http://localhost:8081/route3">http://localhost:8081/route3</a></pre><pre>endpoints:<br>  trace:<br>    sensitive: false</pre><pre>ribbon:<br>  eureka:<br>    enabled: false</pre><p>If you look at the configuration above, you would notice that we have set ribbon.eureka.enabled property to false value. We will change this later. The /trace endpoint is also set as not sensitive since we are not using any authentication in this case. The server port of the API gateway is set to 8000 which will be the entry point for all REST API requests. We have set a prefix for all our requests. So all requests coming in need to have ‘/api’ in the URL which in turn will be removed by Zuul when redirecting the request.</p><p>For example: if the expected URL is <a href="http://localhost:8000/api/route1,">http://localhost:8000/api/route1,</a> it will be redirected to <a href="http://localhost:8080/route1.">http://localhost:8080/route1.</a></p><p>As of now we have introduced a central place to which all requests to our application could be made. The API consumers do not know anything about the microservices, they just know about the gateway address.</p><p>We might need to include a WebConfiguration class to enable CORS (Cross Origin Resource Sharing). CORS is included because the request from the API consumers would be from a different domain. Even the microservices could be in a different domain. So to accommodate that we need to configure CORS. Create a class named WebConfiguration.java and add the following code to it.</p><pre>@Configuration<br>@EnableWebMvc<br>public class WebConfiguration implements WebMvcConfigurer {</pre><pre>   @Override<br>   public void addCorsMappings(final CorsRegistry registry) {<br>      registry.addMapping(“/**”);<br>   }<br>}</pre><p>This would allow requests from all domains. You could make changes in the mappings to restrict requests from certain domains.</p><p>Once these changes are made, we need to start the gateway microservice together with the rest of your existing microservices. After making some requests from the UI, you could verify the working of Zuul by navigating to the URL <a href="http://localhost:8000/trace.">http://localhost:8000/trace.</a> You could see all requests being handled by Zuul and their corresponding responses.</p><p>We have just introduced an API gateway and you could see that we have hardcoded microservice urls in application.yml routes. We will change it now. It’s time to implement Service Discovery.</p><p><strong>Implementing Service Discovery</strong></p><p>In this section we will be adding service discovery and load balancing to all our microservices. First, we will create the service registry. You could use Spring Initializr (http://start.spring.io) to create the new spring boot microservice. Select Eureka Server as dependency, name the project <strong>service-registry</strong> and provide your package name. Download and extract the zip file, and import the contents to your IDE. It is just an empty project. We will now configure our service registry. To convert a service into a Eureka Registry server, we need to use <strong>@EnableEurekaServer</strong> annotation in the main method in the ServiceRegistryApplication.java file as shown below:</p><pre>@EnableEurekaServer<br>@SpringBootApplication<br>public class ServiceRegistryApplication {<br>   public static void main(String[ ] args) {<br>      SpringApplication.run(ServiceRegistryApplication.class, args);<br>   }<br>}</pre><p>In the application.properties file, change the <strong>server.port </strong>to 8761. This is usually the default port expected by the Eureka clients.</p><p>A point to be noted here is that since service registry here is a microservice, Eureka registry will try to register itself. To prevent that we need to add another property in the application.properties file</p><p><strong>eureka.client.register-with-eureka=false.</strong></p><p>Now you need to configure the rest of the services (all of your microservices) so that they can include the Eureka client and send their information to the new Eureka server for registering. For this you need to add the following dependencies to each service’s pom.xml.</p><pre>&lt;dependencyManagement&gt;<br>  &lt;dependencies&gt;<br>    &lt;dependency&gt;<br>      &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;<br>      &lt;artifactId&gt;spring-cloud-dependencies&lt;/artifactId&gt;<br>      &lt;version&gt;${spring-cloud.version}&lt;/version&gt;<br>      &lt;type&gt;pom&lt;/type&gt;<br>      &lt;scope&gt;import&lt;/scope&gt;<br>    &lt;/dependency&gt;<br>  &lt;/dependencies&gt;<br>&lt;/dependencyManagement&gt;</pre><pre>&lt;dependencies&gt; <br>  &lt;dependency&gt;<br>    &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;<br>    &lt;artifactId&gt;spring-cloud-starter-eureka&lt;/artifactId&gt;<br>  &lt;/dependency&gt;</pre><pre>  &lt;dependency&gt;<br>    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;<br>    &lt;artifactId&gt;spring-boot-starter-actuator&lt;/artifactId&gt;.        &lt;dependency&gt;<br>&lt;dependencies&gt;</pre><p>You would notice that we have added a dependency named Actuator. Once this dependency is added to our spring boot application, it would automatically make available some endpoints that are very useful for monitoring health, logs, metrics etc. Our service discovery and load balancer would use the /health endpoint exposed by the actuator to check if a service is up or not.</p><p>We need to make some more changes in each of the microservices to make it register into the service registry during the start up.</p><ol><li>Add <strong>@EnableEurekaClient</strong> annotation in the main application class of each microservice.</li><li>Add the property <strong>eureka.client.service-url.default-zone=http://localhost:8761/eureka. </strong>This is added in the application.properties file to tell Eureka where to find the service registry.</li></ol><p>3. Create a file named <strong>bootstrap.properties </strong>in the same folder as that of the application.properties file. In this file add a property <strong>spring.application.name=&lt;Your-application-name&gt;</strong></p><p>The last step is done to make our application name configurable and not automatically created. This is added in the bootstrap.properties file because the service registration happens during application boot and during that phase the application.properties files wouldn’t have been loaded yet.</p><p>Make the above changes for all the microservices including the API gateway microservice. These small code changes and including the above mentioned dependencies would make your existing microservice application work with a service discovery tool. Now that we are ready with our service discovery tool, we could make the required changes in the application.yml file of the Zuul microservice that was created earlier. The hardcoded links which were given earlier could be changed now. Your application.yml file should be like below:</p><pre>server:<br>  port: 8000</pre><pre>zuul:<br>  ignoredServices: ‘*’<br>  prefix: /api<br>  routes:<br>    route1:<br>      path: /route1/**<br>      serviceId: &lt;service-id-given-in-bootstrap&gt;<br>      strip-prefix: false<br>    <br>    route2:<br>      path: /route2/**<br>      serviceId: &lt;service-id-given-in-bootstrap&gt;<br>      strip-prefix: false</pre><pre>    route3:<br>      path: /route3/**<br>      serviceId: &lt;service-id-given-in-bootstrap&gt;<br>      strip-prefix: false</pre><pre>endpoints:<br>  trace:<br>    sensitive: false</pre><pre>eureka:<br>  client:<br>    service-url:<br>       default-zone: <a href="http://localhost:8761/eureka/">http://localhost:8761/eureka/</a></pre><p>The Zuul can now communicate with the Eureka server to identify the service instances. The major difference you could see here from what we had configured earlier is that instead of url we are setting the serviceId property. i.e a service will be identified using the serviceId given during startup instead of their locations. This gives the flexibility of having services that can change their location dynamically and can scale up with multiple instances. This property value should be the same as the service name we configured in the bootstrap.properties file.</p><p>We are setting the strip-prefix property to false since we will be using explicit routes and we don’t need to remove anything from the specified path.</p><p>We are also setting ignoredServices property to tell Zuul not to dynamically register services which are already registered with Eureka. We have also removed the property that was used to disable Ribbon.</p><p><strong>Ready to Scale</strong></p><p>We have configured Zuul and Eureka now. Every request that comes to Zuul will be redirected to the respective service based on the routes we have just set. Now it’s time to scale up our application.</p><p>Before we go into the configuration part, there are a couple of things that we have to keep in mind before scaling up. It is always better to design stateless microservices, meaning they shouldn’t keep any data or state in memory. Otherwise we will have to make sure that all requests from the same user end up in the same microservice instance because of the context or state information it keeps.</p><p>Another point to be noted is that every instance of a service shouldn’t have its own database instance because that would result in retrieving different data per request. All instances should have a shared database server. As a result of this strategy even though the microservice logic could scale nicely, we can’t say the same for the database because it will be only one shared instance. To solve this problem, in a production environment we would need to choose a database engine that scales and creates a cluster at our database tier. We are not going into such details here.</p><p>Now we will verify how load balancing is done by Spring Cloud Netflix Ribbon. Ribbon basically comes with Eureka and both combine well with Zuul. No additional steps are required to configure the ribbon. To test this you could just start a second instance of one of your microservices on a different port. This will register the instance with Eureka and when a request reaches the Gateway, Eureka and Ribbon would combine together to redirect the request to the appropriate service instance.</p><p>Ribbon will not be responsible for the status check process. It just picks an instance based on the load balancing strategy. (By default it is round robin). It is the Eureka that takes care of registering and deregistering the instances and deregistering happens when an instance is down.</p><p><strong>Conclusion</strong></p><p>We have explored various concepts in the microservices architecture and got familiarized with various tools that could be used. You could build on top of this to get more hands-on experience and to create more complex applications. You could also try various alternatives to the tools that we have covered. There are plenty of other options available. Hope this has helped you to grasp the concepts. Meet you again with a different interesting topic.</p><p>Until then, Happy exploring!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=83c937c16d48" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/service-registry-service-discovery-and-load-balancing-in-microservices-applications-83c937c16d48">API Gateway, Service Discovery and Load Balancing in Microservices applications using Zuul, Eureka…</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building a Simple Web Application With Node, Express & MongoDB]]></title>
            <link>https://medium.com/swlh/building-a-simple-web-application-with-node-express-mongodb-dcd53231e83c?source=rss-c341fd0e40a9------2</link>
            <guid isPermaLink="false">https://medium.com/p/dcd53231e83c</guid>
            <category><![CDATA[nodejs-app-development]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[expressjs]]></category>
            <category><![CDATA[mongodb]]></category>
            <category><![CDATA[node-js-tutorial]]></category>
            <dc:creator><![CDATA[Arjun K S]]></dc:creator>
            <pubDate>Sat, 02 May 2020 06:59:15 GMT</pubDate>
            <atom:updated>2020-05-07T10:39:45.679Z</atom:updated>
            <content:encoded><![CDATA[<p>I recently started exploring node.js and realized that while there is so much content on the web, there is hardly any that gives you an understanding on the combination of node.js, express and mongo DB. I’ve realized that writing down concepts improves clarity of thought, so here I start my journey as a blogger for my sake and for any one who is interested in setting up a node.js based application connecting to MongoDB.</p><p>When you start building a node based application the realization will dawn upon you that it is not just the node.js that you need to master. You will encounter an endless number of other JavaScript libraries to build your application.</p><p>I’m assuming that you might have gone through some of the basics of node.js and you know that it is a JavaScript run-time environment that helps in the execution of JavaScript code.</p><p>I was mainly attracted to Node.js because it offers developers the luxury of writing server-side applications in JavaScript. This means the Node.js developers can write both the front-end as well as the back-end web application in JavaScript. You will also find a large number of JavaScript libraries when you start working on it. Moreover Node.js is blessed to have a large active community of developers who keep on contributing towards its further development and improvement.</p><p>We will be setting up a webserver using Express. Express is a Node.js web application framework which is written in JavaScript and hosted within the Node.js runtime environment.</p><p>In this tutorial we will be mainly covering the following topics required for the application creation.</p><p><strong>1. Setting up the environment.</strong></p><p><strong>2. Establish Mongo DB connection.</strong></p><p><strong>3. Schema and Model creation</strong></p><p><strong>4. Routing</strong></p><p><strong>5. View Creation — Basic sign up and login pages.</strong></p><p><strong>6. Authentication and Authorization</strong></p><p><strong>Prerequisites</strong></p><p>You need to have a basic knowledge in JavaScript. Also you have to be familiar with building web pages using html or EJS. In this tutorial we will be exploring basics of node.js as well as using EJS which is a templating engine that can be used to create html markups using simple JavaScript.</p><p>You need an editor of your choice to begin with and I have chosen Visual Studio Code. Some other options that I would prefer would be Atom, Brackets or Sublime Text.</p><ol><li><strong>Setting up the environment</strong></li></ol><p>Let’s begin with the environment setup before starting the actual coding.</p><p>You first have to create a project Directory and name it. Create a file which in this case I’m naming <em>app.js</em> in that directory. Navigate to that directory in command prompt and run the command:</p><blockquote>npm init</blockquote><p>This command initializes the project and when you run it, will prompt a few questions to set up the <em>package.json</em> file. Once you enter those details, <em>package.json</em> will be created in your directory. This file is the heart of your project — it describes your project and will have details like the project dependencies, entry point to the application and so on.</p><p>Make sure to specify <em>app.js</em> as the entry point during the <em>package.json</em> creation. Even if you have missed it you can edit the file manually. Here we are now with the basic structure of your project created.</p><p>Now we will be installing Express which we will use to set up our web server. Run the command</p><blockquote>npm install express</blockquote><p>This will install the latest version of express. After successfully running this command you can notice that a folder named <em>node_modules</em> will be created in your directory. This is the folder which will have all the dependencies required for your application when you install various dependencies. You will also notice that in <em>package.json</em>, express will be added as a dependency.</p><p>Now we can start off with some coding.</p><p>Open <em>app.js</em> in your editor. Add the following code:</p><pre>var express = require(‘express’);<br>var app = express();<br>app.listen(3000, () =&gt; {<br>   console.log(‘Server listening on 3000’);<br>})</pre><p>This will set up a web server for your application which will be listening at port 3000. You can start the application by running the following command in Command Prompt within your directory.</p><blockquote>node app.js</blockquote><p>Open your browser and access <a href="http://localhost:3000.">http://localhost:3000.</a></p><p>Viola!!!! Your application is up and running. You can verify this by viewing the log in the Command Prompt console. You will see ‘Server listening on 3000’.</p><p>Our application doesn’t have a view now. Before moving into view creation we will first setup the database connection. We will first get our APIs ready and then take care of the views.</p><p><strong>2. Establish Mongo DB Connection</strong></p><p>Here we use yet another one of the endless libraries that I had mentioned before. We will use a third party library called Mongoose for Data modelling in our application. Mongoose is a Data Modelling library for Node.js which is used for the representation of objects in MongoDB. Let’s move on to the installation. Enter the below command in command prompt:</p><blockquote>npm install mongoose</blockquote><p>After completing the installation add the following code in your app.js</p><pre>const mongoose = require(‘mongoose’);<br>mongoose.connect(‘URL’, () =&gt; {<br>   console.log(‘Connected to Mongo DB Successfully!!’);<br>})</pre><p>We now have the code in place to connect to the Mongo DB. Instead of having a local setup of MongoDB, in this tutorial we will use a cloud based database which is MongoDB Atlas. MongoDB Atlas is a fully-managed cloud database developed by the same minds that built MongoDB. Sign up to <a href="https://cloud.mongodb.com/">MongoDB atlas </a>if you do not have an account. Build a cluster and initiate the connection. You will get a connection url which you should copy and paste in the mongoose.connect method call.</p><p>Now run the following command:</p><blockquote>node app.js</blockquote><p>If the connection is successful you will see the log ‘Connected to Mongo DB Successfully!!’ in the console.</p><p><strong>3. Schema and model Creation</strong></p><p>Another step done! We have successfully completed the Database connection. We will now start with the Schema creation. Mongoose maps each schema in our application directly to a collection in the Mongo DB. And from this Schema, mongoose creates models. Let us see how to create a User schema. Create a folder named <em>model </em>in the working directory and create a file named <em>User.js</em>. Copy the following code in that file.</p><pre>const mongoose = require(‘mongoose’);<br>const userSchema = mongoose.Schema({<br>     email: { type: String, unique: true, required: true }<br>     userName: { type: String, unique: true,required: true },<br>     password: { type: String, required: true },<br>     firstName: { type: String, required: true }, <br>     lastName: { type: String, required: true }<br>})</pre><blockquote>module.exports = mongoose.model(‘User’, userSchema);</blockquote><p>This is our User Schema and the mongoose.model() creates a User model. <em>module </em>is a variable that represents the current module and <em>exports </em>is an object that will be exposed as a module. Whatever you assign to <em>module.exports </em>or <em>exports</em>, will be exposed as a module. Modules are self-contained units of functionality that can be shared and reused across projects. In this case we have exposed the User model and from our <em>app.js</em> or from any other file in our project we will be able to access it.</p><p><strong>4. Routing</strong></p><p>We will now start writing the routing codes. Routing determines how our application responds to a client request through an endpoint. We write our APIs using a routing mechanism. Routes will also have one or more handler functions which will be executed when the route is matched. Before writing the routes we need to install another <em>middleware</em> library named <em>body-parser</em>. We need this <em>middleware </em>to handle HTTP POST calls. <em>body-parser</em> extracts the entire body portion of an incoming request stream and exposes it on <em>req.body</em> object. Suppose if the client sends a HTTP POST request with username and password in the body, <em>body-parser</em> will extract this body content and make it available in the <em>req.body</em>. Run the following command to install the <em>body-parser</em></p><blockquote>npm install body-parser</blockquote><p>After installation add the below code to <em>app.js</em> file.</p><pre>const bodyParser = require(‘body-parser’);<br>app.use( bodyParser.urlencoded({ extended: true }));<br>app.use(bodyParser.json());</pre><p>The above statements parse the text URL encoded data and JSON data respectively and expose the resulting object into <em>req.body</em>.</p><p>Let’s start with writing the routing code. We can define the routing using methods of either Express <em>app</em> object or Express <em>Router</em> object. In this case we will use the Router object.</p><p>Add the following code to <em>app.js</em>.</p><pre>const router = express.Router();<br>const User = require(‘../model/User.js’); <br>const bcrypt = require(‘bcrypt’);<br>router.post(‘/user’, (request, response) =&gt; {<br>      const user = new User({<br>         firstName : request.body.firstName,<br>         lastName : request.body.lastName,<br>         userName : request.body.userName,<br>         password : request.body.password,<br>         email : request.body.email<br>      });</pre><pre>bcrypt.hash(user.password, 10, function (err, hash){<br>if (err) { <br>    return next(err);<br>}<br>user.password = hash;<br>user.save().then(data =&gt; {<br>    console.log(‘Successfully created a new User’);<br>}).catch(error =&gt; {<br>     // you can send an error code here<br>    })<br>  })<br>})module.exports = router;</pre><p>We have written an API to create a new user. You might have noticed that we have used the User model that we have created earlier. Here we have added a new endpoint named <em>/user</em>. Any POST request to the <em>/user</em> URL will be handled by this router. This is called a <em>middleware </em>function. <em>Middleware </em>functions are those having access to request and response objects. The body-parser which we have defined earlier will expose the content from the request body to <em>request.body</em> object.</p><p>We have also used <em>bcrypt </em>library which will generate hash passwords. Finally the save method of the User object is invoked which will create a new collection in the mongo DB named <em>User</em>. See the magic! We just had to create an object of the User model, call a save() and here we are with a new table named <em>User</em> with all the columns and data populated.</p><p>Now execute the following command:</p><blockquote>node app.js</blockquote><p>You can access the Mongo Atlas URL and see that a new table named <em>User</em> would have been created. Similarly we can write different APIs for getting, deleting or updating the users. You can directly test this API using a <em>POSTMAN </em>client by passing the required parameters in the body.</p><p>If you want to separate out the User related routes into a separate file say <em>UserRoutes.js</em> under the routes folder, add the following code to <em>app.js</em></p><pre>const usersRoute = require(‘./routes/UserRoutes.js’);</pre><pre>app.use(‘/users’ , usersRoute);</pre><p>This code will redirect all requests to URLs starting with <em>/users</em> to <em>usersRoute </em>and you can add all the user related <em>middleware </em>functions in the <em>UserRoutes.js</em>.</p><p><strong>Serving static files</strong></p><p>We will have a large number of static files in our applications like images, CSS files, fonts etc. We can include those files in our application using the below lines of code:</p><pre>app.use(‘/images’, express.static(__dirname + ‘/images’));<br>app.use(‘/css’, express.static(__dirname + ‘/css’));<br>app.use(‘/fonts’, express.static(__dirname + ‘/fonts’));</pre><p>The <em>__dirname</em> in node returns the path of the folder where the current JavaScript file resides. We can also write <em>middleware </em>functions to serve html files.</p><p><strong>5. View Creation — Basic sign up and login pages.</strong></p><p>The server code of our application is done now. We will now create a simple sign up page. Create a folder named <em>views </em>under the working directory. Under the views directory create <em>signup.html</em> file and add the following code:</p><pre>&lt;!DOCTYPE html&gt;<br>&lt;html&gt;<br>&lt;head&gt;<br>&lt;title&gt; Signup Form&lt;/title&gt;<br>&lt;/head&gt;<br>&lt;body&gt;<br>&lt;div&gt;<br>&lt;form action=”/users” method=”post”&gt;<br>&lt;h1&gt; Signup form &lt;/h1&gt;<br>&lt;input type=”text” name=”firstName” id=”firstName”<br>placeholder=”First Name” required /&gt;&lt;br&gt;<br>&lt;input type=”text” name=”lastName” id=”lastName”<br>       placeholder=”Last Name” required /&gt;&lt;br&gt;<br>&lt;input type=”email” name=”email” id=”email” <br>       placeholder=”Email “ required /&gt;&lt;br&gt;<br>&lt;input type=”text” name=”userName” id=”userName”<br>       placeholder=”User Name “ required/&gt;&lt;br&gt;<br>&lt;input type=”password” name=”password”<br>       id=”password” placeholder=”Password “ required/&gt;&lt;br&gt;&lt;br&gt;&lt;input type=”submit” id=”submitDetails”<br>       name=”submitDetails” value=”Submit” /&gt;&lt;br&gt;<br>&lt;/form&gt;<br>&lt;/div&gt;<br>&lt;/body&gt;</pre><p>Also add the following code in <em>app.js</em></p><pre>router.get(‘/’,function(req,res){<br>    res.sendFile(path.join(__dirname+’/views/signup.html’));<br>});</pre><p>This line of code serves the <em>signup.html</em> as the entry point when we access the application. You can also see that we are calling the <em>/users</em> end point from the actions attribute of the form component. So when we click on the submit button, <em>/users</em> API will be invoked with all the form data available in the <em>req.body</em>.</p><p>In this case we have used a basic HTML page. We can also use <em>EJS </em>so that we can pass data to the view. Consider a scenario where we need to display a list of items from the database in the browser. In such a case we can invoke the API to get the items from the database and once the list is obtained we can render the <em>EJS </em>file and pass the result list to it which can be accessed within the <em>EJS </em>file.</p><p>Install EJS first using the following command:</p><blockquote>npm install ejs</blockquote><p>When the installation is completed add the following lines of code in your <em>app.js</em></p><pre>const ejs = require(‘ejs’);<br>app.set(‘view engine’, ‘ejs’);<br>router.get(‘/users’, (request, response) =&gt; {<br>     User.find({}, (error, result) =&gt; {<br>     if (result) {  <br>         response.render(‘availableUsers’, {‘users’ : result});<br>     } else {<br>          response.status(404);<br>     }<br>   })<br>})</pre><p>In the above code we have added a template engine named <em>EJS</em>. <em>EJS </em>helps to create html pages with minimal JavaScript code. So the above code is an example of rendering an html page using data obtained from the database. Before running this code we need to create a new <em>EJS </em>file named <em>availableUsers.ejs</em> under <em>views </em>directory. In this file you can write the html code to display the list of users. The list of users will be available in this file as <em>users</em> object since we have rendered the <em>availableUsers.ejs</em> by passing the result as <em>users</em> object. We can access the users object in the <em>availableUsers.ejs</em> with the following line of code:</p><pre>&lt;% availableItems.forEach(function (item) { %&gt;</pre><pre>// You can write the html components to display various attributes of each item object</pre><pre>&lt;%})%&gt;</pre><p>This is how we can render views using <em>EJS</em>.</p><p>Now we have come to the last topic of this tutorial.</p><p><strong>6. Authentication and Authorization</strong></p><p>We will be using an interesting and wonderful <em>middleware </em>named <em>Passport </em>for authentication purposes. You can refer to the official documentation of <a href="http://www.passportjs.org/">Passport Middleware</a>. We just have to make a couple of method invocations and it will ensure authentication for all requests. While using Passport authentication, we have to specify a strategy that we will be using for authentication. In our case we will be using <em>local</em> strategy. Local Strategy is used when we are authenticating using <em>username </em>and <em>password</em>. We will write a simple login page. Create <em>login.html</em> under <em>views </em>folder and add the following code.</p><pre>&lt;html lang=”en”&gt;<br>&lt;head&gt;<br>&lt;title&gt;Login&lt;/title&gt;<br>&lt;/head&gt;<br>&lt;body&gt;<br>&lt;div&gt;<br>     &lt;form action=”/login” method=”post”&gt;<br>     &lt;span&gt;Sign In&lt;/span&gt;<br>     &lt;div data-validate=”Please enter username”&gt;<br>     &lt;input type=”text” name=”userName” placeholder=”Username”&gt;<br>&lt;/div&gt;<br>&lt;div data-validate = “Please enter password”&gt;<br>     &lt;input type=”password” name=”password” placeholder=”Password”&gt;<br>&lt;/div&gt;<br>&lt;div&gt;<br>     &lt;button&gt;Sign in&lt;/button&gt;<br>&lt;/div&gt;<br>&lt;/form&gt;<br>&lt;/div&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;</pre><p>This html file has a login form with <em>Username </em>and <em>Password </em>input fields. When we click on the Sign in button, <em>/login</em> API will be invoked. Before starting the application we need to introduce this login API and also the code for authentication. Add the following piece of code in your <em>app.js </em>file.</p><pre>const passport = require(‘passport’);<br>app.use(passport.initialize());<br>app.use(passport.session());<br>passport.use(new LocalStrategy(<br>   { usernameField: ‘userName’ },<br>   (userName, password, done) =&gt; {<br>     User.findOne({userName: userName}, (err, userData) =&gt; {<br>      let passwordCheck = bcrypt.compareSync(password,   userData.password);<br>if(userName === userData.userName &amp;&amp; passwordCheck) {<br>    return done(null, userData)<br>   }<br>  })<br>}<br>));<br>app.post(‘/login’, (req, res, next) =&gt; {<br>passport.authenticate(‘local’, (err, user, info) =&gt; {<br>     req.login(user, (err) =&gt; {<br>     // Write code to redirect to any html page.<br>     res.redirect(‘/index’);<br>     }<br>   })<br>})(req, res, next);<br>})</pre><p>After adding this piece of code in <em>app.js</em> start the application again using the command:</p><blockquote>node app.js</blockquote><p>Earlier we had set <em>signup.html</em> as the entry point to the application. You can replace <em>signup.html </em>with<em> login.html</em>. Now when we access localhost:3000, the login page will be displayed.</p><p>When you click the submit button with the <em>username </em>and <em>password </em>entered, the <em>/login</em> router <em>middleware </em>which we have written now will be invoked. We have also set the authentication strategy as <em>local</em> strategy where the <em>username </em>entered will be checked in the database and if it is present the <em>user </em>object matching that <em>username </em>will be returned. Then we will compare the passwords. You can notice in the above code that we have used <em>bcrypt.compareSync()</em> method to compare the passwords. This is because earlier we have saved the hash of the password in the database.</p><p>To maintain the session after successful login we use a session <em>middleware </em>named <em>express-session</em>. Also to store the session data we use another library <em>middleware </em>named <em>session-file-store</em>. Install it using the following commands:</p><blockquote>npm install express-session</blockquote><blockquote>npm install session-file-store</blockquote><p>After the installation add the following piece of code in <em>app.js</em></p><pre>const session = require(‘express-session’);<br>const FileStore = require(‘session-file-store’)(session);<br>const uuid = require(‘uuid/v4’);<br>app.use(session({<br>   genid: (req) =&gt; {<br>   return uuid() // use UUIDs for session IDs<br>   },<br>   store: new FileStore(),<br>   secret: ‘any key is fine’,<br>   resave: false,<br>   saveUninitialized: true<br>}))<br>passport.serializeUser((user, done) =&gt; {<br>    done(null, user.id);<br>});<br>passport.deserializeUser(function(id, done) {<br>User.findById(id, function(err, user) { <br>    loggedInUser = user;<br>    done(err, user);<br>});<br>});</pre><p>You can notice that we have created the session by generating a <em>uuid using </em>another library named <em>uuid/v4</em>. We have also specified a <em>secret key</em> and <em>FileStore </em>to create the session and to store the session data.</p><p>Passport provides two methods named <em>passport.serializeUser()</em> and <em>passport.deserializeUser()</em> to retrieve the logged in user information. <em>Serializer </em>method is used to store authenticated user id in the file store. User id can be retrieved from <em>req.session.passport</em>. <em>Deserializer </em>method is used to retrieve the <em>user </em>object from db and store in the file store. This can be retrieved from <em>req.user</em>. The first argument of <em>deserializeUser </em>corresponds to the key of the user object and the whole object is retrieved with the help of that key. We can access the logged in user information anywhere in the application using the request object.</p><p>Below image explains how it works.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/953/1*oFxpZjfhlr3ajCGDUmTnnQ.png" /></figure><p>To ensure that a user is logged in, we can also use another <em>middleware </em>library named <em>connect-ensure-login</em>. Access the <a href="https://www.npmjs.com/package/connect-ensure-login">documentation </a>for more details. If a request is received which is unauthenticated we can redirect it to the login page. We can also use this <em>middleware </em>to ensure that all APIs can be accessed only after successful authentication.</p><p><strong>Conclusion</strong></p><p>Here we are after exploring node.js, express and mongo DB step by step to create a simple application with login and sign up pages. Writing this has definitely helped me understand and reinforce the concepts better. Hope this has given you a start off and peek into node.js and MongoDB. Meet you next time with my thoughts after exploring something new and interesting.</p><p>Until next time. Cheers!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dcd53231e83c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/building-a-simple-web-application-with-node-express-mongodb-dcd53231e83c">Building a Simple Web Application With Node, Express &amp; MongoDB</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>