<?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>Forem: Ahmad Ali</title>
    <description>The latest articles on Forem by Ahmad Ali (@ahmad-ali14).</description>
    <link>https://forem.com/ahmad-ali14</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%2F227337%2Fe995b4b9-01bb-4eda-927c-fbd664f7b5af.jpg</url>
      <title>Forem: Ahmad Ali</title>
      <link>https://forem.com/ahmad-ali14</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ahmad-ali14"/>
    <language>en</language>
    <item>
      <title>chrome-extension anatomy and how its parts communicate</title>
      <dc:creator>Ahmad Ali</dc:creator>
      <pubDate>Sat, 21 Nov 2020 19:13:27 +0000</pubDate>
      <link>https://forem.com/ahmad-ali14/chrome-extension-anatomy-and-how-its-parts-communicate-9bm</link>
      <guid>https://forem.com/ahmad-ali14/chrome-extension-anatomy-and-how-its-parts-communicate-9bm</guid>
      <description>&lt;p&gt;there are lots of resources out there talking about extensions. here is one chart that contains &lt;strong&gt;every single layer&lt;/strong&gt; and the relationships between them. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzn9nnnbp224j687t2s2w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzn9nnnbp224j687t2s2w.png" alt="chrome extension anatomy" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  chrome extension contains 6 important files:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;manifest.json:&lt;/code&gt; organises the extension structure ( somehow similar to &lt;strong&gt;package.json&lt;/strong&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;background script&lt;/code&gt;: this resource is shared between all browser windows and every element inside them (so changing a value in the background will &lt;strong&gt;change it everywhere&lt;/strong&gt; on the browser instantly).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;popup script:&lt;/code&gt; contains the markup that the extension displays when you click on &lt;strong&gt;its icon&lt;/strong&gt;, it is containable in the &lt;strong&gt;single tab&lt;/strong&gt;, does not have access to the extension background or page content but it can communicate with them through messages as:&lt;br&gt;
a. &lt;strong&gt;chrome.runtime API&lt;/strong&gt; to communicate with the background.&lt;br&gt;
b. &lt;strong&gt;chrome.tabs API&lt;/strong&gt; to communicate with its tab.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;content scripts:&lt;/code&gt; it is containable in the &lt;strong&gt;page window&lt;/strong&gt; so it has access to the page &lt;strong&gt;DOM&lt;/strong&gt;, it does not have an actual effect on the extension itself unless it starts communicating with the background and popup scripts through messaging events.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;options script:&lt;/code&gt; this is a special file that allows you to give the user some options in order to personalise their extension as they want, any piece of option can be saved to the &lt;strong&gt;chrome.storage.local&lt;/strong&gt; and can be synchronised between multiple browsers where the user logged in to chrome using &lt;strong&gt;chrome.storage.sync&lt;/strong&gt;. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;icons files&lt;/code&gt;: should be in sizes like 16*16 and 32*32px, the &lt;strong&gt;paths&lt;/strong&gt; to those files should be specified in the manifest. &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Communication between different scripts:
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. &lt;code&gt;From everywhere to background scripts and vice versa:&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;you can use ** chrome.runtime.sendMessage()** or any other runtime events, you need an &lt;strong&gt;eventListener&lt;/strong&gt; in the background, which in turn will handle the request and &lt;strong&gt;dispatch its response everywhere&lt;/strong&gt;.&lt;br&gt;&lt;br&gt;
and in order to receive that message (or response) you need &lt;strong&gt;another eventListener&lt;/strong&gt; on the other script listening to the runtime events.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. &lt;code&gt;From popup to content and vice versa:&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;you can't communicate directly between those 2 scripts. since both scripts can communicate with the background we can use it as middleware between them or we can use the tabs API.&lt;/p&gt;

&lt;h4&gt;
  
  
  first: background as middleware
&lt;/h4&gt;

&lt;p&gt;you can use &lt;strong&gt;chrome.runtime API&lt;/strong&gt; to exchange messages between the script you are in (popup or content) and the background using the &lt;strong&gt;chrome.runtime.sendMessage()&lt;/strong&gt;, the background will send its response to &lt;strong&gt;everywhere&lt;/strong&gt;.&lt;br&gt;
in order to receive the message, you need to listen to it on the other script itself.&lt;br&gt;
 so:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;you send a message from popup to the background.&lt;/li&gt;
&lt;li&gt;background will handle the request and send its response to &lt;strong&gt;everywhere&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;both popup and content scripts are containable in &lt;code&gt;everywhere&lt;/code&gt; (check the chart).&lt;/li&gt;
&lt;li&gt;you can listen to that response either in popup or content or both, so you received the message.&lt;/li&gt;
&lt;li&gt;done !!
&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  second: create a connection between popup and content through the tab
&lt;/h4&gt;

&lt;p&gt;you can use &lt;strong&gt;chrome.tabs API&lt;/strong&gt; to create a connection between the popup and the content script using the &lt;strong&gt;chrome.tabs.connect()&lt;/strong&gt; and then &lt;strong&gt;postMessage&lt;/strong&gt; through the tab background.&lt;br&gt;
in order to receive the message, you need to listen to it on the script itself.&lt;br&gt;
 so:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;you open a port between popup and content through the tab background.&lt;/li&gt;
&lt;li&gt;you send a message through that port to the tab background.&lt;/li&gt;
&lt;li&gt;tab background will redirect it to the content script which will handle the request and send it through the same port to the popup.&lt;/li&gt;
&lt;li&gt;you can listen to that response either in the popup, so you received the message.&lt;/li&gt;
&lt;li&gt;you can close the port, or leave it open to send other messages.&lt;/li&gt;
&lt;li&gt;done !!
&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>site views counter tutorial</title>
      <dc:creator>Ahmad Ali</dc:creator>
      <pubDate>Thu, 27 Feb 2020 14:21:53 +0000</pubDate>
      <link>https://forem.com/ahmad-ali14/site-views-counter-tutorial-f8e</link>
      <guid>https://forem.com/ahmad-ali14/site-views-counter-tutorial-f8e</guid>
      <description>&lt;p&gt;new tutorial Site views counter with Node and MongoDB javascript &lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/playlist?list=PLrMI74uzoMfm4fFCCJvLrpYtV4U1A-6Kj"&gt;https://www.youtube.com/playlist?list=PLrMI74uzoMfm4fFCCJvLrpYtV4U1A-6Kj&lt;/a&gt;&lt;/p&gt;

</description>
      <category>node</category>
      <category>mongodb</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Connecting your App to MongoDB Atlas .. Important Notice</title>
      <dc:creator>Ahmad Ali</dc:creator>
      <pubDate>Mon, 13 Jan 2020 18:39:21 +0000</pubDate>
      <link>https://forem.com/ahmad-ali14/connecting-your-app-to-mongodb-atlas-important-notice-2n85</link>
      <guid>https://forem.com/ahmad-ali14/connecting-your-app-to-mongodb-atlas-important-notice-2n85</guid>
      <description>&lt;p&gt;When you are using MongoClient to connect to MongoDB Atlas,  it is important to know that there are 3 layers of connection.&lt;/p&gt;

&lt;p&gt;1- connect to the cluster ..&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="nx"&gt;mongo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;DATABASE_URL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Database error: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Successful database connection&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;


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

&lt;/div&gt;



&lt;p&gt;using the code above will not deliver you to the database, although you will not get any errors, you will find that your operations on the database-collection are not successful.&lt;br&gt;
again, there is no errors in this connection, so you won't understand why your interaction with the DB is not successful.&lt;/p&gt;

&lt;p&gt;2- connect to the database:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="c1"&gt;//DB and authentication&lt;/span&gt;
&lt;span class="nx"&gt;mongo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;DATABASE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;cluster&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Database error: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//connecting to the cluster&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Successful database connection&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;//connetcing to the database&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;cluster&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;db&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;DATABASE_NAME&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

 &lt;span class="p"&gt;});&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;in the code above, the&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;connection.db() or cluster.db()&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
 function -if you want to think of it this way- to connect us our database.&lt;/p&gt;

&lt;p&gt;3- connect to  the collection :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="c1"&gt;//DB and authentication&lt;/span&gt;
&lt;span class="nx"&gt;mongo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;DATABASE_URL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;cluster&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Database error: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//connecting to the cluster&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Successful database connection&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;//connetcing to the database&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;cluster&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;db&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;DATABASE_NAME&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

   &lt;span class="c1"&gt;//conneting to the collection&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;coll&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;collection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;COLLECTION_NAME&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

   &lt;span class="c1"&gt;//do your stuff on the collection here.&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;in the code above we reached our collection, so we can do our operations on it correctly.&lt;/p&gt;

&lt;p&gt;remember to put all of the code that is doing operations on Database inside the MongoClient connection.&lt;/p&gt;

&lt;p&gt;Happy Coding..!!&lt;/p&gt;

</description>
      <category>mongodb</category>
      <category>node</category>
      <category>javascript</category>
      <category>express</category>
    </item>
    <item>
      <title>visits counter to a website using Express.js, MongoDb</title>
      <dc:creator>Ahmad Ali</dc:creator>
      <pubDate>Thu, 02 Jan 2020 16:26:59 +0000</pubDate>
      <link>https://forem.com/ahmad-ali14/visits-counter-to-a-website-using-express-js-mongodb-2gig</link>
      <guid>https://forem.com/ahmad-ali14/visits-counter-to-a-website-using-express-js-mongodb-2gig</guid>
      <description>&lt;p&gt;Website visits Counter can be a vital way to analyze the trafiic that your ebsite encounters.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--P1IQK-uY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/dy10u76kl5enyz3qxb81.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--P1IQK-uY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/dy10u76kl5enyz3qxb81.jpeg" alt="Alt Text" width="800" height="458"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;you can acsess source code on github &lt;a href="https://github.com/aa947/siteViews-counter"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;although you can accsess your website statistics with the built-in functions and data that your service provider gives to you, it still a good practice to show your coustomers the amount of traffic that reaches to your website.&lt;br&gt;
you can show the number of visits to every single page in your website, but I am going to keep things simple and implement a visit counter for the index page only, and I am going mention how to expand this counter to other pages.&lt;br&gt;
this toturial suppose that you have got Node, MongoDb installed on your machine, otherwise just google the and install them on your machine.&lt;br&gt;
first things first, let’s talk a bit about the technologies that we are going to use in this project. we need to use:&lt;br&gt;
1- Express-Generator: a server to do your logic.&lt;br&gt;
2-MongoDb: for data storing.&lt;br&gt;
3-Mongoose: for interacting between the server and database.&lt;br&gt;
4-EJS Embedded JavaScript templating: for fetching the data from your database and show them in your views.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XBlf9u7N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/5zz23siv6iea5imh7wnr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XBlf9u7N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/5zz23siv6iea5imh7wnr.png" alt="our package.json file" width="779" height="473"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;1- installing express-generator:&lt;br&gt;
Express Generator is a quick way to scaffold your App and buiuld it’s skeleton.&lt;br&gt;
we are going to install express-generator using this command:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8HrS__JK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/dba1es73cubs3bmt3gee.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8HrS__JK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/dba1es73cubs3bmt3gee.png" alt="install express-generator&amp;lt;br&amp;gt;
" width="288" height="26"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;this command will install the generator globally so can use it in any folder. now make a new folder and call it what ever you want , and then open it in a termenal and type “express” and then type “npm install” so all the default libraries will be installed automaticly , and then you’ve got your App scaffolded as shown on the photo:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1Fpq7mlj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/1ovpd6kcqfes5a8ktxts.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1Fpq7mlj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/1ovpd6kcqfes5a8ktxts.png" alt="your App skeleton&amp;lt;br&amp;gt;
" width="266" height="342"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;2- Installing mongoose :&lt;br&gt;
in your terminal just type “npm install mongoose”, and you will notice that mongoose have been added to your package.json&lt;br&gt;
3-Connecting Mongoose to your Database:&lt;br&gt;
first we need to create new mongoose model, which is a form of represnting a MongoDb collection on your code.&lt;br&gt;
open a new terminal and type “mongod” → this command will switch on your MongoDb server and you have to keep it up and runnig while you work, you will get a lot of text as shown on the photo :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rI8oe8B_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/d6yk6sn3wifn8bhj651g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rI8oe8B_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/d6yk6sn3wifn8bhj651g.png" alt="terminal after you type mongod indicates that mongoDb server is up and running&amp;lt;br&amp;gt;
" width="800" height="373"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;now, keep that terminal running and go to your code, let us make a new directory and call it models, then create a new file and call it “visits.js” and type the following code in it:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ApUKa7XB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/caxs7wb966mrckgjvpw4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ApUKa7XB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/caxs7wb966mrckgjvpw4.png" alt="visits.js&amp;lt;br&amp;gt;
" width="742" height="470"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;in this file we required mongoose so we can acsess the functionality of this library and one of them is Schema whitch defines the structure of the new collection that we will create whitch can be acsessed by calling the model in our code. then we export visits.js&lt;br&gt;
now open a new termenal and type “mongo”, type “use confusion” → this command will create a new database if it not existed and then will use it on the terminal shell .. now type ..&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OhZ5OgPn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/3gkiwvq2tsw49bgt5qcq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OhZ5OgPn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/3gkiwvq2tsw49bgt5qcq.png" alt="create a new collection" width="670" height="98"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;then insert a new document inside ‘visits’ collections and show them using this commands:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EkUWZ7jT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/8n1aq7k27r771fuv2mlt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EkUWZ7jT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/8n1aq7k27r771fuv2mlt.png" alt="Alt Text" width="281" height="57"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;save that id because we are going to use it later.then we are going to make a new direction inside our App directory called src/visitsUp.js and then we create a file called visitsUp.js and put the following code inside it:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cD-tpOuT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/nvx7bb6cftcawqaq6j9y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cD-tpOuT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/nvx7bb6cftcawqaq6j9y.png" alt="visitsUp.js" width="800" height="359"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;in this file we have defined a function called sitevisitsUp that will use the mongoose findByIdAndUpdate function to update the visit counter property inside our database. we used the _id property of the document that we have created. and we use the mongoose expression $inc to increase the counter by one.&lt;br&gt;
now lets go to routes/index.js and update it with the following code:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--22WE0Z4G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/odea8ihesmqmmqpx2pcs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--22WE0Z4G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/odea8ihesmqmmqpx2pcs.png" alt="routes/index.js" width="729" height="479"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;in this code we have called the function siteViewsUp every time the server faces a get request. then we find this document and pass it’s counter property to render() function → this way we can acsess this value inside our index.ejs whitch will be rendered when we make a get request to main route (server/).&lt;br&gt;
now, lets update our app.js file:&lt;br&gt;
first, we need to connect our App to mongoDb server using mongoose.connect() function whitch takes the url for the database as an argument.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NiN84MSn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/1a6f51pwcgwdmqut01g4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NiN84MSn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/1a6f51pwcgwdmqut01g4.png" alt="app.js&amp;lt;br&amp;gt;
" width="646" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;then we are going to change the views engine so we can render ejs files, which will be our next topic:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rDjjBCx_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/g1ood4u6qjt5io75tsgt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rDjjBCx_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/g1ood4u6qjt5io75tsgt.png" alt="app.js&amp;lt;br&amp;gt;
" width="476" height="68"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;4- rendering EJS files:&lt;br&gt;
EJS is a simple templating language that lets us generate HTML markup with plain JavaScript. so It’s just plain JavaScript.&lt;br&gt;
in the terminal type “npm install ejs”, so you can use it.&lt;br&gt;
lets go to viewsfolder and make a new file views/index.ejs and type the following code:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OqklGq3a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/29gh6rxxl66rz2pol9t6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OqklGq3a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/29gh6rxxl66rz2pol9t6.png" alt="index.ejs&amp;lt;br&amp;gt;
" width="439" height="309"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;the way the ejs files work is that you pass data as an object inside your render() method , and then you can acsess this data by typing js code inside &amp;lt;%= %&amp;gt; tag while the rest of the document is palin html.&lt;br&gt;
now type “npm start” to start your server then go to &lt;a href="http://localhost:3000/"&gt;http://localhost:3000/&lt;/a&gt; you will find somethimg like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9fzr06_O--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/jy05s3xfmhtu0g8l5x1j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9fzr06_O--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/jy05s3xfmhtu0g8l5x1j.png" alt="http://localhost:3000/&amp;lt;br&amp;gt;
" width="800" height="413"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;each time you refresh the page the counter will increases automaticlly.&lt;br&gt;
Now you have implemented a visit counter for index page , in order to expand this to other pages you have many ideas one of them is to add another field in your database so you can use different counter for each page and that’s why I have implemented siteViewsUp in a seprate file so you can import it when needed.&lt;br&gt;
I am going to write another post for different counters in the future.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>mongodb</category>
      <category>express</category>
      <category>mongoose</category>
    </item>
  </channel>
</rss>
