<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mayur Aitavadekar</title>
    <description>The latest articles on DEV Community by Mayur Aitavadekar (@mayuraitavadekar).</description>
    <link>https://dev.to/mayuraitavadekar</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F360314%2Fc2012461-54cd-4895-a46e-5a9a88de09cc.jpeg</url>
      <title>DEV Community: Mayur Aitavadekar</title>
      <link>https://dev.to/mayuraitavadekar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mayuraitavadekar"/>
    <language>en</language>
    <item>
      <title>Where to store some important data fetched from API so that I can use it in multiple routes?</title>
      <dc:creator>Mayur Aitavadekar</dc:creator>
      <pubDate>Thu, 09 Jul 2020 16:38:03 +0000</pubDate>
      <link>https://dev.to/mayuraitavadekar/where-to-store-some-important-data-fetched-from-api-so-that-i-can-use-it-in-multiple-routes-424d</link>
      <guid>https://dev.to/mayuraitavadekar/where-to-store-some-important-data-fetched-from-api-so-that-i-can-use-it-in-multiple-routes-424d</guid>
      <description>&lt;p&gt;I am developing OTP authentication. So here my routes in express -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require("express");
const router = require("express").Router();
const rp = require("request-response");

router.post("/sendOTP", (req, res) =&amp;gt; {

const { mobile_number }  = req.body; // receive the mobile number

const otp = randomize("0000"); // generated 4 digit otp

options = {
     method: "GET", 
     url: "xxxxx/xxxxx/xxxxx/${otp}",   // calling my otp service provider to send otp to my user
}

rp(options).then((response)=&amp;gt;{

       // response.details contains the unique otp session id which I have to use in another route where I am verifying otp
}).catch((err) =&amp;gt; console.log(err));
});

router.post("/getOTP", (req, res) =&amp;gt; {

// user will put otp he received from his mobile

const { otp }  = req.body; // receive the otp

options = {
     method: "GET", 
     url: "xxxxx/xxxxx/xxxxx/${otp}/${otp_session_id}",   // calling my otp service provider with otp and session id of otp
}

rp(options).then((response)=&amp;gt;{
        console.log(response) // otp verified.

}).catch((err) =&amp;gt; console.log(err));
});

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



&lt;p&gt;this is simple code. Now my problem is - how can I store that otp_session_id so that I can use it in two routes &lt;code&gt;sendOTP&lt;/code&gt; and &lt;code&gt;getOTP&lt;/code&gt; ? I used cookieParser, cookieSession, expressSession, nothing worked out. please any simple steps / solutions will be great. &lt;/p&gt;

&lt;p&gt;thanks :)&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Building server with express</title>
      <dc:creator>Mayur Aitavadekar</dc:creator>
      <pubDate>Sat, 04 Apr 2020 09:04:00 +0000</pubDate>
      <link>https://dev.to/mayuraitavadekar/building-server-with-express-5bcj</link>
      <guid>https://dev.to/mayuraitavadekar/building-server-with-express-5bcj</guid>
      <description>&lt;p&gt;&lt;em&gt;I’ve divided this article into three parts. In the first part, I’ve explained HTTP. In the second part, I’ve explained basics of express and finally I’ve written about middleware. So here you go..&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  HTTP guide 🌎
&lt;/h2&gt;

&lt;p&gt;HTTP is hyper text transfer protocol which is one of the most popular protocol in the internet. HTTP works on request and response cycle from client to server and server to client. Every request is independent of other and hence it is also called as stateless protocol. Let’s understand the format of HTTP request and HTTP response.&lt;/p&gt;

&lt;h4&gt;
  
  
  Format of HTTP request 👇
&lt;/h4&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;|--------------------|             
|   request line     |  
|--------------------|     
|   request headers  |           
|--------------------|    
|   request Message  |     
|        body        |     
|--------------------|
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Format request line 👇
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;request-method-name&amp;lt;space&amp;gt;request URL&amp;lt;space&amp;gt;HTTP version
GET /signout HTTP/1.0
POST /signin HTTP/1.1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, lets talk about HTTP request methods. Following are HTTP request methods using them you can interact with server over HTTP.&lt;br&gt;
GET : to get resources from server.&lt;br&gt;
POST : to send resources to the server.&lt;br&gt;
PUT : to send data to server to update existing resource.&lt;br&gt;
HEAD : to get resources from server without response body. Only request-line and request-header will be fetched.&lt;br&gt;
DELETE : to delete specified resource in the server.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Format of request headers 👇&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Request headers are in the form of key-value pairs. Request header is nothing but metadata of entire request.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Host : www.abc.com
Connection : keep-alive
Accept-encoding : gzip, deflate, br
Accept-Language: us-en, fr, cn
Accept : */*
Content-Type : application/json
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Format of request body 👇&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The HTTP requests messages can be of type depending upon what you specify in HTTP header content type. They can be json, url-encoded, form-data, XML, text, etc.&lt;br&gt;
Now, let’s understand what is the format of HTTP response.&lt;/p&gt;
&lt;h4&gt;
  
  
  Format of HTTP response 👇
&lt;/h4&gt;


&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;|--------------------|             
|   status line      |  
|--------------------|     
|   reponse header   |
|--------------------|     
|  response Message  |     
|        body        |     
|--------------------|
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Format of status line 👇
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;HTTP-version&amp;lt;space&amp;gt;status-code&amp;lt;space&amp;gt;reason-phrase
HTTP/1.1  200  OK
HTTP/1.0  404  NOT FOUND
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Format of response headers 👇
Response headers are similar to request headers. They are in the form of key-value pairs.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Date : Thu, 02 Apr 2020 10:29:02 GMT
Connection : keep-alive
Content-Length : 252
Content-Type : application/json
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Format of response body
Like request message’s body, response message’s body can be of any type such as json, text, html, etc. It basically depends on how developer wants to fetch the data from the server.
Remember those HTTP response codes 😺.&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  Express.js 🤔
&lt;/h2&gt;

&lt;p&gt;Installing Express over the Node.js is simple. &lt;a href="https://expressjs.com/en/starter/installing.html"&gt;Click here&lt;/a&gt; to see how to install.&lt;/p&gt;

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

&lt;p&gt;Express is small, fast and minimalist Node.js web framework. It is mainly used for designing and developing back-end/server-side of web apps.&lt;br&gt;
Here is simplest server developed using Express.&lt;/p&gt;

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

&lt;p&gt;Above code is very much self explanatory.&lt;br&gt;
‘&lt;em&gt;app&lt;/em&gt;’ instance can call the various methods in express library. These methods include HTTP requests( GET, PUT, POST,DELETE,HEAD), use(), listen(), etc. Also if you include any third party libraries, ‘&lt;em&gt;app&lt;/em&gt;’ instance will execute their methods too.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// EXECUTING HTTP GET REQUEST
app.get("name-of-route",callback-function);
// START LISTENING THE RESPONSE OF REQUEST ON THE SPECIFIED PORT
app.listen(PORT, callback-function);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  Structure of Callbaks in Express
&lt;/h4&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(req,res) =&amp;gt; {
    /* 
        ... server-side operations
    */ 

    res.send("sending appropriate the response"); 
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;req&lt;/strong&gt; and &lt;strong&gt;res&lt;/strong&gt; parameters in callback function are request and response in HTTP which we discussed in the first part.&lt;/p&gt;




&lt;h2&gt;
  
  
  Express Middleware 🙌
&lt;/h2&gt;

&lt;p&gt;As clearly mentioned on official website, ‘express is routing and middle-ware web framework.’ Handling middleware is one of the core functionalities of Express.&lt;/p&gt;

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

&lt;p&gt;In simple language, whatever functions are written in between request-response cycle, they are known as middleware functions. Notice ‘&lt;strong&gt;next&lt;/strong&gt;’ keyword which is necessary to pass the execution from one middleware to another. This keeps cycle running otherwise you will get error. Now, one thing to note here — In custom middleware from third parties, you need not to write next() because it automatically includes it from their source code.&lt;/p&gt;

&lt;p&gt;Following are some of the types of middleware and there names are self explanatory. When building large scale applications, we can include these middleware in above server code.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Application middleware&lt;/li&gt;
&lt;li&gt;Third Party middleware ( body-parser, cookie-parser )
You can install these using npm.&lt;/li&gt;
&lt;li&gt;Built In middleware&lt;/li&gt;
&lt;li&gt;Error Handling middleware&lt;/li&gt;
&lt;li&gt;Router Level middleware&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can learn a lot by reading &lt;a href="https://expressjs.com/en/guide/writing-middleware.html"&gt;official docs&lt;/a&gt; on express.&lt;/p&gt;

&lt;p&gt;Now, let’s write simple middle-ware so that you can get basic idea about how does the middle-ware works? I’ll include the code in above written code and Then run the server.&lt;/p&gt;

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

&lt;p&gt;After running the above code, we get the following output on route “/” which is starting point of web app.&lt;/p&gt;

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

&lt;p&gt;Then, we get following output on route “/client”. Here, first, client pass the execution to the middleware. You can see in the console. There will be message “the middleware is running”. After it’s execution the client will run and we can see output in the browser.&lt;/p&gt;

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

&lt;p&gt;Now, depending upon requirement of designing the back-end of web application, you can write middleware in web application.&lt;br&gt;
That’s it. I hope you’ve got rough idea for building server-side / back-end of apps using Express.js.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Happy Coding!!&lt;/strong&gt; 👊 😄&lt;/p&gt;

</description>
      <category>npm</category>
      <category>node</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
