<?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: Aisha A.</title>
    <description>The latest articles on Forem by Aisha A. (@aisha0926).</description>
    <link>https://forem.com/aisha0926</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%2F719106%2F6b2639f4-26ed-4596-a072-281a536101ed.png</url>
      <title>Forem: Aisha A.</title>
      <link>https://forem.com/aisha0926</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/aisha0926"/>
    <language>en</language>
    <item>
      <title>React props - A beginner's guide</title>
      <dc:creator>Aisha A.</dc:creator>
      <pubDate>Tue, 16 May 2023 04:25:10 +0000</pubDate>
      <link>https://forem.com/aisha0926/react-props-a-beginners-guide-5gjd</link>
      <guid>https://forem.com/aisha0926/react-props-a-beginners-guide-5gjd</guid>
      <description>&lt;p&gt;This concept is when you're passing the data from the parent down to its direct children.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dR6XJmyM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ctircb4ad6ufrt1xo5w1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dR6XJmyM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ctircb4ad6ufrt1xo5w1.png" alt="Image description" width="444" height="252"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Take for example the image above where the App component has 2 children, the Modal component and the Form component. The App component has the following data.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tEhZxcR2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g2abrm7z19kpcffe9yd8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tEhZxcR2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g2abrm7z19kpcffe9yd8.png" alt="Image description" width="552" height="224"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;How do we then pass the data down from App to Form component? That's when we're going to use "props". Props consists of all attributes that we pass the component to. What do I mean by that? Let's have a look at the example below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2zmmyxus--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sqo2nfc55sxmpxg0ul04.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2zmmyxus--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sqo2nfc55sxmpxg0ul04.png" alt="Image description" width="800" height="124"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Props is a shortened version for properties, quite similar to objects, each component have their own properties "IF" given any. If we were to convert the code above to a vanilla Javascript object, it will look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_bVTlNSy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x8ysmslieszuijnzznpr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_bVTlNSy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x8ysmslieszuijnzznpr.png" alt="Image description" width="800" height="182"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's have a look at our JSX code and identify the attributes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OXUUyseE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ax68cvqoqe3bmdi822r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OXUUyseE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ax68cvqoqe3bmdi822r.png" alt="Image description" width="800" height="304"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you've noticed, we have an attribute property and a value assigned to each attribute. Quite similar to how objects are.&lt;/p&gt;

</description>
      <category>react</category>
      <category>reactjsdevelopment</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>React useState() - A guide for beginners</title>
      <dc:creator>Aisha A.</dc:creator>
      <pubDate>Sun, 14 May 2023 12:17:27 +0000</pubDate>
      <link>https://forem.com/aisha0926/react-usestate-a-guide-for-beginners-2019</link>
      <guid>https://forem.com/aisha0926/react-usestate-a-guide-for-beginners-2019</guid>
      <description>&lt;p&gt;As a beginner, you need to know why we use useState, how its executed and why do we need it. These sections will be discussed as we deep dive into useState hook.&lt;/p&gt;

&lt;p&gt;For a start, we need to understand why we need useState. Using vanilla Javascript code doesn't trigger component re-render. Take for example the code below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--njKeBZW7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h9op0ofro1haqtpkqe8e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--njKeBZW7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h9op0ofro1haqtpkqe8e.png" alt="Image description" width="800" height="334"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Examining the code above, you'll probably think that the title should change because we're re-assigning it a value, right?&lt;/p&gt;

&lt;p&gt;However, that's not what is happening. Rather, you can see that the title has a new value called "Updated" in the console but the DOM did NOT change. Why is that you ask? That is because when React renders the component a second time, the local variables don't persist, and the second reason is that, local variables don't trigger re-render/renders.&lt;/p&gt;

&lt;p&gt;By default, the DOM only gets mounted ONCE and will never re-evaluate even if a change occurred. To update a component, 2 things need to happen, 1, for the data between render to be retained and 2, for React to render the component with the new data. Now let's try to understand that visually.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This entire process is the mounting phase of the React Component Lifecycle&lt;/strong&gt; which only happens ONCE when the components are first evaluated and rendered for the first time&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CcYEXznK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gxlzqnugvi14yf5hn9sm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CcYEXznK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gxlzqnugvi14yf5hn9sm.png" alt="Image description" width="800" height="308"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, we're faced with an issue where React will never repeat the same process. As soon as it's rendered for the first time, it's done. However, most of the time, we want to be able to change the values rendered on the page when a change occurs. That's when we're going to need states.&lt;/p&gt;

&lt;p&gt;As mentioned above, React ignores any normal/vanilla variables even if a change occured. It doesn't trigger the component to re-render and even if it does, it will just re-create the variable with the same value. Even if there's a click or any other event listener, it doesn't trigger the component to run again.&lt;/p&gt;

&lt;p&gt;In useState however, any changes to its values should result in the component function being called again.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;useState&lt;/strong&gt; returns two values, a pointer to the managed state, and the second value is a setter function that can be called later to set a new value, you can access access these values using an &lt;strong&gt;array destructuring&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When the setter function is called and re-renders the component, it will compare the virtual dom and the actual dom to check if there’s a change in the data or the component itself, if there is then it will replace the actual dom with the new values.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--b4Z-n0tB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n6wfnomjz0vn6gvsmc7b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--b4Z-n0tB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n6wfnomjz0vn6gvsmc7b.png" alt="Image description" width="620" height="418"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To be accurate, it's comparing the current evaluation to the previous evaluation. If there's any change, those changes is handed over to the ReactDOM which makes necessary changes in the real DOM.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This phase or state is called the Updating phase in the React component lifecycle.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--V9lOt9zs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zd3diepbesg8xk707eki.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--V9lOt9zs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zd3diepbesg8xk707eki.png" alt="Image description" width="800" height="294"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another thing to note is that the console log doesn’t print the updated value straight away, rather, it prints the value BEFORE it was updated. The reason for that is because useState doesn’t change the value right away, rather, it schedules the update.&lt;/p&gt;

&lt;p&gt;On top of that, states are per component instance basis, meaning, if you change the state in one component, the other component state isn't affected. Think of it as a modular state, separated from the other component. &lt;/p&gt;

&lt;h2&gt;
  
  
  State snapshots
&lt;/h2&gt;

&lt;p&gt;How do we then get the latest state snapshot? Meaning, how do we get the latest value? Well, think of it this way, imagine depositing your money in the bank, they now manage your money for you, as days go by, the interest in your account accumulates. A year later, you withdraw your money which now has interest on top. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--q7NmYTOq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uw439g2uzwmg4uiu1xn1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--q7NmYTOq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uw439g2uzwmg4uiu1xn1.png" alt="Image description" width="800" height="353"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Likewise, in React, it's like telling React to give you the latest snapshot that you asked to be managed, hence, you have an access to its previous change and manipulate it like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; const [detectChange, setDetectedChange] = useState(false);

 setDetectedChange((previousState) =&amp;gt; !previousState);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>webdev</category>
      <category>react</category>
      <category>beginners</category>
    </item>
    <item>
      <title>App.js, what should be in it?</title>
      <dc:creator>Aisha A.</dc:creator>
      <pubDate>Tue, 06 Sep 2022 03:19:13 +0000</pubDate>
      <link>https://forem.com/aisha0926/appjs-what-should-be-in-it-46ai</link>
      <guid>https://forem.com/aisha0926/appjs-what-should-be-in-it-46ai</guid>
      <description>&lt;p&gt;Now that we’ve tackled the concepts, let’s visualise how they are intertwined.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--O11ml3rt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kgr8efyfi2ltlv6tehck.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--O11ml3rt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kgr8efyfi2ltlv6tehck.png" alt="Image description" width="880" height="577"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To explain the image above. When the user first triggers the /users end point, it will trigger the route through the server application which is the file that holds the server. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ynJB4Fwm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0j3jwvccc0sijvuluohf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ynJB4Fwm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0j3jwvccc0sijvuluohf.png" alt="Image description" width="458" height="662"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In my case, the server resides in the app.js file. How do I know that it is my server? Because it is where the instance of the server is declared:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const app = express();&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;On top of that, app.js holds the database connection as well as the grouping routes. Since app.js is like the centre point of everything, it is in our best interest to understand what should be included within it.&lt;/p&gt;

&lt;p&gt;First, we must declare ALL the required modules such as the express and mongoose at this point, like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//! Required Modules
const express = require('express');

//* This allows us to connect in our mongoDB

const mongoose = require('mongoose');

//! Port
const port = 4000;

//! Server
const app = express();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After declaring them, we then need to create a database connection first and foremost by utilising &lt;strong&gt;mongoose&lt;/strong&gt; and use the dot notation to access the method called connect which allows us to connect to &lt;strong&gt;MongoDB Atlas&lt;/strong&gt; remotely. &lt;/p&gt;

&lt;p&gt;The first parameter should have the URI string taken from their site (&lt;a href="https://cloud.mongodb.com/"&gt;https://cloud.mongodb.com/&lt;/a&gt;) after logging in. Locate the &lt;strong&gt;Database&lt;/strong&gt; under the Deployment and click &lt;strong&gt;Connect&lt;/strong&gt; and then click the &lt;strong&gt;Connect your application option&lt;/strong&gt; from the given tools, then copy the &lt;strong&gt;URI&lt;/strong&gt; string displayed.&lt;/p&gt;

&lt;p&gt;The second parameter is optional, but it is ideal to add it in order to prevent the web application from crashing when MongoDB updates. See the full code below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mongoose.connect(
    //mongodb+srv://CG:admin@cluster0.322.mongodb.net/tasks182?retryWrites=true&amp;amp;w=majority
    'mongodb+srv://admin:admin@233-course-booking.g54.mongodb.net/tasks182?retryWrites=true&amp;amp;w=majority', {
        useNewUrlParser: true,
        useUnifiedTopology: true,
    }
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After creating the connection, the programmer has to know if a connection has been established successfully or not, right? In order to do that, we need to access the &lt;em&gt;.connection&lt;/em&gt; method that is made available for us by mongoose and declare it in a variable like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//This will create a notification if the db connection is successful or not.
let db = mongoose.connection;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can then tap the variable declared and check if the connection is successful or not by using the .on and the .once methods. What exactly are they? &lt;br&gt;
&lt;strong&gt;.on&lt;/strong&gt; signifies that the event will be called every time the specified event occurs.&lt;br&gt;
&lt;strong&gt;.once&lt;/strong&gt; signifies that the event will be called only ONCE the first event is triggered.&lt;/p&gt;

&lt;p&gt;Let us first have a look at the .on code below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.on('error', console.error.bind(console, 'DB Connection Error'));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that we’re constantly checking for any error that may occur whilst the server is on as its first parameter. The second parameter is binding the error to both the console and the terminal.&lt;/p&gt;

&lt;p&gt;Let us now have a look at the .once code below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.once('open', () =&amp;gt; console.log('Successfully connected to MongoDB'));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code above, we’re checking if the connection is successful or open then we’re logging a message to the console which notifies the programmer that a connection has been established.&lt;/p&gt;

&lt;p&gt;The next thing we’re tackling are the &lt;em&gt;middleware&lt;/em&gt; that we need in order for our application to run smoothly. The first middleware is the express.json(), its sole purpose is to convert the &lt;em&gt;request body&lt;/em&gt; into a &lt;em&gt;JSON object&lt;/em&gt;. The next middleware is the express.urlencoded which enables the server to accept any data type on top of the string and array data types that it’s already accepting. Let’s have a look at their code snippets:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.use(express.json());
app.use(express.urlencoded({ extended: true }));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, all we need to add to complete our app.js is to add the grouping routes which will allow us to connect our routes to the server. As shown in the image previously, by visiting the /users, we’re triggering our routes. &lt;/p&gt;

&lt;p&gt;Now, what do they look like and how does it work? Let’s have a look at the code snippet below and then dissect what each of them do.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SXbQnYlg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2e1918rpblqviz5j004l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SXbQnYlg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2e1918rpblqviz5j004l.png" alt="Image description" width="880" height="182"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;First, we’re declaring a variable that will hold the route (green box above). Next, we’re requiring the routes that we want to trigger (beige colour) which resides in a different file path which is relative to the root file (app.js).&lt;/p&gt;

&lt;p&gt;We’re now going to use the routes as a middleware which will run after the request is received and before the response is triggered (app.use). The first parameter is the endpoint (orange box) that we want to trigger our route (yellow box).&lt;br&gt;
Lastly, all we need in our app.js is to add the port:&lt;br&gt;
&lt;code&gt;app.listen(port, () =&amp;gt; console.log(&lt;/code&gt;Listening to port: ${port}&lt;code&gt;));&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Route file&lt;br&gt;
The route file is where we are going to trigger the business logic which resides in the controller. In this file, we first need to declare the required modules just like what we did in the app.js. The first module we need is the express.&lt;br&gt;
&lt;code&gt;const express = require('express');&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The next module that we need is the router which will allow us to access HTTP methods without directly connecting to the server which is declared in our app.js.&lt;br&gt;
&lt;code&gt;const router = express.Router();&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;But why do we need it instead of just declaring another server you say? Well, it’s because by doing so, we’re not really separating the concerns that we’re trying to achieve. &lt;br&gt;
After declaring the router, we now need to declare the controller that we need to execute which resides in a relative folder; accessed through the relative path using “../”.&lt;br&gt;
&lt;code&gt;const usersController = require('../controllers/usersController');&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now need to execute the controller that holds the business logic for the /users endpoint. However, let us first understand what’s happening with the code below:&lt;br&gt;
&lt;code&gt;router.post('/', usersController.createUsers);&lt;/code&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>webdev</category>
    </item>
    <item>
      <title>MVC, why do we need it.</title>
      <dc:creator>Aisha A.</dc:creator>
      <pubDate>Tue, 06 Sep 2022 03:02:06 +0000</pubDate>
      <link>https://forem.com/aisha0926/mvc-why-do-we-need-it-3kko</link>
      <guid>https://forem.com/aisha0926/mvc-why-do-we-need-it-3kko</guid>
      <description>&lt;h2&gt;
  
  
  Why do we need to separate the concerns?
&lt;/h2&gt;

&lt;p&gt;Imagine if you have to do your groceries inside a bank, wouldn’t that be chaotic? Or imagine if your groceries aren’t categorized where you’ll have to scour through the entire cupboard just to find the ingredient you need? &lt;/p&gt;

&lt;p&gt;Likewise, programming works the same way. We separate our concerns for code readability and code maintainability. How so? Let’s first understand what a model, controller and routes is.&lt;/p&gt;

&lt;h2&gt;
  
  
  Model
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Model&lt;/strong&gt; is where your structure resides, it is where your data blueprint is. It contains the &lt;strong&gt;properties&lt;/strong&gt; as well as the &lt;strong&gt;data type&lt;/strong&gt; that we’re expecting from the user. This is where the schema and the model should be, like so:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const mongoose = require('mongoose');&lt;/code&gt;&lt;br&gt;
&lt;code&gt;const taskSchema = new mongoose.Schema({&lt;/code&gt;&lt;br&gt;
    &lt;code&gt;name: String,&lt;/code&gt;&lt;br&gt;
   &lt;code&gt;status: String,&lt;/code&gt;&lt;br&gt;
&lt;code&gt;});&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;module.exports = mongoose.model('Task', taskSchema);&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Controller
&lt;/h2&gt;

&lt;p&gt;Controller is where our business logic resides. This is where the brain of the program is. Previously, we’d put all the business logic inside the route like so:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;app.post('/items', (req, res) =&amp;gt; {&lt;/code&gt;&lt;br&gt;
  &lt;code&gt;let newItems = {&lt;/code&gt;&lt;br&gt;
     &lt;code&gt;name: req.body.name,&lt;/code&gt;&lt;br&gt;
    &lt;code&gt;price: req.body.price,&lt;/code&gt;&lt;br&gt;
     &lt;code&gt;isActive: req.body.isActive,&lt;/code&gt;&lt;br&gt;
  &lt;code&gt;};&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;items.push(newItems);&lt;/code&gt;&lt;br&gt;
   &lt;code&gt;res.send(items);&lt;/code&gt;&lt;br&gt;
&lt;code&gt;});&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Our business logic can be quite convoluted if there are hundreds or thousands of them. Hence, we separate it in a different file called “Controller.js” under a folder called “&lt;strong&gt;controllers&lt;/strong&gt;” where all our controllers will reside. &lt;/p&gt;

&lt;p&gt;On that note, please bear in mind that a controller goes side by side with the route.&lt;/p&gt;

&lt;h2&gt;
  
  
  Route
&lt;/h2&gt;

&lt;p&gt;Route is where the routes reside. This is where the **endpoints **should be defined. As mentioned above, the route, work side by side with the controller, like so:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const express = require('express');&lt;/code&gt;&lt;br&gt;
&lt;code&gt;const router = express.Router();&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const taskControllers = require('../controllers/taskControllers');&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;console.log(taskControllers);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;router.post('/', taskControllers.createTaskController);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;module.exports = router;&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Mongoose Router
&lt;/h2&gt;

&lt;p&gt;Mongoose router allows us to use &lt;strong&gt;HTTP methods&lt;/strong&gt; without having direct access to our server. What do I mean by that? In order to have access to the HTTP methods, we need to have access to the server which is defined by calling the server instance declared using the variable **app **and directly access the HTTP methods directly like so (see line number 3):&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const express = require('express');&lt;/code&gt;&lt;br&gt;
&lt;code&gt;const app = express();&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;app.get('/items/getSingleItem/:item', (req, res) =&amp;gt; {&lt;/code&gt;&lt;br&gt;
  &lt;code&gt;res.send(items[+req.params.item]);&lt;/code&gt;&lt;br&gt;
&lt;code&gt;});&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;However, since the route file does NOT have access to the HTTP methods as it is NOT a server, we need a way to access it in order to define the routes and its function that needs to be performed. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;const express = require('express');&lt;/code&gt;&lt;br&gt;
&lt;code&gt;const router = express.Router();&lt;/code&gt;&lt;br&gt;
&lt;code&gt;//Router method - allows access to HTTP methods&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const taskControllers = require('../controllers/taskControllers');&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;console.log(taskControllers);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;//create task route. At the moment, we don't have direct access&lt;/code&gt;to &lt;code&gt;the server, hence, we NEED to use router in order to have access&lt;/code&gt; &lt;code&gt;to the HTTP methods&lt;/code&gt;&lt;br&gt;
&lt;code&gt;router.post('/', taskControllers.createTaskController);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;module.exports = router;&lt;/code&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Runtime Environment and the Javascript Engine</title>
      <dc:creator>Aisha A.</dc:creator>
      <pubDate>Mon, 04 Oct 2021 19:50:25 +0000</pubDate>
      <link>https://forem.com/aisha0926/runtime-environment-and-the-javascript-engine-5571</link>
      <guid>https://forem.com/aisha0926/runtime-environment-and-the-javascript-engine-5571</guid>
      <description>&lt;p&gt;Beginners often jump into coding without fully/partially understanding how the internet works or the runtime environment and the javascript engine. &lt;/p&gt;

&lt;p&gt;It may or may not be beneficial to you depending on your goal. &lt;/p&gt;

&lt;p&gt;However, if you aim to understand how things are connected and their process under the hood then you've come to the right place.&lt;/p&gt;

&lt;p&gt;First of all, let's understand how the internet works from the moment you visit a site. In this example, we'll be using aifolio-journal.herokuapp.com. &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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjf93qvefeh9fnqudo9dw.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjf93qvefeh9fnqudo9dw.png" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you visit "aifolio-journal.herokuapp.com", it will request your ISP (Internet Service Provider) which will then conduct a DNS lookup on the URL entered. DNS lookup is basically like yellow pages where every person has a designated name, address, and phone number. The name in this instance will be the URL, your ISP will check what is the address for the name provided (which is aifolio-journal.herokuapp.com, you can check the IP address by opening your command prompt and run the syntax "ping google.com").&lt;/p&gt;

&lt;p&gt;Upon finding the address, the ISP will send it back to the user which in turn will make a direct request to the server with the IP provided. &lt;/p&gt;

&lt;p&gt;The response will depend on how the route is handled. In this instance, the response will have an HTML file, CSS, and JS. &lt;/p&gt;

&lt;p&gt;The Javascript will run in the runtime environment; but &lt;strong&gt;what exactly is a runtime environment&lt;/strong&gt;? Think of it as a container that has all the necessary components to run your code. It will have the Javascript Engine, Web API, and a Callback queue. &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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0xww0duvhez7izrn9aes.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0xww0duvhez7izrn9aes.png" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The engine comprises 2 components, the Callstack where the codes are executed, and the Heap memory where all the objects are stored.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpbfmreolcr7biwq604pu.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpbfmreolcr7biwq604pu.png" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When the code first enters the engine, the engine will read it and then parse it into an AST (Abstract Syntax Tree) where the codes are de-structured in a meaningful way. For instance, if you have a code as const x = 10, it will be converted into AST (See image below)&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F555slrhr71kyhqgokmk6.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F555slrhr71kyhqgokmk6.png" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The parsed codes (AST Objects) are the ones that are compiled into machine language (0's and 1's). Before we delve deeper, let's first take into consideration the difference between compilation and interpretation, which is the step the comes after the Parsing step.&lt;/p&gt;

&lt;p&gt;An example of the compilation are programs that you've installed on your machine, you may execute them at any time but they are not compiled before execution, rather, they are compiled and bundled long before you even execute it. If you're familiar with Java then to illustrate that, when you compile your code, it provides you with a .class file; a file that is compiled into a machine code.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F266z2j3pw0h9iazaurkr.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F266z2j3pw0h9iazaurkr.png" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;An interpreted language is what Javascript historically used. Where the codes are executed after compilation on a line-by-line basis. This is way slower than compilation as it needs to read each line of code before executing it. Imagine using Google Maps; whenever you pan around, there will be a second or more delay as it needs to calculate your location and the image that needs to be displayed. &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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi3m1a2yo6b4c62v93ad2.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi3m1a2yo6b4c62v93ad2.png" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;JIT or Just-in-time compilation is a mixture of both compilation and interpreted language. It will read the code, compile it and then execute it; whilst the code is still running, it will fire up the optimization, optimizing the compiled code, recompiling it and then executing it. The latter process will enter a loop until the compiled code is fully optimized. The reason for this process is for faster and more efficient code execution, the faster it spits out compiled codes, the faster it can be executed.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjgzescwlxl5wz4ic8190.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjgzescwlxl5wz4ic8190.png" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
