<?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: Praveen Kumar Saini</title>
    <description>The latest articles on DEV Community by Praveen Kumar Saini (@am_pra_veen).</description>
    <link>https://dev.to/am_pra_veen</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%2F193691%2F967c5a25-016d-4b6c-a85f-aea16ebf9e0b.jpeg</url>
      <title>DEV Community: Praveen Kumar Saini</title>
      <link>https://dev.to/am_pra_veen</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/am_pra_veen"/>
    <language>en</language>
    <item>
      <title>Implementing Simple SPA Routing Using Vanilla JavaScript</title>
      <dc:creator>Praveen Kumar Saini</dc:creator>
      <pubDate>Wed, 21 Aug 2019 16:42:59 +0000</pubDate>
      <link>https://dev.to/am_pra_veen/implementing-simple-spa-routing-using-vanilla-javascript-25b6</link>
      <guid>https://dev.to/am_pra_veen/implementing-simple-spa-routing-using-vanilla-javascript-25b6</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2AmHQDsqWBWs3tBrPz2uZWtA.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2AmHQDsqWBWs3tBrPz2uZWtA.jpeg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this article, I am going to implement &lt;strong&gt;SPA&lt;/strong&gt; routing using &lt;strong&gt;Vanilla JavaScript&lt;/strong&gt;.&lt;br&gt;
So, the main motive behind this article to get an understanding of how SPA routing works. Before we get into this we need to understand the main components of routing i.e Web &lt;strong&gt;History&lt;/strong&gt; and &lt;strong&gt;Location&lt;/strong&gt; API.&lt;/p&gt;
&lt;h2&gt;
  
  
  History and Location Object
&lt;/h2&gt;

&lt;p&gt;Let’s first talk about &lt;strong&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/History" rel="noopener noreferrer"&gt;window.history&lt;/a&gt;&lt;/strong&gt;. History contains all the browser history you can simply access it by typing &lt;em&gt;history&lt;/em&gt; and then you’ll get the whole history object and the variety of method’s it has.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2346%2F1%2Ao5aivA_EwWECOOZnq2GYog.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2346%2F1%2Ao5aivA_EwWECOOZnq2GYog.png" alt="window’s history object"&gt;&lt;/a&gt;&lt;em&gt;window’s history object&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;And another one that we are going to use is &lt;strong&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Location" rel="noopener noreferrer"&gt;window.location&lt;/a&gt;.&lt;/strong&gt; It contains all the information about the current location such as origin, pathname, etc. If you type &lt;em&gt;location&lt;/em&gt; in your terminal then you got the whole location object.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2254%2F1%2AGHosXKQnj5bwM8x3jo8wKQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2254%2F1%2AGHosXKQnj5bwM8x3jo8wKQ.png" alt="window’s location object"&gt;&lt;/a&gt;&lt;em&gt;window’s location object&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Let’s start Routing
&lt;/h2&gt;

&lt;p&gt;So, first of all, we need to make an &lt;strong&gt;index.html.&lt;/strong&gt; It contains all the markup for our app. For this right, we just need to make a div of the &lt;em&gt;id *of **root&lt;/em&gt;*.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2840%2F1%2AgbW54Pe4_GxHFe4hxB0V8w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2840%2F1%2AgbW54Pe4_GxHFe4hxB0V8w.png" alt="index.html"&gt;&lt;/a&gt;&lt;em&gt;index.html&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now, we are done with that make our page. That is going to render in &lt;strong&gt;root&lt;/strong&gt; area.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2840%2F1%2AFbzwL2Qa7-_o2CXBgHmTyA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2840%2F1%2AFbzwL2Qa7-_o2CXBgHmTyA.png" alt="Our three different components"&gt;&lt;/a&gt;&lt;em&gt;Our three different components&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now, we make our three different pages let’s import them in our index.html’s &lt;strong&gt;head **section. Now we have to use a server to serve these pages so in this I am going to use **live-server&lt;/strong&gt;. If you have live-server it’s well and good just go to the folder directory and run live-server otherwise install it using npm i -g live-server.&lt;br&gt;
Now, it’s time to make our main JavaScript file that contains all our routing logic.&lt;br&gt;
Let's make a file called app.js. &lt;br&gt;
Then first we have to decide our routes and page specific to that particular route and put them in an object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;routes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;home&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/contact&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;contact&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/about&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;about&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our next work is to render a page when the index initially loaded. For that first, we have to select the &lt;strong&gt;root&lt;/strong&gt; div and then use innerHTML method to add the content to the element according to the path.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;rootDiv&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;root&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;rootDiv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;routes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;location&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pathname&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here &lt;strong&gt;window.location.pathname&lt;/strong&gt; will give the current path and when the page is loaded the current path is going to &lt;strong&gt;‘/’&lt;/strong&gt;. But our work is not done here yet. Now, we also have to make the route for ‘/about’ and ‘/contact’ page. For that, we are going to create a simple navigation menu.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3328%2F1%2AHRolE_-GnyX2C7o-ye5ZIQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3328%2F1%2AHRolE_-GnyX2C7o-ye5ZIQ.png" alt="Simple navigation for navigating through sections."&gt;&lt;/a&gt;&lt;em&gt;Simple navigation for navigating through sections.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now, we are going to make a function that helps us to navigate through section it takes a path and then uses &lt;strong&gt;history.pushState()&lt;/strong&gt; method that takes three parameters i.e state, title, and the route to push and to push the current route into history object so we can also navigate through the browser &lt;em&gt;backward&lt;/em&gt; and &lt;em&gt;forward&lt;/em&gt; button and then render the section according to the current path.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3160%2F1%2Anok3v9iCyS75U1G0dgWHow.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3160%2F1%2Anok3v9iCyS75U1G0dgWHow.png" alt="onNavigate Method"&gt;&lt;/a&gt;&lt;em&gt;onNavigate Method&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now, just call this **onNavigate **method on the **onClick **event of our navigation menu’s a tag. We just need to do this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;href&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;onclick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;onNavigate('/about'); return false;&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;About&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/a&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="c1"&gt;// repeat for other routes also.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Congrats you just make your own router and when you pressing browser’s forward and backword button you see that the route actually changes but wait for your page is not changing. So, this is because when the browser call’s it’s &lt;strong&gt;pushState()&lt;/strong&gt; method than another method is also called i.e &lt;strong&gt;window.onpopstate&lt;/strong&gt; it’s a simple function where you can customize it according to your need. So, whenever the pushState is called we are going to render our section. For that all we need to apply this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onpopstate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;rootDiv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;routes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;location&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pathname&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Congrats you finally have done with it.&lt;/p&gt;

&lt;p&gt;If you want to follow the code. Check out GitHub repo from &lt;a href="https://github.com/praveen-me/simple-vanila-router" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;br&gt;
If you want to connect with me check out my &lt;a href="https://twitter.com/am_pra_veen" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; and &lt;a href="https://github.com/praveen-me" rel="noopener noreferrer"&gt;Github&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>routing</category>
      <category>beginners</category>
      <category>spa</category>
    </item>
    <item>
      <title>Create full-stack boilerplate using reenode.</title>
      <dc:creator>Praveen Kumar Saini</dc:creator>
      <pubDate>Mon, 12 Aug 2019 18:21:29 +0000</pubDate>
      <link>https://dev.to/am_pra_veen/create-full-stack-boilerplate-using-reenode-33i9</link>
      <guid>https://dev.to/am_pra_veen/create-full-stack-boilerplate-using-reenode-33i9</guid>
      <description>&lt;h2&gt;
  
  
  Problems with creating fullstack boilerplate:
&lt;/h2&gt;

&lt;p&gt;Creating a starter kit for full-stack applications need a lot of effort and sometimes it becomes a headache. A small mistake in the initialization of the starter project can break your whole code and then you see a huge pile of console errors.&lt;/p&gt;

&lt;p&gt;When I was learning to create full-stack applications then every time I code application from sketch and repeat that huge pile of code again and again that becomes a headache for me sometimes and if I did something wrong in setup of the application, my whole application got breaks.&lt;/p&gt;

&lt;p&gt;Making mistake can be one reason but the main reason is that its very time consuming like every time you need to install packages, configure webpack, babel, eslint, prettier, etc and sometimes that makes me sick. Then I created some boilerplate like &lt;a href="https://github.com/praveen-me/simple-react-boilerplate" rel="noopener noreferrer"&gt;simple-react-boilerplate&lt;/a&gt;, &lt;a href="https://github.com/praveen-me/MERN-boilerplate" rel="noopener noreferrer"&gt;MERN-boilerplate&lt;/a&gt;, &lt;a href="https://github.com/praveen-me/react-redux-ssr" rel="noopener noreferrer"&gt;react-redux-ssr&lt;/a&gt; and when I need to create a project I simply clone that repository and start working.&lt;br&gt;
But still, the problem is that there are different repositories for every purpose i.e sometimes I don't need node but sometimes I do.&lt;/p&gt;

&lt;p&gt;So, that's why I create &lt;code&gt;reenode&lt;/code&gt;, a CLI for generating boilerplates with all the necessary minimal configuration to code a full-stack applications.&lt;/p&gt;
&lt;h2&gt;
  
  
  What is actually &lt;code&gt;reenode&lt;/code&gt; is ?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;reenode&lt;/code&gt; is a simple CLI that generates starter code for full-stack applications without all pain that you'll have when you write that from scratch. &lt;code&gt;reenode&lt;/code&gt; also have custom options for creating different boilerplates. The best part is that it's time-saving and your starter code will be ready in just a couple of minutes.&lt;/p&gt;
&lt;h2&gt;
  
  
  How to use it?
&lt;/h2&gt;

&lt;p&gt;For that first you need to install it globally by just typing: &lt;br&gt;&lt;br&gt;
&lt;code&gt;npm install -g reenode&lt;/code&gt; or &lt;code&gt;yarn add global reenode&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;That's so simple. 😃&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;reenode&lt;/strong&gt; provides &lt;code&gt;create&lt;/code&gt; command to create a project. For that you just need to run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;reenode create project-name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that terminal prompt will ask some questions:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpraveen-me.github.io%2Freenode-b24111efa3e9d9a9791a2165834c4e0b.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpraveen-me.github.io%2Freenode-b24111efa3e9d9a9791a2165834c4e0b.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's all you need to do. 🚀 &lt;br&gt;&lt;br&gt;
You can get help typing &lt;code&gt;reenode --help&lt;/code&gt; and version of CLI by &lt;code&gt;reenode --version&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Checkout the whole documentation of reenode from here: &lt;br&gt;&lt;br&gt;
NPM Link: &lt;a href="https://www.npmjs.com/package/reenode" rel="noopener noreferrer"&gt;https://www.npmjs.com/package/reenode&lt;/a&gt; &lt;br&gt;&lt;br&gt;
Github Link: &lt;a href="https://github.com/praveen-me/reenode" rel="noopener noreferrer"&gt;https://github.com/praveen-me/reenode&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;REQUEST&lt;/strong&gt;: If you feel issue using it then please comment down here or you can add issue at &lt;strong&gt;reenode's&lt;/strong&gt; GitHub repo.&lt;/p&gt;




&lt;p&gt;Follow me on &lt;a href="https://twitter.com/am_pra_veen" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; and &lt;a href="https://github.com/praveen-me" rel="noopener noreferrer"&gt;Github&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>webpack</category>
      <category>boilerplate</category>
    </item>
  </channel>
</rss>
