<?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: Nikhil Bhutani</title>
    <description>The latest articles on DEV Community by Nikhil Bhutani (@nikkbh).</description>
    <link>https://dev.to/nikkbh</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%2F243205%2F6470ad7a-2194-4cde-8776-97d1f3f90846.jpg</url>
      <title>DEV Community: Nikhil Bhutani</title>
      <link>https://dev.to/nikkbh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nikkbh"/>
    <language>en</language>
    <item>
      <title>8 ways to Handle State in React</title>
      <dc:creator>Nikhil Bhutani</dc:creator>
      <pubDate>Thu, 20 Apr 2023 13:11:31 +0000</pubDate>
      <link>https://dev.to/nikkbh/8-ways-to-handle-state-in-react-ch6</link>
      <guid>https://dev.to/nikkbh/8-ways-to-handle-state-in-react-ch6</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In this blog we will look into 8 techniques used in React to handle state. I will briefly cover each of the method and list down some examples of when a certain state management method can be used.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;1) URL&lt;/strong&gt;&lt;br&gt;
It is the most visible piece of state we can access. URL should be considered as a record; read from it when necessary and update it when needed. It stores the current app location and certain app settings:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Item we are currently viewing, e.g; record id&lt;/li&gt;
&lt;li&gt;Filters&lt;/li&gt;
&lt;li&gt;Pagination&lt;/li&gt;
&lt;li&gt;Sorting&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Having state in the URL helps us to share links to certain parts of our application easily. &lt;strong&gt;&lt;em&gt;React Router&lt;/em&gt;&lt;/strong&gt; is an excellent third party library to handle URL state and is widely used in production applications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2) Web Storage&lt;/strong&gt;&lt;br&gt;
Some native browser technologies such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;local storage &lt;/li&gt;
&lt;li&gt;cookies &lt;/li&gt;
&lt;li&gt;IndexedDB 
are used to handle application state. 
These are used to &lt;em&gt;&lt;u&gt;persist state between page reloads&lt;/u&gt;&lt;/em&gt;.
Some of it's usage includes storing shopping cart items, partially completed form data, etc. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;3) Local State&lt;/strong&gt;&lt;br&gt;
When the state is not shared across multiple components and hierarchies, and is used only by one component &lt;strong&gt;&lt;em&gt;Local State&lt;/em&gt;&lt;/strong&gt; is the best choice for state management. Just like local variables in a function the state is not accessible outside of this component and is deleted when the component is destroyed.&lt;br&gt;
Useful for storing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Checkbox status&lt;/li&gt;
&lt;li&gt;Toggles&lt;/li&gt;
&lt;li&gt;Form data and validation status&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;4) Lifted State&lt;/strong&gt;&lt;br&gt;
In case we are dealing with multiple similar components that need access to same data, we should consider &lt;u&gt;lifting the state to a common parent and passing state down via props&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;Use case:- &lt;br&gt;
We have two child components who need to access the same data, then instead of storing the state on them individually we can lift the state to a common parent component that handles it and pass it down via props.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5)Derived State&lt;/strong&gt;&lt;br&gt;
We can even derive state from existing state instead of creating a new state altogether. This approach has two advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Created in sync state which is bug-free.&lt;/li&gt;
&lt;li&gt;Simplifies code for better understading of the state flow.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Examples:-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Instead of storing a separate state of the count of items for an array, we could simply derive it by using &lt;strong&gt;&lt;em&gt;.length&lt;/em&gt;&lt;/strong&gt; array method. No need to update a new state when array contents change.&lt;/li&gt;
&lt;li&gt;Validate a form: We don't need a separate state to store the from validation status, we can simply derive that from the form.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;6) Context&lt;/strong&gt;&lt;br&gt;
Context is used for global state management. The advantage of using context is that it avoids &lt;a href="https://dev.to/iamrishavraj1/what-is-react-prop-drilling-and-context-api-cjl"&gt;&lt;strong&gt;&lt;em&gt;prop drilling&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Best examples of using Context are:-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;User login information.&lt;/li&gt;
&lt;li&gt;Role related information.&lt;/li&gt;
&lt;li&gt;Global app settings such as Themes.&lt;/li&gt;
&lt;li&gt;Internationalization settings.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;7) Refs&lt;/strong&gt;&lt;br&gt;
React does support state management via &lt;a href="https://legacy.reactjs.org/docs/refs-and-the-dom.html"&gt;Refs&lt;/a&gt;. Some cases where Refs might be helpful are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;DOM Reference: Uncontrolled form inputs whose values are not controlled by React.

&lt;ul&gt;
&lt;li&gt;State that is not displayed: Component is mounted, Hold Timer, Previous state values for features like undo and redo. &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;8) Third Party Libraries&lt;/strong&gt;&lt;br&gt;
Now, to handle state via above method is a great starting point, but as the application grows in size we need to have some state management mechanism in place. That's when Third Party libraries which are built over the above state handling approaches, simplify state management for us.&lt;/p&gt;

&lt;p&gt;Some of the famous libraries that are currently used in the market are:-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://react-redux.js.org/"&gt;React Redux&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://mobx.js.org/README.html"&gt;Mobx&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://recoiljs.org/"&gt;Recoil&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For managing state that involves handling data from API calls there are other great libraries:-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.npmjs.com/package/react-query"&gt;React-Query&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://swr.vercel.app/"&gt;SWR&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.apollographql.com/docs/react/"&gt;Apollo&lt;/a&gt; - &lt;em&gt;For GraphQL&lt;/em&gt;
-&lt;a href="https://relay.dev/"&gt;Relay&lt;/a&gt; &lt;em&gt;For GraphQL&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I hope you found this compilation of some of the best ways to handle state management in React helpful. Adding all the useful resources related to the above state management techniques in the References below, so do check them out. Feel free to reach out in the comments section.&lt;/p&gt;

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




&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://legacy.reactjs.org/docs/getting-started.html"&gt;https://legacy.reactjs.org/docs/getting-started.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pierrehedkvist.com/posts/react-state-url"&gt;https://pierrehedkvist.com/posts/react-state-url&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://react.dev/learn/sharing-state-between-components#:%7E:text=Sometimes%2C%20you%20want%20the%20state,down%20to%20them%20via%20props"&gt;https://react.dev/learn/sharing-state-between-components#:~:text=Sometimes%2C%20you%20want%20the%20state,down%20to%20them%20via%20props&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/comsystoreply/totally-in-sync-using-derived-state-in-react-applications-98cbd5acaa91"&gt;https://medium.com/comsystoreply/totally-in-sync-using-derived-state-in-react-applications-98cbd5acaa91&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>react</category>
      <category>architecture</category>
      <category>javascript</category>
      <category>frontend</category>
    </item>
    <item>
      <title>How to create a PostgreSQL Database on Azure</title>
      <dc:creator>Nikhil Bhutani</dc:creator>
      <pubDate>Tue, 18 Apr 2023 10:57:14 +0000</pubDate>
      <link>https://dev.to/nikkbh/how-to-create-a-postgresql-database-on-azure-2go6</link>
      <guid>https://dev.to/nikkbh/how-to-create-a-postgresql-database-on-azure-2go6</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In this blog we will learn how to create your own PostgreSQL database on the Azure. We will then generate a SQL script that creates a table on our Azure PostgreSQL Database with dummy data inserted into it.&lt;/p&gt;




&lt;h2&gt;
  
  
  Pre-requisite
&lt;/h2&gt;

&lt;p&gt;An &lt;a href="https://azure.microsoft.com/en-in/free/search/?ef_id=_k_EAIaIQobChMIwcbo-f6y_gIVFXl9Ch0w5w56EAAYASAAEgKQ3_D_BwE_k_&amp;amp;OCID=AIDcmmf1elj9v5_SEM__k_EAIaIQobChMIwcbo-f6y_gIVFXl9Ch0w5w56EAAYASAAEgKQ3_D_BwE_k_&amp;amp;gclid=EAIaIQobChMIwcbo-f6y_gIVFXl9Ch0w5w56EAAYASAAEgKQ3_D_BwE" rel="noopener noreferrer"&gt;Azure Account&lt;/a&gt; &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;Pro Tip:&lt;/strong&gt; If you are a student in college, you can easily get Azure Student Subscription for free without a credit card. With this subscription you get $100 free credit for 12 months as well. Signup here:- &lt;a href="https://azure.microsoft.com/en-in/free/students/" rel="noopener noreferrer"&gt;https://azure.microsoft.com/en-in/free/students/&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Getting started
&lt;/h2&gt;

&lt;p&gt;To follow along this tutorial I hope you have some basic knowledge of what a database is? what is PostgreSQL?, if not do check these resources:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.oracle.com/in/database/what-is-database/" rel="noopener noreferrer"&gt;What is database?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.postgresql.org/about/" rel="noopener noreferrer"&gt;What is PostgreSQL?&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Create the database on Azure&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Go to &lt;a href="https://portal.azure.com" rel="noopener noreferrer"&gt;Azure Portal&lt;/a&gt; and sign-in using your account.&lt;/li&gt;
&lt;li&gt;Once you are on the portal search for a big plus icon that says &lt;strong&gt;&lt;em&gt;Create a resource&lt;/em&gt;&lt;/strong&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%2Fv0t4mahjci6n3vcdy8uf.png" alt="Create a resource"&gt;
&lt;/li&gt;
&lt;li&gt;Search for "postgresql" in the  search box and select &lt;strong&gt;&lt;em&gt;Create&lt;/em&gt;&lt;/strong&gt; on  the &lt;strong&gt;Azure Database for PostgreSQL Flexible Server&lt;/strong&gt; as seen below.
&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%2Fe0dldsxwcp9pohjw4tdl.png" alt="Azure Database for PostgreSQL Flexible Server"&gt;
&lt;/li&gt;
&lt;li&gt;A page will open asking for Basic details for this flexible server.
First one being the &lt;strong&gt;Project details&lt;/strong&gt;.

&lt;ul&gt;
&lt;li&gt;Select your &lt;strong&gt;&lt;em&gt;Subscription&lt;/em&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Select a &lt;strong&gt;&lt;em&gt;Resource group&lt;/em&gt;&lt;/strong&gt; or create a new one like below.
&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%2Fo5vdt53dpra6p4groc6c.png" alt="Project details"&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Next, we enter the server details. Select the options as seen below. Server name does not have to be the same.
&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%2Fyvo8s9png3o6744yz6z8.png" alt="Server details"&gt;
&lt;/li&gt;
&lt;li&gt;Skip the High Availablity section and move onto Authentication.
Select &lt;strong&gt;&lt;em&gt;PostgreSQL authentication only&lt;/em&gt;&lt;/strong&gt; and enter the Admin username, password you like.
&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%2Fb5ix6yblr6mhovu4kgga.png" alt="Authentication"&gt;
Click on &lt;strong&gt;&lt;em&gt;Next: Networking &amp;gt;&lt;/em&gt;&lt;/strong&gt; and move to the next configuration.&lt;/li&gt;
&lt;li&gt;In the Networking tab we will allow our computer/laptop to be able to connect to the Azure database. 
For this we will click on the '&lt;strong&gt;&lt;em&gt;Add current client IP address (YOUR IP ADDRESS)&lt;/em&gt;&lt;/strong&gt; in &lt;strong&gt;Firewall Rules&lt;/strong&gt; section.
&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%2F2eop9ryrp61dh41kvgdy.png" alt="Firewall Rules"&gt;
&lt;/li&gt;
&lt;li&gt;Lastly, we will click on &lt;strong&gt;&lt;em&gt;Review + Create&lt;/em&gt;&lt;/strong&gt; button and review out details and then click &lt;strong&gt;&lt;em&gt;Create&lt;/em&gt;&lt;/strong&gt; button as a final step.
&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%2Fss4gozklg2ujg1xx23lp.png" alt="Review+Create"&gt;
&lt;/li&gt;
&lt;li&gt;You should see your deployment in progress. It will take some time to create the resource and when it does you should see a screen like below with your connection details.
&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%2F8gvvcp1f0s2y5fjh7jdz.png" alt="Deployment page"&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Connecting to the database&lt;/strong&gt;&lt;br&gt;
To connect to your newly created database we will head over to our created resource.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fko715wj7xj2uz6ebz1bg.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%2Fko715wj7xj2uz6ebz1bg.png" alt="Resource page"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To connect to the database we have two options:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;pgAdmin&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;psql cli&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With the given server name and credentials you can connect to it using either of the two options but I will be using &lt;strong&gt;psql&lt;/strong&gt; in powershell.&lt;/p&gt;

&lt;p&gt;Open a powershell/commmand prompt and type the below command by replacing the connection details to your server details:&lt;br&gt;
&lt;code&gt;psql --host=&amp;lt;servername&amp;gt; --port=&amp;lt;port&amp;gt; --username=&amp;lt;user&amp;gt; --dbname=&amp;lt;dbname&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SUCCESS!&lt;/strong&gt; You should see the connection was sucessfull and the psql prompt pointing to postgres database.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fca55b3fgorcsbgwz17oi.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%2Fca55b3fgorcsbgwz17oi.png" alt="Connecting to database"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Creating a table with dummy data&lt;/strong&gt;&lt;br&gt;
We will create a SQL script that will create a table named &lt;strong&gt;Car&lt;/strong&gt; and Insert into this table 1000 records.&lt;/p&gt;

&lt;p&gt;We can generate this SQL script by simply heading over to &lt;a href="https://www.mockaroo.com/" rel="noopener noreferrer"&gt;Mockaroo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Mockaroo&lt;/strong&gt; is the best website to generate mock data for your projects. It's highly customizable in terms of the types of fields you want in your data, precision of the data, leaving a certain percentage of data blank and so on. You can just download the mock data in a number of fromats available.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add in the fields as seen in the below image. &lt;/li&gt;
&lt;li&gt;Select the &lt;strong&gt;Format&lt;/strong&gt; as SQL and give the table a name. &lt;/li&gt;
&lt;li&gt;Check the option which says &lt;strong&gt;&lt;em&gt;inlcude CREATE TABLE&lt;/em&gt;&lt;/strong&gt; and click &lt;strong&gt;Download Data&lt;/strong&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%2Fjsy606fs6t4er7e7arbx.png" alt="Mockaroo dummy data"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Open the downloaded &lt;strong&gt;&lt;em&gt;car.sql&lt;/em&gt;&lt;/strong&gt; script in an editor.&lt;br&gt;
You will mainly see two SQL commands.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;CREATE&lt;/strong&gt; - This will create a table in our database with the given columns.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;INSERT INTO&lt;/strong&gt; - This will insert data into our table 'Car'.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I will modify this script a bit and add some constraints such as all columns to be &lt;strong&gt;NOT NULL&lt;/strong&gt; and id column should be a &lt;strong&gt;PRIMARY KEY&lt;/strong&gt;. The resultant script should look like this:&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffnpp486nf0okwehnm3z5.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%2Ffnpp486nf0okwehnm3z5.png" alt="Car.sql"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Running the script on our Azure PostgreSQL database&lt;/strong&gt;&lt;br&gt;
Make sure you are connected to the database as we saw in the section above.&lt;br&gt;
In the PLSQL CLI we will type the following command to run the script.&lt;br&gt;
&lt;code&gt;\i 'D:/Tutorial/car.sql'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;\i&lt;/code&gt; - It means we want to execute the commands from the file path mentioned after this.&lt;/p&gt;

&lt;p&gt;After hitting the above command with the correct path to car.sql script you should see multiple &lt;code&gt;INSERT 0 1&lt;/code&gt; in the terminal which means that the records are being inserted into the table. It should take some time as there 1000 records.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frhvn8x53afm1c13uw8ot.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%2Frhvn8x53afm1c13uw8ot.png" alt="Insert Into"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Final Result&lt;/strong&gt;&lt;br&gt;
You should see the data has been inserted into the database by executing the following SQL:&lt;br&gt;
&lt;code&gt;SELECT * FROM car;&lt;/code&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0qgcnm06hfnraafqkmly.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%2F0qgcnm06hfnraafqkmly.png" alt="SELECT"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;IMPORTANT:&lt;/strong&gt; After you are done with trying out your database make sure to clean up the resource on Azure so that you don't incur charges on your credit card.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;We have now created our own PostgreSQL Database hosted on Azure without the need to configuring a server to host the same. We also created a table and inserted dummy data into it using Mockaroo. &lt;/p&gt;

&lt;p&gt;I hope you found this blog helpful and feel free to reach out in the comments section for any doubts or suggestions. Thank you for reading.&lt;/p&gt;

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

&lt;p&gt;Cover Image Credits: &lt;a href="https://www.freepik.com/free-vector/server-room-cloud-storage-icon-datacenter-database-concept-data-exchange-process_3628676.htm#query=database&amp;amp;position=43&amp;amp;from_view=search&amp;amp;track=robertav1_2_sidr" rel="noopener noreferrer"&gt;Image by fullvector&lt;/a&gt; on Freepik&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://learn.microsoft.com/en-us/azure/postgresql/single-server/overview" rel="noopener noreferrer"&gt;https://learn.microsoft.com/en-us/azure/postgresql/single-server/overview&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.oracle.com/in/database/what-is-database/" rel="noopener noreferrer"&gt;https://www.oracle.com/in/database/what-is-database/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.postgresql.org/about/" rel="noopener noreferrer"&gt;https://www.postgresql.org/about/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://learn.microsoft.com/en-us/azure/postgresql/flexible-server/quickstart-create-server-portal" rel="noopener noreferrer"&gt;https://learn.microsoft.com/en-us/azure/postgresql/flexible-server/quickstart-create-server-portal&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>azure</category>
      <category>postgres</category>
      <category>database</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Web Development Beginner's Learning Roadmap- API Development</title>
      <dc:creator>Nikhil Bhutani</dc:creator>
      <pubDate>Sun, 16 Apr 2023 04:00:00 +0000</pubDate>
      <link>https://dev.to/nikkbh/web-development-beginners-learning-roadmap-api-development-2o83</link>
      <guid>https://dev.to/nikkbh/web-development-beginners-learning-roadmap-api-development-2o83</guid>
      <description>&lt;p&gt;In this blog we will cover technologies a beginner web developer can learn on the API development and also explore some frameworks that support rapid extensive backend development support with feature rich API's. This is part-2 of two part blog, the first blog covered the frontend. If you missed that feel free to check it out- &lt;a href="https://dev.to/nikkbh/web-development-beginners-learning-roadmap-frontend-2d7"&gt;Web Development Beginner's Learning Roadmap- Frontend&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting started
&lt;/h2&gt;

&lt;p&gt;Backend development is quite different than frontend, while writing code on the client-side we do get instant feedback as to where we are headed with the results in front of our eyes, but developing backend requires some basic understanding of software architecture.&lt;/p&gt;

&lt;p&gt;UI of your application gets all its data such user information, login details, application data, etc from a database which stores all this information in various tables. But, the data is not directly fetched from the databse, we need a middleware called &lt;strong&gt;&lt;em&gt;API or Application Programming Interface.&lt;/em&gt;&lt;/strong&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxeas4k47hjzpftmnaax1.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%2Fxeas4k47hjzpftmnaax1.png" alt="Database"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;API's connect to the database and &lt;strong&gt;&lt;em&gt;abstract&lt;/em&gt;&lt;/strong&gt; away the business logic to perform CRUD operations on it. API gets a request of the type GET/PUT/POST/DELETE (&lt;em&gt;which are HTTP verbs for Fetch/Update/Create/Delete&lt;/em&gt;) and respond back with the data.&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%2Fdqjv7qksc4458irdkkel.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%2Fdqjv7qksc4458irdkkel.png" alt="API"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Languages used for backend development
&lt;/h2&gt;

&lt;p&gt;There are many popular languages out there which are used for API development. As a beginner if you have some experience with frontend development then I am guessing you are already familiar with &lt;strong&gt;JavaScript&lt;/strong&gt; already.&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%2Fo4jdlom389pzkc2rie8o.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%2Fo4jdlom389pzkc2rie8o.png" alt="JavaScipt"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript" rel="noopener noreferrer"&gt;JavaScipt&lt;/a&gt; is a versatile language, and apart from being used on the frontend it can also be used to write API's on the backend.&lt;/li&gt;
&lt;/ul&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%2Fehf2dso3au17kwaze8sc.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%2Fehf2dso3au17kwaze8sc.png" alt="Python"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://g.co/kgs/16WvVD" rel="noopener noreferrer"&gt;Python&lt;/a&gt; is yet another language which has become popular for API development. It has some great backend frameworks which can be used to build production ready APIs.&lt;/li&gt;
&lt;/ul&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%2Fsw14jfq5n3ce187pa9vs.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%2Fsw14jfq5n3ce187pa9vs.png" alt="C#"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://en.wikipedia.org/wiki/C_Sharp_(programming_language)" rel="noopener noreferrer"&gt;C#&lt;/a&gt; was designed by Microsoft. It is high-level programming language and it supports a wide arary of development tools. The &lt;strong&gt;.NET&lt;/strong&gt; framework using C# can be used to build anything ranging from UI, API, IoT apps, Windows application, CLIs etc. It is a multipupose language and highly used within banking software applications.&lt;/li&gt;
&lt;/ul&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%2F6ibx0vz516xkhj8vidpd.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%2F6ibx0vz516xkhj8vidpd.png" alt="Java"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://blogs.oracle.com/oracleuniversity/post/why-does-java-remain-so-popular#:~:text=One%20of%20the%20most%20widely,%2C%20games%2C%20and%20numerical%20computing." rel="noopener noreferrer"&gt;Java&lt;/a&gt; is yet another popular language and had the number one spot among the programming langues from 2004 to 2018. The &lt;strong&gt;&lt;em&gt;Spring&lt;/em&gt;&lt;/strong&gt; framework developed in Java is quite popular for API dev, Cloud applications etc.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Frameworks
&lt;/h2&gt;

&lt;p&gt;Here is the list of popular API frameworks/libraries which you can learn as a backend web developer:-&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Express.js&lt;/strong&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc9gu4f1e35zey4ce4big.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%2Fc9gu4f1e35zey4ce4big.png" alt="Express.js"&gt;&lt;/a&gt;&lt;br&gt;
Express is a Node.js web application framework that provides a host of features for web and mobile applications. For API development it provides a lot of HTTP utility methods and middleware to creat APIs quickly. It is great for beginners with experience in JavaScript and you can write a basic CRUD API in Express in less than 30 mins. I have published 2 blogs on getting started with Express, feel free to check them out:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/nikkbh/beginners-guide-to-rest-api-in-javascript-using-express-jp"&gt;Beginner's Guide to REST API in JavaScript using Express&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/nikkbh/exception-handling-and-logging-errors-in-rest-api-using-javascript-express-5h93"&gt;Exception Handling and Logging Errors in REST API using Javascript &amp;amp; Express&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;u&gt;Learning resources:-&lt;/u&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://expressjs.com/en/starter/installing.html" rel="noopener noreferrer"&gt;https://expressjs.com/en/starter/installing.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.youtube.com/watch?v=Oe421EPjeBE" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=Oe421EPjeBE&lt;/a&gt; (Video Tutorial)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Django&lt;/strong&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F88ixcdl2n83jfcq9vrlg.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%2F88ixcdl2n83jfcq9vrlg.png" alt="Django"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. Django REST Framework is a wrapper over default Django Framework, basically used to create APIs of various kinds. It documentation and community support is quite strong. If you are a Python guy then you can surely check it out.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;Learning resources:-&lt;/u&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.django-rest-framework.org/" rel="noopener noreferrer"&gt;https://www.django-rest-framework.org/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.youtube.com/watch?v=F5mRW0jo-U4" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=F5mRW0jo-U4&lt;/a&gt; (Video Tutorial)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;ASP .NET Core&lt;/strong&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0bw1731um3f5lz09e05n.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%2F0bw1731um3f5lz09e05n.png" alt="ASP .NET Core"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;ASP.NET Core is a free, open-source, and cross-platform framework for building cloud-based applications, such as web apps, IoT apps, and mobile backends. It is designed to run on the cloud as well as on-premises. It supports creating web APIs using controllers or using minimal APIs. Controllers in a web API are classes that derive from ControllerBase. All you need it Visual Studio as .NET Core comes preinstalled with it. This framewok is usually preferred by veterans already having experience with C# and may not be a good starting choice for a beginner backend web developer.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;Learning resources:-&lt;/u&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-web-api" rel="noopener noreferrer"&gt;https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-web-api&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.youtube.com/watch?v=5YB49OEmbbE" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=5YB49OEmbbE&lt;/a&gt; (Video Tutorial)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Spring&lt;/strong&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F89ojsbu124dyoykwq92w.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%2F89ojsbu124dyoykwq92w.png" alt="Spring"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Spring Framework is an application framework and inversion of control container for the Java platform. The framework's core features can be used by any Java application, but there are extensions for building web applications on top of the Java EE platform. &lt;strong&gt;&lt;em&gt;Spring Boot&lt;/em&gt;&lt;/strong&gt; separates the boiler-plate code and configuration settings in an application to highlight focus on REST API development. All the JAVA folks will tend to love Spring Boot as it is easy to implement APIs in. &lt;/p&gt;

&lt;p&gt;&lt;u&gt;Learning resources:-&lt;/u&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://spring.io/guides/gs/rest-service" rel="noopener noreferrer"&gt;https://spring.io/guides/gs/rest-service&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.youtube.com/watch?v=9SGDpanrc8U" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=9SGDpanrc8U&lt;/a&gt; (Video Tutorial)&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I hope you found this blog informative. The resources mentioned above are sufficient enough for you to get started in API development, but you can explore more. Thank you for reading.&lt;/p&gt;

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




&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/What_is_JavaScript#:%7E:text=JavaScript%20is%20a%20scripting%20language,and%20pretty%20much%20everything%20else" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/What_is_JavaScript#:~:text=JavaScript%20is%20a%20scripting%20language,and%20pretty%20much%20everything%20else&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Python_(programming_language)" rel="noopener noreferrer"&gt;https://en.wikipedia.org/wiki/Python_(programming_language)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/C_Sharp_(programming_language)" rel="noopener noreferrer"&gt;https://en.wikipedia.org/wiki/C_Sharp_(programming_language)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://blogs.oracle.com/oracleuniversity/post/why-does-java-remain-so-popular#:%7E:text=One%20of%20the%20most%20widely,%2C%20games%2C%20and%20numerical%20computing" rel="noopener noreferrer"&gt;https://blogs.oracle.com/oracleuniversity/post/why-does-java-remain-so-popular#:~:text=One%20of%20the%20most%20widely,%2C%20games%2C%20and%20numerical%20computing&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;a href="https://expressjs.com/" rel="noopener noreferrer"&gt;https://expressjs.com/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://spring.io/" rel="noopener noreferrer"&gt;https://spring.io/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>api</category>
      <category>backend</category>
    </item>
    <item>
      <title>Web Development Beginner's Learning Roadmap- Frontend</title>
      <dc:creator>Nikhil Bhutani</dc:creator>
      <pubDate>Thu, 13 Apr 2023 09:21:47 +0000</pubDate>
      <link>https://dev.to/nikkbh/web-development-beginners-learning-roadmap-frontend-2d7</link>
      <guid>https://dev.to/nikkbh/web-development-beginners-learning-roadmap-frontend-2d7</guid>
      <description>&lt;p&gt;Web Development is probably the easiest tech stack to start learning as a beginner. When I was in college web development technologies instantly caught my eyes, and I started exploring it right away. The best part about being a web developer is that you get to see your code paint a magic canvas on the screen, which makes you proud of the work done. In this blog I will guide you through what you should learn when starting out as a web developer and all the related technologies you can explore as you become a pro in this journey.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;These are my personal opinions and suggestions and can be different for you, the technlogies are limitless and you are free to explore anything that intrigues you.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Getting started
&lt;/h2&gt;

&lt;p&gt;Web devlopment can be broadly classified into three categories:-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Frontend&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Backend&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Operations(More like DevOps)&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All three of them involve &lt;em&gt;creating, building, testing and maintaining&lt;/em&gt; of websites and applications that serve on the internet. In this blog we will explore the first cateogry- &lt;strong&gt;Frontend&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;But let's take a pause on that for a minute and let me tell what you should learn before you start to learn any new tech stack.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Git(Version Control)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FKyYdBDu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nhzy5cm9etn1dpmsoce9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FKyYdBDu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nhzy5cm9etn1dpmsoce9.png" alt="Git- Version Control" width="282" height="179"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Version control facilitates coordination, sharing, and collaboration across the entire software development team. It enables teams to work in distributed and asynchronous environments, manage changes and versions of code and artifacts, and resolve merge conflicts and related anomalies.&lt;br&gt;
&lt;a href="https://about.gitlab.com/topics/version-control/#the-basics-of-version-control"&gt;Reference&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Every developer should know how to work with Git(a Version Control Software) and it's terminologies such as push, pull, commit, stage etc. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It is a &lt;em&gt;&lt;strong&gt;strong&lt;/strong&gt;&lt;/em&gt; recommendation to create a git repository on &lt;a href="https://github.com/"&gt;GitHub&lt;/a&gt; for all of your future projects as it helps you display you work to the world and adds credibility to what you do.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;u&gt;Learning Resources&lt;/u&gt;:-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.github.com/en/get-started/quickstart/git-and-github-learning-resources"&gt;https://docs.github.com/en/get-started/quickstart/git-and-github-learning-resources&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.w3schools.com/git/"&gt;https://www.w3schools.com/git/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Clean coding principles&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FFCWiH7H--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t45t0zhwv9rdef7hy4sh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FFCWiH7H--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t45t0zhwv9rdef7hy4sh.png" alt="Clean Code" width="299" height="168"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Writing clean code is something you learn over time as a developer but atleast we can keep some points in mind while writing code which helps us write better quality code, increase maintainability, makes degugging easy and following standard coding patterns. You can learn more about it in this &lt;a href="https://betterprogramming.pub/12-conventions-for-writing-clean-code-e16c51e3939a"&gt;article&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Frontend
&lt;/h2&gt;

&lt;p&gt;To start out you will probably want to learn the technologies that &lt;br&gt;
are forward facing or in other words websites and appliation which we see and interact with as a user. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LKExAR9---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1it3v20ty2bi20hl1e2y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LKExAR9---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1it3v20ty2bi20hl1e2y.png" alt="Frontend" width="213" height="237"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You should start out by learning the following things in the order below but can shuffle if you like.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;HTML&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;HTML&lt;/strong&gt; is the language of websites and is used designing documents to be displayed in a web browser. It is fun to learn HTML and invloves knowing a bunch of tags and what their purpose is. It's like formatting a word document using different tools such as bold, list items, paragraphs etc.&lt;br&gt;
&lt;u&gt;Learning Resources&lt;/u&gt;:- &lt;br&gt;
One of the best resources out there to learn HTML is &lt;a href="https://www.w3schools.com/html/"&gt;W3schools&lt;/a&gt;. You would come across this site a lot in your web development journey and is known to be the dictionay of it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;CSS&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;CSS&lt;/strong&gt; stands for &lt;em&gt;Cascading-Style-Sheets&lt;/em&gt; and it is what makes your websites look and feel amazing. It is used to style your website components such as buttons, input box, logo or anything you see on the website. It is also used to create animations and effects along with making your sites responsive for screens such as mobile devices and tablets. &lt;br&gt;
&lt;u&gt;Learning Resources&lt;/u&gt;:-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.w3schools.com/css/"&gt;W3schools- CSS Tutorials&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://css-tricks.com/"&gt;CSS Tricks&lt;/a&gt; - You can find mind blowing css designs here to get some insipiration and also use them in your websites.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;JavaScript&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zp-tDP-c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7sibbvl12m9sbhuob9b9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zp-tDP-c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7sibbvl12m9sbhuob9b9.png" alt="JavaScript" width="225" height="225"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;JavaScript&lt;/strong&gt; or JS is a programming language that is used to handle webpage behaviour and interactions that user make such as clicking a button, scrolling, mouse events and much more. It is not only used on the frontend but also used to build backend and API development using Node.js. There is a lot of community support for JavaScript and many third-party libraries developed over it to help you speed up your Web development. It is practically the core and heart of you web application.&lt;br&gt;
&lt;u&gt;Learning Resources&lt;/u&gt;:-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://javascript.info/"&gt;https://javascript.info/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.codecademy.com/learn/introduction-to-javascript"&gt;https://www.codecademy.com/learn/introduction-to-javascript&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Frontend Libraries/Frameworks
&lt;/h2&gt;

&lt;p&gt;After you have mastered HTML, CSS and JavaScript you can pretty much build any web application with them. All the websites you see on the internet use the same HTML, CSS and JavaScript, but there are some frameworks which have now become a must learn when starting out as a frontend developer. These frameworks help you speed up your application development, provide a vast array of tools and come with great community support and documentations.&lt;/p&gt;

&lt;p&gt;Let's explore some of the frameworks which are popular and you should learn:-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Bootstrap&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;&lt;a href="https://getbootstrap.com/"&gt;Bootstrap&lt;/a&gt; is a free and open-source CSS framework directed at responsive, mobile-first front-end web development. It contains HTML, CSS and JavaScript-based design templates for typography, forms, buttons, navigation, and other interface components. &lt;a href="https://en.wikipedia.org/wiki/Bootstrap_(front-end_framework)"&gt;Reference&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It has a suite of components used in a website pre-built for you which is responsive out of the box and can be customized to your website's style. It provides support for flexbox and grid based system as well.&lt;/p&gt;

&lt;p&gt;There are other CSS frameworks out there which are also preferred and becoming popular such as &lt;a href="https://tailwindcss.com/"&gt;TailwindCSS&lt;/a&gt;. You can surely check them out as well.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;React&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7uTiyYJU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n0rlyskezsm08c15v57a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7uTiyYJU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n0rlyskezsm08c15v57a.png" alt="React" width="259" height="194"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When it comes to front-end development, you must have heard about &lt;a href="https://react.dev/"&gt;React&lt;/a&gt;. It is one of the most popular front-end library out there built by Facebook in 2013. It is a JavaScipt library used to front-end in a component based architechture. Every button, text, image, sections, forms you see on a site can be a component. These components have a state which can change based on certain events be it user driven or code behaviour.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;React&lt;/strong&gt; is built on the same principle keeping the component based architechture in mind which is great for testing and reusability in any appliation. There are many high paying jobs for React developers and is must learn library for every front-end developer.&lt;br&gt;
&lt;u&gt;Learning Resources&lt;/u&gt;:-&lt;br&gt;
&lt;a href="https://react.dev/learn"&gt;React Documentation&lt;/a&gt; - Probably the best resouce out there to learn React&lt;br&gt;
There are many great YouTube vidoes on it as well.&lt;/p&gt;

&lt;p&gt;Apart from React there are other great front-end frameworks such as &lt;a href="https://angular.io/"&gt;Angular&lt;/a&gt; and &lt;a href="https://vuejs.org/"&gt;Vue.js&lt;/a&gt;. Angular is different than React as it is a full fledged framework while React is just a JavaScript library.&lt;/p&gt;




&lt;h2&gt;
  
  
  Final Words
&lt;/h2&gt;

&lt;p&gt;I hope you found this blog helpful in starting out your web development journey as a front-end developer. You are not limited to the technologies mentioned in this blog can can discover many more but I hope this gives you a headstart.&lt;/p&gt;

&lt;p&gt;If you have some suggestions or doubts feel free to drop a comment below.&lt;/p&gt;

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




&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://en.wikipedia.org/wiki/Web_development#:%7E:text=Web%20development%20is%20the%20work,businesses%2C%20and%20social%20network%20services"&gt;https://en.wikipedia.org/wiki/Web_development#:~:text=Web%20development%20is%20the%20work,businesses%2C%20and%20social%20network%20services&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;a href="https://about.gitlab.com/topics/version-control/#the-basics-of-version-control"&gt;https://about.gitlab.com/topics/version-control/#the-basics-of-version-control&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/HTML"&gt;https://en.wikipedia.org/wiki/HTML&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS"&gt;https://developer.mozilla.org/en-US/docs/Web/CSS&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Bootstrap_(front-end_framework)"&gt;https://en.wikipedia.org/wiki/Bootstrap_(front-end_framework)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://angular.io/"&gt;https://angular.io/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://vuejs.org/"&gt;https://vuejs.org/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>frontend</category>
      <category>programming</category>
    </item>
    <item>
      <title>Exception Handling and Logging Errors in REST API using Javascript &amp; Express</title>
      <dc:creator>Nikhil Bhutani</dc:creator>
      <pubDate>Sat, 01 Apr 2023 04:00:00 +0000</pubDate>
      <link>https://dev.to/nikkbh/exception-handling-and-logging-errors-in-rest-api-using-javascript-express-5h93</link>
      <guid>https://dev.to/nikkbh/exception-handling-and-logging-errors-in-rest-api-using-javascript-express-5h93</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In this blog we are going to add exception handling to JavaScript REST API by coding our own &lt;strong&gt;&lt;em&gt;Exception Middleware&lt;/em&gt;&lt;/strong&gt;. We will also learn how to add &lt;strong&gt;&lt;em&gt;multiple exception handlers&lt;/em&gt;&lt;/strong&gt; to our API and &lt;em&gt;build a reusable error module&lt;/em&gt; that can be handy to when having multiple repositories in an API. Lastly, we will also see how we can add &lt;strong&gt;&lt;em&gt;error logging&lt;/em&gt;&lt;/strong&gt; capabilities to our code which logs errors to a file.&lt;/p&gt;




&lt;h2&gt;
  
  
  Setup
&lt;/h2&gt;

&lt;p&gt;In this blog I will &lt;em&gt;not&lt;/em&gt; be covering how to build a REST API in JavaScript, and I will use the exisiting GroceryAPI that I built in my other blog. It covers all the coding required to build an API from scratch in JavaScript using the &lt;strong&gt;Express&lt;/strong&gt; framework, so do check it out:- &lt;em&gt;&lt;strong&gt;&lt;a href="https://dev.to/nikkbh/beginners-guide-to-rest-api-in-javascript-using-express-jp"&gt;Beginner's Guide to REST API in JavaScript using Express&lt;/a&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The completed code for this tutorial can be found in the following &lt;a href="https://github.com/nikkbh/GroceryAPI/tree/exception-handling" rel="noopener noreferrer"&gt;GitHub Repo&lt;/a&gt; in the &lt;u&gt;&lt;em&gt;exception-handling&lt;/em&gt;&lt;/u&gt; branch.&lt;/p&gt;




&lt;h2&gt;
  
  
  Code
&lt;/h2&gt;

&lt;p&gt;We'll start by creating a reusable error module. &lt;/p&gt;

&lt;p&gt;Create a file named &lt;code&gt;errorHelper.js&lt;/code&gt; in the &lt;code&gt;/helpers&lt;/code&gt; directory&lt;/p&gt;

&lt;p&gt;We will write our first helper method &lt;code&gt;errorBuilder&lt;/code&gt; inside of the &lt;code&gt;errorHelpers&lt;/code&gt; object which we will export.&lt;br&gt;
&lt;code&gt;errorBuilder&lt;/code&gt; will return a custom object to client with status 500 and statusText as "Internal Server Error", the rest of the object will contain tht error message, error number and error syscall. This errorBuilder will accept an error object. We can then use this errorBuilder to build error messages for logging errors to console, logging errors to file etc.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

let errorHelpers = {
  errorBuilder: (err) =&amp;gt; {
    return {
      status: 500,
      statusText: "Internal Server Error",
      message: err.message,
      error: {
        errno: err.errno,
        call: err.syscall,
        code: "INTERNAL_SERVER_ERROR",
        message: err.message,
      },
    };
  },
};


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

&lt;/div&gt;
&lt;p&gt;Now, we create a &lt;code&gt;logErrorsToConsole&lt;/code&gt; helper function inside errorHelpers which will use &lt;code&gt;errorBuilder&lt;/code&gt; and print the error to the server console whenever it occurs.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In &lt;code&gt;logErrorsToConsole&lt;/code&gt; function we simple do a &lt;code&gt;console.error&lt;/code&gt; of the error by passing our err object to previously created &lt;code&gt;errorBuilder&lt;/code&gt; we simply add * to distinguish the error in the console and then we use the &lt;code&gt;next(err);&lt;/code&gt; to move onto the next middleware available.&lt;/p&gt;

&lt;p&gt;Now let's configure our middleware in index.js&lt;/p&gt;

&lt;p&gt;Simply import the error helper by adding:&lt;br&gt;
&lt;code&gt;let errorHelper = require("./helpers/errorHelpers");&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Then, we will add the middleware just after the router configuration.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

// Configure router so all routes are prefixed with /api/
app.use("/api/", router);

// Configure exception logger to console
app.use(errorHelper.logErrorsToConsole);


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

&lt;/div&gt;
&lt;p&gt;Now to see our error logger middleware work we will change the spelling of the gorceries.json file in the file name variable in &lt;code&gt;groceryRepo.js&lt;/code&gt; by removing the &lt;strong&gt;s&lt;/strong&gt; from the &lt;code&gt;const FILE_NAME = "./assets/grocerie.json";&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Finally, when we hit the GET request to fetch all groceries we should see the error logged in our terminal like 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%2F3j7vilt1chhyotsu9tbv.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%2F3j7vilt1chhyotsu9tbv.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Adding Error Logging to a log file&lt;/strong&gt;&lt;br&gt;
So far we setup our own middleware that logs error to console whenever an Internal Server Error occurs. &lt;br&gt;
What if we want the error logged out in a log file that stores all logs of all the errors occured for fututre refernce?&lt;/p&gt;

&lt;p&gt;We will achieve this in three steps:&lt;br&gt;
&lt;strong&gt;1)&lt;/strong&gt; Create a logRepo.js file to handle the writes to a log file.&lt;br&gt;
&lt;strong&gt;2)&lt;/strong&gt; Add a helper method to write errors to the log file using the logRepo.&lt;br&gt;
&lt;strong&gt;3)&lt;/strong&gt; Add the helper method as a middleware to our index.js file.&lt;/p&gt;

&lt;p&gt;Let's get started!&lt;/p&gt;

&lt;p&gt;First step is to create a &lt;code&gt;logRepo.js&lt;/code&gt; file in the &lt;code&gt;/repo&lt;/code&gt; directory. The code for this will be very similar to the calls to get, update, delete and write data to our grocery store repo. &lt;/p&gt;

&lt;p&gt;We create a write function which takes data, req and res as the parameters.  On line 7 we create a &lt;code&gt;toWrite&lt;/code&gt; variable which will store the information to be logged.&lt;/p&gt;

&lt;p&gt;Notice that we have assigned a file to the &lt;code&gt;FILE_NAME&lt;/code&gt; field on line 3, make sure to create an empty log.txt file in a logs folder.&lt;/p&gt;

&lt;p&gt;We just append all the information that we need to see in our log file such as the &lt;em&gt;&lt;strong&gt;Date and Time&lt;/strong&gt;&lt;/em&gt; the error occured, &lt;em&gt;&lt;strong&gt;Exception Info&lt;/strong&gt;&lt;/em&gt;  and some formatting of the log by appending * at the start and end of the message.&lt;/p&gt;

&lt;p&gt;After that we simply use the &lt;code&gt;fs&lt;/code&gt; package to write the exception message to the log file on our local file system.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Next, we configure a helper method to log errors to log file in the &lt;code&gt;errorHelpers.js&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;We add the &lt;code&gt;logErrorsToFile&lt;/code&gt; method object just after &lt;code&gt;logErrorsToConsole&lt;/code&gt; method. The implementation of which is pretty straight-forward. We build the errorObject using our &lt;code&gt;errorBuilder&lt;/code&gt; and assign some extra properties to it from the request object. Then we call in the &lt;code&gt;logRepo.write&lt;/code&gt; and pass in this errorObject.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;NOTE: Don't forget to import the logRepo in errorHelpers.js file at the top.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

logErrorsToFile: (err, req, res, next) =&amp;gt; {
    let errorObject = errorHelpers.errorBuilder(err);
    errorObject.requestInfo = {
      hostName: req.hostName,
      path: req.path,
      app: req.app,
    };
    logRepo.write(
      errorObject,
      (data) =&amp;gt; {
        console.log(data);
      },
      (err) =&amp;gt; console.error(err)
    );
    next(err);
  }


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

&lt;/div&gt;

&lt;p&gt;Lastly, one final step is to add our middleware to handle this logging functionality in &lt;code&gt;index.js&lt;/code&gt; file just after our logErrorsToConsole middleware.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

// Configure router so all routes are prefixed with /api/
app.use("/api/", router);

// Configure exception logger to console
app.use(errorHelper.logErrorsToConsole);

// Configure exception logger to console
app.use(errorHelper.logErrorsToFile);



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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Testing logging to text file&lt;/strong&gt;&lt;br&gt;
Similar to how we tested our &lt;code&gt;logErrorsToConsole&lt;/code&gt;, just remove the appending 's' from the file name in groceryRepo.js and send a GET request to &lt;code&gt;http://localhost:5000/api/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You should see the error logged to console as well as the exception logged to the log.txt file as seen below with Date and Time.&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%2Fsz04n8asnhiy0q1siz15.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%2Fsz04n8asnhiy0q1siz15.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you have any doubts you can ask in the comments section below, I will be happy to help.&lt;/p&gt;

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




&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://expressjs.com/en/guide/using-middleware.html" rel="noopener noreferrer"&gt;https://expressjs.com/en/guide/using-middleware.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://expressjs.com/en/guide/error-handling.html" rel="noopener noreferrer"&gt;https://expressjs.com/en/guide/error-handling.html&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>tutorial</category>
      <category>webdev</category>
      <category>api</category>
    </item>
    <item>
      <title>Beginner's Guide to REST API in JavaScript using Express</title>
      <dc:creator>Nikhil Bhutani</dc:creator>
      <pubDate>Wed, 22 Mar 2023 11:40:26 +0000</pubDate>
      <link>https://dev.to/nikkbh/beginners-guide-to-rest-api-in-javascript-using-express-jp</link>
      <guid>https://dev.to/nikkbh/beginners-guide-to-rest-api-in-javascript-using-express-jp</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;API&lt;/strong&gt; is the most used keyword in the IT world, and I would be surprised if you haven't heard of it or used it ever as a coder.  &lt;em&gt;Application Programming Interface&lt;/em&gt; is a concept that applies everywhere from command-line tools to enterprise code, microservices, and cloud-native architectures. An API is an interface that software developers use to programmatically interact with software components or resources outside of their own code.&lt;/p&gt;

&lt;p&gt;In this blog we'll learn to code our own API from scratch in JavaScript using Express. This will be a step-by-step tutorial so rest assured nothing will be skipped.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/nikkbh/GroceryAPI"&gt;Project GitHub Repo&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Pre-requisites
&lt;/h2&gt;

&lt;p&gt;We will require the folloiwng things before getting started:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NodeJS&lt;/strong&gt; &lt;br&gt;
Node. js is an open-source, cross-platform JavaScript runtime environment and library for running web applications outside the client's browser. With NodeJS you get &lt;strong&gt;&lt;em&gt;npm&lt;/em&gt;&lt;/strong&gt; which is the package manager which we will use to install express and other packages as required.&lt;br&gt;
&lt;a href="https://nodejs.org/en"&gt;Download Link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Editor&lt;/strong&gt;&lt;br&gt;
I will be using VS Code as my preferred editor but you can use any code editor of your choice.&lt;br&gt;
&lt;a href="https://code.visualstudio.com/"&gt;Download Link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Rest Client&lt;/strong&gt;&lt;br&gt;
We will need a REST client to test our API calls. I will be using &lt;strong&gt;Insomnia&lt;/strong&gt;, but you can use any other client as well. &lt;strong&gt;Postman&lt;/strong&gt; is also popular among them.&lt;br&gt;
&lt;a href="https://insomnia.rest/"&gt;Download Link- Insomnia&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.postman.com/"&gt;Download Link- Postman&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Before we start I would like to introduce &lt;strong&gt;&lt;em&gt;Express&lt;/em&gt;&lt;/strong&gt;. It is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. With a myriad of HTTP utility methods and middleware at your disposal, creating a robust API is quick and easy. I'll leave a referrence to its documentation below incase you want to check it out.&lt;/p&gt;
&lt;h2&gt;
  
  
  Project Setup
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;API Description&lt;/strong&gt;: We will be building an API for a Grocery Store. It will help us fetch all gorcery items data, get single grocery item, add an item to the list of groceries available in the store, update any item, such as price or quantity and also delete the item from the store.&lt;/p&gt;

&lt;p&gt;Firstly, we'll start by creating an empty directory where our project will live. Next open the terminal on the project's root and type in the following command(I am assuming you have nodeJs installed by this point): &lt;code&gt;npm init&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This command will initailize our poject with a &lt;code&gt;pacakage.json&lt;/code&gt; file which will have all our project pacakages, scripts etc.&lt;/p&gt;

&lt;p&gt;After you hit enter you will be asked some questions, most of them need to be left on default, but you can add to some fields as seen below.&lt;/p&gt;

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

&lt;p&gt;I am using VS Code's inbuilt terminal.&lt;/p&gt;

&lt;p&gt;After the package.json file is created we will install some packages using the commands below.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install express&lt;/code&gt;: Express pacakge which we will use to develop API's quickly.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install nodemon&lt;/code&gt;: Nodemon will save us some time by compiling the code every time we hit save and publish the chages automatically, instead of re-running the API everytime we make some change.&lt;/p&gt;

&lt;p&gt;With these two commands completed we shall see our packages with their version in the &lt;code&gt;package.json&lt;/code&gt; file in the dependecies section.&lt;/p&gt;
&lt;h2&gt;
  
  
  Generating Mock data for our API
&lt;/h2&gt;

&lt;p&gt;To work with CRUD functionalities we will need mock data for our Grocery Store. We can generate mock data from &lt;a href="https://www.mockaroo.com/"&gt;Mockaroo&lt;/a&gt;. It generates mock data for all types of fields and number of records. You can generate the dummy data yourself by modifying or copying the fields like the image below and saving it under an assets directory in the project's root directory, or you can find it in the code's git repository under &lt;strong&gt;&lt;em&gt;assets/groceries.json&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--k1XOsHNK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/teq44djw5775nzcwgonw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--k1XOsHNK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/teq44djw5775nzcwgonw.png" alt="Mockaroo- Dummy Grocery data" width="800" height="559"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Let's code!
&lt;/h2&gt;

&lt;p&gt;Now that we have all the setup in place, we can gets our hands dirty with some JavaScript coding.&lt;/p&gt;

&lt;p&gt;Create a file called &lt;code&gt;index.js&lt;/code&gt; in the root of your project and add in the code as seen below.&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Code explanation&lt;/strong&gt;&lt;br&gt;
The first line imports &lt;strong&gt;express&lt;/strong&gt; and the second line will initialize the express server app.&lt;/p&gt;

&lt;p&gt;Next, we use the &lt;code&gt;express.Router()&lt;/code&gt; to configure our router which will hold our API endpoints and configuration to each call. On Line 11, we are configuring our router to prefix all routes with a &lt;strong&gt;&lt;em&gt;/api/&lt;/em&gt;&lt;/strong&gt; so that all API endpoints will have a /api/ before them.&lt;/p&gt;

&lt;p&gt;Lastly, we setup the server to listen on port 5000.&lt;/p&gt;

&lt;p&gt;We'll now add code for our CRUD functionality.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;GET/ Read All Items from Grocery Store&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Create a directory called &lt;strong&gt;repo&lt;/strong&gt; under the root of the project and a file called &lt;code&gt;groceryRepo.js&lt;/code&gt; under it.&lt;br&gt;
Your folder structure should look something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0yOj_2l7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/241lu7s10r59nvnh2lb1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0yOj_2l7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/241lu7s10r59nvnh2lb1.png" alt="Folder structure" width="245" height="300"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The gorceryRepo.js will contain the logic to get/update/delete the data from filesystem(in real world apps it will be a database) and we will use the fs package to work with the filesystem. Our first call will be to get the list of grocery items.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--loqYEFYd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ympmzye4bym08qo6nf8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--loqYEFYd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ympmzye4bym08qo6nf8.png" alt="groceryRepo.js" width="736" height="399"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code explanation&lt;/strong&gt;&lt;br&gt;
We are importing the &lt;code&gt;fs&lt;/code&gt; package on line 1. We create a variable called &lt;code&gt;FILE_NAME&lt;/code&gt; which points to the groceries.js file containing our mock JSON data.&lt;br&gt;
The groceryRepo object contains a function object called &lt;strong&gt;get&lt;/strong&gt; which accepts two parameters &lt;code&gt;resolve&lt;/code&gt; and &lt;code&gt;reject&lt;/code&gt;. These two will handle the data fetch success and failure conditions and return either the data or error.&lt;/p&gt;

&lt;p&gt;On line 7, we read the file by passing the &lt;code&gt;FILE_NAME&lt;/code&gt; and an arrow function  as a second paramter, which inturn takes two parameters &lt;code&gt;err&lt;/code&gt; and &lt;code&gt;data&lt;/code&gt;. If there occurs an error in fetching from the file we call the &lt;code&gt;reject(err)&lt;/code&gt; function passing the error occured to it, else we return the JSON data by parsing it and sending it to &lt;code&gt;resolve(JSON.parse(data))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Next, we make changes to &lt;code&gt;index.js&lt;/code&gt; file.&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Code explanation&lt;/strong&gt;&lt;br&gt;
We use the &lt;code&gt;router.get()&lt;/code&gt; function and specify the endpoint at which we would get the list of grocery items returned to us. The second parameter is again an arrow function which has three parameters:&lt;br&gt;
&lt;code&gt;req&lt;/code&gt;: It will contain the request object that the client send.&lt;br&gt;
&lt;code&gt;res&lt;/code&gt;: The response we want to send back to the client i.e; data requested.&lt;br&gt;
&lt;code&gt;next&lt;/code&gt;: The next method will chain the function call to a middleware which will handle errors. (I will leave this for now in this blog)&lt;/p&gt;

&lt;p&gt;We use the &lt;code&gt;res.status().json()&lt;/code&gt; to send the result back to client with a 200 status and the JSON data and other info we want the client to know. To know more about what status codes are and how many are there you can follow this resource &lt;a href="https://moz.com/learn/seo/http-status-codes"&gt;here&lt;/a&gt;. A status of 200 means the request was responded with success "OK" and all is good.&lt;/p&gt;

&lt;p&gt;Next, to test out our first API we need to add a script in the &lt;code&gt;package.json&lt;/code&gt; file under the scripts. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VdlITBKG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/21re4wz1ru3qx2undnb0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VdlITBKG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/21re4wz1ru3qx2undnb0.png" alt="Image description" width="619" height="252"&gt;&lt;/a&gt;&lt;br&gt;
The &lt;code&gt;start&lt;/code&gt; script will run nodemon and run our express app &lt;br&gt;
To run this script, simply type &lt;code&gt;npm start&lt;/code&gt; in the command line. You app should now be runnning on &lt;em&gt;&lt;a href="http://localhost:5000/api/"&gt;http://localhost:5000/api/&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Let's test our GET API in Insomnia:&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;SUCCESS!&lt;/strong&gt; You just created an API that fetches data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;GET By ID/ Read one single Item from Grocery Store&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now that you can fetch all the items, we can write a code to get a single item from the list of items by its id.&lt;br&gt;
To do that the code will not change much as we saw above, only thing extra will be to get an id from the user in request from client.&lt;/p&gt;

&lt;p&gt;Code changes in &lt;code&gt;index.js&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UdKaKF_8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/83b5cgsg4pe9w352341x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UdKaKF_8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/83b5cgsg4pe9w352341x.png" alt="Get item by id" width="730" height="666"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Code changes in &lt;code&gt;groceryRepo.js&lt;/code&gt;&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Code explanation&lt;/strong&gt;&lt;br&gt;
The code is almost similar to GET All, the things that need explanation are the endpoint &lt;code&gt;/:id&lt;/code&gt; and &lt;code&gt;req.params.id&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The endpoint here is &lt;code&gt;/:id&lt;/code&gt; which will be read by API as the grocery id like &lt;a href="http://localhost:5000/api/2"&gt;http://localhost:5000/api/2&lt;/a&gt;.&lt;br&gt;
The &lt;code&gt;req.params.id&lt;/code&gt; is the grocery id used to pass to the groceryRepo.getById call  which we receive from the request parameters above.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;groceryRepo.js&lt;/code&gt; we define another function getById. The code remains same as get function only change is the return data. We use the &lt;code&gt;array.find()&lt;/code&gt; method to find the item by matching id from the list of groceries and return the match.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dnAYBBZL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/is6fk7msycuytbhglbic.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dnAYBBZL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/is6fk7msycuytbhglbic.png" alt="Get by Id- Result" width="800" height="261"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;POST/ Add item to the list&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Before we add codes for POST/PUT add this line just after line number 7 in &lt;code&gt;index.js&lt;/code&gt;. It will allow us to send JSON data from Client to API&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Use the express router object
let router = express.Router();

// Middle ware to support JSON data passing to API
app.use(express.json());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Performing an insert involves the following steps on the &lt;code&gt;groceryRepo.js&lt;/code&gt; side:&lt;br&gt;
1) First read all the data array.&lt;br&gt;
2) Push the newData to that array.&lt;br&gt;
3) Write the new items list back to the file using &lt;code&gt;fs.writeFile&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--M8aTBd8x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uc3ixzvc4tznknj3x72c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--M8aTBd8x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uc3ixzvc4tznknj3x72c.png" alt="groceryRepo.js- Insert" width="737" height="396"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Addition in the &lt;code&gt;index.js&lt;/code&gt; file are minimal&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--anU_dnc6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v3y433tfyfnq6x7vt1g2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--anU_dnc6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v3y433tfyfnq6x7vt1g2.png" alt="index.js- POST request" width="482" height="413"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The call to insert from groceryRepo on line 58 accepts the item to be added to the database. The item to be added will be passed from the client in the body of the request, hence, we use &lt;code&gt;req.body&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;The status that we return in our response is 201 instead of 200, sice we are inserting an item and 201 is the appropriate status to send, but you can also send 200 and not face any error.&lt;/p&gt;

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

&lt;p&gt;Once you hit the API with the body as seen above you get a 201 Created response, which means the item was added successfully. We can then check if it was really added by calling either the GET All call and scrolling to the bottom of the result or just call our GET By ID call with the ID 51.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PUT/ Update an existing item&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To update an exisiting item we need the id and the object with updated values. The code needed in &lt;code&gt;index.js&lt;/code&gt; will be similar to getById, only change would be to call the &lt;code&gt;groceryRepo.update&lt;/code&gt; with &lt;code&gt;req.body&lt;/code&gt; and &lt;code&gt;req.params.id&lt;/code&gt; &lt;/p&gt;

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

&lt;p&gt;&lt;code&gt;groceryRepo.js&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_BQH7LW8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z58x28a8ngcxqvbszm7l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_BQH7LW8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z58x28a8ngcxqvbszm7l.png" alt="groceryRepo.js- PUT" width="796" height="398"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Code Explanation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The code is pretty similar to the other operations we did. We need to understand line 48. &lt;br&gt;
&lt;code&gt;items = items.map((p) =&amp;gt; (p.id == id ? { ...p, ...newData } : p));&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I am using array &lt;code&gt;map()&lt;/code&gt; function here to update the item from the array of items. &lt;strong&gt;map()&lt;/strong&gt; performs a condition check on each element in the array. We check if the id passed as request params match the item in the grocery list, if it is true then we are replacing that item with the updated &lt;code&gt;newData&lt;/code&gt;. We are using &lt;strong&gt;Object destructuring&lt;/strong&gt; to replace the existing object with newData. You can read more about object destructuring from the link in references at the end of this blog.&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;DELETE an item from the grocery list&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The code for delete is also similar to the update code and I am confident enough that by now you would be able to code it by yourself, but I am adding the code for both &lt;code&gt;index.js&lt;/code&gt; and &lt;code&gt;groceryRepo.js&lt;/code&gt; below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--haqlEG9W--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j7ju6ukhlaczgaobur0c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--haqlEG9W--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j7ju6ukhlaczgaobur0c.png" alt="DELETE- index.js" width="749" height="711"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0Mu6Ejed--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mza9tsfl97oqn6elcs5h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0Mu6Ejed--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mza9tsfl97oqn6elcs5h.png" alt="Image description" width="713" height="395"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Code explanation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We use the HTTP Delete verb to delete an item from the DB. In our case to achieve the same on a file system I am using the &lt;code&gt;array.filter()&lt;/code&gt; function. It returns an array that match the given condition. In our case I wrote the condition to check if the requested id does not match the id in the array. If it does then do not include that in the resultant array.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qKpT-5KT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ar4g8guvtpcegsoskjfk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qKpT-5KT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ar4g8guvtpcegsoskjfk.png" alt="Result- DELETE" width="800" height="221"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Voila! You just created your own REST API that performs CRUD operations in JavaScript. &lt;/p&gt;

&lt;p&gt;Here is the Github repo of the project: &lt;a href="https://github.com/nikkbh/GroceryAPI"&gt;Project Repo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thank you for reading this detailed blog. Happy Coding!💻&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.infoworld.com/article/3269878/what-is-an-api-application-programming-interfaces-explained.html"&gt;What is an API?&lt;/a&gt;&lt;br&gt;
&lt;a href="https://expressjs.com/"&gt;Express documentation&lt;/a&gt;&lt;br&gt;
&lt;a href="https://moz.com/learn/seo/http-status-codes"&gt;HTTP Status codes&lt;/a&gt;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment"&gt;Object Destructuring&lt;/a&gt;&lt;br&gt;
&lt;a href="https://medium.com/poka-techblog/simplify-your-javascript-use-map-reduce-and-filter-bd02c593cc2d"&gt;JavaScript's map() and filter()&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>api</category>
      <category>node</category>
    </item>
    <item>
      <title>How to use Ubuntu on Windows without any setup using Docker</title>
      <dc:creator>Nikhil Bhutani</dc:creator>
      <pubDate>Thu, 16 Mar 2023 11:04:23 +0000</pubDate>
      <link>https://dev.to/nikkbh/how-to-use-ubuntu-on-windows-without-any-setup-using-docker-7nh</link>
      <guid>https://dev.to/nikkbh/how-to-use-ubuntu-on-windows-without-any-setup-using-docker-7nh</guid>
      <description>&lt;p&gt;Docker has revolutionized the way developers build, ship, and run applications. It's an open-source platform that provides a containerization solution, enabling developers to create portable and efficient environments for their applications to run on any system, regardless of the underlying infrastructure. In this blog we will learn how to run Ubuntu on Windows.&lt;/p&gt;

&lt;h2&gt;
  
  
  Scenario
&lt;/h2&gt;

&lt;p&gt;Let's say you are new to Linux and want to try it out or follow some tutorial to learn some commands on CLI, you will have to install Ubuntu on a separate machine, dual boot your machine with Ubuntu or use WSL (Windows Subsytem for Linux). All options are valid but require a lot of setup and configuration to make it work(WSL is still easy to install and use as compared to others). To solve this problem we can use Ubuntu as a container using Docker. We don't need much setup or the know-how of installation to run Ubuntu on Windows using Docker.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pre-requisites
&lt;/h2&gt;

&lt;p&gt;Before we get started, You will need to make sure that you have Docker installed on your Windows machine. You can download Docker from the official website, &lt;a href="https://www.docker.com/" rel="noopener noreferrer"&gt;https://www.docker.com/&lt;/a&gt;, and follow the installation instructions.&lt;/p&gt;

&lt;p&gt;Once, it's installed, you need to launch the Docker Desktop application from the system tray.&lt;/p&gt;

&lt;h2&gt;
  
  
  Get Ubuntu Image
&lt;/h2&gt;

&lt;p&gt;We need to get the ubuntu image from docker hub on our local machine. To do this you need to open Powershell/Terminal and type the following command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker pull ubuntu&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It will take some time based on your connection speed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Run Ubuntu as a container
&lt;/h2&gt;

&lt;p&gt;Once the image is pulled you can check it by using the command &lt;code&gt;docker images&lt;/code&gt; which will list all the images available on your machine. You can also check the same on Docker Desktop.&lt;/p&gt;

&lt;p&gt;Now we need run this Ubuntu image as a container. For this we will type in the following command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker run -t -d --name myubuntu ubuntu&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;-t&lt;/code&gt; - Tells the container to run in a TTY mode. &lt;a href="https://www.howtogeek.com/428174/what-is-a-tty-on-linux-and-how-to-use-the-tty-command/" rel="noopener noreferrer"&gt;Reference&lt;/a&gt;&lt;br&gt;
&lt;code&gt;-d&lt;/code&gt; - Specifies the image to run in a detached mode.&lt;br&gt;
&lt;code&gt;--name&lt;/code&gt; - We specify the name of the conatiner we want, which can be anything.&lt;br&gt;
And, lastly the image name, which is ubuntu.&lt;/p&gt;

&lt;p&gt;After this command is executed, it will return the container ID, which tells us that the container is up and running successfully. You can verify the same by typing &lt;code&gt;docker ps&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Linux bash in action
&lt;/h2&gt;

&lt;p&gt;Finally to work with the Linux CLI, we type in the following command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker exec -it myubuntu bash&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;-it&lt;/code&gt; - Will open an interative bash terminal&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%2F8ne2qiij38y1mg6pd2i1.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%2F8ne2qiij38y1mg6pd2i1.png" alt="Linux on Windows"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;NOTE: The linux will be minified version and thus you will need to  install the packages and dependencies you require using &lt;code&gt;apt install &amp;lt;pacakage-name&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Mission acomplished!&lt;/strong&gt; We can now play around with linux and do a lot of cool stuff with it. For beginners you can learn basic navigation commands, admin tasks such as newtwork administartion, securing linux servers, user and groups managements, etc.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://docs.docker.com/" rel="noopener noreferrer"&gt;Docker Documentation&lt;/a&gt;&lt;/p&gt;

</description>
      <category>docker</category>
      <category>ubuntu</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
