<?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 Srinivasa Varanasi on Medium]]></title>
        <description><![CDATA[Stories by Srinivasa Varanasi on Medium]]></description>
        <link>https://medium.com/@MrVSiK?source=rss-8ea8ad9241fb------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*zg_CmZCmbMYOeL6QUS3UEA.jpeg</url>
            <title>Stories by Srinivasa Varanasi on Medium</title>
            <link>https://medium.com/@MrVSiK?source=rss-8ea8ad9241fb------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 07:00:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@MrVSiK/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[Integrating Redis into your Nodejs web app]]></title>
            <link>https://medium.com/@MrVSiK/integrating-redis-into-your-nodejs-web-app-ecde3f97d66?source=rss-8ea8ad9241fb------2</link>
            <guid isPermaLink="false">https://medium.com/p/ecde3f97d66</guid>
            <category><![CDATA[api]]></category>
            <category><![CDATA[cache]]></category>
            <category><![CDATA[redis]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[expressjs]]></category>
            <dc:creator><![CDATA[Srinivasa Varanasi]]></dc:creator>
            <pubDate>Sat, 15 Jan 2022 18:32:41 GMT</pubDate>
            <atom:updated>2022-01-15T18:32:41.199Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Qdf__QB0INBtDcK_iPalzA.png" /></figure><p>In 2009, an Italian software engineer Salvatore Sanfilippo was trying to improve the scalability of a start-up. After facing significant problems in scaling some types of workloads he began to prototype a concept version of Redis. After implementing the first data type, the list, in C, Sanfilippo decided to open source it.</p><p>Redis, or REmote DIctionary Server, is an in-memory data structure store, typically used as an in-memory key-value database or cache. Redis popularized the idea of a system that can be considered at the same time a store and a cache, using a design where data is always modified and read from the main computer memory, but also stored on disk in a format that is unsuitable for random access of data, but only to reconstruct the data back in memory once the system restarts.</p><p>In 2021, Redis is one of the most popular databases, used by several multinational companies such as GitHub, Twitter, Snapchat, etc. It boasts a huge community of developers and open-source contributors.</p><h3>Using Redis in your web app</h3><p>Redis is often used as a secondary database, working in parallel with a primary database, to cache data. Due to its immense popularity, most frameworks can integrate very easily with Redis (locally or on the cloud).</p><p>Let us make a simple web app using Nodejs and Redis to understand its role.</p><h3>Requirements</h3><ol><li><a href="https://docs.microsoft.com/en-us/windows/dev-environment/javascript/nodejs-on-wsl">Nodejs</a></li><li><a href="https://ubuntu.com/wsl">Windows Subsystem for Linux 2</a></li><li><a href="https://redis.io/topics/quickstart">Redis</a></li></ol><p><strong>Note: Redis does not officially support Windows, however, with the release of Windows Subsystem for Linux Windows users can use Redis inside their Linux terminal.</strong></p><h3>Setting up the project</h3><p>We are going to make a simple API that will respond with information about the country requested. It will fetch the information about the requested country from free and open-source API <a href="https://restcountries.eu/">RESTCountries</a>.</p><p>To start, open the terminal and create a new folder for this project and run</p><pre>npm init -y</pre><p>It will initialize a npm project inside the current directory.</p><p>Then run the following command to create <em>index.js</em></p><pre>touch index.js</pre><p>To store HTML files, create a public folder,</p><pre>mkdir public</pre><p>Given below is the current file hierarchy,</p><pre>|<br>|___ public/<br>|___ index.js<br>|___ package.json</pre><p>To make an API, we need to install some modules, namely express, redis and axios. Express is a minimal and flexible Nodejs web application framework that provides a robust set of features for web and mobile applications, redis provides a suite of tools to connect and integrate with Redis locally or on the cloud, axios is a promise-based HTTP client for the browser and nodejs.</p><p>To install all of these packages in one go, run</p><pre>npm i express axios redis</pre><p>This will create a new file <em>package-lock.json</em> and a new folder <em>node_modules</em>. The current file hierarchy is</p><pre>|___ node_modules/<br>|___ public/<br>|___ index.js<br>|___ package.json<br>|___ package-lock.json</pre><p>Finally, install nodemon as a dev dependency.</p><pre>npm i -D nodemon</pre><p>Open the current directory in your IDE or Text Editor of choice. VS Code users can run the following command to open the current directory in VS Code.</p><pre>code .</pre><h3>Setting up index.js</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/47f087da41099241ad566ff3525a51fe/href">https://medium.com/media/47f087da41099241ad566ff3525a51fe/href</a></iframe><p>We have set up an express instance and declared the <em>public</em> folder as the static file folder followed by ensuring that the app listens for requests at PORT 3000.</p><p>Upon running the <em>npm test</em> in the command line, we see nodemon start the server with the following output.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/557/1*MEJMXhaGh2luKSalVY-LzQ.png" /><figcaption>Nodemon Output (image by author)</figcaption></figure><p>We can now define routes for express to handle.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/18caf410b26439cbfe510bb78ee01f95/href">https://medium.com/media/18caf410b26439cbfe510bb78ee01f95/href</a></iframe><p>We have defined GET requests for express to handle at 2 different routes. One of the GET requests uses a dynamic route which will help us capture the requested country name.</p><p>As you can see, the response to the GET request at route “/” is an HTML file located inside the <em>public</em> folder. Hence, let’s create an HTML file named “result.html” inside the public folder. The HTML code is given below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dac9deece5e85b56e657af94fd38275b/href">https://medium.com/media/dac9deece5e85b56e657af94fd38275b/href</a></iframe><p>Let’s start the server by running <em>npm test.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/545/1*6z8mZrIcwYAe7zedrj_xOw.png" /><figcaption>nodemon output (image by author)</figcaption></figure><p>Open a browser and navigate to <em>localhost:3000.</em> We should see the html page rendered.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qP7-uv4JsYCDhJsZSxhrbw.png" /><figcaption>Rendered HTML page (image by author)</figcaption></figure><p>If we type in “India” in the text field and click on the search button, we will see that “Requested country is:- India” is logged to the console indicating that both GET requests are being handled perfectly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/370/1*7GpZ0L1DWUhMAuQtmNunPw.png" /><figcaption>Output when requested country is India (image by author)</figcaption></figure><h3>Fetching data</h3><p>We are going to use <a href="https://restcountries.eu/">Rest Countries API</a> to fetch data. For this purpose, we are going to use axios as it is easy to use.</p><p>Coming back to index.js, we are going to import axios and make a GET request to <a href="https://restcountries.eu/rest/v2/name/">https://restcountries.eu/rest/v2/name/</a>{name}. We can replace {name} with the name of the country we want information about.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/69eef893f62d96200a8db5eca34c17af/href">https://medium.com/media/69eef893f62d96200a8db5eca34c17af/href</a></iframe><p>With this done, restart the server and we can now search for any country’s information. The time taken for the response to arrive is also displayed above the table.</p><p>If we continue to make requests, we notice that the time taken for the response to arrive fluctuates between 300 ms and 500 ms. While this is not too bad, if we have data that is constantly being requested but not often changed, we can implement a much faster way to handle requests by caching that data. So let’s try that here using Redis.</p><h3>Integrating Redis into our app</h3><p>To start the Redis server locally, we can open a terminal and run</p><pre>redis-server</pre><p>By default, the server runs on PORT 6379.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bgUnFXKuxeKJRIXC-p3eqg.png" /><figcaption>redis-server command output (image by author)</figcaption></figure><p>Moving onto our index.js file, we can now import redis and create a client as shown in the code below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3858e1a78ee6bb3417d11f453383b37b/href">https://medium.com/media/3858e1a78ee6bb3417d11f453383b37b/href</a></iframe><p>Redis stores data in a key-value store. To <em>set</em> data (or <em>store</em> data) we call the <em>.set()</em> method of the client and to <em>get</em> data(or <em>retrieve</em> data) we call the <em>.get()</em> method of the client.</p><p>Inside the second GET request, we need to check if Redis has the data and if it does then we send that data instead of fetching it from the given API. If Redis does not have the required data then we fetch it from the API, store it in Redis and send it back to the user.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f9d561a29a69275cf52f57aad8e469f7/href">https://medium.com/media/f9d561a29a69275cf52f57aad8e469f7/href</a></iframe><p>Start the server using <em>npm test</em> and make a new request. The first time the request is made, it will take the usual amount of time (high 300ms to low 500ms), but every request following it will take less than 30ms, 10–15 times faster.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgfycat.com%2Fifr%2Fmammothtiredliger&amp;display_name=Gfycat&amp;url=https%3A%2F%2Fgfycat.com%2Fmammothtiredliger&amp;image=https%3A%2F%2Fthumbs.gfycat.com%2FMammothTiredLiger-size_restricted.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=gfycat" width="1920" height="1080" frameborder="0" scrolling="no"><a href="https://medium.com/media/2443b7b2b4509667f701a924a685c5cd/href">https://medium.com/media/2443b7b2b4509667f701a924a685c5cd/href</a></iframe><p>There you have it, our web app is done. The full code is available on my GitHub linked below.</p><p><a href="https://github.com/MrVSiK/redis.git">GitHub - MrVSiK/redis: A simple web app made to demonstrate the caching abilities of Redis</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ecde3f97d66" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What are Javascript Promises?]]></title>
            <link>https://medium.com/adg-vit/what-are-javascript-promises-cd0314de9eab?source=rss-8ea8ad9241fb------2</link>
            <guid isPermaLink="false">https://medium.com/p/cd0314de9eab</guid>
            <category><![CDATA[web]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[promises]]></category>
            <category><![CDATA[code]]></category>
            <category><![CDATA[asynchronous]]></category>
            <dc:creator><![CDATA[Srinivasa Varanasi]]></dc:creator>
            <pubDate>Mon, 20 Sep 2021 04:37:10 GMT</pubDate>
            <atom:updated>2021-09-20T04:37:10.079Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nsZMwq7vQXJaEI78yUob1g.jpeg" /><figcaption>Javascript Promises</figcaption></figure><p>A <em>Promise</em> is a proxy for a value that has not been computed yet. It allows a process to run side-by-side or <em>asynchronously</em> on the premise that upon completion, would return an immutable value or an error.</p><p>At any given point in time, a promise can be in one of three states:</p><p>· Pending: <em>initial state, neither resolved nor rejected</em></p><p>· Resolved or Fulfilled: <em>process has been completed successfully</em></p><p>· Rejected: <em>process has failed and returned an error</em></p><p>A pending Promise can be resolved with a value or rejected with an error. With ES6, a new Promise can be created using the following syntax:</p><pre>new Promise((resolve,reject) =&gt; {…})</pre><p>The above-mentioned method takes one argument in the form of a function with two predefined parameters <em>resolve</em> and <em>reject</em> that can be used to resolve a<br> pending promise or reject a pending promise respectively.</p><p>Example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/02c7240f74808b79b3a970dc9a1feb20/href">https://medium.com/media/02c7240f74808b79b3a970dc9a1feb20/href</a></iframe><p>Handlers can be chained to the Promise using the <em>Promise.prototype.then(), Promise.prototype.catch()</em> and <em>Promise.prototype.finally()</em> methods.</p><h3><strong>Promise.prototype.then()</strong></h3><p><em>.then()</em> is invoked when a promise is fulfilled or rejected. It takes 2 functions as arguments. One to handle resolved promise state and the other to handle rejected promise state(optional).</p><pre>Promise(….).then(handleResolved, handleRejected)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/761/1*kzj-sGPNeNzWbbBLXZMb-A.png" /><figcaption>Representation of <em>.then</em>() method</figcaption></figure><p>Example:-</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/239d6c2c01d556ecf32612e14a65c0b0/href">https://medium.com/media/239d6c2c01d556ecf32612e14a65c0b0/href</a></iframe><h3>Promise.prototype.catch()</h3><p><em>.catch()</em> method deals with rejected states only. It is a shorthand for<br> <em>.then(NULL,errorHandler)</em> as it internally calls <em>.then(NULL, errorHandler)</em> method.</p><pre>.catch((error) =&gt; {…})</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/788/1*qOKAilhQKuc0imFv37IQFw.png" /><figcaption>Representation of .catch() method</figcaption></figure><p>Example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5a844e6699961c8fe6b5dce5ccfabe37/href">https://medium.com/media/5a844e6699961c8fe6b5dce5ccfabe37/href</a></iframe><h3>Promise.prototype.finally()</h3><p><em>.finally()</em> method is executed when the <em>Promise</em> is settled, .i.e. fulfilled or rejected. This provides a way to execute code once the <em>Promise</em> has been dealt with.</p><pre>.finally(() =&gt; {...})</pre><p>Example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/06f1beb2eb581b0c3cd528359b7ec787/href">https://medium.com/media/06f1beb2eb581b0c3cd528359b7ec787/href</a></iframe><h3>Chaining Promises</h3><p><em>Promise.prototype.then(), Promise.prototype.catch() </em>and <em>Promise.prototype.finally() </em>return a new promise allowing multiple handlers to be chained to a promise.</p><p>Example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/03bc6d4fd47856558e1e8f99fc1ae060/href">https://medium.com/media/03bc6d4fd47856558e1e8f99fc1ae060/href</a></iframe><p>Let’s take a closer look at what’s happening here. The <em>Promise</em> p can settle into 1 of 2 states — <em>resolved </em>or <em>rejected.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/611/1*vOQlRJzZS9HL0wWirvAhmQ.png" /></figure><h4><strong>Case 1: p settles into a resolved state</strong></h4><p>In this case, the message “I am greater than half” is passed to the first <em>.then()</em> method. Since the promise has been resolved, the first argument to the <em>.then()</em> method is called with the parameter <em>resolved</em> to be the initial message itself.</p><p>Any value <strong>returned</strong> from <em>.then() </em>is interpreted to be the product of a <em>resolved promise, </em>meaning that the value returned from the first argument of the first <em>.then()</em> method will be passed to the first argument of the second <em>.then()</em> function.</p><p>Finally, the <em>.finally()</em> method would be called since <em>.catch()</em> cannot handle <em>resolved</em> state and therefore would be effectively skipped.</p><p>Output:</p><pre>inside first .then() method --- I am greater than half<br>inside second .then() method --- This has been resolved<br>Execution has been completed</pre><h4>Case 2: p settles into a rejected state</h4><p>In this case, the message “I am less than half” is passed to the first <em>.then()</em> method. Since the promise has been rejected, the second argument to the <em>.then()</em> method is called with the parameter <em>rejected</em> being the initial message itself.</p><p>Any value <strong>returned</strong> from <em>.then() </em>is interpreted to be the product of a <em>resolved promise, </em>meaning that the value returned from the second argument of the first <em>.then()</em> method will be passed to the first argument of the <em>.then()</em> method</p><p>Finally, the <em>.finally()</em> method would be called.</p><p>Output:</p><pre>inside first .then() method and rejected ---I am less than half<br>inside second .then() method --- This has been rejected<br>Execution has been completed</pre><h4>Case 3: Let’s slightly modify the example</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/56973ed3260b441734655f5e45d6e3ff/href">https://medium.com/media/56973ed3260b441734655f5e45d6e3ff/href</a></iframe><p>As you can see from line 11, a new <em>error</em> is being thrown by the resolve function in the first <em>.then() </em>method. Hence, when p settles into a <em>resolved</em> state, a new <em>error</em> is thrown which can only be handled by the <em>.catch()</em> method.</p><p><em>.finally()</em> is called at the end and the execution ends.</p><p>Output:</p><pre>inside first .then() method --- I am greater than half<br>Inside .catch() method --- Error: This is an error<br>Execution has been completed</pre><h3>Applications of Javascript Promises</h3><p>With JavaScript Promises you can manage multiple asynchronous operations easily, whereas callbacks can create callback hell leading to unmanageable code. Moreover, promises give you an incredibly powerful way to handle asynchronous operations.</p><p>If you wish to go through the whole code do follow the below given GitHub Repo link:</p><p><a href="https://github.com/MrVSiK/Javascript-Promises.git">GitHub - MrVSiK/Javascript-Promises</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cd0314de9eab" width="1" height="1" alt=""><hr><p><a href="https://medium.com/adg-vit/what-are-javascript-promises-cd0314de9eab">What are Javascript Promises?</a> was originally published in <a href="https://medium.com/adg-vit">ADGVIT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>