<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Santhosh Reddy Pemmaka on Medium]]></title>
        <description><![CDATA[Stories by Santhosh Reddy Pemmaka on Medium]]></description>
        <link>https://medium.com/@santhosh.pemmaka?source=rss-767a8602a87b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*JTL4FoSEXDJvuujf8GAedA.jpeg</url>
            <title>Stories by Santhosh Reddy Pemmaka on Medium</title>
            <link>https://medium.com/@santhosh.pemmaka?source=rss-767a8602a87b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 01:51:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@santhosh.pemmaka/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Learn About CSS Specificity]]></title>
            <link>https://medium.com/@santhosh.pemmaka/learn-about-css-specificity-f5b1a38456ee?source=rss-767a8602a87b------2</link>
            <guid isPermaLink="false">https://medium.com/p/f5b1a38456ee</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[css]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[html]]></category>
            <dc:creator><![CDATA[Santhosh Reddy Pemmaka]]></dc:creator>
            <pubDate>Thu, 02 Jun 2022 13:08:28 GMT</pubDate>
            <atom:updated>2022-06-02T13:08:28.923Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*17Eil2RBit5H-jC9Pm9Ayw.png" /></figure><p>CSS Specificity is what decides what styles are applied to elements, the more specific the CSS selector.</p><p>There are five main levels that determine a style’s specificity and we explore the syntax for selectors.</p><ul><li>They are, in order of least-specific to most-specific.</li></ul><h4><strong><em>Element Selectors</em></strong></h4><p>These are styles using a select element as a selector:</p><pre>h2 {<br>  font-size : 20px;<br>  color : lightblue;<br>}</pre><h4><strong>Class Selectors</strong></h4><p>These are styles using a select class name as a selector:</p><pre>.heading-name {<br>    font-size : 18px;<br>    color : lightgreen;<br>}</pre><h4><strong>ID Selectors</strong></h4><p>These are styles using a select id name as a selector:</p><pre>#heading-name {<br>    font-size : 16px;<br>    color : lightred;<br>}</pre><p>The three shown above are the most common, but there are two more levels that are different one shown below.</p><h4><strong>Inline Selectors</strong></h4><p>These are styles directly applied to HTML attributes:</p><pre>&lt;h2 style=&quot;font-size: 18px; color: blue;&quot;&gt;neoG Bootcamp&lt;/h2&gt;</pre><h4><strong>!important Selectors</strong></h4><p>If an individual style is declared to be !important, then it will override anything else:</p><pre>h2 {<br>   font-size : 18px !important;<br>   color : orange !important;<br>}</pre><ul><li>Selectors can use a combination of attributes, so CSS uses a weight system to compute final specificity. let’s explain with an example:</li></ul><pre>&lt;!DOCTYPE html&gt;<br>&lt;html&gt;<br>&lt;head&gt;<br>    &lt;title&gt;Page Title&lt;/title&gt;<br>    &lt;style&gt;<br>       #heading h1{<br>          color : blue;<br>       }<br>       #heading {<br>          color : red;<br>       }<br>    &lt;/style&gt;<br>&lt;/head&gt;<br>&lt;body&gt;<br>    &lt;h1 id=&quot;heading&quot;&gt;neoG bootcamp&lt;/h1&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;</pre><p>There are two CSS selectors with two different weights, let&#39;s see how to calculate a CSS weight.</p><ol><li>Since five different factors influence CSS specificity. we use five numbers to track how many times we see a type of selector, We start the weight with zero specificity, which is represented as <em>(0, 0, 0, 0, 0)</em>. Each represents. Each digit represents (# of !important, # of inline style, # of IDs, # of classes, # of elements).</li><li>Using the selector #heading h1 as we can see ID and element selectors. Our weight is now (0,0,1,0,1). Next #heading as we can see ID selector. Our weight is now (0,0,1,0,0). So the final result style ‘ color: blue’ has a weight of (0,0,1,0,1).</li></ol><p>That’s the basic idea of CSS specificity, but there are some tricky questions. Let’s discuss that one below.</p><ol><li>What happens if two styles have the same specificity?.</li></ol><pre>h1{<br>  color : blue;<br>}</pre><pre>h1 {<br>  color : red;<br>}</pre><p>Both styles have a specificity of (0,0,0,0,1), but the color: red comes last in the stylesheet, So it wins.</p><p>2. What happens if two styles have the same specificity, but one is used !important.</p><pre>h1{<br>  color : blue !important;  /* (1,0,0,0,1) */<br>}</pre><pre>h1{<br>  color : red;  /* (0,0,0,0,1) */<br>}</pre><p>In the case of color: blue, because !important is the highest priority.</p><p>3. I want to cause other people frustration and confusion. Any tips?</p><pre>#heading#heading {<br>    color : blue;   /* (0, 0, 2, 0, 0) */<br>    color : red;    /* (0, 0, 1, 0, 0) */<br>}</pre><p>There’s nothing stopping you from reusing the same IDs and classes in your selectors to bump up their style’s specificity.</p><p>We need to rarely use ‘!important’, as it will affect the maintainability of our CSS.</p><p>Thanks for reading this <strong>😆 😆 </strong>👏</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f5b1a38456ee" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DS with JavaScript Linked List.]]></title>
            <link>https://medium.com/@santhosh.pemmaka/ds-with-javascript-linked-list-2aee66ed3ef0?source=rss-767a8602a87b------2</link>
            <guid isPermaLink="false">https://medium.com/p/2aee66ed3ef0</guid>
            <category><![CDATA[datastrucutre]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[javascript-tips]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Santhosh Reddy Pemmaka]]></dc:creator>
            <pubDate>Tue, 30 Nov 2021 17:20:53 GMT</pubDate>
            <atom:updated>2021-11-30T17:20:53.920Z</atom:updated>
            <content:encoded><![CDATA[<h4>Implementing Double Linked List with JavaScript.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/845/1*kHsLDnNOxkWsHHWxB-S9Jg.png" /></figure><ul><li><em>A doubly linked list is a linked data structure that consists of a set of sequentially linked records called nodes. Each node contains two fields, called links, that are references to the previous and to the next node in the sequence of nodes.</em></li></ul><p><strong>Before proceeding to further step at least basic idea about how single linked lists works. If no ideal about linked list first read this article </strong><a href="https://medium.com/@santhosh.pemmaka/linked-list-in-javascript-a5a4b1ed18e2"><strong>DS with JS — Single Linked Lists.</strong></a></p><p><strong>Core</strong></p><p>So we are going to create our two class.</p><pre>class Node{<br>   constructor(data){<br>       this.data = data;<br>       this.next = null;<br>       this.prev = null;<br>  }<br>}</pre><pre>class DoubleLinkedList {<br>      constructor(){<br>         this.head = null;<br>         this.tail = null;<br>     }<br>}</pre><p>In double linked lists nodes represents by a head that is the first node in the list and tail that is the last node in the list. Each node has to reference/pointer to the next node and previous node.</p><p>As you see we are represent two classes. One is Node class in that constructor one parameter that will represent data . When every Node class call the parameter data,next and prev default will be null. Second class have the constructor that will represent head to null and tail to null, and <strong>why they are null ? </strong>because of the DoubleLinkedList class has no nodes are there represent null pointer.</p><p>🎴 All the code you will see below are Pseudo code.</p><p>So we are going to create Append() function in Linked List class:</p><ul><li><strong>Append()</strong></li></ul><pre>Append( item){<br>   let node = new Node ( item );<br>   <br>  if(!this.head)<br>   {<br>     this.head = node;<br>     this.tail = node;<br>   }<br>   else{<br>      node.prev = this.tail;<br>      this.tail.next = node;<br>      this.tail = node;<br>    }<br> }</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wlC5F88iaHuZylBwcGVxrw.png" /><figcaption>Append Operation</figcaption></figure><ul><li><strong>AppendAt()</strong></li></ul><p>AppendAt() function will be two parameters item and position of the element. Based on the position value item will be inserted. We have change the pev and next pointer of the item connected previous and next nodes linked list.</p><pre>AppendAt( item , pos )<br>  {<br>    let current = this.head;<br>    node = new Node(item);<br>    let counter = 1;<br>    if(pos == 0)<br>    {<br>      this.head.prev = node;<br>      node.next = this.head;<br>      this.head = node;<br>    }<br>    while(current)<br>    {<br>      current = current.next;<br>      if(counter == pos)<br>      {<br>        node.prev = current.prev;<br>        current.prev.next = node;<br>        node.next = current;<br>        current.prev = node;<br>      }<br>      counter++;<br>    }<br>  }</pre><p>The example we have to taken insert item at 0 position.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iU6CHHDesfcmCUJRB_M5Gw.png" /><figcaption>AppendAt Operation</figcaption></figure><ul><li><strong>Remove()</strong></li></ul><p>Remove function will be remove item form the linked list.</p><pre>Remove( item )<br>  {<br>    let current = this.head;<br>    while(current)<br>    {<br>      if(current.data == item)<br>      {<br>      if(current == this.head&amp;&amp;current == this.tail)<br>      {<br>        this.head = null;<br>        this.tail = null;<br>      }<br>      else if(current == this.head){<br>        this.head = this.head.next;<br>        this.head.prev = null;<br>      }<br>      else if(current == this.tail)<br>      {<br>        this.tail = this.tail.prev;<br>        this.tail.next = null;<br>      }<br>      <br>      else{<br>        current.prev.next = current.next;<br>        current.next.prev = current.prev;<br>      }<br>      }<br>      current = current.next;<br>  }<br>  }</pre><p>The example we have take remove item equals to 1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8DvieeG_bKdv4W1tI3nmNA.png" /></figure><ul><li><strong>RemoveAt()</strong></li></ul><p>RemoveAt function will be one parameter that will be position of the item. When counter equals to the position that element will be remove from the list.</p><pre>removeAt ( pos )<br>  {<br>    let current = this.head;<br>    let counter = 1;<br>    if( pos == 0 )<br>    {<br>      this.head = this.head.next;<br>      this.head.prev = null;<br>    }<br>    else<br>    {<br>      while( current )<br>      {<br>        current = current.next;<br>        if( current == this.tail )<br>        {<br>          this.tail = this.tail.prev;<br>          this.tail.next = null;<br>        }<br>        else if(counter == pos)<br>        {<br>          current.prev.next = current.next;<br>          current.next.prev = current.prev;<br>        }<br>        counter++;<br>      }<br>    }<br>    <br>  }</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EaRc2Ag1OeGL2LnXcXqpwg.png" /><figcaption>RemoveAt Operation</figcaption></figure><ul><li><strong>Reverse()</strong></li></ul><pre>Reverse ()<br>  {<br>    let current = this.head;<br>    let prev = null;<br>    while(current)<br>    {<br>      let next = current.next;<br>      current.next = prev;<br>      current.prev = next;<br>      prev = current;<br>      current = next;<br>    }<br>    this.tail = this.head;<br>    this.head = prev;<br>  }</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ggbWs4aAG5xE8uG2cB2UXw.png" /><figcaption>Reverse Operation.</figcaption></figure><ul><li><strong>Swap()</strong></li></ul><p>The Swap() function will be swap of the two elements in the linked list.</p><pre>Swap( nodeone , nodetwo )<br>  {<br>    let current = this.head;<br>    let counter = 0;<br>    let firstnode;<br>    while(current)<br>    {<br>      if(nodeone == counter)<br>      {<br>        firstnode = current;<br>      }<br>      else if(nodetwo == counter)<br>      {<br>        let temp = current.data;<br>        current.data = firstnode.data;<br>        firstnode.data = temp;<br>      }<br>      current = current.next;<br>      counter++;<br>    }<br>    return true;<br>  }</pre><p>The example we have take swap items 1 and 5.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tuEyapSN-0u3eFp51xoxfA.png" /><figcaption>Swap Operation</figcaption></figure><ul><li>Length()</li></ul><p>Length() function tells that how many no of nodes in the linked list.</p><pre>Length()<br>  {<br>    let current = this.head;<br>    let counter = 0;<br>    while(current != null)<br>    {<br>      counter++;<br>      current = current.next;<br>    }<br>    return counter;<br>  }</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rsdtI5668YtZZRRZvep_5A.png" /><figcaption>Length Operation.</figcaption></figure><ul><li><strong>Traverse()</strong></li></ul><p>The Traverse() function will be change the actual data of the list by the operation on individual data of the linked list.</p><pre>Traverse( fn )<br>  {<br>    let current = this.head;<br>    while(current)<br>    {<br>      fn( current );<br>      current = current.head;<br>    }<br>    return true;<br>  }</pre><p>We take example add 10 to each element in the list.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C15zBxiX4j19IiMcaWtoIQ.png" /><figcaption>Traverse Operation.</figcaption></figure><ul><li>Search()</li></ul><p>The Search() function will be item present in the linked list. If present item in linked list return the position of the item.</p><pre>Search( item )<br>  {<br>    let current = this.head;<br>    let counter = 0;<br>    while( current )<br>    {<br>      if(current.data == item)<br>      {<br>        return counter;<br>      }<br>      current = current.next;<br>      counter++;<br>    }<br>    return false;<br>  }</pre><p>The we taken item equals to the 5.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hRZLYlS7M-3ReS8_9ObWrQ.png" /><figcaption>Search Operation.</figcaption></figure><p><strong>Practice</strong></p><ul><li><strong>Hacker Rank </strong><a href="https://www.hackerrank.com/domains/data-structures?filters%5Bsubdomains%5D%5B%5D=linked-lists"><strong>Double Linked List</strong></a><strong>.</strong></li></ul><p>If you like this article , please give it some claps 👏 and share it! . If any questions regarding in this post. You are feel free ask questions to me.</p><p><strong>Happy Coding !! 😆 😆 😆</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2aee66ed3ef0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Linked List in JavaScript.]]></title>
            <link>https://medium.com/@santhosh.pemmaka/linked-list-in-javascript-8cb74838240b?source=rss-767a8602a87b------2</link>
            <guid isPermaLink="false">https://medium.com/p/8cb74838240b</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[linked-lists]]></category>
            <category><![CDATA[data-structure-algorithm]]></category>
            <dc:creator><![CDATA[Santhosh Reddy Pemmaka]]></dc:creator>
            <pubDate>Mon, 29 Nov 2021 07:26:39 GMT</pubDate>
            <atom:updated>2021-11-29T07:26:39.826Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*WGEN63kTClxm_nnK9t1DpA.jpeg" /></figure><p><em>What is a data Structure?</em></p><p>A data structure is a way of organizing data that is stored in a computer so that it can be used efficiently.</p><p>A Linked List, that as its name says, is a linked list of nodes that are represented by a head that is the first node in the list and the tail that is the last one. Each node has to reference/pointer to the previous and the next node.</p><p>The Linked list data structure has two types, the first one is a single linked list, the nodes of this type have a pointer to the next one but for their previous node.</p><p>In this article, we are exploring the Single Linked List, that the nodes have a next pointer, not the previous pointer.</p><p><strong>Linked List Drawbacks:</strong></p><ul><li>Random access is not allowed. We must access nodes sequentially the starting from the first one. Therefore, we cannot do binary searching in the linked list. So searching for an element is slow.</li><li>Extra memory space for a link is required for each element of the list.</li></ul><p>In this article, we are going to have a single Linked List data structure.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/553/1*JLI31Df0JrprSKrM_NOnHA.png" /></figure><p>So we are going to create our two classes :</p><pre>class Node{<br>   constructor(data,next=null){<br>       this.data=data;<br>       this.next=next;<br>     }<br>}</pre><pre>class LinkedList{<br>     constructor(){<br>        this.head=null;<br>      }<br>}</pre><p>As you see we are represent two classes. One is Node class in that constructor two parameters that will represent data and next. When every Node class call only one parameter data and next parameter default will be null. The second class has the constructor that will represent head equal to null, and <strong>why they are null? </strong>Because they don’t have any code in the beginning.</p><p>So if we now create, for example:</p><pre>const list = new LinkedList();<br>const node = new Node(5);<br>list.head=node;<br>console.log(list);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HO64SttFC21r-eleEAUy0A.png" /></figure><p>So we are going to create<strong> Insert First()</strong> function in Linked List class:</p><pre><strong>&#39;one approach method&#39; //Putting one statement </strong><br>insertFirst(){<br>  this.head= new Node(data,this.head);<br>}<br><strong>&#39;Second approach method&#39;</strong><br>insertFirst(){<br>  const node = new Node(data,this.head);<br>  this.head = node;<br>}</pre><p>In this insertFirst(), we insert an element in the head of the linked list and the previous element passing through to the insert element next. In this method we are not deleting elements, we are writing insert elements in the head linked list and linked list next passing through the previous element.</p><p>So if we now create, for example:</p><pre>list.insertFirst(16);</pre><p>So we are going to create <strong>Size() </strong>function in the Linked List class:</p><ul><li>All this below code type inside the linked list class.</li></ul><pre>Size(){<br>let counter=0;<br>let node = this.head;<br>while(node){<br> counter++;<br> node=node.next;<br>}<br>return counter;<br>}</pre><p>In this Size(), we are finding out the size of the Linked List. firstly initialize two temporary variables like counter and node. Let counter variable assign to zero and node equals to Linked List head. Using while loop, if the node is present counter will be increment and node will be assigned to node next part of Linked List. When condition false didn’t enter to the loop and return value counter. That will be the size of the Linked List.</p><p>So if we now create, for example:</p><pre>list.Size();</pre><p>So we are going to create the <strong>getFirst() </strong>function in the Linked List class:</p><pre>getFirst(){<br> return this.head;<br>}</pre><p>In this getFirst(), we are returning the first element of the Linked List. By simply returning the head of the Linked List. Because head will be assigned to starting element Linked List.</p><p>So if we now create, for example:</p><pre>list.getFirst();</pre><p>So we are going to create <strong>getLast()</strong> function in the Linked List class:</p><pre>getLast(){<br> if(!this.head){<br>  return null;<br> }<br>let node= this.head;<br>while(node){<br> if(!node.next){<br>  return node;<br>  }<br>node=node.next;<br>}</pre><p>In this getLast(), we are return the last element of the Linked List. In case first check in Linked list any element is there or not. If element is not there return null, otherwise element is there return last element Linked List. consider temporary variable node assign to head of linked list. Using while loop if there node is there going to the inside loop and again check for node next element. If node next element is not there return node, otherwise node next element is there node will be assigned to the next element of the node. Finally return the last element Linked List.</p><p>So we are now create, for example:</p><pre>list.getLast();</pre><p>So we are going to create <strong>clear()</strong> function in Linked List:</p><pre>clear(){<br> this.head=null;<br>}</pre><p>In this clear(), we are clear the Linked List. In this case simple assign head of the Linked List equal to null.</p><p>So we are now create, for example:</p><pre>list.clear();</pre><p>So we are going to create <strong>removeFirst() </strong>in Linked List:</p><pre>removeFirst(){<br> if(!this.head){<br>   return;<br> }<br>this.head= this.head.next;<br>}</pre><p>In this removeFirst(),we are going to remove first element in Linked List. So firstly check for the first element in linked list is there or not. If element is not there simple return null, otherwise we using reassign head linked list to the second element of the linked list. Due to first element linked list will over written to the second element of the linked list.</p><p>So we are now create, for example:</p><pre>list.removeFirst();</pre><p>So we are going to create <strong>removeLast()</strong> in Linked List:</p><pre>removeLast(){<br> if(!this.head){<br>  return ;<br> }<br>if(!this.head.next){<br> this.head=null;<br> }<br>let previous=this.head;<br>let node= this.head.next;<br>while(node){<br> previous=node;<br> node=this.head.next;<br> }<br>previous.head=null;<br>}</pre><p>In this removeLast(),we are going to remove last element in Linked List. So firstly check for the first element in linked list is there or not. If element is not there simple return null, otherwise check for the second element in linked list is there or not. If the no second element in linked list assign first element will be null. In case linked list contains more than two elements. Let consider two temporary like previous and node assign to the first and second elements of linked list respectively. In while loop will used to find out last and previous elements of the linked list.</p><p>I hope you like it and it will help to you.</p><p><strong>Great courses to learn Data Structures and Algorithms:</strong></p><ul><li><a href="https://www.udemy.com/learning-data-structures-in-javascript-from-scratch/">Learning Data Structures in JavaScript from Scratch</a></li></ul><p>Thank you 😃</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8cb74838240b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building AWS Serverless]]></title>
            <link>https://medium.com/@santhosh.pemmaka/building-aws-serverless-534e54ed947f?source=rss-767a8602a87b------2</link>
            <guid isPermaLink="false">https://medium.com/p/534e54ed947f</guid>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[cloud-computing]]></category>
            <category><![CDATA[serverless-architecture]]></category>
            <category><![CDATA[lambda-function]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Santhosh Reddy Pemmaka]]></dc:creator>
            <pubDate>Fri, 05 Apr 2019 12:14:52 GMT</pubDate>
            <atom:updated>2019-04-05T13:32:30.830Z</atom:updated>
            <content:encoded><![CDATA[<ul><li>In article will introduce you to Amazon Web Services (AWS) serverless architecture . Amazon services like S3, AWS Lambda, AWS Cloudfront, Amazon API Gateway.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2OMQ2HzL_TI8vrrq2rBSIQ.jpeg" /><figcaption>Server less application</figcaption></figure><h4>Sign in</h4><p>As the build serverless architecture on Amazon Web Services, you need to create account on aws. If don’t have account in aws, you can set up <a href="https://aws.amazon.com/free/?sc_channel=PS&amp;sc_campaign=acquisition_IN&amp;sc_publisher=google&amp;sc_medium=ACQ-P%7CPS-GO%7CBrand%7CDesktop%7CSU%7CCore%7CCore%7CIN%7CEN%7CText&amp;sc_content=Brand_Account_e&amp;sc_detail=aws%20account&amp;sc_category=Core&amp;sc_segment=293607067662&amp;sc_matchtype=e&amp;sc_country=IN&amp;s_kwcid=AL!4422!3!293607067662!e!!g!!aws%20account&amp;ef_id=Cj0KCQjw1pblBRDSARIsACfUG13aRWuiwsX42Sp7ZipRdn41rmFHtexVN228AgXUVFlev53qr48MiMAaAqsjEALw_wcB:G:s">here</a> and click “Create a Free Account”.</p><h4>First example about S3</h4><p><strong><em>Amazon Simple Storage Service is an object storage service that offers industry-leading scalability , data availability, security, and performance. This means customers of all sizes and industries can use it to store and protect any amount of data for a range of use cases,such as websites, mobile applications.</em></strong></p><ol><li>First creating a S3 bucket and configuring as a static website.</li></ol><ul><li>In AWS Management Console in the search box type S3 and choose S3.</li><li>Click <strong>Create bucket</strong>.</li><li>In the bucket name field, type unique DNS-complaint name for your new bucket.</li><li>Click <strong>NEXT.</strong></li><li>Click <strong>Create bucket.</strong></li><li>Click on the your create <strong>bucket name</strong> and select the <strong>properties</strong>, choose <strong>static website hosting</strong>.</li><li>Select <strong>Use this bucket to host a website</strong>.</li><li>Under <strong>Index document type</strong> the <strong>text.html.</strong></li><li>Under <strong>Error document type</strong> the <strong>error.html.</strong></li><li>Click <strong>Save.</strong></li></ul><p>2. Upload objects to a existing S3 bucket.</p><ul><li>Download the ZIP file. Click <a href="https://s3.amazonaws.com/awsu-hosting/CSA-TF-100-SCSRVL-10-EN/s3website.zip">here</a></li><li>Unzip it on your local machine.</li><li>Click <strong>Upload </strong>and drag the files needed for the site form your local file explore.</li><li>After upload all files to S3. Click on the <strong>text.html</strong>, it’s looks like given below.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7mrVCXHbDx66OcCBX06vhQ.jpeg" /></figure><ul><li>Click on <strong>Object URL, </strong>it’s<strong> </strong>open the front page the website looks like given below.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/431/1*HtasvAUj9wVdG6fsyUfYdw.png" /></figure><ul><li>As of right now every city you choose will return <strong>20 degress</strong>. This is because of the static web site.</li></ul><h4>Second example about CloudFront.</h4><ul><li><strong><em>Amazon CloudFront is a fast content delivery network (CDN) service that securely delivery data, videos, applications, and APIs to customers globally with low latency, high transfer speeds, all with in a developer- friendly environment. CloudFront is integrated with AWS- both physical locations that are directly connected to the AWS global infrastructure, as well as other AWS services.</em></strong></li><li><strong><em>Creating a CloudFront distribution.</em></strong></li><li>In the AWS Management Console and in the <strong>Find Service </strong>search box type cloud choose <strong>CloudFront.</strong></li><li>Click <strong>Create Distribution.</strong></li><li>Under <strong>Web </strong>click<strong> Get Strated.</strong></li><li>For <strong>Origin Domain Name </strong>in that, Pick the website bucket your created.</li><li>The <strong>Origin ID </strong>should be have been pre-populated when you choose your bucket.</li><li>Under Viewer Protocol Policy select <strong>Redirect HTTP to HTTPS.</strong></li><li>For Allowed <strong>HTTP</strong> Methods choose <strong>GET,HEAD.</strong></li><li>It’s looks like given below.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XiKyILMlznF5HramUy4hCw.jpeg" /></figure><ul><li>For leave remaining will be <strong>default settings</strong>.</li><li>We can also leave <strong>Lambda Function Associations</strong> as the default.</li><li>Click <strong>Create Distribution</strong>.</li><li>Click on Distributions at the top left to see your CloudFront distribution being built.</li><li>This can take 15–20 minutes to complete.</li><li>CloudFront Distributions looks like given below.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FbRG3Y11HMwWfNWwVa4o8w.jpeg" /></figure><ul><li>Copy the URL under <strong>Domain Name</strong>.</li><li>Browser to that URL and you should now see the <strong>text.html</strong> page, what is created in the S3 same page access through <strong>Domain Name</strong>.</li></ul><h4>Third explain about Amazon API Gateway.</h4><ul><li><strong><em>Amazon API Gateway is a fully managed service that makes it easy for developers to create, publish, maintain, monitor, and secure APIs at any scale. You can create REST and WebSocket APIs that act as a “front door” for applications to access data, business logic, or functionality from your back end services, such as workloads running on Amazon Elastic Compute Cloud (Amazon EC2), code running on AWS Lambda, any web application, or real time communication applications.</em></strong></li><li><em>Creating an API Gateway endpoint.</em></li><li>Sign in to AWS Management Console and in the <strong>Find Services </strong>search box type api and choose <strong>API Gateway</strong>.</li><li>Under Create new API choose <strong>NEW API.</strong></li><li>For settings use the following.</li><li>API name: Weather (as your wise).</li><li>Optionally, add a brief description in Description.</li><li>Click <strong>Create API.</strong></li><li>Click <strong>Actions </strong>and <strong>Create Method.</strong></li><li>Choose <strong>POST </strong>form the list under the <strong>/ </strong>resources and click <strong>mark icon.</strong></li><li>for <strong>Integration type</strong> choose <strong>Mock </strong>and click<strong> save.</strong></li><li>In the <strong>Post </strong>Methods.</li><li>Click <strong>Integration Response.</strong></li><li>Click the arrow to open up the response properties.</li><li>Click the drop down arrow for <strong>Mapping Templates.</strong></li><li>The type in the <strong>Integration Response</strong> looks like given below.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rPTN1u1T_QfFiONKkbdmhw.png" /><figcaption>API Gate Way</figcaption></figure><ul><li>Also click <strong>Save at the top right.</strong></li><li>Choose <strong>Method Execution</strong> again.</li><li>Click <strong>Test</strong>.</li><li>Set the request body as</li></ul><pre>{<br>   &quot;city_str&quot;:&quot;SEATLLE&quot;<br>}</pre><ul><li>Click <strong>Test </strong>again<strong>.</strong></li><li>The output response will as follows:-</li></ul><pre>{<br>  &quot;temp_int&quot;:45<br>}</pre><ul><li>Under Logs looks like this:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/937/1*zphTB97bGfsn3cD55vc1zg.png" /><figcaption>Response Header</figcaption></figure><h4>Steps to enable CORS in the API gateway console.</h4><ul><li>Click on the resource “<strong>/” </strong><em>(NOT POST) </em>above the post.</li><li>Select <strong>Actions </strong>and<strong> Enable CORS, </strong>select the<strong> DEFAULT 4XX and DEFAULT 5XX. </strong>Leave the other default settings.</li><li>On the <strong>Deploy API </strong>settings pop-up select. Deployment stage <strong>[New Stage] </strong>and stage name will be <strong>test.</strong></li><li>Click<strong> Deploy</strong>.</li><li>Copy down <strong>Invoke URL, </strong>present in the<strong> </strong>new create API gateway.</li><li>we need to do is change the <strong>API_GATEWAY_URL_STR </strong>value in the website’s in <strong>config.js</strong> file. Save it and re-upload it back to S3.</li><li>In <strong>config.js </strong>looks like this:</li></ul><pre>var API_GATEWAY_URL_STR = null;</pre><ul><li>Deploy gateway URL like this:</li></ul><pre>var API_GATEWAY_URL_STR = &quot; API gateway URL&quot;</pre><ul><li>Browse to your website (i.e your CloudFront Domain name), looks like this:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1011/1*i5lIOLoYSmH9lQYsxX8aPg.png" /></figure><ul><li>The weather should always return will be <strong>45 degrees </strong>no matter which city chosen because website interaction with <strong>MOCK API, </strong>we give in the integrated response temp will be 45 degrees.</li><li>To get different weathers in the different cities, website interaction with the lambda function. We did next step.</li></ul><h4>Next explain about AWS Lambda function.</h4><ul><li><strong><em>AWS Lambda lets you run code without provisioning or managing servers. You pay only for the the compute time you consume, there is no charge when your code is not running. With Lambda, you can run code for virtually any type of application or back-end service, all with zero administration. Just upload your code and lambda takes care of everything required to run and scale your code with high availability. You can set up your code to automatically trigger from other AWS services or call it directly from any web or mobile app.</em></strong></li><li><em>Steps for create a simple Lambda function</em></li><li>In the Management Console search for <strong>lambda.</strong></li><li>Click <strong>Create function</strong>.</li><li>Select Author from scratch.</li><li>Name the function <strong>get_weather1.</strong></li><li>Select Node.js from the<strong> Runtime</strong> test.</li><li>For using existing <strong>policy attach</strong> to the lambda function.</li><li>Click <strong>Create function.</strong></li><li>After created function, scroll down to the inline code editor.</li><li>Paste the following code into the index.js tab replacing the existing code.</li></ul><pre>function handler(event, context, callback){<br>    var <br>       city_str = event.city_str,<br>       response = {<br>             city_str: city_str,<br>             temp_int: Math.floor(Math.random() * 100);<br>           };<br>       console.log(response);<br>       callback(null,response);<br>  }<br> exports.handler = handler;</pre><ul><li>You will notice we console log out the response too. This is so you can view this information in CloudWatch logs.</li><li>Click <strong>Save</strong></li><li><em>Steps to create a test case for our lambda Mock</em>.</li><li>Click the drop down that says “<strong>select a test event</strong>”.</li><li>Choose <strong>configure test events.</strong></li><li>For <strong>Event name</strong> as <strong>hello.</strong></li><li>Paste the following into the inline code editor.</li></ul><pre>{<br>   &quot;city_str&quot;:&quot;SEATLLE&quot;<br>}</pre><ul><li>Click <strong>Create.</strong></li><li>Click <strong>Test.</strong></li><li>It’s shows that execution succeeded and looks like that:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lz30nUWoHoJNpFHwSLtlTw.jpeg" /></figure><ul><li><em>Steps to replace the API mock endpoint with the get_weather1 Lambda function</em></li><li>Select the <strong>Weather</strong> API under APIs.</li><li>For the <strong>Integration type </strong>change <strong>Mock </strong>to <strong>Lambda Function</strong>.</li><li>Leave Use <strong>Default Timeout</strong> checked.</li><li>Click <strong>Save.</strong></li><li>At the <strong>switch to Lambda integration</strong> pop-up. Click <strong>OK</strong>.</li><li>After changed lambda function, once again test it ones the same process done in the <strong>API Gateway</strong> and see Response Body.</li><li>Click on the resource “<strong>/” </strong><em>(NOT POST) </em>above the post.</li><li>Select <strong>Actions </strong>and<strong> Enable CORS, </strong>select the<strong> DEFAULT 4XX and DEFAULT 5XX. </strong>Leave the other default settings.</li><li>Click <strong>Actions </strong>again and <strong>Deploy API</strong>.</li><li><strong>Click Deploy</strong>.</li><li>Choose a city, and <strong>Ask Weather Bot</strong>.</li><li>Every city showing different weather because of the each city interact with lambda every time.</li><li>I will give pics of the two cities selected before and observe two different weather corresponding to city.</li><li>Browse to your website (i.e your CloudFront Domain name), looks like this:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/918/1*7x3ZuvVyThqWxTQq9qvFyA.png" /></figure><p><strong><em>Awesome now have a server-less website that hits a functional Lambda function in back-end.</em></strong></p><p><em>Thanks for following this article. I hope you enjoy it. If you did then please hit that clap button and follow me for more articles. 😃 😜 🙌 👏.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=534e54ed947f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Welcome to Amazon Lex Chatbot]]></title>
            <link>https://medium.com/@santhosh.pemmaka/welcome-to-amazon-lex-chatbot-efd0acb2dc22?source=rss-767a8602a87b------2</link>
            <guid isPermaLink="false">https://medium.com/p/efd0acb2dc22</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[bots]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[botox]]></category>
            <category><![CDATA[cloud-computing]]></category>
            <dc:creator><![CDATA[Santhosh Reddy Pemmaka]]></dc:creator>
            <pubDate>Sun, 10 Mar 2019 15:13:02 GMT</pubDate>
            <atom:updated>2019-03-11T14:55:44.042Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>Build your own chatbot using Amazon Lex.</blockquote><p>This article will guide create your own amazon lex chatbot. We will start with setup some simple response and then create complex online shopping chatbot.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1017/1*nEWcR-IR7Zcxkxn7LJyhBw.png" /><figcaption>Lex Bot Structure</figcaption></figure><h4>Sign in</h4><p>As the chatbot build on Amazon Web Services, you need to create account on aws. If don’t have account in aws, you can set one up <a href="https://aws.amazon.com/">here</a> and click “Create a Free Account”.</p><p>Once you have your account set up, we can set up the chatbot. On the <a href="https://console.aws.amazon.com/console/home">AWS Management Console</a>, search for “Lex” or click on the service drop down menu on the top left of the page and you’ll find under “Machine Learning “ category.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TmGYmCZ__kkSTEw451eb-Q.png" /></figure><p>Once on the Lex page, click “Get Started”<strong><em> </em></strong>to get onto the bot setup page. You’ll be presented with the options. We’ll be creating a custom bot so select that option.</p><p>You now get to name your bot and select an output voice. The voice will be used if you ever want to make a voice chat version of your bot. We will also set the session timeout to 5 minutes and select “No”<strong> </strong>for the COPPA (unless you intend to include non PG replies).</p><p>With all set up, you can click “Create”. You’ll be taken to a new dashboard below. I’ll explain what everything means as we go along.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5BwD9YmVV7vz1HSKIZcrgA.png" /></figure><h4>Intents</h4><p>A chatbot is a set of responses that it gives to message. These are sorted in <em>Intents </em>which are like talking points.</p><h4>Getting started with your bot</h4><p>We’ll keep our first intent will be simple. Create a new intent by click on the Create Intent menu will be left of the page. After click on the Create Intent pop up window will comes select the again “Create intent”. Give a unique name for the new intent something like “Name _App” and then click add button.</p><p>This puts us through to the Intent screen.For now the important sections are <strong>Sample utterances</strong> and <strong>Response</strong>. The rest of the settings we will cover later.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DhFvDwBm99_x-u7Xfo6gIQ.png" /></figure><h4>Sample Utterances</h4><p>Utterances are the phrase that you want this intent to reply to. Add the following phrases as sample utterances.</p><p>Add the following as response:</p><ul><li>“what is your good name please”</li><li>“what is your name”</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/643/1*dXW1NhCp52TchwmW2f_E8g.png" /></figure><p>The smart bit Amazon Lex will be the that understand Natural Language Understanding. What ever user trying say, if say user will “What is good name please” and “What is your name”. Lex will match both phrases response will be same.</p><h4>Response</h4><p>we need to reply to this message. Click the “Add message” button in the response box. Click Message and fill the message box and click on the “+” button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/795/1*xJEz3v8yBjuqTJ1-sZfa6Q.png" /></figure><p>In here you can type in whatever you want the bot to respond. You can enter multiple answer so the user can get varied and more natural responses.</p><p>Add the following as responses:</p><ul><li>“My name is awesome”</li><li>“you call me awesome”</li></ul><p>Now click “Save Intent” at the bottom of the page and you’ve created your first intent.</p><h4>Building and Testing the bot</h4><p>To get your new chatbot working we first need to build it. This allows lex to take your sample utterances and put them all together. Click the “Build” button on the top right side of the page. It can take a few minutes of finish building the chatbot.</p><p>When it’s finished you get a new area on the right called <em>Test Bot. </em>This is where you can try chatting to your newly created chatbot and test it out.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/399/1*UKKlTarMThT0WonVD6eTeA.png" /><figcaption>ChatBot</figcaption></figure><p><strong>Slots</strong></p><p>Let’s add slots to the chatbot.</p><ul><li>property name</li><li>slot type</li><li>prompt</li></ul><p>In the slots selection, add the following information to create a new slot.</p><ul><li>Required Name : “Name”</li><li>Slot type : “Amazon_us_first_name”</li><li>prompt: “What is your name?”</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Yq3KUuXVQZ7soS-kVgAlrQ.png" /><figcaption>Slot in the bot</figcaption></figure><p>The prompt is sent if the <em>Required</em> checkbox is ticked and the intent doesn’t know the value for the variable, and click on the “Save Intent”. After that click “Build” button top of the right side page, chatbot test it.</p><p>Observe below chatbot message carefully.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/403/1*ehNa-iR9qVu5WlChPwtMnw.png" /></figure><p>After the message “what is your name?” given that “santhosh” but response showing the “My name is awesome”. In case interaction to the chatbox not in the effectively. By overcome problem using “Slots” in the prompt click on the “setting” button right side of the page, open “Name setting window” in that “Corresponding utterances” add the “My name is {Name}”. In the <em>Slots Required Name </em>variable with curly brackets around the variable<em>.</em>Click on<em> </em>save button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/827/1*7SvxPcq3V3zrGlkDF-POjQ.png" /></figure><h4>Response</h4><p>In here you can type in whatever you want the bot to respond.In the place awesome use curly braces placed in the “Required Name”.</p><p>Add the following as responses:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/824/1*IJr3NLOzDd_jo82cm4hWTw.png" /></figure><p>Click on the “Save Intent”. After that click “Build” button top of the right side page, chatbot test it out!.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/428/1*zJrdKsnFt44VEmP_9-yzHg.png" /></figure><p>We can observe chatbot message after adding “Slots” and before adding “Slots”. In this way chatbot interaction will be the more effectively.</p><h4>Create Online Shopping chatbot</h4><p>Create a new intent by clicking “Create Intent” or click on the + next to Intents on the menu at the left of the page. A menu will pop up and we want to choose “Create Intent” again. We have to name the intent so call it something like <em>OnlineShopping.</em></p><p>This puts us through to the Intent screen. For now the important sections are <em>Sample Utterance</em> and <em>Response</em>.</p><p><strong>Sample Utterance</strong></p><ul><li>Utterance are the phrases that you want this intent to reply to.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/953/1*1QeVoHWULsJoyL93FuAIPA.png" /></figure><p><strong>Response</strong></p><p>Click the “Add Message” button in the response box.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/937/1*h-OiebKQEfaLT5CaxyDC1w.png" /></figure><p>Now create new Intent by click on the “Intents” top of the left page below the “Editor” option, Click “+” and create a new intent by clicking “Create Intent”. We have to name the intent so call it something like <strong>Buy_shoe.</strong></p><p><strong>Sample Utterances</strong></p><ul><li>Utterance are the phrases that you want this intent to reply to.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1004/1*6b6JIfXPts0hjaoWyE7qFg.png" /></figure><p>Slots</p><p>In the slots section,add the following information to create a new slot.</p><ul><li>Required Name</li><li>Slot type</li><li>Prompt</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qyeuWkUNMDqZ70UanVlpWw.png" /><figcaption>Slots option</figcaption></figure><p><strong>Response</strong></p><p>Click the “Add Message” button in the response box.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fEFx0qYJZxc-6brcMhRcnA.png" /></figure><p><strong>Confirmation Prompt</strong></p><p>Click on the confirmation prompt add the below information.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qddb5--il8nohtR4ZSE7eg.png" /><figcaption>Confirmation prompt</figcaption></figure><p>With all of this updated, it’s time to build again and try it out!</p><p>Try creating an intent that works like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/409/1*fG7rqHmi11nrnrteDZymEg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/413/1*FbOecKUvbJS9PEkZ_8gbVg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/396/1*bs3ritx8swTnzHNGmAYG5Q.png" /></figure><p>The Online Shopping Chatbot finally working like this click <a href="https://s3-us-west-2.amazonaws.com/s3retentionsample-s3bucket-u197e01vui7u/ScreenCaptureProject3.mp4">here</a></p><ul><li>URL video of the working Chatbot :-</li></ul><p><a href="https://s3-us-west-2.amazonaws.com/s3retentionsample-s3bucket-u197e01vui7u/ScreenCaptureProject3.mp4">https://s3-us-west-2.amazonaws.com/s3retentionsample-s3bucket-u197e01vui7u/ScreenCaptureProject3.mp4</a></p><p>Thanks for following this article. I hope you enjoy it. If you did then please hit that clap button and follow me for more articles.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=efd0acb2dc22" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DS with JavaScript Linked List.]]></title>
            <link>https://medium.com/@santhosh.pemmaka/ds-with-javascript-linked-list-140701a069cd?source=rss-767a8602a87b------2</link>
            <guid isPermaLink="false">https://medium.com/p/140701a069cd</guid>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[linked-lists]]></category>
            <category><![CDATA[interview]]></category>
            <dc:creator><![CDATA[Santhosh Reddy Pemmaka]]></dc:creator>
            <pubDate>Sun, 06 Jan 2019 14:01:32 GMT</pubDate>
            <atom:updated>2019-01-18T09:17:44.638Z</atom:updated>
            <content:encoded><![CDATA[<h4>Implementing Double Linked List with JavaScript.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/845/1*kHsLDnNOxkWsHHWxB-S9Jg.png" /></figure><ul><li><em>A doubly linked list is a linked data structure that consists of a set of sequentially linked records called nodes. Each node contains two fields, called links, that are references to the previous and to the next node in the sequence of nodes.</em></li></ul><p><strong>Before proceeding to further step at least basic idea about how single linked lists works. If no ideal about linked list first read this article </strong><a href="https://medium.com/@santhosh.pemmaka/linked-list-in-javascript-a5a4b1ed18e2"><strong>DS with JS — Single Linked Lists.</strong></a></p><p><strong>Core</strong></p><p>So we are going to create our two class.</p><pre>class Node{<br>   constructor(data){<br>       this.data = data;<br>       this.next = null;<br>       this.prev = null;<br>  }<br>}</pre><pre>class DoubleLinkedList {<br>      constructor(){<br>         this.head = null;<br>         this.tail = null;<br>     }<br>}</pre><p>In double linked lists nodes represents by a head that is the first node in the list and tail that is the last node in the list. Each node has to reference/pointer to the next node and previous node.</p><p>As you see we are represent two classes. One is Node class in that constructor one parameter that will represent data . When every Node class call the parameter data,next and prev default will be null. Second class have the constructor that will represent head to null and tail to null, and <strong>why they are null ? </strong>because of the DoubleLinkedList class has no nodes are there represent null pointer.</p><p>🎴 All the code you will see below are Pseudo code.</p><p>So we are going to create Append() function in Linked List class:</p><ul><li><strong>Append()</strong></li></ul><pre>Append( item){<br>   let node = new Node ( item );<br>   <br>  if(!this.head)<br>   {<br>     this.head = node;<br>     this.tail = node;<br>   }<br>   else{<br>      node.prev = this.tail;<br>      this.tail.next = node;<br>      this.tail = node;<br>    }<br> }</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wlC5F88iaHuZylBwcGVxrw.png" /><figcaption>Append Operation</figcaption></figure><ul><li><strong>AppendAt()</strong></li></ul><p>AppendAt() function will be two parameters item and position of the element. Based on the position value item will be inserted. We have change the pev and next pointer of the item connected previous and next nodes linked list.</p><pre>AppendAt( item , pos )<br>  {<br>    let current = this.head;<br>    node = new Node(item);<br>    let counter = 1;<br>    if(pos == 0)<br>    {<br>      this.head.prev = node;<br>      node.next = this.head;<br>      this.head = node;<br>    }<br>    while(current)<br>    {<br>      current = current.next;<br>      if(counter == pos)<br>      {<br>        node.prev = current.prev;<br>        current.prev.next = node;<br>        node.next = current;<br>        current.prev = node;<br>      }<br>      counter++;<br>    }<br>  }</pre><p>The example we have to taken insert item at 0 position.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iU6CHHDesfcmCUJRB_M5Gw.png" /><figcaption>AppendAt Operation</figcaption></figure><ul><li><strong>Remove()</strong></li></ul><p>Remove function will be remove item form the linked list.</p><pre>Remove( item )<br>  {<br>    let current = this.head;<br>    while(current)<br>    {<br>      if(current.data == item)<br>      {<br>      if(current == this.head&amp;&amp;current == this.tail)<br>      {<br>        this.head = null;<br>        this.tail = null;<br>      }<br>      else if(current == this.head){<br>        this.head = this.head.next;<br>        this.head.prev = null;<br>      }<br>      else if(current == this.tail)<br>      {<br>        this.tail = this.tail.prev;<br>        this.tail.next = null;<br>      }<br>      <br>      else{<br>        current.prev.next = current.next;<br>        current.next.prev = current.prev;<br>      }<br>      }<br>      current = current.next;<br>  }<br>  }</pre><p>The example we have take remove item equals to 1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8DvieeG_bKdv4W1tI3nmNA.png" /></figure><ul><li><strong>RemoveAt()</strong></li></ul><p>RemoveAt function will be one parameter that will be position of the item. When counter equals to the position that element will be remove from the list.</p><pre>removeAt ( pos )<br>  {<br>    let current = this.head;<br>    let counter = 1;<br>    if( pos == 0 )<br>    {<br>      this.head = this.head.next;<br>      this.head.prev = null;<br>    }<br>    else<br>    {<br>      while( current )<br>      {<br>        current = current.next;<br>        if( current == this.tail )<br>        {<br>          this.tail = this.tail.prev;<br>          this.tail.next = null;<br>        }<br>        else if(counter == pos)<br>        {<br>          current.prev.next = current.next;<br>          current.next.prev = current.prev;<br>        }<br>        counter++;<br>      }<br>    }<br>    <br>  }</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EaRc2Ag1OeGL2LnXcXqpwg.png" /><figcaption>RemoveAt Operation</figcaption></figure><ul><li><strong>Reverse()</strong></li></ul><pre>Reverse ()<br>  {<br>    let current = this.head;<br>    let prev = null;<br>    while(current)<br>    {<br>      let next = current.next;<br>      current.next = prev;<br>      current.prev = next;<br>      prev = current;<br>      current = next;<br>    }<br>    this.tail = this.head;<br>    this.head = prev;<br>  }</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ggbWs4aAG5xE8uG2cB2UXw.png" /><figcaption>Reverse Operation.</figcaption></figure><ul><li><strong>Swap()</strong></li></ul><p>The Swap() function will be swap of the two elements in the linked list.</p><pre>Swap( nodeone , nodetwo )<br>  {<br>    let current = this.head;<br>    let counter = 0;<br>    let firstnode;<br>    while(current)<br>    {<br>      if(nodeone == counter)<br>      {<br>        firstnode = current;<br>      }<br>      else if(nodetwo == counter)<br>      {<br>        let temp = current.data;<br>        current.data = firstnode.data;<br>        firstnode.data = temp;<br>      }<br>      current = current.next;<br>      counter++;<br>    }<br>    return true;<br>  }</pre><p>The example we have take swap items 1 and 5.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tuEyapSN-0u3eFp51xoxfA.png" /><figcaption>Swap Operation</figcaption></figure><ul><li>Length()</li></ul><p>Length() function tells that how many no of nodes in the linked list.</p><pre>Length()<br>  {<br>    let current = this.head;<br>    let counter = 0;<br>    while(current != null)<br>    {<br>      counter++;<br>      current = current.next;<br>    }<br>    return counter;<br>  }</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rsdtI5668YtZZRRZvep_5A.png" /><figcaption>Length Operation.</figcaption></figure><ul><li><strong>Traverse()</strong></li></ul><p>The Traverse() function will be change the actual data of the list by the operation on individual data of the linked list.</p><pre>Traverse( fn )<br>  {<br>    let current = this.head;<br>    while(current)<br>    {<br>      fn( current );<br>      current = current.head;<br>    }<br>    return true;<br>  }</pre><p>We take example add 10 to each element in the list.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C15zBxiX4j19IiMcaWtoIQ.png" /><figcaption>Traverse Operation.</figcaption></figure><ul><li>Search()</li></ul><p>The Search() function will be item present in the linked list. If present item in linked list return the position of the item.</p><pre>Search( item )<br>  {<br>    let current = this.head;<br>    let counter = 0;<br>    while( current )<br>    {<br>      if(current.data == item)<br>      {<br>        return counter;<br>      }<br>      current = current.next;<br>      counter++;<br>    }<br>    return false;<br>  }</pre><p>The we taken item equals to the 5.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hRZLYlS7M-3ReS8_9ObWrQ.png" /><figcaption>Search Operation.</figcaption></figure><p><strong>Practice</strong></p><ul><li><strong>Hacker Rank </strong><a href="https://www.hackerrank.com/domains/data-structures?filters%5Bsubdomains%5D%5B%5D=linked-lists"><strong>Double Linked List</strong></a><strong>.</strong></li></ul><p>If you like this article , please give it some claps 👏 and share it! . If any questions regarding in this post. You are feel free ask questions to me.</p><p><strong>Happy Coding !! 😆 😆 😆</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=140701a069cd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Linked List in JavaScript.]]></title>
            <link>https://medium.com/@santhosh.pemmaka/linked-list-in-javascript-a5a4b1ed18e2?source=rss-767a8602a87b------2</link>
            <guid isPermaLink="false">https://medium.com/p/a5a4b1ed18e2</guid>
            <category><![CDATA[coding-interviews]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[data-structures]]></category>
            <dc:creator><![CDATA[Santhosh Reddy Pemmaka]]></dc:creator>
            <pubDate>Fri, 14 Dec 2018 09:15:58 GMT</pubDate>
            <atom:updated>2021-11-29T07:21:59.170Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*WGEN63kTClxm_nnK9t1DpA.jpeg" /></figure><p><em>What is a data Structure?</em></p><p>A data structure is a way of organizing data that is stored in a computer so that it can be used efficiently.</p><p>A Linked List, that as its name says, is a linked list of nodes that are represented by a head that is the first node in the list and the tail that is the last one. Each node has to reference/pointer to the previous and the next node.</p><p>The Linked list data structure has two types, the first one is a single linked list, the nodes of this type have a pointer to the next one but for their previous node.</p><p>In this article, we are exploring the Single Linked List, that the nodes have a next pointer, not the previous pointer.</p><p><strong>Linked List Drawbacks:</strong></p><ul><li>Random access is not allowed. We must access nodes sequentially the starting from the first one. Therefore, we cannot do binary searching in the linked list. So searching for an element is slow.</li><li>Extra memory space for a link is required for each element of the list.</li></ul><p>In this article, we are going to have a single Linked List data structure.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/553/1*JLI31Df0JrprSKrM_NOnHA.png" /></figure><p>So we are going to create our two classes :</p><pre>class Node{<br>   constructor(data,next=null){<br>       this.data=data;<br>       this.next=next;<br>     }<br>}</pre><pre>class LinkedList{<br>     constructor(){<br>        this.head=null;<br>      }<br>}</pre><p>As you see we are represent two classes. One is Node class in that constructor two parameters that will represent data and next. When every Node class call only one parameter data and next parameter default will be null. The second class has the constructor that will represent head equal to null, and <strong>why they are null? </strong>Because they don’t have any code in the beginning.</p><p>So if we now create, for example:</p><pre>const list = new LinkedList();<br>const node = new Node(5);<br>list.head=node;<br>console.log(list);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HO64SttFC21r-eleEAUy0A.png" /></figure><p>So we are going to create<strong> Insert First()</strong> function in Linked List class:</p><pre><strong>&#39;one approach method&#39; //Putting one statement </strong><br>insertFirst(){<br>  this.head= new Node(data,this.head);<br>}<br><strong>&#39;Second approach method&#39;</strong><br>insertFirst(){<br>  const node = new Node(data,this.head);<br>  this.head = node;<br>}</pre><p>In this insertFirst(), we insert an element in the head of the linked list and the previous element passing through to the insert element next. In this method we are not deleting elements, we are writing insert elements in the head linked list and linked list next passing through the previous element.</p><p>So if we now create, for example:</p><pre>list.insertFirst(16);</pre><p>So we are going to create <strong>Size() </strong>function in the Linked List class:</p><ul><li>All this below code type inside the linked list class.</li></ul><pre>Size(){<br>let counter=0;<br>let node = this.head;<br>while(node){<br> counter++;<br> node=node.next;<br>}<br>return counter;<br>}</pre><p>In this Size(), we are finding out the size of the Linked List. firstly initialize two temporary variables like counter and node. Let counter variable assign to zero and node equals to Linked List head. Using while loop, if the node is present counter will be increment and node will be assigned to node next part of Linked List. When condition false didn’t enter to the loop and return value counter. That will be the size of the Linked List.</p><p>So if we now create, for example:</p><pre>list.Size();</pre><p>So we are going to create the <strong>getFirst() </strong>function in the Linked List class:</p><pre>getFirst(){<br> return this.head;<br>}</pre><p>In this getFirst(), we are returning the first element of the Linked List. By simply returning the head of the Linked List. Because head will be assigned to starting element Linked List.</p><p>So if we now create, for example:</p><pre>list.getFirst();</pre><p>So we are going to create <strong>getLast()</strong> function in the Linked List class:</p><pre>getLast(){<br> if(!this.head){<br>  return null;<br> }<br>let node= this.head;<br>while(node){<br> if(!node.next){<br>  return node;<br>  }<br>node=node.next;<br>}</pre><p>In this getLast(), we are return the last element of the Linked List. In case first check in Linked list any element is there or not. If element is not there return null, otherwise element is there return last element Linked List. consider temporary variable node assign to head of linked list. Using while loop if there node is there going to the inside loop and again check for node next element. If node next element is not there return node, otherwise node next element is there node will be assigned to the next element of the node. Finally return the last element Linked List.</p><p>So we are now create, for example:</p><pre>list.getLast();</pre><p>So we are going to create <strong>clear()</strong> function in Linked List:</p><pre>clear(){<br> this.head=null;<br>}</pre><p>In this clear(), we are clear the Linked List. In this case simple assign head of the Linked List equal to null.</p><p>So we are now create, for example:</p><pre>list.clear();</pre><p>So we are going to create <strong>removeFirst() </strong>in Linked List:</p><pre>removeFirst(){<br> if(!this.head){<br>   return;<br> }<br>this.head= this.head.next;<br>}</pre><p>In this removeFirst(),we are going to remove first element in Linked List. So firstly check for the first element in linked list is there or not. If element is not there simple return null, otherwise we using reassign head linked list to the second element of the linked list. Due to first element linked list will over written to the second element of the linked list.</p><p>So we are now create, for example:</p><pre>list.removeFirst();</pre><p>So we are going to create <strong>removeLast()</strong> in Linked List:</p><pre>removeLast(){<br> if(!this.head){<br>  return ;<br> }<br>if(!this.head.next){<br> this.head=null;<br> }<br>let previous=this.head;<br>let node= this.head.next;<br>while(node){<br> previous=node;<br> node=this.head.next;<br> }<br>previous.head=null;<br>}</pre><p>In this removeLast(),we are going to remove last element in Linked List. So firstly check for the first element in linked list is there or not. If element is not there simple return null, otherwise check for the second element in linked list is there or not. If the no second element in linked list assign first element will be null. In case linked list contains more than two elements. Let consider two temporary like previous and node assign to the first and second elements of linked list respectively. In while loop will used to find out last and previous elements of the linked list.</p><p>I hope you like it and it will help to you.</p><p><strong>Great courses to learn Data Structures and Algorithms:</strong></p><ul><li><a href="https://www.udemy.com/learning-data-structures-in-javascript-from-scratch/">Learning Data Structures in JavaScript from Scratch</a></li></ul><p>Thank you 😃</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a5a4b1ed18e2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[E-mail Authentication Using Node.js REST APIs with JSON Web Tokens]]></title>
            <link>https://medium.com/@santhosh.pemmaka/e-mail-authentication-using-node-js-rest-apis-with-json-web-tokens-c916820b29f2?source=rss-767a8602a87b------2</link>
            <guid isPermaLink="false">https://medium.com/p/c916820b29f2</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Santhosh Reddy Pemmaka]]></dc:creator>
            <pubDate>Thu, 11 Oct 2018 18:02:56 GMT</pubDate>
            <atom:updated>2018-10-11T18:57:51.056Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Actually explain about user login any website .How the authentication works in the server side.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QB3OAu_8NckVCgjJFrk9Ig.png" /></figure><p>Have you ever wondered how authentication works? what’s behind of the complexity.Actually,nothing magic.When you login to any website using E-mail and Password.It’s a way of encrypting a value, in turn creating unique token that will be identifiers correct User (or) Not.How create token to user, I’ll explain everything below.</p><p>This will be step by step process first register and login to any website.The user E-mail and Password add token based authentication to an existing REST API. The authentication strategy in question is JWT(JSON web Token).</p><p>The official definition of JWT:</p><h4>JSON Web Token (JWT) is a compact, URL-safe means of representing claims to be transferred between two parties. The claim JSON Web Signature (JWS) structure or as the plain text of a JSON Web Encryption (JWE), enabling the claims to be digitally signed or integrity protected with a Message Authentication Code (MAC) and/or encrypted.</h4><p><a href="https://tools.ietf.org/html/rfc7519">-Internet Engineering Task Force(IETF)</a></p><p>In simple one statement looks like. A JWT is an encoded string of characters which is safe to send between two computers both have HTTPS. A token represent a value that is accessible only by the computer that is secret key which as encrypted.</p><p>Think in application view? Let’s consider user register and login account to website. After time user wants sign in to website.They send a request with the required credential such as email and password to the server. The server checks to see credential valid. If there, the server creates a token using the payload and secret key. Then server token send it back to the client. The client, in turn saves the token to use it in every other request the user will send.If user wants see their credential adding token to request headers is a way of authorizing the user credentials.</p><p>Okay,before going dirty your hands by coding. I will except you already basic ideal given below resources. If not, I am strongly recommend knowing resources before dying coding part. They are:</p><ul><li>Node.js</li><li>mlab</li><li>Visual Studio Code</li><li>Postman</li></ul><h4>Let’s start writing some code, shall we?</h4><p>well,, not yet actually. We need set up environment in the visual studio code looks like</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cdNvrcmZc0Y73UxOqXsBCQ.png" /></figure><p>In visual studio code editor left hand side will be location path folders,right hand side top window will be actually writing coding and below that terminal window actually install any packages and server running in this terminal.</p><p>Jump over to <a href="https://mlab.com/">mlab,</a> create an account on database, and open up your dashboard. Create a new, name a new database, name it as you wise and proceed to its configuration page. Add a database user to your database and copy the connection string from the dashboard to your code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xy6yKvquamaRziyI2j7N2Q.png" /></figure><p>All you need to do now is to change the placeholder values for &lt;duser&gt; and &lt;dbpassword&gt;. Replace them with in the username and password of the user create for the database.</p><h4>Finally, some code.</h4><p>Well, first of all we want to add user authentication. Meaning, implement a system for registering and logging user in. In Visual studio code terminal window type below sentence one after one execution once completed.</p><pre>npm init<br>npm i express gravatar bcryptjs jsonwebtoken passport<br>npm i validator mongoose body-parser</pre><p>Let’s open file in visual studio code server.js. Here server.js is name of the file,name it as you wise.In above all the packages install in the package.json. All the packages used in your code and also REST api call used crud application.</p><p><em>You should see Express server listening on port 5000 get logged to the terminal.</em></p><ul><li>Add this demo piece of code to the top the server.js</li></ul><pre>const express = require(&quot;express&quot;);<br>const mongoose = require(&quot;mongoose&quot;);</pre><pre>const app=express();</pre><pre>app.get(&#39;/&#39;,(req,res)=&gt;{<br> res.json({message:&quot;Get posts working&quot;});<br>}</pre><pre>const port = process.env.PORT || 5000;</pre><pre>app.listen(port, () =&gt; console.log(`Server running on port&#39;+port);</pre><p>Save file run in the terminal window type by below sentence.</p><pre>$ node server</pre><p>Let’s say the user I created for the database is named awesome with a password of awesome123. Having that in mind, the keys.js that willl be open new folder in that save file.</p><ul><li>Add piece of code adding to keys.js</li></ul><pre>module.exports = {<br>  mongoURI:<br>    &quot;mongodb://awesome:<a href="/service/mailto:santhoshjnv1@ds121183.mlab.com">awesome123@ds121183.mlab.com</a>:21183/create&quot;,<br>  secretOrKey: &quot;secret&quot;<br>};</pre><ul><li>Add remaining piece of code to server.js</li></ul><pre>const express = require(&quot;express&quot;);<br>const mongoose = require(&quot;mongoose&quot;);<br>const bodyParser = require(&quot;body-parser&quot;);<br>const passport = require(&quot;passport&quot;);</pre><pre>const users = require(&quot;./routers/api/users&quot;);</pre><pre>const app = express();</pre><pre>// Passport middleware<br>app.use(passport.initialize());</pre><pre>// Passport Config<br>require(&quot;./config/passport&quot;)(passport);<br>// Body parser middleware<br>app.use(bodyParser.urlencoded({ extended: false }));<br>app.use(bodyParser.json());</pre><pre>// DB Config<br>const db = require(&quot;./config/keys&quot;).mongoURI;</pre><pre>// Connect to MongoDB<br>mongoose<br>  .connect(db)<br>  .then(() =&gt; console.log(&quot;MongoDB Connected&quot;))<br>  .catch(err =&gt; console.log(err));</pre><pre>// Use Routes<br>app.use(&quot;/api/users&quot;, users);</pre><pre>const port = process.env.PORT || 5000;</pre><pre>app.listen(port, () =&gt; console.log(`Server running on port&#39;+port);</pre><p><strong><em>Now entering into user register in any website</em></strong><em>.</em></p><p>Let’s start you’re ready to start adding the register logic.Create a folder named routers in that other folder api and start out by adding a file named<strong> users.js.</strong></p><ul><li>Add this piece of code to the top of the <strong>users.js</strong></li></ul><pre>const express = require(&quot;express&quot;);<br>const router = express.Router();<br>const gravatar = require(&quot;gravatar&quot;);<br>const bcrypt = require(&quot;bcryptjs&quot;);<br>const jwt = require(&quot;jsonwebtoken&quot;);<br>const keys = require(&quot;../../config/keys&quot;);<br>const passport = require(&quot;passport&quot;);</pre><pre>const User = require(&quot;../../models/User&quot;);</pre><p>That’s all the module we need to implement our desired register. Now you’re ready to create a <em>/register </em>endpoint. Add this piece of code to your <strong>users.js.</strong></p><pre>// Load Input Validation<br>const validateRegisterInput = require(&quot;../../validation/register&quot;);<br>const validateLoginInput = require(&quot;../../validation/login&quot;);</pre><pre>router.post(&quot;/register&quot;, (req, res) =&gt; {<br>  const { errors, isValid } = validateRegisterInput(req.body);</pre><pre>// Check Validation<br>  if (!isValid) {<br>    return res.status(400).json(errors);<br>  }</pre><pre>User.findOne({ email: req.body.email }).then(user =&gt; {<br>    if (user) {<br>      errors.email = &quot;Email already exists&quot;;<br>      return res.status(400).json(errors);<br>    } else {<br>      const avatar = gravatar.url(https://medium.com/req.body.email,%20%7B%3Cbr%3E%20%20%20%20%20%20%20%20s:%20&quot;200&quot;,%20//%20Size%3Cbr%3E%20%20%20%20%20%20%20%20r:%20&quot;pg&quot;,%20//%20Rating%3Cbr%3E%20%20%20%20%20%20%20%20d:%20&quot;mm&quot;%20//%20Default%3Cbr%3E%20%20%20%20%20%20%7D);</pre><pre>const newUser = new User({<br>        name: req.body.name,<br>        email: req.body.email,<br>        avatar,<br>        password: req.body.password<br>      });</pre><pre>bcrypt.genSalt(10, (err, salt) =&gt; {<br>        bcrypt.hash(newUser.password, salt, (err, hash) =&gt; {<br>          if (err) throw err;<br>          newUser.password = hash;<br>          newUser<br>            .save()<br>            .then(user =&gt; res.json(user))<br>            .catch(err =&gt; console.log(err));<br>        });<br>      });<br>    }<br>  });<br>});<br>module.exports = router;</pre><ul><li>Let’s start you’re ready to start adding the <strong>User Schema</strong>.Create a folder named <strong>models </strong>start out by adding a file named <strong>User.js. </strong>In this User Schema what are the minimum requirements of the user information that create register to website.</li><li><em>Add this piece of code to the </em><strong><em>User.js.</em></strong></li></ul><pre>const mongoose = require(&quot;mongoose&quot;);<br>const Schema = mongoose.Schema;</pre><pre>// Create Schema<br>const UserSchema = new Schema({<br>  name: {<br>    type: String,<br>    required: true<br>  },<br>  email: {<br>    type: String,<br>    required: true<br>  },<br>  password: {<br>    type: String,<br>    required: true<br>  },<br>  avatar: {<br>    type: String<br>  },<br>  date: {<br>    type: Date,<br>    default: Date.now<br>  }<br>});</pre><pre>module.exports = User = mongoose.model(&quot;users&quot;, UserSchema);</pre><h4>Let’s test in the Postman.</h4><p>open up your REST API testing tool of choice,I use <a href="https://www.getpostman.com/">Postman</a> if any other application like Insomnia free to use.</p><p>If check it once server running or not in terminal window. If not running terminal window type.</p><pre>$ node server<br></pre><p>Open up postman and hit the register endpoint <strong>(/api/users/register). </strong>Make sure to pick POST method and <strong>x-www-form-url-encoded. </strong>Now, if not adding anything in the body. Showing minimum requirement information about user mandatory looks like given below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Mu6b_5341Xfk-fGvPdTQzg.png" /></figure><p>Let’s added body information like name,email,password and after that again send request in the postman.It’s looks like register completed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ikVv4noMTKTBIl981iNicA.png" /></figure><p><strong><em>Now entering into user login in website</em></strong></p><p>Let’s start you’re ready to start adding the login logic.Create a folder named routers in that other folder api and start out by adding a file named<strong> users.js.</strong></p><ul><li>Add this piece of code to the top of the <strong>users.js</strong></li></ul><pre>// <a href="http://twitter.com/route">@route</a>   GET api/users/login<br>// <a href="http://twitter.com/desc">@desc</a>    Login User / Returning JWT Token<br>// <a href="http://twitter.com/access">@access</a>  Public</pre><pre>router.post(&quot;/login&quot;, (req, res) =&gt; {<br>  const { errors, isValid } = validateLoginInput(req.body);</pre><pre>// Check Validation<br>  if (!isValid) {<br>    return res.status(400).json(errors);<br>  }</pre><pre>const email = req.body.email;<br>  const password = req.body.password;</pre><pre>// Find user by email<br>  User.findOne({ email }).then(user =&gt; {<br>    // Check for user<br>    if (!user) {<br>      errors.email = &quot;User not found&quot;;<br>      return res.status(404).json(errors);<br>    }</pre><pre>// Check Password<br>    bcrypt.compare(password, user.password).then(isMatch =&gt; {<br>      if (isMatch) {<br>        // User Matched<br>        const payload = { id: user.id, name: user.name, avatar: user.avatar }; // Create JWT Payload</pre><pre>// Sign Token<br>        jwt.sign(<br>          payload,<br>          keys.secretOrKey,<br>          { expiresIn: 3600 },<br>          (err, token) =&gt; {<br>            res.json({<br>              success: true,<br>              token: &quot;Bearer &quot; + token<br>            });<br>          }<br>        );<br>      } else {<br>        errors.password = &quot;Password incorrect&quot;;<br>        return res.status(400).json(errors);<br>      }<br>    });<br>  });<br>});<br>module.exports = router;</pre><h4>Let’s test in the Postman.</h4><p>open up your REST API testing tool of choice,I use <a href="https://www.getpostman.com/">Postman</a> if any other use any other application like Insomnia free to use.</p><p>If check it once server running or not in terminal window. If not running terminal window type.</p><pre>$ node server</pre><p>Open up postman and hit the register endpoint <strong>(/api/users/login). </strong>Make sure to pick POST method and <strong>x-www-form-url-encoded. </strong>After implementing the registration, we should create a way for existing users to log in. Server check user login credential matches users register stored in database. If they match we <strong>.sign() </strong>a token. That’s pretty much it. Let’s try it out.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qQeWI3nPgfQnRS7Ic7rLVA.png" /></figure><p>Let’s start you’re ready to start adding the password logic.Create a folder named <strong>config</strong> start out by adding a file named<strong> password.js.</strong></p><ul><li>Add this piece of code to the top of the <strong>password.js</strong></li></ul><pre>const JwtStrategy = require(&quot;passport-jwt&quot;).Strategy;<br>const ExtractJwt = require(&quot;passport-jwt&quot;).ExtractJwt;<br>const mongoose = require(&quot;mongoose&quot;);<br>const User = mongoose.model(&quot;users&quot;);<br>const keys = require(&quot;../config/keys&quot;);</pre><pre>const opts = {};<br>opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();<br>opts.secretOrKey = keys.secretOrKey;</pre><pre>module.exports = passport =&gt; {<br>  passport.use(<br>    new JwtStrategy(opts, (jwt_payload, done) =&gt; {<br>      User.findById(jwt_payload.id)<br>        .then(user =&gt; {<br>          if (user) {<br>            return done(null, user);<br>          }<br>          return done(null, false);<br>        })<br>        .catch(err =&gt; console.log(err));<br>    })<br>  );<br>};</pre><p>Let’s start you’re ready to start adding the login logic.Create a folder named routers in that other folder api and start out by adding a file named <strong>users.js </strong>In this check authenticate work retrieve user data from the token.</p><ul><li>Add this piece of code to the top of the <strong>users.js</strong></li></ul><pre>// <a href="http://twitter.com/route">@route</a>   GET api/users/current<br>// <a href="http://twitter.com/desc">@desc</a>    Return current user<br>// <a href="http://twitter.com/access">@access</a>  Private<br>router.get(<br>  &quot;/current&quot;,<br>  passport.authenticate(&quot;jwt&quot;, { session: false }),<br>  (req, res) =&gt; {<br>    res.json({<br>      id: req.user.id,<br>      name: req.user.name,<br>      email: req.user.email<br>    });<br>  }<br>);<br>module.exports = router;</pre><h4>Let’s test in the Postman.</h4><p>Open up postman and hit the current endpoint <strong>(/api/users/current). </strong>Make sure to pick GET method.<strong> </strong>After implementing login user we generating token. The token added to the <strong>Headers</strong> part, in the body part make sure <strong>Key </strong>will be Authorization added and <strong>value </strong>will be token of the user added.This is way user check information obtain token. It’s working awesome.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*igU7JanP7v_tsk7Swij0QA.png" /></figure><h4>Wrap Up</h4><p>I am sharing my experience. Some of the concepts are hard to understand. I am recommend you go through the code by yourself and try your best to get it to work.Again, here’s <a href="https://github.com/santhoshpemmaka/node">here’s the GitHub repository</a>.This first blog writing medium.com if any mistakes in the blog sorry for that.</p><p><em>Do you think this tutorial will be of help to someone? Do not hesitate to share.</em></p><p>Thank you for sending your valuable time reading in this Blog. 👍 👍 👍</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c916820b29f2" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>