<?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: Deepak Kumar</title>
    <description>The latest articles on DEV Community by Deepak Kumar (@dipakkr).</description>
    <link>https://dev.to/dipakkr</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%2F209981%2F4a6a4b12-7e7c-4e62-8510-edce5bb46ffb.jpg</url>
      <title>DEV Community: Deepak Kumar</title>
      <link>https://dev.to/dipakkr</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dipakkr"/>
    <language>en</language>
    <item>
      <title>Best practises for loading environment variable in NextJS application</title>
      <dc:creator>Deepak Kumar</dc:creator>
      <pubDate>Mon, 13 Mar 2023 19:18:45 +0000</pubDate>
      <link>https://dev.to/dipakkr/best-practises-for-loading-environment-variable-in-nextjs-application-3hni</link>
      <guid>https://dev.to/dipakkr/best-practises-for-loading-environment-variable-in-nextjs-application-3hni</guid>
      <description>&lt;h1&gt;
  
  
  How to load environment variables in a NextJS application ?
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;First, create a new &lt;strong&gt;&lt;code&gt;.env&lt;/code&gt;&lt;/strong&gt; file in the root directory of your Next.js application. Add the environment variables you want to use in your application in the format &lt;strong&gt;&lt;code&gt;ENV_VARIABLE_NAME=env_value&lt;/code&gt;&lt;/strong&gt;. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;API_URL=https://example.com/api
API_KEY=abc123
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Next, please install the &lt;strong&gt;&lt;code&gt;dotenv&lt;/code&gt;&lt;/strong&gt; npm package dependency:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save-dev dotenv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Now, in your Next.js application, create a new file named &lt;strong&gt;&lt;code&gt;next.config.js&lt;/code&gt;&lt;/strong&gt; in the root directory. This file will be used to load the environment variables from the &lt;strong&gt;&lt;code&gt;.env&lt;/code&gt;&lt;/strong&gt; file.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const dotenv = require('dotenv');
dotenv.config();

module.exports = {
  // Your Next.js configuration 
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Now you can use the environment variables in your nextjs project. For example, to access the &lt;strong&gt;&lt;code&gt;API_URL&lt;/code&gt;&lt;/strong&gt; environment variable:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const apiUrl = process.env.API_URL;
console.log(apiUrl); // prints https://example.com/api
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For more approach on how to load env file in nextjs application, you can refer this page - &lt;a href="https://nextjs.org/docs/basic-features/environment-variables"&gt;Link&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Things to keep in mind when working with env files.
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;You should never commit your &lt;strong&gt;&lt;code&gt;.env&lt;/code&gt;&lt;/strong&gt; file to git. Env secrets generally contains sensitive information such as API keys and passwords, database secrets.&lt;/li&gt;
&lt;li&gt;You should add it to your &lt;strong&gt;&lt;code&gt;.gitignore&lt;/code&gt;&lt;/strong&gt; file to prevent it from being committed. &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Here are the best practices you can consider for setting env secrets for your application :
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Use a secure method to store your secrets: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Avoid storing your secrets in plain text files or hardcoding them in your code. Instead, use a secure method such as environment variables, key vaults, or configuration files.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Limit access to your secrets&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Restrict access to your secrets to only those who need them. Use access controls and permissions to ensure that only authorized users can access your secrets.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Avoid storing sensitive data in plain text&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you need to store sensitive data such as passwords or API keys, encrypt them before storing them in your secrets store.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Rotate your secrets regularly:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Regularly rotating your secrets (e.g. passwords, api secret keys) helps to prevent unauthorised access and improve security. &lt;/li&gt;
&lt;li&gt;Set up automated processes to rotate your secrets at regular intervals.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Use strong, complex passwords: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When creating passwords for your secrets, use strong, complex passwords that are difficult to guess. &lt;/li&gt;
&lt;li&gt;Use a password manager to generate and store passwords securely.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Use a version control system: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can use a version control system to manage changes to your secrets, and keep a record of who made changes and when.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>javascript</category>
      <category>nextjs</category>
    </item>
    <item>
      <title>WTF is Higher Order Function ?</title>
      <dc:creator>Deepak Kumar</dc:creator>
      <pubDate>Sat, 07 Jan 2023 18:57:09 +0000</pubDate>
      <link>https://dev.to/dipakkr/wtf-is-higher-order-function--3lfo</link>
      <guid>https://dev.to/dipakkr/wtf-is-higher-order-function--3lfo</guid>
      <description>&lt;p&gt;A higher-order function is a function that takes one or more functions as arguments, or returns a function as its result. Higher-order functions are often used in functional programming languages. They allow you to abstract over actions, rather than just values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function applyTwice(func, arg) {
  return func(func(arg));
}

function sum(x) {
  return x + 5;
}

console.log(applyTwice(sum, 10));  // 20
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, &lt;code&gt;applyTwice&lt;/code&gt; is a higher-order function because it takes the function &lt;code&gt;sum&lt;/code&gt; as an argument and calls it twice on the argument 10.&lt;/p&gt;

&lt;p&gt;Higher-order functions are often used in functional programming languages like JavaScript to abstract over actions, rather than just values. They can be used to create reusable abstractions that can simplify complex code and make it easier to understand.&lt;/p&gt;

&lt;p&gt;Here are few examples of HOC functions in javascript : &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;Array.prototype.map()&lt;/code&gt; : This function takes an array and a callback function as arguments, and returns a new array with the results of calling the callback function on every element in the original array.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [1, 2, 3, 4, 5];
const doubledNumbers = numbers.map(x =&amp;gt; x * 2);
console.log(doubledNumbers); // [2, 4, 6, 8, 10]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;Array.prototype.filter()&lt;/code&gt; : This function takes an array and a callback function as arguments, and returns a new array with only the elements from the original array that pass the test implemented by the callback function.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [1, 2, 3, 4, 5];
const evenNumbers = numbers.filter(x =&amp;gt; x % 2 === 0);
console.log(evenNumbers); // [2, 4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Array.prototype.reduce(): This function takes an array and a callback function as arguments, and returns a single value that is the result of applying the callback function to each element in the array, from left to right.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((accumulator, currentValue) =&amp;gt; accumulator + currentValue, 0);
console.log(sum); // 15

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Array.prototype.sort(): This function takes an array and an optional compare function as arguments, and returns a new array with the elements in the original array sorted according to the return value of the compare function.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [4, 1, 5, 2, 3];
const sortedNumbers = numbers.sort((a, b) =&amp;gt; a - b);
console.log(sortedNumbers); // [1, 2, 3, 4, 5]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I hope you understand now what higher order functions are. &lt;/p&gt;

&lt;p&gt;Thanks for reading. Follow me on &lt;a href="https://twitter.com/HQdeepak"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How to build a career in web development?</title>
      <dc:creator>Deepak Kumar</dc:creator>
      <pubDate>Sun, 13 Dec 2020 16:13:00 +0000</pubDate>
      <link>https://dev.to/dipakkr/how-to-build-a-career-in-web-development-2pmn</link>
      <guid>https://dev.to/dipakkr/how-to-build-a-career-in-web-development-2pmn</guid>
      <description>&lt;p&gt;Do you know  &lt;a href="http://info.cern.ch/"&gt;info.cern.ch&lt;/a&gt; was the first website on the Internet published on Aug 6, 1991?&lt;/p&gt;

&lt;p&gt;Today after &lt;strong&gt;29 years&lt;/strong&gt;, there are more than &lt;strong&gt;1.5 billion&lt;/strong&gt; published websites on the Internet out of which more than &lt;strong&gt;300 million&lt;/strong&gt; are currently active. &lt;/p&gt;

&lt;p&gt;Most of these websites on the Internet are built using HTML, CSS, and Javascript (To keep things simple let's not talk about the fancy js frameworks for now. )&lt;/p&gt;

&lt;p&gt;According to stack overflow developer survey 2020, javascript is the most used programming language by developers. &lt;/p&gt;

&lt;p&gt;So, it's clear that there is a lot of demand for this technology. &lt;/p&gt;

&lt;p&gt;Before we talk about &lt;strong&gt;how to learn web development&lt;/strong&gt; and what are the steps, let's find your motivation behind building for the web. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q. Do I need a Computer Science Degree to become a web developer or get a job?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;NO. Whoever you are and wherever you are, you can become a web developer if you have a &lt;strong&gt;laptop, internet connection, a determination&lt;/strong&gt; to learn and change your life. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By learning this technology, you can make money online by full-time job or by freelancing or building your own app/company. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q.  What's the salary range for a web developer?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A web developer in India makes from Rs. 20k/month to Rs. 4-5 lakh/month, depending on the expertise. The majority of &lt;strong&gt;entry-level full-stack/frontend web developer salary in India is Rs. 5-12 LPA&lt;/strong&gt;. ( $6600 - $16,000 / year) &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I feel sorry for people who are working at Rs. 20-30k/month as a web developer, the companies are exploiting you. Upskill yourself and know your value. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q. What's the day to day work look like as a web developer?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Take an example of a website where you are reading this blog i.e Hash node. Your work can be building an app like this or improving existing features, add a new feature like a comment on the blog, sharing functionality, word count widget, image uploader, making the website responsive, etc. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Web Development in itself is a very broad domain. So, if you have decided you want to become a web developer, narrow down whether you want to become a &lt;strong&gt;frontend developer, backend developer, or full-stack web developer&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Umm, confused with frontend, backend, and full-stack ??&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;There are 3 roles in web development. *&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Frontend developer
&lt;/h3&gt;

&lt;p&gt;The interface to which any user interaction with a website is the Frontend. Basically, frontend developers are responsible for the development and design of UI ( User Interface) components. &lt;/p&gt;

&lt;p&gt;Typically, a person enters into the field of front-end development by learning to develop HTML, CSS, and JavaScript which commonly runs in a web browser. &lt;/p&gt;

&lt;p&gt;A few years back, the tech stack of frontend developers was HTML, CSS, jquery. That pretty can do the designing of web components. But, nowadays these roles have evolved a lot. &lt;/p&gt;

&lt;p&gt;In 2020, the frontend developer tech stack looks like &lt;strong&gt;HTML, CSS, React/Vue/Angular, Javascript&lt;/strong&gt; including knowledge of tons of tools and libraries like &lt;strong&gt;npm, babel, webpack&lt;/strong&gt;. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Don't worry, these tech jargons are not as difficult as it looks. We will talk about how and when to learn these things. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  2. Backend Developer
&lt;/h3&gt;

&lt;p&gt;In common, There are two types of websites, static and dynamic websites. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In a static website, the data and content are hardcoded using HTML and CSS. For example, the website where you are reading this blog is not a static site but a dynamic site, which means If the blog owner changes some information in the blog, it will reflect you(reader) immediately. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These types of websites are dynamic sites. &lt;/p&gt;

&lt;p&gt;Any dynamic site needs a cloud database and some backend logic that can decide what to do when the user does something. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tech Stack for a backend developer&lt;/strong&gt; - NodeJS, PHP, Python, Golang, MYSQL Database, MongoDB, Postgres, &lt;/p&gt;

&lt;p&gt;These are some common backend technologies used in 2020. &lt;/p&gt;

&lt;h3&gt;
  
  
  3. Full-Stack Developer
&lt;/h3&gt;

&lt;p&gt;A full-stack web developer has an understanding of both frontend and backend and can develop both client-side and server-side apps. &lt;/p&gt;

&lt;p&gt;Now, you might be thinking why should I do both if I can get a job by learning either frontend or backend. &lt;/p&gt;

&lt;p&gt;A full-Stack developer is one of the hottest roles in the industry. Being a full-stack developer, you have knowledge of both frontend and backend, and the ability to build a server enabled app from scratch. And that's why these roles are most sought in startup companies. &lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Tech stack involves both from frontend and backend. *&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you want to make a career in web development, start with frontend, know the basics of HTML, CSS, create few static sites, and decide your interest.&lt;/p&gt;

&lt;p&gt;That's it for the first blog of Web Developer Series, in the next blog I will post more about Frontend development, how you can get started and resources you can follow. &lt;/p&gt;

&lt;p&gt;Connect with me on  &lt;a href="https://twitter.com/HQdeepak"&gt;Twitter&lt;/a&gt; and stay updated for next blog. &lt;/p&gt;

&lt;p&gt;Thanks for reading !&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Understanding Client-Side Storage</title>
      <dc:creator>Deepak Kumar</dc:creator>
      <pubDate>Fri, 23 Oct 2020 19:10:54 +0000</pubDate>
      <link>https://dev.to/dipakkr/understanding-client-side-storage-2c3f</link>
      <guid>https://dev.to/dipakkr/understanding-client-side-storage-2c3f</guid>
      <description>&lt;p&gt;Why do we need clientSide storage?  &lt;/p&gt;

&lt;p&gt;Do you know why all websites show these popup to accept cookie policy? &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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fb7lq51243eiz9fvjpo64.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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fb7lq51243eiz9fvjpo64.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Because they want your permission to save user-specific data to store/access cookies in your browser. The browser has some storage limit which websites use to personalize user experience. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Features of clientSide data storage&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;data persistence&lt;/code&gt; which means you won't lose the data on page reload or on closing the browser. This property helps in personalizing the user experience on the app.  &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Different websites can have different clientSide data(i.e cookie, session, and localStorage). &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Types of ClientSide Storages
&lt;/h3&gt;




&lt;p&gt;&lt;strong&gt;1. localStorage&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It's a key-value pair type client-side storage. It offers a maximum of 5MB limit. At 91wheels, we are using localStorage to store user-specific information like current city and user name for better personalization.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pros&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  - Data has no expiry time. However, it can be removed by end-users by clearing browser data. 
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Cons&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The stored data is in plain-text. Hence, it's not advisable to store critical user information in localStorage. &lt;/li&gt;
&lt;li&gt;It can only be read on the client-side. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;How to save data to localStorage:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;username&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dipakkr&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Retrieving data from localStorage:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const data = localStorage.getItem('username');

console.log(data); // dipakkr

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzlqlh1zdewgesbtk5pt5.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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzlqlh1zdewgesbtk5pt5.PNG" alt="localStorage"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Session Storage&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Features&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;It stores data only for a particular session. Session means till the time the browser tab is not closed. Once you close the browser, sessionStorage will be auto-deleted. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Like localStorage, it can only be accessed from the client. This also means that data is never transferred to the server. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Session storage also works as a key-value pair type storage. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The maximum limit of data saving in SessionStorage is about 5 MB.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;3. Cookie&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Cookies are the earliest form of clientSide data storage. It is used to store information to personalize user experience on websites.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The size of the cookie must be less than 4KB.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Expiry time can be defined in the cookie. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2tptjmgrw9f7rvect5al.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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2tptjmgrw9f7rvect5al.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Let's connect on  &lt;a href="https://twitter.com/HQdeepak" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>database</category>
    </item>
    <item>
      <title>Understanding Execution Context, Thread, and Functions in Javascript</title>
      <dc:creator>Deepak Kumar</dc:creator>
      <pubDate>Wed, 21 Oct 2020 04:09:19 +0000</pubDate>
      <link>https://dev.to/dipakkr/understanding-execution-context-thread-and-functions-in-javascript-3cnc</link>
      <guid>https://dev.to/dipakkr/understanding-execution-context-thread-and-functions-in-javascript-3cnc</guid>
      <description>&lt;p&gt;In today's post, I am going to write about how javascript works. We will throw some light on terminologies like Execution context, function level execution context, function invoking, and threads.  &lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Do you know what happens behind the scene when you run a javascript code? *&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Let's take an example of the below code snippet.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const a = 10;

function add(x, y){
    const result = x + y;
    return result;
}

const name = 'github';
const final = add(10, 20);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As soon as you run this program, it creates a &lt;code&gt;Global Execution Content&lt;/code&gt;, the place where the javascript thread will run line by line and run your code. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You can think of &lt;code&gt;Global execution context&lt;/code&gt; as an environment where the code runs. &lt;/p&gt;

&lt;p&gt;There can be multiple &lt;code&gt;execution context&lt;/code&gt; in a program but a single &lt;code&gt;Global execution context&lt;/code&gt;. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now let's go back to the above code snippet and see what the JS engine does when we run the code.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;JS Engine parses your code line by line &amp;amp; identifies variables &amp;amp; functions created by code (which will be used in the execution phase)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;JS Engine setup memory space for variables &amp;amp; functions ( called as &lt;strong&gt;Hoisting&lt;/strong&gt;)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;*&lt;em&gt;Hoisting *&lt;/em&gt; is basically the JS Engine set-asides memory space for variables and functions used inside the code before your code is executed. These variables &amp;amp; functions comprise the Execution Context of any function that is being executed. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A new Execution Context is created whenever function invocation is called. &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;All variables in JS are initially set to &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fmn2unuwm2ubk5oj56ky0.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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fmn2unuwm2ubk5oj56ky0.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's take one more example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function y(){
    const p = 100;
}

function x(){
    y();
    const t = 10;
}

x();
const r = 20;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;EC : Execution Context&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When we run this code, here is what the Javascript engine does.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;First, a &lt;code&gt;Global Execution Context&lt;/code&gt; is going to be created.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Interpreter encounters &lt;code&gt;call x()&lt;/code&gt;, and again a new EC is created for x. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now the &lt;code&gt;EC&lt;/code&gt; for x is created it will run the code line by line inside x. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Inside x, a new function call &lt;code&gt;y()&lt;/code&gt; is invoked, it again creates an EC for y. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When y is finished running, it will back to EC of &lt;code&gt;x&lt;/code&gt;, and variable t is assigned. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After x run is finished, it will be back to &lt;code&gt;Global EC&lt;/code&gt;, and variable r is assigned. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Global EC&lt;/code&gt; finishes running and the program stops. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In tomorrow's blog, I will explain how the call stack works in Javascript. I would love to know your feedback on this blog. &lt;/p&gt;




&lt;p&gt;Stay tuned -  Follow me on &lt;a href="https://twitter.com/HQdeepak" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; &lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>What is TypeScript? </title>
      <dc:creator>Deepak Kumar</dc:creator>
      <pubDate>Tue, 07 Jul 2020 04:33:10 +0000</pubDate>
      <link>https://dev.to/dipakkr/what-is-typescript-5f7l</link>
      <guid>https://dev.to/dipakkr/what-is-typescript-5f7l</guid>
      <description>&lt;p&gt;TypeScript is a superset of JavaScript that facilitates writing robust applications by giving you type-safety and features such as modules, classes, and interfaces.  Any valid javascript code will run as expected in Typescript.&lt;/p&gt;

&lt;p&gt;It is highly recommended for building large and complex programs, as it reduces an entire class of bugs and refactoring mistakes. &lt;/p&gt;

&lt;p&gt;Typescript gives &lt;code&gt;static type check ability&lt;/code&gt; which means it can detect and throw errors at &lt;code&gt;compile-time&lt;/code&gt; instead of &lt;code&gt;runtime&lt;/code&gt;. With the new type-check/safety features, it is easier to enhance code quality and reduce bugs in production. &lt;/p&gt;

&lt;p&gt;When Typescript code compiles, you can eliminate a lot of bugs. It like a suite of unit tests that run the moment you write code, and they catch the bugs even before you even run the tests. &lt;/p&gt;

&lt;h3&gt;
  
  
  ShortComings of Javascript
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1. Javascript allows accessing properties that are not present.&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;In the code snippet below, despite having an error in obj properties(num22) it won't show any error in Native javascript while in TS it does.&lt;br&gt;
&lt;br&gt;
 &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const obj = { num1: 10, num2: 15 };
const multiply = obj.num1 * obj.num22 ;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--w2VEMtYY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/pu5f8bqy4b9rc90203ax.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--w2VEMtYY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/pu5f8bqy4b9rc90203ax.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Static Type Checking&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In .ts file, variable assigned once can't be re-initialized with another data type, thus it shows an error while in Native javascript, it works fine. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ibT2sKsJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/uwbnmu826hw4q8me69r4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ibT2sKsJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/uwbnmu826hw4q8me69r4.png" alt="TypeScript Vs Javascript"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Features of TypeScript&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;We have already seen the two use cases of a static type language that how it can help in detecting removing potential bugs. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Static Typing&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Detecting errors in code without running it is referred to as static checking.&lt;/p&gt;

&lt;p&gt;Determining what's an error and what's not based on the kinds of values being operated on is known as static type checking.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function sum(a:number, b:number):number{
     return a+b;
}
sum(3, 5);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Classes and Interfaces&lt;/strong&gt;&lt;br&gt;
Just like Classes, Interfaces can extend each other. This allows you to copy the members of one interface into another, which gives you more flexibility in how you separate your interfaces into reusable components.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface Employee{
   firstName : string, 
   lastName : string, 
   employeeID : number
}
const p1 : Employee = {
    firstName : 'Bob',
    lastName :  'Dale',
    employeeID : 395
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Modules&lt;/strong&gt;&lt;br&gt;
Modules in TS are used just like Javascript. It can contain both code and declarations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Compiles to JavaScript&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;TypeScript →  ES6 → Babel → ES5 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Typescript compiles down to Native Javascript. So, all your javascript will be valid in TypeScript.  &lt;/p&gt;

&lt;p&gt;To see how to run a Typescript file, click this &lt;a href="https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes.html"&gt;LINK&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tsc index.ts
// This will generate a `index.js` file
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6l1z8fp4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/j291z97t3sumgzzvpyzv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6l1z8fp4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/j291z97t3sumgzzvpyzv.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Types in TypeScript
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;undefined&lt;/code&gt;, &lt;code&gt;number&lt;/code&gt;, &lt;code&gt;string&lt;/code&gt;, &lt;code&gt;boolean&lt;/code&gt;, &lt;code&gt;object&lt;/code&gt;, &lt;code&gt;function&lt;/code&gt;, &lt;code&gt;symbol&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;No int, double, or other non-native JS types. &lt;br&gt;
Classes are standard JS prototypal classes. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Additional Pointers&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Typescript doesn't ship with a runtime. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;ES6 syntax is handled, but ES6 operations are not. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's all for today folks. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NEXT POST&lt;/strong&gt; → How to Setup and Run Typescript in NodeJS? &lt;/p&gt;

&lt;p&gt;Subscribe to my &lt;a href="https://dipakkr.co/newsletter"&gt;Newsletter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Liked what you read? Let's connect on &lt;a href="https://twitter.com/HQdeepak"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>typescript</category>
    </item>
    <item>
      <title>Free Online Courses In Covid-19</title>
      <dc:creator>Deepak Kumar</dc:creator>
      <pubDate>Thu, 23 Apr 2020 19:48:30 +0000</pubDate>
      <link>https://dev.to/dipakkr/free-online-courses-in-covid-19-5aga</link>
      <guid>https://dev.to/dipakkr/free-online-courses-in-covid-19-5aga</guid>
      <description>&lt;p&gt;When the world is in crisis, edTech Companies across the globe are offering limited edition free courses in COVID-19. If you want to learn something new whether is coding, design, marketing, blogging, SEO, content marketing etc, there can't be a best time than this. &lt;/p&gt;

&lt;p&gt;We are privileged section of society who have got the comfort to Learn From Home and Work From Home, not everyone has it. So, Utilize in learning and becoming the better version of yourself. Here I am listing few courses which are free. &lt;/p&gt;

&lt;p&gt;If you know any more free or discounted courses which you think should be helpful for the community, do share by creating a issue or sending a Pull Request. &lt;/p&gt;

&lt;p&gt;You may also like to see &lt;a href="https://github.com/dipakkr/A-to-Z-Resources-for-Students"&gt;A2Z Resource For Students&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;This resource is originally published on my github repo. &lt;a href="https://github.com/dipakkr/Free-Online-Courses-In-Covid19"&gt;Free Online Courses in Covid-19&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Online Courses
&lt;/h1&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;SNo&lt;/th&gt;
&lt;th&gt;Title&lt;/th&gt;
&lt;th&gt;Category&lt;/th&gt;
&lt;th&gt;Link&lt;/th&gt;
&lt;th&gt;Timeline&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;CodeAcademy&lt;/td&gt;
&lt;td&gt;Coding&lt;/td&gt;
&lt;td&gt;&lt;a href="https://bit.ly/2xzKIgf"&gt;Link&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;90 Days PRO FREE&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;PluralSight&lt;/td&gt;
&lt;td&gt;Coding&lt;/td&gt;
&lt;td&gt;&lt;a href="https://bit.ly/2VsBgTG"&gt;Link&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;Free Till April 2020&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Ahref (Blogging For Business)&lt;/td&gt;
&lt;td&gt;Content Marketing&lt;/td&gt;
&lt;td&gt;&lt;a href="https://ahrefs.com/academy/blogging-for-business"&gt;Link&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;Limited Period Offer&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;Progate&lt;/td&gt;
&lt;td&gt;Coding&lt;/td&gt;
&lt;td&gt;&lt;a href="https://linktr.ee/progatespecialaccess"&gt;Link&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;Free Till April 2020&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;Coding Blocks&lt;/td&gt;
&lt;td&gt;Coding- Competitive Programming&lt;/td&gt;
&lt;td&gt;&lt;a href="http://cb.lk/cpcpyt"&gt;Link&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;Limited Period Offer&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;Coursera Courses&lt;/td&gt;
&lt;td&gt;Tech&lt;/td&gt;
&lt;td&gt;&lt;a href="https://bit.ly/2VPu1Wn"&gt;Link&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;Limited Period Offer&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;Free Courses on Edx&lt;/td&gt;
&lt;td&gt;Tech&lt;/td&gt;
&lt;td&gt;&lt;a href="https://bit.ly/2xTJBbj"&gt;Link&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;Limited Period Offer&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;Udemy Free Courses&lt;/td&gt;
&lt;td&gt;All Domains&lt;/td&gt;
&lt;td&gt;&lt;a href="https://bit.ly/3cGJTkQ"&gt;Link&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;Limited Period Offer&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;td&gt;Linux Foundation Scholarship&lt;/td&gt;
&lt;td&gt;Tech&lt;/td&gt;
&lt;td&gt;&lt;a href="https://bit.ly/3bwm7I1"&gt;Link&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;Limited Period Offer&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;Udacity 1Month Free Nanodegree&lt;/td&gt;
&lt;td&gt;Tech&lt;/td&gt;
&lt;td&gt;&lt;a href="https://bit.ly/3cGldc1"&gt;Link&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;Limited Period Offer&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h1&gt;
  
  
  Books, Journals, Podcast
&lt;/h1&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;SNo&lt;/th&gt;
&lt;th&gt;Title&lt;/th&gt;
&lt;th&gt;Category&lt;/th&gt;
&lt;th&gt;Link&lt;/th&gt;
&lt;th&gt;Timeline&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Scribd Library&lt;/td&gt;
&lt;td&gt;Books, Journals&lt;/td&gt;
&lt;td&gt;&lt;a href="https://www.scribd.com/readfree?utm_source=readfree"&gt;Link&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;30 Days FREE Without Credit Card&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Cambridge Library&lt;/td&gt;
&lt;td&gt;Books, Journals&lt;/td&gt;
&lt;td&gt;&lt;a href="https://bit.ly/2VsBgTG"&gt;Link&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;Free Till April 2020&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Other Free Courses
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;SNo&lt;/th&gt;
&lt;th&gt;Title&lt;/th&gt;
&lt;th&gt;Category&lt;/th&gt;
&lt;th&gt;Link&lt;/th&gt;
&lt;th&gt;Timeline&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Frontend Masters&lt;/td&gt;
&lt;td&gt;Frontend (Coding)&lt;/td&gt;
&lt;td&gt;&lt;a href="http://bit.ly/2X37LGS"&gt;Link&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;6 Months Free With Github Student Pack&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;PluralSight&lt;/td&gt;
&lt;td&gt;Coding&lt;/td&gt;
&lt;td&gt;&lt;a href="https://bit.ly/2VsBgTG"&gt;Link&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;Free Till April 2020&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Share this resource in your community. Let's connect on &lt;a href="https://twitter.com/HQdeepak"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>2019: A Year In Review</title>
      <dc:creator>Deepak Kumar</dc:creator>
      <pubDate>Sat, 14 Mar 2020 14:54:57 +0000</pubDate>
      <link>https://dev.to/dipakkr/2019-a-year-in-review-4fk1</link>
      <guid>https://dev.to/dipakkr/2019-a-year-in-review-4fk1</guid>
      <description>&lt;p&gt;It’s 14th of March 2020. Due to #CoronaVirus, I am working home today. And, I was curious about life, the progress so far.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;In this hyperconnected world, where information reaches millions in a few minutes, the FOMO(fear of missing out) gets real&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;It's has been already two months in 2020, and here I am reviewing my 2019. Well, it's never too late to start. So, let's take a recap of the last year of the decade. &lt;/p&gt;

&lt;p&gt;From starting a startup, graduating, starting freelancing, shutting down the startup, joining and leaving an MNC company, making my first international trip, a lot has happened this year.&lt;/p&gt;

&lt;h2&gt;
  
  
  Career
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Founded a startup in college, &lt;strong&gt;FrontBench&lt;/strong&gt;. Built a community of more than 50 highly motivated students from different colleges in India to help promote the initiative. It was always my dream to startup while in college. That got completed, but the Idea is just 1% rest is how you execute. The journey was full of learning. I still don't get regret the decision. &lt;/li&gt;
&lt;/ul&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fli7pt6220fpm4dn53sic.jpg" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fli7pt6220fpm4dn53sic.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Got Featured On LinkedIn.&lt;/li&gt;
&lt;/ul&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fgzedinjrzaavld929kjt.jpg" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fgzedinjrzaavld929kjt.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;After the failure in a startup, I joined an MNC. Yes, I regret it. The series of events that happened that times made me insecure. However, I left it in a couple of months and joined an early age startup. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;2019 was full of learning new technology and building. Starting from Android Development in 2017, Deep Learning in 2018 to Full Stack Web Development in 2019. I tried it all. I am not trying to be the master of all jack but explored fields according to requirement. Be a Problem Solver, not a tech stack guy. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Later in the year, deep-dived into learning &lt;strong&gt;Core Javascript&lt;/strong&gt;, &lt;strong&gt;ReactJS&lt;/strong&gt;, &lt;strong&gt;NodeJs&lt;/strong&gt; and full-stack web development.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Gave 2 talks in a different college. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Life
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;I started journaling and writing my thoughts. It helped me a lot in reviewing my past, learning from it and connecting dots in the future. Now whenever I feel low, I just read those journals. And, I again feel motivated realizing "This too shall pass".&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;I failed to build a habit,  whether it is to wake up in the morning, read books daily, meditation or exercise. I failed. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;I failed to meet some of my goals, yeah a lot of goals. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Travel
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;I completed my first International trip. And, it was Lisbon Portugal. I went there to attend WebSummit 2019. t&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Traveled to Rishikesh with friends and escape drowning in Ganga. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;I learned to drive this year, drove approx 1000km from Delhi to Patna. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fni2mbshxa2a14uom2yyu.jpg" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fni2mbshxa2a14uom2yyu.jpg" alt="Alt Text"&gt;&lt;/a&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0bk7stgnil2fds8p450n.jpg" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0bk7stgnil2fds8p450n.jpg" alt="Alt Text"&gt;&lt;/a&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F80cjrn015upgeevnn70q.jpg" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F80cjrn015upgeevnn70q.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Reading / Writing
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Read a lot of books in 2019. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Started Journaling, it really helped me a lot. I am thinking to do bullet journaling in 2020.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;I failed to create enough content like blogs, videos that I planned.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  What do I want in 2020??
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Be the best in what I do, Software Development.&lt;/li&gt;
&lt;li&gt;Read at least 10 books. I will post it soon.&lt;/li&gt;
&lt;li&gt;Write at least 50 posts on Medium and Dev.to this year. Target to reach 1M users.&lt;/li&gt;
&lt;li&gt;Learn about Personal &lt;strong&gt;Finance&lt;/strong&gt;, &lt;strong&gt;Stocks&lt;/strong&gt;, &lt;strong&gt;Investment&lt;/strong&gt;, and &lt;strong&gt;Capitalism&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;I want to complete a 10km Marathon.&lt;/li&gt;
&lt;li&gt;Learn freestyle swimming.&lt;/li&gt;
&lt;li&gt;I wanted to speak at more meetups/events/conferences this year. Bad Luck due to #CoronaOutbreak, I guess.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Want to see my progress 2020 progress blog, subscribe here to get an update by signing up below!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dipakkr.substack.com/" rel="noopener noreferrer"&gt;Subscribe here to get an update !&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Connect with me on &lt;a href="https://twitter.com/HQdeepak" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;!&lt;/p&gt;

&lt;p&gt;Thanks for reading! I would love to know your suggestions in a comment.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Let's Know Each Other? </title>
      <dc:creator>Deepak Kumar</dc:creator>
      <pubDate>Sun, 23 Feb 2020 18:08:49 +0000</pubDate>
      <link>https://dev.to/dipakkr/let-s-know-each-other-37b0</link>
      <guid>https://dev.to/dipakkr/let-s-know-each-other-37b0</guid>
      <description>&lt;p&gt;I have been writing on DEV from the last 6 months and the support I have received from the community motivates me to write and share more.&lt;/p&gt;

&lt;p&gt;I want to connect with more of you and learn from you. &lt;/p&gt;

&lt;h2&gt;
  
  
  Here is my story :
&lt;/h2&gt;

&lt;p&gt;After completing my graduation in 2019, I started working on my startup, Frontbench. It was an education platform to help students of tier2/3 colleges get better mentorship and guidance. We felt that there is a gap between what these students are doing and what the industry requires. &lt;/p&gt;

&lt;p&gt;It was a unique experience for me to working on to solve such a problem.  However, due to lack of &lt;strong&gt;Money&lt;/strong&gt;/&lt;strong&gt;Patience&lt;/strong&gt; and &lt;strong&gt;Persistence&lt;/strong&gt;, I QUIT. &lt;/p&gt;

&lt;p&gt;The learning, experience, and interactions I had during those few months are invaluable and it will definitely help in my future ventures. &lt;/p&gt;

&lt;p&gt;Currently, I am working as a software engineer at an early age startup in Gurgaon India building an e-commerce platform for automobiles products and services. &lt;/p&gt;

&lt;p&gt;My tech stack includes but not limited to  Javascript, ReactJS, NodeJS, MongoDB, PHP. &lt;/p&gt;

&lt;p&gt;I am aim to share my insights, learning, and tutorials here to my fellow community members.&lt;/p&gt;

&lt;p&gt;Before that, it would be my pleasure to know about you and your work!&lt;/p&gt;

&lt;p&gt;Let's get started! I am eagerly looking forward to hearing from you all. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://twitter.com/diipakkr"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

</description>
      <category>discuss</category>
    </item>
    <item>
      <title>Authentication in NodeJS With Express and Mongo - CodeLab #1</title>
      <dc:creator>Deepak Kumar</dc:creator>
      <pubDate>Sat, 21 Dec 2019 17:16:59 +0000</pubDate>
      <link>https://dev.to/dipakkr/implementing-authentication-in-nodejs-with-express-and-jwt-codelab-1-j5i</link>
      <guid>https://dev.to/dipakkr/implementing-authentication-in-nodejs-with-express-and-jwt-codelab-1-j5i</guid>
      <description>&lt;p&gt;I am starting a CodeLab Series in which I will building something cool and sharing with the community.&lt;/p&gt;

&lt;p&gt;Today, We are going to implement Authentication API in Node using JWT, express, and MongoDB. &lt;/p&gt;

&lt;p&gt;I advise you to follow the table of content and don't miss any steps. I will provide the full app code link at the end. &lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Content
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
1. Introduction &lt;/li&gt;
&lt;li&gt;
2. Prerequisites &lt;/li&gt;
&lt;li&gt;3. Tools and Packages Required&lt;/li&gt;
&lt;li&gt;4. Initiate Project&lt;/li&gt;
&lt;li&gt;5. Setup MongoDB Database&lt;/li&gt;
&lt;li&gt;6. Configure User Model&lt;/li&gt;
&lt;li&gt;7. User Signup &lt;/li&gt;
&lt;li&gt;8. User Login &lt;/li&gt;
&lt;li&gt;9. Get LoggedIn User &lt;/li&gt;
&lt;li&gt;10. Conclusion&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  1. Introduction
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Authentication&lt;/strong&gt; - It is a process of identifying user identity. &lt;/p&gt;

&lt;p&gt;User Authentication contains various steps, please check out this flowchart to know more. We will be using this flow to build the authentication system in our application.&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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fk2vi3g73qy12ebznxqzs.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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fk2vi3g73qy12ebznxqzs.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  2. Prerequisites
&lt;/h3&gt;

&lt;p&gt;You should have prior knowledge of &lt;code&gt;javascript basics&lt;/code&gt;, &lt;code&gt;nodejs&lt;/code&gt;. Knowledge of ES6 syntax is a plus. And, at last &lt;strong&gt;nodejs&lt;/strong&gt; should be installed on your system. &lt;/p&gt;




&lt;h3&gt;
  
  
  3. Packages Required
&lt;/h3&gt;

&lt;p&gt;You will be needing these following 'npm' packages. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;express&lt;/strong&gt;&lt;br&gt;
Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;express-validator&lt;/strong&gt;&lt;br&gt;
To Validate the body data on the server in the express framework, we will be using this library. It's a server-side data validation library. So, even if a malicious user bypasses the client-side verification, the server-side data validation will catch it and throw an error. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;body-parser&lt;/strong&gt;&lt;br&gt;
It is &lt;code&gt;nodejs&lt;/code&gt; middleware for parsing the body data. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;bcryptjs&lt;/strong&gt;&lt;br&gt;
This library will be used to hash the password and then store it to database.This way even app administrators can't access the account of a user. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;jsonwebtoken&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;jsonwebtoken&lt;/strong&gt; will be used to encrypt our data payload on registration and return a token. We can use that &lt;strong&gt;token&lt;/strong&gt; to authenticate ourselves to secured pages like the dashboard. There would also an option to set the validity of those token, so you can specify how much time that token will last. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;mongoose&lt;/strong&gt;&lt;br&gt;
Mongoose is a MongoDB object modeling tool designed to work in an asynchronous environment. Mongoose supports both promises and callbacks.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  4. Initiate Project
&lt;/h3&gt;

&lt;p&gt;We will start by creating a node project. So, Create a new folder with the name 'node-auth' and follow the steps below. All the project files should be inside the 'node-auth' folder.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm init

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;npm init&lt;/em&gt;&lt;/strong&gt; will ask you some basic information about project. Now, you have created the node project, it's time to install the required packages. So, go ahead and install the packages by running the below command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;npm&lt;/span&gt; &lt;span class="nx"&gt;install&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;validator&lt;/span&gt; &lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;parser&lt;/span&gt; &lt;span class="nx"&gt;bcryptjs&lt;/span&gt; &lt;span class="nx"&gt;jsonwebtoken&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;save&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, create a file &lt;strong&gt;&lt;em&gt;index.js&lt;/em&gt;&lt;/strong&gt; and add this code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// File : index.js&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;bodyParser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;body-parser&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// PORT&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;4000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;API Working&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;


&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Server Started at PORT &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you type &lt;code&gt;node index.js&lt;/code&gt; in the terminal, the server will start at PORT 4000. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You have successfully set up your NodeJS app application. It's time to set up the database to add more functionality. &lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  5. Setup MongoDB Database
&lt;/h3&gt;

&lt;p&gt;We will be using MongoDB Database to store our users. You can use either a cloud MongoDB server or a local MongoDB server. &lt;/p&gt;

&lt;p&gt;In this CodeLab, we will be using a Cloud MongoDB server known as &lt;a href="https://mlab.com/" rel="noopener noreferrer"&gt;mLab&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;So, First, go ahead and signup on mLab. And follow the below steps. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;After successful signup, Click on &lt;strong&gt;Create New&lt;/strong&gt; Button on home page. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now, choose any cloud provider for example AWS. In the &lt;strong&gt;Plan Type&lt;/strong&gt; choose the free SandBox and then Click on the &lt;strong&gt;Continue&lt;/strong&gt; button at the bottom right.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Select the region(any) and click continue. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Enter a DB name(any). I am using &lt;strong&gt;node-auth&lt;/strong&gt;. Click continue and then submit the order on the next page. Don't worry it's free of cost. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now, You will be re-directed to the homepage. Select your DB i.e node-auth.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Copy the standard MongoDB URI.  &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now, you need to add a user to your database. From the 5 tabs below, click on &lt;strong&gt;Users&lt;/strong&gt; and add a user by clicking on &lt;strong&gt;Add Database User&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, you have got your database user. Replace the  &amp;amp;&amp;amp;  with your DB username and password.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mongodb://&amp;lt;dbuser&amp;gt;:&amp;lt;dbpassword&amp;gt;@ds257698.mlab.com:57698/node-auth

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, the Mongo Server Address(MongoURI) should look like this. Don't try to connect on my MongoURI. It's just a dummy username &amp;amp; password. 😄😄&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mongodb://test:hello1234@ds257698.mlab.com:57698/node-auth

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Now, you have the mongoURI you are ready to connect your &lt;strong&gt;node-auth&lt;/strong&gt; app to the database. Please follow the below steps. &lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  6. Configure User Model
&lt;/h3&gt;

&lt;p&gt;Let's go and first create a &lt;code&gt;config&lt;/code&gt; folder. This folder will keep the database connection information. &lt;/p&gt;

&lt;p&gt;Create a file named: &lt;strong&gt;db.js&lt;/strong&gt; in &lt;strong&gt;config&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//FILENAME : db.js&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;mongoose&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Replace this with your MONGOURI.&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MONGOURI&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;mongodb://testuser:testpassword@ds257698.mlab.com:57698/node-auth&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;InitiateMongoServer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;MONGOURI&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;useNewUrlParser&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Connected to DB !!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;InitiateMongoServer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we are done the database connection. Let's create the User Model to save our registered users. &lt;/p&gt;

&lt;p&gt;Go ahead and create a new folder named &lt;strong&gt;model&lt;/strong&gt;. Inside the model folder, create a new file &lt;strong&gt;User.js&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;We will be using &lt;strong&gt;mongoose&lt;/strong&gt; to create UserSchema. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;User.js&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="c1"&gt;//FILENAME : User.js&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;mongoose&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;UserSchema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Schema&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;required&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;required&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;required&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;createdAt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// export model user with UserSchema&lt;/span&gt;
&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;model&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;user&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;UserSchema&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we are done with &lt;code&gt;Database Connection&lt;/code&gt;, &lt;code&gt;User Schema&lt;/code&gt;. So, let's go ahead and update our index.js to connect our API to the database. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;index.js&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;bodyParser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;body-parser&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;InitiateMongoServer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./config/db&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Initiate Mongo Server&lt;/span&gt;
&lt;span class="nc"&gt;InitiateMongoServer&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// PORT&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;4000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Middleware&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bodyParser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;API Working&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;


&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Server Started at PORT &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Congratulations 😄😄 , You have successfully connected your app to the MongoDB server.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, the next thing we have to do is make a &lt;code&gt;/user/signup&lt;/code&gt; route to register a new user. We will see this in the next section. &lt;/p&gt;




&lt;h3&gt;
  
  
  7. User Signup
&lt;/h3&gt;

&lt;p&gt;The Route for user registration will be &lt;strong&gt;'/user/signup'&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Create a folder named routes. In the 'routes' folder, create a file named &lt;code&gt;user.js&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;routes/user.js&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="c1"&gt;// Filename : user.js&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;check&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;validationResult&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;express-validator/check&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;bcrypt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bcryptjs&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;jwt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;jsonwebtoken&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;router&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Router&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../model/User&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="cm"&gt;/**
 * @method - POST
 * @param - /signup
 * @description - User SignUp
 */&lt;/span&gt;

&lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/signup&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="nf"&gt;check&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;username&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Please Enter a Valid Username&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;not&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isEmpty&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
        &lt;span class="nf"&gt;check&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;email&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Please enter a valid email&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;isEmail&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
        &lt;span class="nf"&gt;check&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;password&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Please enter a valid password&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;isLength&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
            &lt;span class="na"&gt;min&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;
        &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;errors&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;validationResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;errors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isEmpty&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
                &lt;span class="na"&gt;errors&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;errors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;array&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="p"&gt;});&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="nx"&gt;password&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findOne&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
                &lt;span class="nx"&gt;email&lt;/span&gt;
            &lt;span class="p"&gt;});&lt;/span&gt;
            &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
                    &lt;span class="na"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;User Already Exists&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
                &lt;span class="p"&gt;});&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;

            &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
                &lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="nx"&gt;password&lt;/span&gt;
            &lt;span class="p"&gt;});&lt;/span&gt;

            &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;salt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;bcrypt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;genSalt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;password&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;bcrypt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;password&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;salt&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

            &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

            &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;payload&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="na"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;};&lt;/span&gt;

            &lt;span class="nx"&gt;jwt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sign&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="nx"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;randomString&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="na"&gt;expiresIn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10000&lt;/span&gt;
                &lt;span class="p"&gt;},&lt;/span&gt;
                &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;token&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
                        &lt;span class="nx"&gt;token&lt;/span&gt;
                    &lt;span class="p"&gt;});&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error in Saving&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we have created the user registration in &lt;strong&gt;'routes/user.js'&lt;/strong&gt;. So, we need to import this in &lt;strong&gt;index.js&lt;/strong&gt; to make it work. &lt;/p&gt;

&lt;p&gt;So, the updated &lt;strong&gt;index&lt;/strong&gt; file code should look like this. &lt;br&gt;
&lt;strong&gt;index.js&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;bodyParser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;body-parser&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./routes/user&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//new addition&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;InitiateMongoServer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./config/db&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Initiate Mongo Server&lt;/span&gt;
&lt;span class="nc"&gt;InitiateMongoServer&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// PORT&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;4000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Middleware&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bodyParser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;API Working&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;


&lt;span class="cm"&gt;/**
 * Router Middleware
 * Router - /user/*
 * Method - *
 */&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/user&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Server Started at PORT &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's start the user registration using postman. A postman is a tool for API testing. &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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fv1gh9j2z9dl3yib0hsoq.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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fv1gh9j2z9dl3yib0hsoq.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  8. User Login
&lt;/h3&gt;

&lt;p&gt;Now, it's time to implement the Login router which will be mounted on '/user/login'. &lt;/p&gt;

&lt;p&gt;Here is the code snippet for login functionality. Add the below code snippet in &lt;strong&gt;user.js&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/login&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nf"&gt;check&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;email&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Please enter a valid email&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;isEmail&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="nf"&gt;check&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;password&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Please enter a valid password&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;isLength&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;min&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;errors&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;validationResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;errors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isEmpty&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
        &lt;span class="na"&gt;errors&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;errors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;array&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;password&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findOne&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
        &lt;span class="nx"&gt;email&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
          &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;User Not Exist&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;

      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isMatch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;bcrypt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;compare&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;password&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;password&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;isMatch&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
          &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Incorrect Password !&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;

      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;payload&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;};&lt;/span&gt;

      &lt;span class="nx"&gt;jwt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sign&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="nx"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;randomString&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;expiresIn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3600&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;token&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
          &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
            &lt;span class="nx"&gt;token&lt;/span&gt;
          &lt;span class="p"&gt;});&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
        &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Server Error&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fvmpa6vhe86u4tap0ciny.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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fvmpa6vhe86u4tap0ciny.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  9. Get LoggedIn User
&lt;/h3&gt;

&lt;p&gt;Now, your User Signup and User Login is working, and you are getting a token in return. &lt;/p&gt;

&lt;p&gt;So, our next task will be to Retrieve the LoggedIn user using the &lt;strong&gt;token&lt;/strong&gt;. Let's go and add this functionality. &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;/user/me&lt;/code&gt; route will return your user if you pass the token in the header.  In the file &lt;strong&gt;route.js&lt;/strong&gt;, add the below code snippet.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * @method - GET
 * @description - Get LoggedIn User
 * @param - /user/me
 */&lt;/span&gt;


&lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/me&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;auth&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// request.user is getting fetched from Middleware after token authentication&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error in Fetching user&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, we added the auth middleware as a parameter in the &lt;strong&gt;/user/me&lt;/strong&gt; GET route, so let's define &lt;strong&gt;auth&lt;/strong&gt; function. &lt;/p&gt;

&lt;p&gt;Go ahead and create a new folder named &lt;strong&gt;middleware&lt;/strong&gt;. Inside this folder, create a file named &lt;strong&gt;auth.js&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This auth middleware will be used to verify the token, retrieve user based on the token payload. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;middleware/auth.js&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;jwt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;jsonwebtoken&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;token&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;header&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;token&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;token&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;401&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Auth Error&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;decoded&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;jwt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;verify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;token&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;randomString&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;decoded&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Invalid Token&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yayy !! You have successfully created an authentication API in nodejs. Now, You can go ahead and test the &lt;strong&gt;/user/me&lt;/strong&gt; endpoint after logging in. &lt;/p&gt;

&lt;h4&gt;
  
  
  How to Test the application?
&lt;/h4&gt;

&lt;p&gt;PostMan is required for Testing the API. If you don't have PostMan installed first, install it. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;First, register the user or login if you are already registered. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;From step 1, you will get a token. Copy that token and put in the &lt;strong&gt;header&lt;/strong&gt;. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Hit Submit&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here is a preview of testing. &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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fn1ddg1leyp0gajg01ddm.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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fn1ddg1leyp0gajg01ddm.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  10. Conclusion
&lt;/h3&gt;

&lt;p&gt;In this &lt;a href=""&gt;CodeLab - 1&lt;/a&gt;, we covered authentication in nodejs using &lt;strong&gt;express&lt;/strong&gt;, &lt;strong&gt;jsonwebtoken&lt;/strong&gt; and MongoDB. We learned about how to write middleware. &lt;/p&gt;

&lt;p&gt;Here is the link of full code for this CodeLab: &lt;a href="https://github.com/dipakkr/node-auth" rel="noopener noreferrer"&gt;https://github.com/dipakkr/node-auth&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Also, I would love to know what else you want to cover in the next CodeLabs. &lt;/p&gt;




&lt;p&gt;I am glad you read till here, Please give some ❤️ ❤️ !!&lt;/p&gt;

&lt;p&gt;If you face in problem in running/understanding this application, let me know in the comments. Don't forget to give your feedback. Getting feedback helps me improve. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://dipakkr.substack.com/" rel="noopener noreferrer"&gt;Subscribe to my email newsletter and stay updated!&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I write about new stuff almost daily. Please follow me on &lt;a href="https://twitter.com/diipakkr" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; | &lt;a href="https://instagram.com/diipakkr" rel="noopener noreferrer"&gt;Instagram&lt;/a&gt;&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>mongodb</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How I Got 10k stars on my Github Repository? </title>
      <dc:creator>Deepak Kumar</dc:creator>
      <pubDate>Thu, 19 Dec 2019 04:27:13 +0000</pubDate>
      <link>https://dev.to/dipakkr/how-i-got-10k-stars-on-my-github-repository-13dj</link>
      <guid>https://dev.to/dipakkr/how-i-got-10k-stars-on-my-github-repository-13dj</guid>
      <description>&lt;p&gt;When I was in University, I missed a lot of opportunities like hackathons, conferences, internships and many global events due to lack of awareness. Being a freshman, I was not aware of what kind of opportunities were available for students. Of course, We get to know about this thing with time and active involvement in the community. &lt;/p&gt;

&lt;p&gt;I didn't want other students/juniors to suffer the same problem. Hence, I created a hand-curated list of all the resources for students/developers. &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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fwhfmju6848p88o7pohie.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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fwhfmju6848p88o7pohie.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The list has got more than 1M+ views/impressions on Github and other publications. &lt;/li&gt;
&lt;li&gt;Almost 10k ⭐&lt;/li&gt;
&lt;li&gt;3k forks&lt;/li&gt;
&lt;li&gt;590+ Contributors from across the world. &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How I got such massive traffic ?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Well, It's was never about getting stars ⭐ on repo but more for the value it would provide.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I published the list on GitHub, and people from all across the world helped me make this resource list better and better. For the next 2-3 months, I reviewed every single Pull Request on the repository and closed more than 800 Pull requests.&lt;/p&gt;

&lt;h3&gt;
  
  
  Community is Key
&lt;/h3&gt;

&lt;p&gt;Students/professionals/Developers from all across the world contributed and made the resources list richer. We have listed most of the contributed Name with their name, social links, and country name.&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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F5b27wgcc25m8dg3dh752.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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F5b27wgcc25m8dg3dh752.png" alt="Contributors"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, let me tell you a brief what all we have added in the list known as &lt;code&gt;A-to-Z-Resources-for-Students&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The list contains a brief and important resource in the following categories :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Coding Resource&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Hackathons and Events&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Student Benefits and Programs&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Campus Ambassador Programs&lt;/li&gt;
&lt;li&gt;Student Benefits and Packs&lt;/li&gt;
&lt;li&gt;Student Fellowship Programs&lt;/li&gt;
&lt;li&gt;Scholarships&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Open Source Programs&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Startup Programs and Incubators&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Internship Portal&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Developer Club and Meetups&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Conferences&lt;/strong&gt; &lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Bootcamps&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Top People to follow(In Different Category)&lt;/strong&gt;. &lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Yes, the list was huge and I literally took more than a week to search and compile the whole list. &lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Create and Build thing which you think can be helpful for others. Finding your own problem and solving is the key. There may be someone who is facing the same problem&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Focus on creating valuable Content, Traffic and Audience will be the byproduct&lt;/strong&gt;. &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Finally, You can check out the repository below.&lt;/p&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/dipakkr" rel="noopener noreferrer"&gt;
        dipakkr
      &lt;/a&gt; / &lt;a href="https://github.com/dipakkr/A-to-Z-Resources-for-Students" rel="noopener noreferrer"&gt;
        A-to-Z-Resources-for-Students
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      ✅  Curated list of resources for college students 
    &lt;/h3&gt;
  &lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;I am glad you are still here. I hope you liked the post. Please don't forget to give your feedback in the comments. Getting feedback helps me improve. &lt;/p&gt;

&lt;p&gt;I write about Javascript, Web, and new stuff almost daily. You can follow me on &lt;a href="https://twitter.com/diipakkr" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; | &lt;a href="https://instagram.com/diipakkr" rel="noopener noreferrer"&gt;Instagram&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dipakkr.substack.com/" rel="noopener noreferrer"&gt;Subscribe to my email newsletter and stay updated!&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you liked the post, please give some ❤️!! Cheers !!&lt;/p&gt;

</description>
      <category>github</category>
      <category>beginners</category>
      <category>sideprojects</category>
    </item>
    <item>
      <title>How CORS (Cross-Origin Resource Sharing) Works? </title>
      <dc:creator>Deepak Kumar</dc:creator>
      <pubDate>Tue, 17 Dec 2019 19:27:41 +0000</pubDate>
      <link>https://dev.to/dipakkr/how-cors-cross-origin-resource-sharing-works-31n</link>
      <guid>https://dev.to/dipakkr/how-cors-cross-origin-resource-sharing-works-31n</guid>
      <description>&lt;p&gt;If you are a web developer, you must have seen the '&lt;strong&gt;CORS&lt;/strong&gt;' error appearing often on your screen when you try to call the API.  But, Why does it happen?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7zT2llSe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/y38l053yjayw0e3m8e04.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7zT2llSe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/y38l053yjayw0e3m8e04.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Well, For security reasons, browsers restrict cross-origin HTTP requests initiated from scripts. For example, if you want to access your API hosted at &lt;code&gt;https://api.github.com&lt;/code&gt; from your client-side frontend application which is hosted at &lt;code&gt;https://example.com&lt;/code&gt;. The browser will not allow this request to complete. &lt;/p&gt;

&lt;p&gt;You only need to think about CORS when :&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;API accessed by the browser.&lt;/li&gt;
&lt;li&gt;API is hosted on a separate domain.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1s5i61Aj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/6boak0gpa976zx4qfy15.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1s5i61Aj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/6boak0gpa976zx4qfy15.png" alt="CORS Same Origin"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, why does it happens? &lt;/p&gt;

&lt;p&gt;The browsers enforce a security feature called &lt;code&gt;Same Origin Policy&lt;/code&gt;. According to &lt;code&gt;Same Origin Policy&lt;/code&gt;, the Same Origin calls are allowed and Different Origin calls are not allowed. &lt;/p&gt;

&lt;p&gt;Uhh !! What is this Same Origin, Different Origin? Let's see this more in detail. Browsers define the &lt;code&gt;Origin&lt;/code&gt; as a combination of &lt;code&gt;Scheme&lt;/code&gt;, &lt;code&gt;Host&lt;/code&gt;, and &lt;code&gt;Port&lt;/code&gt;. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Scheme name&lt;/strong&gt; — It is the protocol to be used to access the resource on the Internet. The scheme names followed by the three characters &lt;code&gt;://&lt;/code&gt; .The most commonly used protocols are &lt;code&gt;http://&lt;/code&gt;, &lt;code&gt;https://&lt;/code&gt;, &lt;code&gt;ftp://&lt;/code&gt;, and &lt;code&gt;mailto://&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Hostname&lt;/strong&gt; — It is the address of the host where the resource is located. A hostname is a domain name assigned to a host computer. This is usually a combination of the host's local name with its parent domain's name. For example, &lt;code&gt;www.dev.to&lt;/code&gt; consists of the host's machine name &lt;code&gt;www&lt;/code&gt; and the domain name &lt;code&gt;dev.to&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Port Number&lt;/strong&gt; — Port is a communication endpoint where your application run. For more information about Port Number. Check out this [Link](&lt;a href="https://en.wikipedia.org/wiki/Port_(computer_networking)"&gt;https://en.wikipedia.org/wiki/Port_(computer_networking)&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If these three combinations of &lt;strong&gt;Scheme, Hostname, and Port&lt;/strong&gt; are same then the browser identifies it as the Same Origin. And, the request gets complete. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;So, Does it means that it is impossible to make &lt;code&gt;Cross-Origin HTTP request&lt;/code&gt; ??&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The answer is NO. &lt;/p&gt;

&lt;p&gt;That's where the &lt;strong&gt;CORS&lt;/strong&gt; comes into picture, Cross-Origin Resource Sharing mechanism. &lt;/p&gt;




&lt;h3&gt;
  
  
  How CORS Works
&lt;/h3&gt;

&lt;p&gt;CORS allows the server to explicitly whitelist certain origin and help to bypass the &lt;code&gt;same-origin&lt;/code&gt; policy. &lt;/p&gt;

&lt;p&gt;If your server is configured for CORS, it will return an extra header with "Access-Control-Allow-Origin" on each response.&lt;/p&gt;

&lt;p&gt;For example, if my API server hosted at &lt;code&gt;https://api.dipakkr.com/users&lt;/code&gt; is CORS configured and I am making a request from my client application &lt;code&gt;https://github.com&lt;/code&gt; to fetch some data. The response will have this header.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Access-Control-Allow-Origin: https://github.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--w8tK6pkO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/szdr3sdbyu2m38ptj01h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--w8tK6pkO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/szdr3sdbyu2m38ptj01h.png" alt="CORS header"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  CORS Preflight Request
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Preflighted&lt;/code&gt; requests first send an HTTP request by the &lt;code&gt;OPTIONS&lt;/code&gt; method to the resource on the other domain, to determine if the actual request is safe to send or not. &lt;/p&gt;

&lt;p&gt;You can read more about CORS Preflight request at &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#Simple_requests"&gt;MDN&lt;/a&gt; or check out this &lt;a href="https://www.youtube.com/watch?v=tcLW5d0KAYE"&gt;video&lt;/a&gt; by Akshay Saini. &lt;/p&gt;




&lt;h3&gt;
  
  
  How to Enable CORS
&lt;/h3&gt;

&lt;p&gt;For enabling CORS on your server application. You need two things. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;First, you need to determine the origins of whitelist.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Second, you have to add the CORS middleware to the server.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here, I am explaining to you the steps to configure CORS on your NodeJS server. &lt;/p&gt;

&lt;p&gt;First install the &lt;code&gt;cors&lt;/code&gt; npm package from this &lt;a href="https://www.npmjs.com/package/cors"&gt;link&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install cors
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then go to your server application and add the below code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// require the cors package&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;cors&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cors&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// enables cors&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;cors&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;origin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;*&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;methods&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;GET,HEAD,PUT,PATCH,POST,DELETE&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;preflightContinue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here you can see I am using &lt;code&gt;origin: "*"&lt;/code&gt; which means any domain can access this application. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Please note that it is dangerous to put &lt;code&gt;origin:"*"&lt;/code&gt; in a production application and you should never do that&lt;/strong&gt;. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To know more about CORS, please visit &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS"&gt;MDN&lt;/a&gt;. It's a great place for web developers. &lt;/p&gt;

&lt;p&gt;If you read till last, don't forget to give your feedback in the comments. Getting feedback helps me improve. &lt;/p&gt;

&lt;p&gt;I write about new stuffs almost daily. You can follow me on &lt;a href="https://twitter.com/diipakkr"&gt;Twitter&lt;/a&gt; | &lt;a href="https://instagram.com/diipakkr"&gt;Instagram&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dipakkr.substack.com/"&gt;Subscribe to my email newsletter and stay updated!&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you liked the post, give some ❤️!! Cheers&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>tutorial</category>
      <category>node</category>
    </item>
  </channel>
</rss>
