<?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: Daniel Musembi</title>
    <description>The latest articles on DEV Community by Daniel Musembi (@danmusembi).</description>
    <link>https://dev.to/danmusembi</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%2F807001%2Fdad1faf8-d368-49b3-83c7-d463afbdbcdc.jpg</url>
      <title>DEV Community: Daniel Musembi</title>
      <link>https://dev.to/danmusembi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/danmusembi"/>
    <language>en</language>
    <item>
      <title>How to Create a Recipe Generator with NextJS, LangChain, and OpenAI</title>
      <dc:creator>Daniel Musembi</dc:creator>
      <pubDate>Thu, 15 Aug 2024 22:39:14 +0000</pubDate>
      <link>https://dev.to/danmusembi/how-to-create-a-recipe-generator-with-nextjs-langchain-and-openai-1e0d</link>
      <guid>https://dev.to/danmusembi/how-to-create-a-recipe-generator-with-nextjs-langchain-and-openai-1e0d</guid>
      <description>&lt;p&gt;Hey there 👋 , I hope you're doing great. This is just a quick guide on how you can build amazing applications using AI.&lt;/p&gt;

&lt;p&gt;Inspiration for me to build a recipe generator comes from wanting to solve the mental problem of deciding what to eat. you know they say making too many small decisions causes you to suffer from&lt;a href="https://onlinemba.wsu.edu/mba/resources/decision-fatigue" rel="noopener noreferrer"&gt;decisions fatigue&lt;/a&gt; - which is basically mental exhaustion resulting from the sheer number of decisions a person must make daily, leading to difficulty making—or making good—decisions. Now just imagine an application in which you just need to key in the ingredients, click the generate button then boom 💥 there you have generated the recipe. That is what we are going to build step by step. Check below what we will be building 👇👇&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F47q6awzyn71drx69q291.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F47q6awzyn71drx69q291.PNG" alt=" " width="800" height="348"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Without any further ado let's dive in &lt;/p&gt;

&lt;h2&gt;
  
  
  Step1. Create new project
&lt;/h2&gt;

&lt;p&gt;First, ensure you have Node.js installed, then run the following command on the terminal to initialise a Nextjs project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-next-app recipe-generator
cd recipe-generator
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step2. Install required packages
&lt;/h2&gt;

&lt;p&gt;Run the following to install packages we will be using&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install langchain
npm install openai dotenv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's get to understand the two,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.langchain.com/" rel="noopener noreferrer"&gt; LangChain&lt;/a&gt; LangChain is a tool that helps create applications using large language models (LLMs). It provides a framework to connect these models with various data sources, such as APIs and databases, making it easier to build more advanced AI applications like chatbots, content generators, and data analyzers. Essentially, LangChain streamlines the process of integrating and using AI models in different projects. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://openai.com/api/" rel="noopener noreferrer"&gt;OpenAI&lt;/a&gt; is the organization behind some of the most advanced AI models, like GPT, which can generate text, answer questions, and perform a variety of other tasks. The OpenAI API allows developers to access these powerful models and use them in their own applications. While OpenAI provides the AI models, LangChain acts as the framework that can connect these models with other tools and data sources, enabling the creation of complex AI-driven applications.&lt;/p&gt;

&lt;p&gt;Together, OpenAI and LangChain can be used to build sophisticated AI systems. OpenAI offers the intelligence, and LangChain provides the tools to integrate that intelligence into useful, real-world applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step3. Configure Environment variables
&lt;/h2&gt;

&lt;p&gt;Create a &lt;code&gt;.env.local&lt;/code&gt; file in the root directory. Go to &lt;a href="https://openai.com/api/" rel="noopener noreferrer"&gt;openai&lt;/a&gt; and create a new project, click on the dashboard and select API keys from the left sidebar to copy your openai-api-key. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F8c0xdukw9p62kicpnv6b.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F8c0xdukw9p62kicpnv6b.PNG" alt=" " width="800" height="316"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Insert your api key in the .env.local file as shown below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;OPENAI_API_KEY=your opeai-api-key
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step4. Setup the API connection
&lt;/h2&gt;

&lt;p&gt;In the app directory create &lt;code&gt;langchain.ts&lt;/code&gt; file and in it insert the following code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// aap/langchain.ts

import { OpenAI } from '@langchain/openai'
import  dotenv  from 'dotenv';

dotenv.config();

const model = new OpenAI({
  openAIApiKey: process.env.NEXT_PUBLIC_OPENAI_API_KEY,
  temperature: 0.7, // Adjust for creativity vs. accuracy
});

export default model;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we set up an OpenAI model using the LangChain library, configuring it with an API key and creativity level from environment variables. We then export this model so it can be used throughout the web application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step5. Creating recipe generator logic
&lt;/h2&gt;

&lt;p&gt;In the app directory create a new file &lt;code&gt;recipeGenerator.ts&lt;/code&gt; and copy and paste the following code into it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// app/recipeGenerator.ts
import model from './langchain';

export async function generateRecipe(ingredients: string[]): Promise&amp;lt;string&amp;gt; {
  const prompt = `Generate a recipe using the following ingredients: ${ingredients.join(', ')}. Please include the steps for preparation.`;
  const response = await model.call(prompt);
  return response;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we are defining a function generateRecipe that takes a list of ingredients as input and uses the OpenAI model (imported from langchain.ts) to create a recipe. The function constructs a prompt with the ingredients, sends it to the model to generate a recipe, and then returns the recipe text.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step6. Create frontend to use the API
&lt;/h2&gt;

&lt;p&gt;Insert the following code into the page.tsx file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// app/page.tsx

"use client"; // Ensure this is a client component

import { useState } from 'react';
import { generateRecipe } from './recipeGenerator'; // Adjust path if needed

const Home = () =&amp;gt; {
  const [ingredients, setIngredients] = useState&amp;lt;string[]&amp;gt;([]);
  const [recipe, setRecipe] = useState&amp;lt;string&amp;gt;('');
  const [isLoading, setIsLoading] = useState(false);
  const [error, setError] = useState&amp;lt;string | null&amp;gt;(null);

  const handleSubmit = async (e: React.FormEvent) =&amp;gt; {
    e.preventDefault();
    setIsLoading(true);
    setError(null);

    try {
      const newRecipe = await generateRecipe(ingredients);
      setRecipe(newRecipe);
    } catch (err) {
      console.error("Error generating recipe:", err);
      setError('Failed to generate recipe. Please try again.');
    } finally {
      setIsLoading(false);
    }
  };

  const handleIngredientsChange = (e: React.ChangeEvent&amp;lt;HTMLInputElement&amp;gt;) =&amp;gt; {
    const newIngredients = e.target.value
      .split(',')
      .map(i =&amp;gt; i.trim())
      .filter(i =&amp;gt; i.length &amp;gt; 0);

    setIngredients(newIngredients);
  };

  return (
    &amp;lt;div style={{ padding: '20px', border: '2px solid blue' }}&amp;gt; {/* Debugging styles */}
      &amp;lt;h1&amp;gt;AI Recipe Generator&amp;lt;/h1&amp;gt;
      &amp;lt;form onSubmit={handleSubmit}&amp;gt;
        &amp;lt;input 
          type="text" 
          value={ingredients.join(', ')} 
          onChange={handleIngredientsChange}
          placeholder="Enter ingredients separated by commas"
        /&amp;gt;
        &amp;lt;button type="submit" disabled={isLoading}&amp;gt;
          {isLoading ? 'Generating...' : 'Generate Recipe'}
        &amp;lt;/button&amp;gt;
      &amp;lt;/form&amp;gt;
      {error &amp;amp;&amp;amp; &amp;lt;p style={{ color: 'red' }}&amp;gt;{error}&amp;lt;/p&amp;gt;}
      {recipe &amp;amp;&amp;amp; (
        &amp;lt;div&amp;gt;
          &amp;lt;h2&amp;gt;Generated Recipe:&amp;lt;/h2&amp;gt;
          &amp;lt;p&amp;gt;{recipe}&amp;lt;/p&amp;gt;
        &amp;lt;/div&amp;gt;
      )}
    &amp;lt;/div&amp;gt;
  );
};

export default Home;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we are creating a React component for an AI recipe generator. Users input ingredients into a text field, which is then processed to generate a recipe using the generateRecipe function. The component handles form submission, displays loading states, and shows any errors or the generated recipe.&lt;/p&gt;

&lt;p&gt;Step7. Styling our component&lt;/p&gt;

&lt;p&gt;To style our application, insert the following CSS code in the global.css file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@tailwind base;
@tailwind components;
@tailwind utilities;

:root {
  --foreground-rgb: 50, 50, 50; /* Slightly lighter dark grey for text */
  --background-start-rgb: 255, 248, 240; /* Warm beige for start */
  --background-end-rgb: 255, 255, 255; /* Soft white for end */
  --highlight-color: 255, 140, 0; /* Orange for highlights and accents */
}

@media (prefers-color-scheme: dark) {
  :root {
    --foreground-rgb: 240, 240, 240; /* Light grey text for dark mode */
    --background-start-rgb: 34, 34, 34; /* Dark grey for start */
    --background-end-rgb: 0, 0, 0; /* Black for end */
    --highlight-color: 255, 165, 0; /* Lighter orange for highlights in dark mode */
  }
}

body {
  color: rgb(var(--foreground-rgb));
  background: linear-gradient(
      to bottom,
      rgb(var(--background-start-rgb)),
      rgb(var(--background-end-rgb))
    );
  font-family: 'Poppins', sans-serif;
  line-height: 1.7;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
}

h1, h2, h3, h4, h5, h6 {
  color: rgb(var(--highlight-color)); /* Warm orange color for headings */
  font-family: 'Merriweather', serif; /* Add a serif font for headings */
  text-align: center;
  margin-bottom: 20px;
}

.container {
  width: 90%;
  max-width: 600px;
  box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);
  padding: 20px;
  border-radius: 8px;
  background-color: #fff;
  margin-bottom: 20px;
}

button {
  background-color: rgb(var(--highlight-color));
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 12px 24px;
  font-size: 18px;
  font-family: 'Poppins', sans-serif;
  cursor: pointer;
  box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.1);
  transition: background-color 0.3s ease, transform 0.3s ease;
  width: 100%;
  margin-top: 10px;
}

button:hover {
  background-color: #e68a00; /* Darker orange on hover */
  transform: translateY(-2px); /* Slight lift effect */
}

input {
  border: 2px solid rgb(var(--highlight-color));
  border-radius: 8px;
  padding: 12px;
  font-size: 16px;
  margin-bottom: 12px;
  width: 100%;
  box-sizing: border-box;
  font-family: 'Poppins', sans-serif;
}

input:focus {
  outline: none;
  border-color: #e68a00; /* Darker orange on focus */
  box-shadow: 0 0 5px rgba(255, 140, 0, 0.5);
}

/* Remove debugging styles */
* {
  border: none;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, we've set up a layout.tsx file to define the overall structure of our AI Recipe Generator application. This file applies global styles, sets metadata (like the page title and description), and organizes the main layout by wrapping all content (children) in a consistent header, main section, and footer. The debugging border helps visualize the main content area during development.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// app/layout.tsx
import './globals.css';

export const metadata = {
  title: 'AI Recipe Generator',
  description: 'Generate recipes using AI',
};

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    &amp;lt;html lang="en"&amp;gt;
      &amp;lt;body&amp;gt;
        &amp;lt;header&amp;gt;
          {/* Header content */}
        &amp;lt;/header&amp;gt;
        &amp;lt;main&amp;gt;
          &amp;lt;div style={{ border: '1px solid red' }}&amp;gt; {/* Added for debugging */}
            {children}
          &amp;lt;/div&amp;gt;
        &amp;lt;/main&amp;gt;
        &amp;lt;footer&amp;gt;
          {/* Footer content */}
        &amp;lt;/footer&amp;gt;
      &amp;lt;/body&amp;gt;
    &amp;lt;/html&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;p&gt;Run the application and see it in action&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fbemkpqqgjsu31s355w4z.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fbemkpqqgjsu31s355w4z.gif" alt=" " width="1336" height="423"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's it guys, if you liked this tutorial kindly share it with others and make sure you follow me for much more 🥂. let me also know if you need the source code.&lt;/p&gt;

&lt;p&gt;Follow me on &lt;a href="https://x.com/isMusembi" rel="noopener noreferrer"&gt;X&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Follow me on &lt;a href="https://www.linkedin.com/in/daniel-musembi-a05852283/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; &lt;/p&gt;

</description>
    </item>
    <item>
      <title>How to Center a Div: 5 Simple Methods</title>
      <dc:creator>Daniel Musembi</dc:creator>
      <pubDate>Wed, 14 Aug 2024 17:55:16 +0000</pubDate>
      <link>https://dev.to/danmusembi/how-to-center-a-div-5-simple-methods-3ip8</link>
      <guid>https://dev.to/danmusembi/how-to-center-a-div-5-simple-methods-3ip8</guid>
      <description>&lt;p&gt;Centering a div element in HTML and CSS can be approached in several ways, depending on the layout requirements, browser support, and the complexity of the content within the div. Here's a comprehensive look at various methods:&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Using Margin: Auto
&lt;/h2&gt;

&lt;p&gt;This is one of the simplest methods for centering a block-level element like a div.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;html
&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8"&amp;gt;
    &amp;lt;title&amp;gt;Margin Auto Centering&amp;lt;/title&amp;gt;
    &amp;lt;style&amp;gt;
        .centered-div {
            width: 50%; /* You can adjust this */
            margin: auto; /* This centers the div horizontally */
            background-color: lightblue;
        }
    &amp;lt;/style&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;div class="centered-div"&amp;gt;
        This div is centered horizontally.
    &amp;lt;/div&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Flexbox
&lt;/h2&gt;

&lt;p&gt;Flexbox provides a powerful way to center content both vertically and horizontally.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;html
&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8"&amp;gt;
    &amp;lt;title&amp;gt;Flexbox Centering&amp;lt;/title&amp;gt;
    &amp;lt;style&amp;gt;
        .container {
            display: flex;
            justify-content: center; /* Horizontal center */
            align-items: center; /* Vertical center */
            height: 100vh; /* Full viewport height */
        }
        .centered-div {
            width: 50%;
            background-color: lightcoral;
        }
    &amp;lt;/style&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;div class="container"&amp;gt;
        &amp;lt;div class="centered-div"&amp;gt;
            This div is centered both vertically and horizontally.
        &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. Grid Layout
&lt;/h2&gt;

&lt;p&gt;CSS Grid is another modern way to center elements, offering similar capabilities to Flexbox but with some additional features for layout design.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; html
&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8"&amp;gt;
    &amp;lt;title&amp;gt;Grid Centering&amp;lt;/title&amp;gt;
    &amp;lt;style&amp;gt;
        .container {
            display: grid;
            place-items: center; /* Centers both horizontally and vertically */
            height: 100vh;
        }
        .centered-div {
            width: 50%;
            background-color: lightgreen;
        }
    &amp;lt;/style&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;div class="container"&amp;gt;
        &amp;lt;div class="centered-div"&amp;gt;
            This div is centered using Grid.
        &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. Absolute Positioning
&lt;/h2&gt;

&lt;p&gt;For elements that need to be centered within a relatively positioned parent, or within the viewport:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;html
&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8"&amp;gt;
    &amp;lt;title&amp;gt;Absolute Positioning&amp;lt;/title&amp;gt;
    &amp;lt;style&amp;gt;
        .container {
            position: relative;
            height: 100vh;
        }
        .centered-div {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            width: 50%;
            background-color: lightgoldenrodyellow;
        }
    &amp;lt;/style&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;div class="container"&amp;gt;
        &amp;lt;div class="centered-div"&amp;gt;
            This div is centered using absolute positioning.
        &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  5. Table Layout
&lt;/h2&gt;

&lt;p&gt;Though less common today, using display: table and table-cell can center content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;html
&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8"&amp;gt;
    &amp;lt;title&amp;gt;Table Layout Centering&amp;lt;/title&amp;gt;
    &amp;lt;style&amp;gt;
        .container {
            display: table;
            width: 100%;
            height: 100vh;
        }
        .centered-div {
            display: table-cell;
            text-align: center;
            vertical-align: middle;
            width: 50%;
            margin: auto;
            background-color: lightpink;
        }
    &amp;lt;/style&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;div class="container"&amp;gt;
        &amp;lt;div class="centered-div"&amp;gt;
            This div is centered using table layout.
        &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each method has its use cases:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Margin:&lt;/strong&gt; Auto is simple for horizontal centering.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Flexbox&lt;/strong&gt; and &lt;strong&gt;Grid&lt;/strong&gt; are modern, flexible, and support both vertical and horizontal centering with ease.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Absolute Positioning&lt;/strong&gt; works well for overlays or fixed elements.&lt;br&gt;
Table Layout can be useful for older browsers or when you need to support very old versions of Internet Explorer.&lt;/p&gt;

&lt;p&gt;Choose the method that best fits your project's needs, considering factors like browser support, the complexity of your layout, and the type of content you're centering.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>THE ART OF MINIMALIST LIVING</title>
      <dc:creator>Daniel Musembi</dc:creator>
      <pubDate>Wed, 14 Aug 2024 17:40:59 +0000</pubDate>
      <link>https://dev.to/danmusembi/the-art-of-minimalist-living-2b9j</link>
      <guid>https://dev.to/danmusembi/the-art-of-minimalist-living-2b9j</guid>
      <description>&lt;h2&gt;
  
  
  What is Minimalist Living?
&lt;/h2&gt;

&lt;p&gt;Minimalist living is a lifestyle where you intentionally reduce the number of possessions you own and the amount of clutter in your life. It's not just about decluttering your space but also about simplifying your life, focusing on what's truly important, and reducing distractions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Adopt Minimalism?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Mental Clarity:&lt;/strong&gt; Fewer possessions mean fewer things to worry about, leading to a clearer mind.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Financial Freedom:&lt;/strong&gt; By buying less, you save more money, which can lead to financial independence.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Time Management:&lt;/strong&gt; With less to manage, you have more time for activities that bring you joy or growth.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Environmental Impact:&lt;/strong&gt; Consuming less means a smaller ecological footprint.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How to Start:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Assess Your Belongings:&lt;/strong&gt; Go through your items room by room. Ask yourself if each item brings you joy or serves a necessary function. If not, consider letting it go.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The One-Year Rule:&lt;/strong&gt; If you haven't used an item in a year, you might not need it. This rule helps in deciding what to keep or discard.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Digital Minimalism:&lt;/strong&gt; Apply the same principles to your digital life. Unsubscribe from emails, clear out old files, and limit your time on social media.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Quality Over Quantity:&lt;/strong&gt; Instead of buying many cheap items, invest in fewer, higher-quality pieces that last longer.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Mindful Consumption:&lt;/strong&gt; Before buying something new, ask if you really need it. Consider the lifecycle of the product, from production to disposal.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Experiences Over Things:&lt;/strong&gt; Focus on creating memories rather than accumulating objects. Travel, learn new skills, or spend quality time with loved ones.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  A Simple Exercise to Start:
&lt;/h2&gt;

&lt;h3&gt;
  
  
  The 30-Day Challenge
&lt;/h3&gt;

&lt;p&gt;Choose a category (like clothes, books, or kitchen gadgets) and commit to not buying anything new in that category for 30 days. This helps you appreciate what you already have and reduces impulse buying.&lt;/p&gt;

&lt;p&gt;By embracing minimalism, you're not just decluttering your space; you're creating space for what truly matters in life. It's about living with intention, reducing stress, and finding joy in simplicity.&lt;br&gt;
Ask anything&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Beginner's Tutorial for CRUD Operations in NodeJS and MongoDB</title>
      <dc:creator>Daniel Musembi</dc:creator>
      <pubDate>Tue, 16 Jul 2024 21:40:48 +0000</pubDate>
      <link>https://dev.to/danmusembi/beginners-tutorial-for-crud-operations-in-nodejs-and-mongodb-k7k</link>
      <guid>https://dev.to/danmusembi/beginners-tutorial-for-crud-operations-in-nodejs-and-mongodb-k7k</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;CRUD operations stand for Create, Read, Update, and Delete. This procedure allows you to work with data from the MongoDB database.&lt;/p&gt;

&lt;p&gt;With these four operations, you can create, read, update, and delete data in MongoDB.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is MongoDB
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.mongodb.com/" rel="noopener noreferrer"&gt;MongoDB&lt;/a&gt; is a powerful and flexible solution for handling modern data needs. As a leading &lt;a href="https://www.mongodb.com/resources/basics/databases/nosql-explained" rel="noopener noreferrer"&gt;NoSQL database&lt;/a&gt;, MongoDB offers a dynamic schema design, enabling developers to store and manage data in a way that aligns seamlessly with contemporary application requirements.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Nodejs
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://nodejs.org/en/about" rel="noopener noreferrer"&gt;Node.js&lt;/a&gt; is a runtime environment that allows you to run JavaScript on the server side, rather than just in the browser. It's built on Chrome's V8 JavaScript engine, making it fast and efficient. With Node.js, you can build scalable network applications easily, using a single programming language for both client and server-side code. It's especially good for handling many simultaneous connections and real-time applications like chat servers or online games.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prequisite
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Install Node.js: Download and install Node.js from the official &lt;a href="https://nodejs.org/en/about" rel="noopener noreferrer"&gt;website&lt;/a&gt; if you haven't already.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Install MongoDB: Install MongoDB on your machine. Follow the instructions on the official MongoDB &lt;a href="https://www.mongodb.com/" rel="noopener noreferrer"&gt;website&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 1: Initialize a new project
&lt;/h2&gt;

&lt;p&gt;Open your preferred code editor (VS code) and cd into the directory where you want to create the project, then enter the command below to create a new project.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Next, use the command below to install the necessary packages.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; npm install express mongodb
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To ensure that everything is working properly, add '"serve": "node index.js"' beneath the script in the package.JSON for starting the program. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F12kx8hhxmx8q55p6411q.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F12kx8hhxmx8q55p6411q.PNG" alt=" " width="471" height="80"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Create and test express.js server
&lt;/h2&gt;

&lt;p&gt;In the root directory create an &lt;code&gt;index.js&lt;/code&gt; file and enter the following code in it.&lt;br&gt;
&lt;/p&gt;

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

app.listen(3000, () =&amp;gt;{
    console.log('Server is running on port 3000')
});

app.get('/', (req, res) =&amp;gt;{
    res.send("Hello Node API")
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code, we create a basic Node.js server using the Express framework, which listens on port 3000. When you visit the root URL ("/"), it responds with the message "Hello Node API".&lt;/p&gt;

&lt;p&gt;Now run the application with the command 'npm run serve' to observe the results, as shown below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fdh0vrvk32qnd8pefexdu.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fdh0vrvk32qnd8pefexdu.PNG" alt=" " width="681" height="174"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To test if the server is working enter&lt;code&gt;localhost:3000&lt;/code&gt; on the browser and see the results as shown below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fpvpcbm2hp6eu5tvitxba.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fpvpcbm2hp6eu5tvitxba.PNG" alt=" " width="400" height="111"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating MongoDB connection
&lt;/h2&gt;

&lt;p&gt;Visit&lt;a href="https://www.mongodb.com/" rel="noopener noreferrer"&gt; mongodb&lt;/a&gt; and sign in into your account or create one if not yet.&lt;/p&gt;

&lt;p&gt;Create a new project.  Give it a name and click "Create Project."&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Falrubq6ot9x8cb987tr2.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Falrubq6ot9x8cb987tr2.PNG" alt=" " width="800" height="282"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click the Create button, and then select the free m0 template. Scroll down, give your cluster a name, and click Create deployment.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fm8exxrnrm5i6pu4nnsvr.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fm8exxrnrm5i6pu4nnsvr.PNG" alt=" " width="699" height="414"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F530ah5f8spqwi0fih291.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F530ah5f8spqwi0fih291.PNG" alt=" " width="800" height="225"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On the following screen, enter a username and password for your cluster; make sure to copy or remember the password. Click Create Database user. &lt;/p&gt;

&lt;p&gt;After that click on the Choose a connection method button and select Connect&lt;br&gt;
&lt;a href="https://media2.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%2Fb0p4g1h3ya512mfd7k63.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fb0p4g1h3ya512mfd7k63.PNG" alt=" " width="756" height="210"&gt;&lt;/a&gt; via drivers, copy the connection string.&lt;/p&gt;

&lt;p&gt;To access mongodb we need a dependency called mongoose, so make sure to install it &lt;code&gt;npm i mongoose&lt;/code&gt; and add it at the top in the &lt;code&gt;index.js&lt;/code&gt; file &lt;code&gt;const mongoose = require('mongoose');&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Create a &lt;code&gt;db.js&lt;/code&gt; file in the root directory and paste the following code, replacing  with the password you created.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mongoose = require('mongoose');

mongoose.connect("mongodb+srv://admindan:admin1234@crudbackend.5goqnqm.mongodb.net/?retryWrites=true&amp;amp;w=majority&amp;amp;appName=crudbackend")
.then(() =&amp;gt; {
    console.log('Connected to the database');
})
.catch((error) =&amp;gt; {
    console.log('Connection failed', error);
});

module.exports = mongoose;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Import &lt;code&gt;const mongoose = require('./db');&lt;/code&gt; at the top of the &lt;code&gt;index.js&lt;/code&gt; file&lt;/p&gt;

&lt;p&gt;To test if the database is connecting run the application and you should see results on the terminal as shown below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fwhebactnow9cddqez5eu.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fwhebactnow9cddqez5eu.PNG" alt=" " width="769" height="227"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating CRUD functions
&lt;/h2&gt;

&lt;p&gt;Now that we've our database connected, we need a model to store our data in the mongodb. &lt;/p&gt;

&lt;p&gt;Make a models folder in the root directory, then create a file called "product.model.js" and insert the following code into it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mongoose = require("mongoose");

const ProductSchema = mongoose.Schema(
  {
    name: {
      type: String,
      required: [true, "Please enter product name"],
    },

    quantity: {
      type: Number,
      required: true,
      default: 0,
    },

    price: {
      type: Number,
      required: true,
      default: 0,
    },

    image: {
      type: String,
      required: false,
    },
  },
  {
    timestamps: true,
  }
);


const Product = mongoose.model("Product", ProductSchema);

module.exports = Product;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;h2&gt;
  
  
  Create: defining a function to insert data into mongodb
&lt;/h2&gt;

&lt;p&gt;We're creating a function for storing data in our MongoDB collection. Create a controllers folder in the root directory and a file called 'product.controller.js' within it. Paste the following code in it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Product = require("../models/product.model");

const getProducts = async (req, res) =&amp;gt; {
  try {
    const products = await Product.find({});
    res.status(200).json(products);
  } catch (error) {
    res.status(500).json({ message: error.message });
  }
};

const getProduct = async (req, res) =&amp;gt; {
  try {
    const { id } = req.params;
    const product = await Product.findById(id);
    res.status(200).json(product);
  } catch (error) {
    res.status(500).json({ message: error.message });
  }
};

const createProduct = async (req, res) =&amp;gt; {
  try {
    const product = await Product.create(req.body);
    res.status(200).json(product);
  } catch (error) {
    res.status(500).json({ message: error.message });
  }
};

const updateProduct = async (req, res) =&amp;gt; {
  try {
    const { id } = req.params;

    const product = await Product.findByIdAndUpdate(id, req.body);

    if (!product) {
      return res.status(404).json({ message: "Product not found" });
    }

    const updatedProduct = await Product.findById(id);
    res.status(200).json(updatedProduct);
  } catch (error) {
    res.status(500).json({ message: error.message });
  }
};

const deleteProduct = async (req, res) =&amp;gt; {
  try {
    const { id } = req.params;

    const product = await Product.findByIdAndDelete(id);

    if (!product) {
      return res.status(404).json({ message: "Product not found" });
    }

    res.status(200).json({ message: "Product deleted successfully" });
  } catch (error) {
    res.status(500).json({ message: error.message });
  }
};

module.exports = {
  getProducts,
  getProduct,
  createProduct,
  updateProduct,
  deleteProduct,
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code, we define CRUD operations for our Product model It includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;getProducts: Fetches all products.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;getProduct: Fetches a product by its ID.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;createProduct: Creates a new product with provided data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;updateProduct: Updates a product by its ID.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;deleteProduct: Deletes a product by its ID.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each function handles possible errors and returns appropriate HTTP responses.&lt;/p&gt;

&lt;h2&gt;
  
  
  Define routes
&lt;/h2&gt;

&lt;p&gt;Create a routes folder at the root directory and in it create product.route.js. Insert the below code in it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require("express");
const Product = require("../models/product.model.js");
const router = express.Router();
const {getProducts, getProduct, createProduct, updateProduct, deleteProduct} = require('../controllers/product.controller.js');


router.get('/', getProducts);
router.get("/:id", getProduct);

router.post("/", createProduct);

// update a product
router.put("/:id", updateProduct);

// delete a product
router.delete("/:id", deleteProduct);




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

&lt;/div&gt;



&lt;p&gt;In this code, we set up Express routes for CRUD operations for our  Product model. it imports the necessary modules, creates an Express router, and defines routes for:&lt;/p&gt;

&lt;p&gt;Fetching all products (GET /).&lt;br&gt;
Fetching a single product by ID (GET /:id).&lt;br&gt;
Creating a new product (POST /).&lt;br&gt;
Updating a product by ID (PUT /:id).&lt;br&gt;
Deleting a product by ID (DELETE /:id).&lt;/p&gt;

&lt;p&gt;Finally, it exports the router for use in the main application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing your API
&lt;/h2&gt;

&lt;p&gt;Now that we've created our CRUD operations, let's test them. We are going to use &lt;a href="https://app.insomnia.rest/app/dashboard/organizations" rel="noopener noreferrer"&gt;insomnia&lt;/a&gt; if you don't have it you can download and create an account.&lt;/p&gt;

&lt;p&gt;Follow the steps outlined below to test create operation ( POST REQUEST).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Open Insomnia: Launch the Insomnia application.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a New Request: Click on the "+" button to create a new request.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Set Request Type: Select "POST" from the dropdown menu next to the request name.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Enter URL: Input the URL of your API endpoint ( &lt;a href="http://localhost:3000/api/products" rel="noopener noreferrer"&gt;http://localhost:3000/api/products&lt;/a&gt;)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Enter Request Body: Click on the "Body" tab, select "JSON," and enter the JSON data you want to send.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Send Request: Click the "Send" button to send the request to the server.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;View Response: Inspect the response from the server in the lower pane of Insomnia.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The api for creating a product on the MongoDB was successful, as indicated by the status 200 ok in the image below, which illustrates how it should operate.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F969ppxgws45d6dypvpz6.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F969ppxgws45d6dypvpz6.PNG" alt=" " width="733" height="253"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thus, you ought to be able to view the item created when you navigate to the database and select Browse Collection.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fcqqrn3xo64rgfnpfkm27.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fcqqrn3xo64rgfnpfkm27.PNG" alt=" " width="800" height="303"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Test Read operations
&lt;/h2&gt;

&lt;p&gt;The technique of testing a get request is almost the same as what we discussed above; you make a new request and select GET from the dropdown menu.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Farf1pnvdl0njv47gklst.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Farf1pnvdl0njv47gklst.PNG" alt=" " width="764" height="541"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Proceed to try the final two options, which are update and remove (PUT, DELETE) from the dropdown menu.&lt;/p&gt;

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

&lt;p&gt;With this beginner's tutorial, you should now have a solid foundation for performing CRUD operations using NodeJS and MongoDB. For more tech insights, follow me on &lt;a href="https://x.com/musembiwebdev" rel="noopener noreferrer"&gt;X&lt;/a&gt; and connect on &lt;a href="https://www.linkedin.com/in/daniel-musembi-a05852283/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Building Real-Time Apps with Next.js and WebSockets</title>
      <dc:creator>Daniel Musembi</dc:creator>
      <pubDate>Fri, 05 Jul 2024 19:18:06 +0000</pubDate>
      <link>https://dev.to/danmusembi/building-real-time-apps-with-nextjs-and-websockets-2p39</link>
      <guid>https://dev.to/danmusembi/building-real-time-apps-with-nextjs-and-websockets-2p39</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In today's fast-paced digital world, real-time data exchange is essential for developing dynamic and interactive web applications. WebSockets are a powerful method for enabling real-time, bidirectional communication between clients and servers. In this blog post, we'll look at how to use WebSockets with Next.js to create real-time apps.&lt;/p&gt;

&lt;p&gt;A WebSocket is a communication protocol that provides full-duplex communication channels over a single TCP connection. It enables a real-time, event-driven connection between a client and a server.&lt;/p&gt;

&lt;p&gt;Unlike traditional HTTP software, which follows a request-response model, WebSockets allow two-way (bi-directional) communication. This means that the client and the server can send data to each other anytime without continuous polling. To learn more about WebSocket check &lt;a href="https://www.geeksforgeeks.org/what-is-web-socket-and-how-it-is-different-from-the-http/" rel="noopener noreferrer"&gt;here&lt;/a&gt;. Without further ado, let's get started developing our real-time communication application. We'll start by bootstrapping a new Next.js project. &lt;/p&gt;

&lt;p&gt;Open your preferred code editor and run the following command on the terminal to create a new project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; npx create-next-app@latest real-time-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure to setup the project as shown below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Flqyoju23l60hvbvzjv7n.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Flqyoju23l60hvbvzjv7n.PNG" alt=" " width="658" height="396"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After it has finished, run the following commands to open the projects folder in the code editor.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd real-time-app
code .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Installing dependencies
&lt;/h2&gt;

&lt;p&gt;Run the following command to install &lt;code&gt;ws&lt;/code&gt;  a WebSocket library for Node.js.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install ws
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Creating a WebSocket server
&lt;/h2&gt;

&lt;p&gt;Let's start by creating &lt;code&gt;server.js&lt;/code&gt; file in the root directory and insert the following code in it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; const { createServer } = require('http');
 const { parse } = require('url');
 const next = require('next');
 const WebSocket = require('ws');

 const dev = process.env.NODE_ENV !== 'production';
 const app = next({ dev });
 const handle = app.getRequestHandler();

 app.prepare().then(() =&amp;gt; {
     const server = createServer((req, res) =&amp;gt; {
         const parsedUrl = parse(req.url, true);
         handle(req, res, parsedUrl);
     });

     const wss = new WebSocket.Server({ server });

     wss.on('connection', (ws) =&amp;gt; {
         console.log('New client connected');

         ws.on('message', (message) =&amp;gt; {
             console.log(`Received message: ${message}`);
             ws.send(`Server: ${message}`);
         });

         ws.on('close', () =&amp;gt; {
             console.log('Client disconnected');
         });
     });

     server.listen(3000, (err) =&amp;gt; {
         if (err) throw err;
         console.log('&amp;gt; Ready on http://localhost:3000');
     });
 });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code, we set up a server. It first imports the necessary modules and sets up a Next.js app in development mode if NODE_ENV is not set to 'production'. The app prepares the server to handle HTTP requests with Next.js's request handler. It also creates a WebSocket server (wss) attached to the HTTP server.&lt;/p&gt;

&lt;p&gt;When a WebSocket client connects, the server logs the connection. It listens for messages from the client, logs them, and sends back a response. It also logs when the client disconnects. The server listens on port 3000.&lt;/p&gt;

&lt;p&gt;Next, modify the Script in the package.json file to use the custom server we've just created.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  "scripts": {
    "dev": "node server.js",
    "build": "next build",
    "start": "NODE_ENV=production node server.js"
},
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Integrating WebSocket client in Next.js
&lt;/h2&gt;

&lt;p&gt;In this part, we will create a WebSocket client in our Next.js application. Begin by creating a hooks folder in the app directory.&lt;br&gt;
Then, create a file called &lt;code&gt;useWebSocket.js&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fc9o8s2kpbdki3r2xv9i4.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fc9o8s2kpbdki3r2xv9i4.PNG" alt=" " width="173" height="66"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Insert the following code in that file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; import { useEffect, useState } from 'react';

 const useWebSocket = (url) =&amp;gt; {
     const [messages, setMessages] = useState([]);
     const [ws, setWs] = useState(null);

     useEffect(() =&amp;gt; {
         const socket = new WebSocket(url);
         setWs(socket);

         socket.onmessage = (event) =&amp;gt; {
             setMessages((prevMessages) =&amp;gt; [...prevMessages, event.data]);
         };

         return () =&amp;gt; {
             socket.close();
         };
     }, [url]);

     const sendMessage = (message) =&amp;gt; {
         if (ws) {
             ws.send(message);
         }
     };

     return { messages, sendMessage };
 };

 export default useWebSocket;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code we define a custom React hook &lt;code&gt;useWebSocket&lt;/code&gt; for managing WebSocket connections. It establishes a connection to a WebSocket server using the provided URL, stores incoming messages in a state array, and provides a function to send messages through the WebSocket.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;useEffect&lt;/code&gt;: Opens a WebSocket connection when the component mounts and closes it when the component unmounts.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;onmessage&lt;/code&gt;: Updates the messages state with incoming messages.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sendMessage&lt;/code&gt;: Sends a message through the WebSocket if it's connected.&lt;br&gt;
The hook returns the messages array and the sendMessage function for use in components.&lt;/p&gt;
&lt;h2&gt;
  
  
  Using the WebSocket in a component
&lt;/h2&gt;

&lt;p&gt;To use WebSocket in our component, update the &lt;code&gt;page.js&lt;/code&gt; file with the following code.&lt;br&gt;
&lt;/p&gt;

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

import { useState } from 'react';
import useWebSocket from '@/app/hooks/useWebSocket';

const Home = () =&amp;gt; {
    const { messages, sendMessage } = useWebSocket('ws://localhost:4000');
    const [input, setInput] = useState('');

    const handleSubmit = (e) =&amp;gt; {
        e.preventDefault();
        sendMessage(input);
        setInput('');
    };

    return (
        &amp;lt;div&amp;gt;
            &amp;lt;h1&amp;gt;Real-time Chat&amp;lt;/h1&amp;gt;
            &amp;lt;form onSubmit={handleSubmit}&amp;gt;
                &amp;lt;input
                    type="text"
                    value={input}
                    onChange={(e) =&amp;gt; setInput(e.target.value)}
                /&amp;gt;
                &amp;lt;button type="submit"&amp;gt;Send&amp;lt;/button&amp;gt;
            &amp;lt;/form&amp;gt;
            &amp;lt;div&amp;gt;
                {messages.map((msg, index) =&amp;gt; (
                    &amp;lt;div key={index}&amp;gt;{msg}&amp;lt;/div&amp;gt;
                ))}
            &amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;
    );
};

export default Home;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a React component for our real-time chat functionality using WebSockets. The component leverages React's &lt;code&gt;useState&lt;/code&gt; hook to manage the state of the input field, allowing users to type and send messages.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Features:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;WebSocket Integration: Utilizes a custom useWebSocket hook to establish a WebSocket connection to ws://localhost:4000.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;State Management: Uses useState to handle the current message input.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Message Handling: The handleSubmit function sends the typed message via WebSocket and then clears the input field.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Real-time Updates: Displays received messages in real-time by mapping over the messages array.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;How It Works:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Connection: On component mount, useWebSocket initiates a WebSocket connection.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Sending Messages: When the form is submitted, sendMessage sends the message, and the input field is reset.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Displaying Messages: Received messages are displayed dynamically as they arrive.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Running the application
&lt;/h2&gt;

&lt;p&gt;Run the following command on the terminal to start the application. Open your browser and navigate to &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt;. You should see a simple chat interface where you can send messages and receive real-time updates.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fdpkvsc202kgok1wdzexa.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fdpkvsc202kgok1wdzexa.PNG" alt=" " width="561" height="164"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;Finally, developing real-time applications with Next.js and WebSockets is an effective way to improve user experiences through instant communication and updates. This post demonstrates how to set up a WebSocket server and a Next.js application to create a smooth real-time chat interface. Using these technologies, developers may incorporate dynamic, interactive features into their online applications, paving the way for more engaging and responsive user engagements. This method not only increases functionality but also lays the groundwork for future real-time application development.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>tutorial</category>
      <category>nextjs</category>
    </item>
    <item>
      <title>Creating a Tag Input System with React JS: A Step-by-Step Guide</title>
      <dc:creator>Daniel Musembi</dc:creator>
      <pubDate>Wed, 31 Jan 2024 17:22:40 +0000</pubDate>
      <link>https://dev.to/danmusembi/creating-a-tag-input-system-with-react-js-a-step-by-step-guide-4l7g</link>
      <guid>https://dev.to/danmusembi/creating-a-tag-input-system-with-react-js-a-step-by-step-guide-4l7g</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;We will learn how to develop a &lt;strong&gt;tag input system&lt;/strong&gt; in &lt;strong&gt;React JS&lt;/strong&gt; by following the instructions in this article! Building a tag input system is an excellent project that will help you hone your abilities in state management, processing user input, and rendering dynamic components. If you are starting with React or are seeking to improve your expertise, this project is an excellent choice.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is a tag input system?
&lt;/h3&gt;

&lt;p&gt;A tag input system is a user interface component that allows users to input and manage tags. Tags are small, keyword-like labels that provide a convenient way to categorise and organise information. Building a tag input system in React will not only give you hands-on experience with fundamental React concepts but also provide a practical and reusable component for your future projects.&lt;/p&gt;

&lt;h3&gt;
  
  
  What You'll Learn
&lt;/h3&gt;

&lt;p&gt;This tutorial will show you how to build a tag input system from the ground up. You will gain knowledge of fundamental validation, state management, user input, and dynamic component rendering. Upon completion, you will possess an entirely operational tag input system that can be seamlessly incorporated into your React applications.&lt;/p&gt;

&lt;h3&gt;
  
  
  Prerequisites
&lt;/h3&gt;

&lt;p&gt;Before diving in, make sure you have a basic understanding of React fundamentals, including components, states, and event handling. If you're new to React, consider going through the official &lt;a href="https://react.dev/" rel="noopener noreferrer"&gt;React documentation&lt;/a&gt; or introductory tutorials to get a solid foundation.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Set up your React project
&lt;/h3&gt;

&lt;p&gt;Set up your React project in the Visual Studio code editor by navigating to your selected directory and using the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app input-tag
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fnp2i5fcvfuhiqhk4v7pp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fnp2i5fcvfuhiqhk4v7pp.png" width="711" height="249"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Following the completion of the process of creating the project, open the project folder in Visual Studio Code by utilizing the following command, and then navigate inside it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;code .cd input-tag
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 2: Install the react-tag-input package
&lt;/h3&gt;

&lt;p&gt;Run the following command while inside the projects folder to install the &lt;em&gt;react-tag-input package&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install react-tag-input
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 3: Creating input tag component
&lt;/h3&gt;

&lt;p&gt;Now, within the &lt;strong&gt;src&lt;/strong&gt; folder of your project, create a file with the name &lt;strong&gt;InputTag.js,&lt;/strong&gt; into which you will paste the code below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";import { WithContext as ReactTags } from "react-tag-input";// specifies which characters should terminate tags input. An array of character codes.const KeyCodes = { comma: 188, enter: 13,};const delimiters = [KeyCodes.comma, KeyCodes.enter];const InputTag = () =&amp;gt; { const [tags, setTags] = React.useState([]); // Method to delete tag from Array const handleDelete = (i) =&amp;gt; { setTags(tags.filter((tag, index) =&amp;gt; index !== i)); }; // Method to Add tag into Array const handleAddition = (tag) =&amp;gt; { setTags([...tags, tag]); }; return ( &amp;lt;div id="tags"&amp;gt; &amp;lt;ReactTags tags={tags} delimiters={delimiters} handleDelete={handleDelete} handleAddition={handleAddition} inputFieldPosition="bottom" autocomplete allowDragDrop={false} /&amp;gt; &amp;lt;/div&amp;gt; );};export default InputTag;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code is a React component named InputTag that utilizes the "react-tag-input" library to create a tag input system. It uses the WithContext wrapper from the library to handle tag input events. The component initializes the state using the useState hook to manage an array of tags. Two functions, handleDelete and handleAddition, are defined to respectively remove and add tags to the array, updating the state accordingly. The ReactTags component is then rendered with various props, including the current tags, delimiters to determine when a tag is added, and callbacks to handle tag deletion and addition. Additionally, specific configurations such as input field position, autocomplete, and drag-and-drop functionality are set within the ReactTags component. This code provides a simple yet functional tag input system in React using the "react-tag-input" library.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 4 &lt;strong&gt;:&lt;/strong&gt; Styling InputTag Component
&lt;/h3&gt;

&lt;p&gt;At this point, we will customise our component to make it more pleasing to the eye. In the App.css file, paste the code that is provided below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#tags { width: 50%; margin: 50px auto;}#tags .ReactTags __tags { position: relative; display: flex; align-items: center; background-color: #e2e2e2; padding: 10px; border-radius: 8px;}#tags .ReactTags__ selected { display: flex;}#tags .ReactTags __clearAll { cursor: pointer; padding: 12px; margin: 12px; background: #ff6b6b; color: #fff; border: none; border-radius: 4px;}#tags .ReactTags__ tagInput { border-radius: 4px; display: inline-block;}#tags .ReactTags __tagInput input.ReactTags__ tagInputField,#tags .ReactTags __tagInput input.ReactTags__ tagInputField:focus { height: 38px; margin: 0; font-size: 14px; min-width: 180px; padding: 0px 12px; border: 1px solid #ccc; border-radius: 4px;}#tags .ReactTags __editInput { border-radius: 4px;}#tags .ReactTags__ editTagInput { display: inline-flex;}#tags .ReactTags __selected span.ReactTags__ tag { border: 1px solid #bbb; background: #5cb85c; color: white; font-size: 14px; display: flex; padding: 8px; margin: 0 8px; border-radius: 4px;}#tags .ReactTags __selected a.ReactTags__ remove { color: #888; margin-left: 8px; cursor: pointer;}#tags .ReactTags __suggestions { position: absolute;}#tags .ReactTags__ suggestions ul { list-style-type: none; box-shadow: 0.1em 0.2em 0.5em rgba(0, 0, 0, 0.1); background: #fff; width: 220px; border-radius: 4px;}#tags .ReactTags __suggestions li { border-bottom: 1px solid #ddd; padding: 10px 12px; margin: 0;}#tags .ReactTags__ suggestions li mark { text-decoration: underline; background: none; font-weight: 600;}#tags .ReactTags __suggestions ul li.ReactTags__ activeSuggestion { background: #a4c2f4; cursor: pointer;}#tags .ReactTags__remove { border: none; cursor: pointer; background: none; color: white; margin-left: 8px;}#tags input { background-color: #e2e2e2; border: 1px solid #ccc; border-radius: 4px; padding: 10px; font-size: 14px; color: #333;}#tags input:before { border: none;}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 5: Integrating InputTag in the app.js
&lt;/h3&gt;

&lt;p&gt;Now, add the InputTag component to your App.js file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import "./App.css";import InputTag from "./InputTag";function App() { return ( &amp;lt;div className="App"&amp;gt; &amp;lt;InputTag /&amp;gt; &amp;lt;/div&amp;gt; );}export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 6: Running the application
&lt;/h3&gt;

&lt;p&gt;The application can be viewed in the browser after it has been executed by using the following command.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;As you can see, we successfully built a tag input system using React.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Flh7-us.googleusercontent.com%2FnDbOdxvEafLINF6qRWDE5uXPW3j-KUaHuyYgSXDDNk3KAoBiiODNREEPtCsdIr2mMq4S8n2fw7p6T64ZjF9o8tkL9UjG1KllvWUm6a4PMElYE7GDMJ42NFUmECi45Yyo6LPLtdiiDF5ZquyYVUh8aXE" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Flh7-us.googleusercontent.com%2FnDbOdxvEafLINF6qRWDE5uXPW3j-KUaHuyYgSXDDNk3KAoBiiODNREEPtCsdIr2mMq4S8n2fw7p6T64ZjF9o8tkL9UjG1KllvWUm6a4PMElYE7GDMJ42NFUmECi45Yyo6LPLtdiiDF5ZquyYVUh8aXE" width="1000" height="115"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;In wrapping up this guide on crafting a tag input system with React JS, we've navigated through a series of practical steps aimed at empowering web developers in their React journey. By breaking down fundamental concepts like state management and event handling, and seamlessly integrating a third-party library, you now possess the tools to implement an efficient and interactive tag input component. Overcoming challenges in documentation interpretation and CSS styling reflects the real-world problem-solving skills that developers often encounter. Consider this guide not just as a tutorial but as a companion in your React development endeavours, offering a hands-on approach to building a feature-rich tag input system for your web applications.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>A comprehensive guide on how to create a weather widget in Next.js</title>
      <dc:creator>Daniel Musembi</dc:creator>
      <pubDate>Sun, 28 Jan 2024 14:42:51 +0000</pubDate>
      <link>https://dev.to/danmusembi/a-comprehensive-guide-on-how-to-create-a-weather-widget-in-nextjs-5gh8</link>
      <guid>https://dev.to/danmusembi/a-comprehensive-guide-on-how-to-create-a-weather-widget-in-nextjs-5gh8</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Websites and programs no longer function without weather widgets, which provide consumers with instantaneous access to weather data. This detailed tutorial will teach us how to use the Next.js framework to build a highly functional and adaptable weather widget. The demand for attractive and useful widgets that can be seamlessly integrated into dynamic web applications is growing.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Overview of Weather Widgets&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Widgets that display the current weather and forecasts on a webpage are known as weather widgets. For websites that focus on geography or travel, they not only improve the user experience but also offer functionality.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Importance of Weather Widgets in Web Development&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;There are more practical uses for weather widgets in web development than just making the site look nicer. It provides helpful, location-based data, which adds to a user-centric experience. Weather widgets improve the functionality of web apps in many different sectors, making them more useful for travel planning, daily forecast checking, and general news consumption.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Introduction to Next.js for Building Dynamic Web Applications&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;As a React-based framework, &lt;a href="https://nextjs.org/" rel="noopener noreferrer"&gt;Next.js&lt;/a&gt; makes it easy for developers to create powerful and efficient online apps. Because of its modular design and server-side rendering capabilities, it is perfect for making widgets that can respond to user input. Our weather widget now works flawlessly for users on any device, regardless of network circumstances, thanks to Next.js.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Setting Up the Next.js Project&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Navigate to your desired location in the VS code and run the following command, either using &lt;strong&gt;npx&lt;/strong&gt; or &lt;strong&gt;yarn&lt;/strong&gt; , to set up the project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-next-app weather-widget yarn create next-app weather-widget
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fctozdhi8bcqk7yj4at98.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fctozdhi8bcqk7yj4at98.png" width="645" height="169"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, when prompted, make use of the parameters that are listed below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F0rwsqdzeq894z2hpdnjn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F0rwsqdzeq894z2hpdnjn.png" width="599" height="235"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After it finishes installing, use the following commands to open the project in VS Code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd weather-widgetcode .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Creating a weather widget component
&lt;/h2&gt;

&lt;p&gt;Make a new folder named &lt;strong&gt;components&lt;/strong&gt; in your project root folder. In that folder, create a file called &lt;strong&gt;weatherWidget.tsx.&lt;/strong&gt; Add the following code to it:.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState, useEffect } from 'react';import styles from '../styles/widget.module.css';interface WeatherWidgetProps { city?: string; coordinates?: { lat: number; lon: number };}interface WeatherData { name: string; main: { temp: number; feels_like: number; }; weather: { description: string; icon: string; }[];}const WeatherWidget: React.FC&amp;lt;WeatherWidgetProps&amp;gt; = ({ city, coordinates }) =&amp;gt; { const [weatherData, setWeatherData] = useState&amp;lt;WeatherData | null&amp;gt;(null); useEffect(() =&amp;gt; { const fetchData = async () =&amp;gt; { try { let query = ''; if (city) { query = `q=${city}`; } else if (coordinates) { query = `lat=${coordinates.lat}&amp;amp;lon=${coordinates.lon}`; } else { console.error('Please provide either city or coordinates.'); return; } const response = await fetch(`/api/weather?${query}`); const data: WeatherData = await response.json(); setWeatherData(data); } catch (error) { console.error('Error fetching weather data:', error); } }; fetchData(); }, [city, coordinates]); console.log(weatherData); return ( &amp;lt;div className={styles.weatherWidget}&amp;gt; {!weatherData ? ( &amp;lt;div&amp;gt;Loading weather ...&amp;lt;/div&amp;gt; ) : ( &amp;lt;&amp;gt; &amp;lt;h2&amp;gt;{weatherData.name}&amp;lt;/h2&amp;gt; &amp;lt;p className={styles.weather}&amp;gt;{weatherData.weather[0].description}&amp;lt;/p&amp;gt; &amp;lt;div className={styles.currentWeather}&amp;gt; &amp;lt;img src={`https://openweathermap.org/img/wn/${weatherData.weather[0].icon}@2x.png`} alt={weatherData.weather[0].description} /&amp;gt; &amp;lt;div&amp;gt;{Math.round(weatherData.main.temp)}C&amp;lt;/div&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;p className={styles.feelsLike}&amp;gt; Feels like: {Math.round(weatherData.main.feels_like)}C &amp;lt;/p&amp;gt; &amp;lt;/&amp;gt; )} &amp;lt;/div&amp;gt; );};export default WeatherWidget;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above&lt;code&gt;WeatherWidget&lt;/code&gt; React component fetches weather data from an API based on the provided city or coordinates using &lt;code&gt;useState&lt;/code&gt; and &lt;code&gt;useEffect&lt;/code&gt; hooks. It dynamically renders weather details, including city name, description, and temperature, with a loading message displayed while data is being fetched. Styled using CSS modules, it ensures a clean and modular structure for presenting weather information in a visually appealing manner.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting OpenWeatherMap API Key
&lt;/h2&gt;

&lt;p&gt;You can get 60 calls per minute and 1,000,000 calls per month with OpenWeatherMap's free API tier. Therefore, there is more than enough for this tutorial.&lt;/p&gt;

&lt;p&gt;Head over to &lt;a href="http://openweathermap.org" rel="noopener noreferrer"&gt;openweathermap.org&lt;/a&gt; and sign up for an account to receive your free key.&lt;/p&gt;

&lt;p&gt;After setting up your account:&lt;/p&gt;

&lt;p&gt;Copy your API key from the page called &lt;a href="https://home.openweathermap.org/api_keys" rel="noopener noreferrer"&gt;API Keys&lt;/a&gt;. Add the following code to a new file called **.env.local** in the root folder of your weather widget project. Replace &lt;em&gt;[YOUR_API_KEY_HERE]&lt;/em&gt; with the API key you got from OpenWeatherMap:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; # .env.local OPENWEATHERMAP_API_KEY=[YOUR_API_KEY_HERE]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fsnft7uru6izcm4vng43d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fsnft7uru6izcm4vng43d.png" width="800" height="275"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating weather data API
&lt;/h2&gt;

&lt;p&gt;In the&lt;code&gt;pages/api&lt;/code&gt; folder, create a file called&lt;code&gt;weather.ts&lt;/code&gt;, and add the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// pages/api/weather.tsimport type { NextApiRequest, NextApiResponse } from 'next';const apiKey = process.env.OPENWEATHERMAP_API_KEY;const apiUrl = 'https://api.openweathermap.org/data/2.5/weather';export default async function handler( req: NextApiRequest, res: NextApiResponse) { const { q, lat, lon } = req.query; if (!apiKey) { res.status(500).json({ error: 'API key not set' }); return; } if (!q &amp;amp;&amp;amp; (!lat || !lon)) { res .status(400) .json({ error: 'Please provide either city or coordinates.' }); return; } try { const query = q ? `q=${q}` : `lat=${lat}&amp;amp;lon=${lon}`; const response = await fetch( `${apiUrl}?${query}&amp;amp;appid=${apiKey}&amp;amp;units=metric` ); const data = await response.json(); res.status(200).json(data); } catch (error: unknown) { if (error instanceof Error) { res.status(500).json({ error: error.message }); } else { res .status(500) .json({ error: error || 'Server error while trying to fetch weather data', }); } }}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This Next.js API route handles requests for weather data from the OpenWeatherMap API. Key aspects:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Import Statements:&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Request Handling:&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;API Call and Response:&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Response to Frontend:&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Error Handling:&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Styling the weather widget
&lt;/h2&gt;

&lt;p&gt;Please make a new file in the styles folder and name it weather.module.css. Then, add the following CSS code to it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.weatherWidget { border: 2px solid #4CAF50; /* Green border */ border-radius: 8px; max-width: 300px; padding: 15px; margin: 15px; text-align: center; background-color: #f2f2f2; /* Light gray background */}.currentWeather { margin: 0 auto; display: flex; flex-direction: column; /* Change to column for a cleaner look */ justify-content: center; align-items: center;}.currentWeather div { margin-top: 10px; /* Add some spacing between elements */ font-size: 2rem; /* Adjust font size */ color: #333; /* Darker text color */}.feelsLike { font-size: 1rem; /* Adjust font size */ font-style: italic; color: #666; /* Lighter text color */}.weather { font-weight: normal; /* Remove bold weight */ color: #4CAF50; /* Green text color */}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Updating index page
&lt;/h2&gt;

&lt;p&gt;Our new widget will be implemented in the &lt;strong&gt;index.tsx&lt;/strong&gt; page after we update it. Just copy and paste the following code into &lt;strong&gt;pages/index.tsx&lt;/strong&gt; to replace the original boilerplate:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';import WeatherWidget from '../components/weatherWidget';const Home: React.FC = () =&amp;gt; { return ( &amp;lt;div className="App"&amp;gt; {/* Example using city name */} &amp;lt;WeatherWidget city="Montreal" /&amp;gt; {/* Example using coordinates */} {/* &amp;lt;WeatherWidget coordinates={{ lon: -73.5878, lat: 45.5088 }} /&amp;gt; */} &amp;lt;/div&amp;gt; );};export default Home;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the application using the following command and open the link &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt; to see it in the browser:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fvy0i2ii7tfzitrl1ezda.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fvy0i2ii7tfzitrl1ezda.png" width="528" height="312"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;In conclusion, this comprehensive instruction showed how to use Next.js to create a dynamic and attractive weather widget. This guide gives developers a solid foundation to construct a feature-rich component that gets real-time weather data and provides a seamless user experience.&lt;/p&gt;

&lt;p&gt;It teaches you how to easily integrate API calls with Next.js API routes after setting up a project and creating the Weather component. State management and &lt;em&gt;useEffect&lt;/em&gt; created a responsive, interactive widget by handling data well.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>A Beginner's Guide to Dockerizing React Applications Built with Vite</title>
      <dc:creator>Daniel Musembi</dc:creator>
      <pubDate>Fri, 26 Jan 2024 19:53:04 +0000</pubDate>
      <link>https://dev.to/danmusembi/a-beginners-guide-to-dockerizing-react-applications-built-with-vite-25gj</link>
      <guid>https://dev.to/danmusembi/a-beginners-guide-to-dockerizing-react-applications-built-with-vite-25gj</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Containerizing React apps introduces developers to Docker, which streamlines deployment, scalability, and consistency across environments. This beginner's guide covers Dockerizing Vite-built React apps. This detailed essay explains Docker containers and provides step-by-step directions for containerising React apps and streamlining the development workflow with Docker and Vite.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites for this technical article:
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Basic knowledge of React:&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Understanding JavaScript and ES6:&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Experience with Web Development:&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Introduction to Node.js and npm:&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Basic Docker Understanding (Optional):&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Development Environment Setup:&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Access to a Docker Environment (Optional):&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Our Vite + React app requires Node 16 or higher and a relevant npm version. Run the following command on the command prompt to check the node version:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fu5n5rygvraxof2tf7h36.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fu5n5rygvraxof2tf7h36.png" width="391" height="160"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up the project
&lt;/h2&gt;

&lt;p&gt;To set up our project, open up the terminal on VS Code and run the following command: Give a project name, choose React as a framework and Javascript as a variant.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm create vite@latest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fryyr9w1tr0esqlb7s9c9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fryyr9w1tr0esqlb7s9c9.png" width="617" height="163"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Run the following commands to navigate to your project directory and open it in the VS Code Editor:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd docker-react-appcode .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2F9szgtgufxyu2cchnovvt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F9szgtgufxyu2cchnovvt.png" width="293" height="445"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The image above shows what the file structure should look like.&lt;/p&gt;

&lt;p&gt;Run the following command to install the necessary packages:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Use the following command to run the application:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you open the link &lt;a href="http://localhost:5173/" rel="noopener noreferrer"&gt;http://localhost:5173/&lt;/a&gt; in the browser, you should see the following:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fldh930dd7v2i0fa691iw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fldh930dd7v2i0fa691iw.png" width="800" height="506"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Configuring vite. config.js
&lt;/h2&gt;

&lt;p&gt;The Vite configuration is an object having keys like plugins, server, build, preview, optimizeDeps, worker, and more. Check more here &lt;a href="https://vitejs.dev/config/" rel="noopener noreferrer"&gt;config&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Only the most essential configuration choices will be covered in this tutorial. You can easily get everything set up by copying and pasting the configuration parameters provided below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { defineConfig } from 'vite'import React from '@vitejs/plugin-react'// https://vitejs.dev/config/export default defineConfig({ base: "/", plugins: [react()], preview: { port: 8080, strictPort: true, }, server: { port: 8080, strictPort: true, host: true, origin: "http://localhost:8080", },});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's break down the key elements of this configuration:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;import { defineConfig } from "vite";&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;react from "@vitejs/plugin-react";&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;export default defineConfig({ ... });&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;base: "/"&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;plugins: [react()]&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;preview: { port: 8080, strictPort: true }&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;server: { port: 8080, strictPort: true, host: true, origin: "&lt;/code&gt;&lt;a href="http://0.0.0.0:8080" rel="noopener noreferrer"&gt;&lt;code&gt;http://0.0.0.0:8080&lt;/code&gt;&lt;/a&gt;&lt;code&gt;" }&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Run the following command to build our app for production.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fwxv9r3jvp0ud3bqkmxpt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fwxv9r3jvp0ud3bqkmxpt.png" width="628" height="266"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Run the following command to serve the build&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;serve -s dist
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fybwi1tbca77ztul9l8vs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fybwi1tbca77ztul9l8vs.png" width="661" height="231"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Dockerizing the application
&lt;/h2&gt;

&lt;p&gt;Dockerization requires Docker installation. Running docker in the terminal checks if docker is installed. If not installed head here to install it &lt;a href="https://docs.docker.com/" rel="noopener noreferrer"&gt;install docker&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Frwco1q9d6k3ik8fjz5rd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Frwco1q9d6k3ik8fjz5rd.png" width="510" height="442"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If it looks like the image above, docker is installed. Remember to start Docker Desktop if you use it.&lt;/p&gt;

&lt;p&gt;Let's break down each section of the following Dockerfile:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM node:18-alpineWORKDIR /appCOPY package.json.RUN npm installCOPY . .RUN npm run buildEXPOSE 8080CMD ["npm", "run", "preview"]

FROM node:18-alpine
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Specifies the base image for the Docker container. In this case, it's using the official Node.js image with Alpine Linux as the base. The Alpine variant is a lightweight distribution, making the resulting Docker image smaller.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Sets the working directory inside the container to &lt;code&gt;/app&lt;/code&gt;. This is where your application code will be copied, and subsequent commands will be executed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;COPY package.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Copies the &lt;code&gt;package.json&lt;/code&gt; file from the local directory (the directory where the Dockerfile is located) into the container at the current working directory (&lt;code&gt;/app&lt;/code&gt;).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;RUN npm install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Runs the &lt;code&gt;npm install&lt;/code&gt; command inside the container. This installs the dependencies specified in the &lt;code&gt;package.json&lt;/code&gt; file. The &lt;code&gt;node:18-alpine&lt;/code&gt; base image includes Node.js and npm, allowing you to use them in your container.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Copies all the remaining files from the local directory into the container at the current working directory (&lt;code&gt;/app&lt;/code&gt;). This includes your application source code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;RUN npm run build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Runs the &lt;code&gt;npm run build&lt;/code&gt; command inside the container. This assumes that you have a build script defined in your &lt;code&gt;package.json&lt;/code&gt; file. The build script is responsible for building your Next.js application.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Exposes port 8080. This is a declaration rather than an actual command to open the port. It informs anyone who might run the container that the application inside will use port 8080.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CMD ["npm", "run", "preview"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Specifies the command to run when the container starts. This uses the &lt;code&gt;CMD&lt;/code&gt; instruction to set the default command for the container. In this case, it runs the &lt;code&gt;npm run preview&lt;/code&gt; command, assuming you have a &lt;code&gt;preview&lt;/code&gt; script defined in your &lt;code&gt;package.json&lt;/code&gt; file. This command starts the application.&lt;/p&gt;

&lt;p&gt;Now create a &lt;strong&gt;Dockerfile&lt;/strong&gt; at the root directory of your application and copy and paste the code above in it.&lt;/p&gt;

&lt;p&gt;We can construct a Docker image from this Dockerfile before adding a docker-compose file. The following command can be executed to create an image from a Dockerfile:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker build . -t "sample-project:v1.0"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command accomplishes two goals.&lt;/p&gt;

&lt;p&gt;Builds a Docker image from the current context; appends the "sample-project" and "v1" versions to the picture.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fp25ql7u2olj9j45fqrnr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fp25ql7u2olj9j45fqrnr.png" width="760" height="253"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we can run the following command on our local PC to see what docker images are accessible.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fk1ncgo4mdhd3hvvlikge.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fk1ncgo4mdhd3hvvlikge.png" width="462" height="91"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Very well. This image is now known as a container, so let's see if it runs. To launch a container using the "sample-project:v1.0" image, use the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run -p 8080:8080 sample-project:v1.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we are! Let's check &lt;a href="http://localhost:8080" rel="noopener noreferrer"&gt;localhost:8080&lt;/a&gt; now. Within a Docker container, we have an application called Vite + React running.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fz6quuifrtbmynigqbpk1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fz6quuifrtbmynigqbpk1.png" width="800" height="414"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Use the following command to get an ID in return:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run -d -p 8080:8080 sample-project:v1.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2F6k4jnumsjfgrhuv53q1s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F6k4jnumsjfgrhuv53q1s.png" width="491" height="130"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To make sure, we can execute the following command to get a list of all the containers that are now operating.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fliq0a87v58n0zqpblhuk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fliq0a87v58n0zqpblhuk.png" width="486" height="107"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;Finally, this beginner's guide has given a thorough rundown of Dockerizing React applications created with Vite, providing an easy way to containerize your apps. For consistent deployment across different settings, developers can package their Vite-based React applications using the Dockerfile and execute the Docker image.&lt;/p&gt;

&lt;p&gt;Dockerization improves the scalability and mobility of React apps and makes teamwork in development easier. Docker containers enable a uniform and reproducible development and deployment workflow by encapsulating application code, environment settings, and dependencies.&lt;/p&gt;

&lt;p&gt;Developers who can figure out how to incorporate Docker into the React and Vite stack will be in high demand as containerization approaches gain traction. This tutorial helps newcomers to web development lay the groundwork for Dockerizing their React apps and reaping the benefits of this technology.&lt;/p&gt;

&lt;p&gt;So that's the end of it. Throughout this post, we have discussed a great deal of material. I hope that you find this post to be informative. Be sure to give it a round of applause if you found it to be enjoyable. Additionally, make sure to share it with your friends and associates.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Installing and Implementing Tailwind CSS on Next.js: A Comprehensive Guide</title>
      <dc:creator>Daniel Musembi</dc:creator>
      <pubDate>Thu, 25 Jan 2024 18:13:23 +0000</pubDate>
      <link>https://dev.to/danmusembi/installing-and-implementing-tailwind-css-on-nextjs-a-comprehensive-guide-30gi</link>
      <guid>https://dev.to/danmusembi/installing-and-implementing-tailwind-css-on-nextjs-a-comprehensive-guide-30gi</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://tailwindcss.com/" rel="noopener noreferrer"&gt;Tailwind CSS&lt;/a&gt; is a popular styling method for developers who value quickness and flexibility. In this detailed article, we examine how Tailwind CSS and &lt;a href="https://nextjs.org/" rel="noopener noreferrer"&gt;Next.js&lt;/a&gt; work together to seamlessly integrate utility-first styling concepts into the dynamic framework.&lt;/p&gt;

&lt;p&gt;Tailwind CSS, a utility-first CSS framework, offers a strong set of pre-defined utility classes to quickly build and style interfaces. Tailwind simplifies styling with a rich library of utility classes for popular design patterns and a utilitarian and pragmatic approach.&lt;/p&gt;

&lt;p&gt;Tailwind CSS combined with Next.js, a powerful React framework for web apps, offers many benefits. From easy setup and configuration to seamless responsive design, this combination gives developers a smooth workflow. This post walks through installing Tailwind CSS in a Next.js project and using its features to improve development.&lt;/p&gt;

&lt;h2&gt;
  
  
  Explain Next.js
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://nextjs.org/" rel="noopener noreferrer"&gt;Next.js&lt;/a&gt; is a popular open-source React framework for building modern web applications. Developed by Vercel, it simplifies the process of building React-based projects by providing a set of conventions and features for server-side rendering, static site generation, and client-side rendering. Key features of Next.js include automatic code splitting, server-side rendering for React components, efficient routing, and support for API routes. It facilitates a seamless developer experience, offering a balance between performance optimization and ease of use. Next.js is widely adopted for its versatility, enabling developers to build scalable, SEO-friendly, and high-performance web applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up the Next.js project
&lt;/h2&gt;

&lt;p&gt;Open &lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer"&gt;VS code&lt;/a&gt;, navigate to the desired directory to create an application, and run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-next-app@latest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will first ask you to name your app; you can name it &lt;em&gt;my-sample-app.&lt;/em&gt; Make sure to configure your project as shown below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F8h8nq1xzmz46pa7jmy8a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F8h8nq1xzmz46pa7jmy8a.png" width="673" height="232"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Navigate to the projects directory using the following command: Use the second command to open the projects folder in VS Code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd my-sample-appcode .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To begin running your server on the available port on your PC, run any of the commands provided below.&lt;/p&gt;

&lt;p&gt;Using npm&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using yarn&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;What you should see in your app when you go to the localhost port in your browser is:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fsmb8866brtqzpo3wbb21.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fsmb8866brtqzpo3wbb21.png" width="800" height="496"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Configuration Tailwind css
&lt;/h2&gt;

&lt;p&gt;In this section, we're going to learn how to configure the tailwind.config.js file in different directories.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using pages directory
&lt;/h3&gt;

&lt;p&gt;This is the expected structure of your tailwind.config.js file when working with the Next.js pages directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/**@type {import('tailwindcss').Config} */module.exports = { content: ["./pages/**/*.{js,ts,jsx,tsx}", "./components/**/*.{js,ts,jsx,tsx}",], theme: { extend: {}, }, plugins: [],}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Using src directory
&lt;/h3&gt;

&lt;p&gt;Assuming your project's structure has a src folder preceding the pages folder, your tailwind.config.js file needs to resemble the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/**@type {import('tailwindcss').Config} */module.exports = { content: ["./src/**/*.{js,ts,jsx,tsx}",], theme: { extend: {}, }, plugins: [],}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Using app directory
&lt;/h3&gt;

&lt;p&gt;In the case of the app directory, the setting of your tailwind.config.js file should resemble this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/**@type {import('tailwindcss').Config} */module.exports = { content: ["./app/**/*.{js,ts,jsx,tsx}", "./pages/ **/*.{js,ts,jsx,tsx}", "./components/** /*.{js,ts,jsx,tsx}",], theme: { extend: {}, }, plugins: [],}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Last but not least, open the globals.css file in your project's root directory and insert the &lt;em&gt;&lt;a class="mentioned-user" href="https://dev.to/tailwind"&gt;@tailwind&lt;/a&gt; directives&lt;/em&gt; for every layer of Tailwind, as illustrated below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@tailwind base;@tailwind components;@tailwind utilities;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Testing
&lt;/h2&gt;

&lt;p&gt;You may check if Tailwind is set up correctly by adding the following code to your index.js file or by using one of the Tailwind utility classes, such as text-red-900 or bg-red-900, in an existing component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Head from "next/head";export default function Home() { return ( &amp;lt;&amp;gt; &amp;lt;Head&amp;gt; &amp;lt;title&amp;gt;My Awesome Next.js App&amp;lt;/title&amp;gt; &amp;lt;meta name='description' content='A stylish Next.js app' /&amp;gt; &amp;lt;meta name='viewport' content='width=device-width, initial-scale=1' /&amp;gt; &amp;lt;link rel='icon' href='/favicon.ico' /&amp;gt; &amp;lt;/Head&amp;gt; &amp;lt;main className='bg-gray-100 min-h-screen flex items-center justify-center'&amp;gt; &amp;lt;section className='bg-white p-8 rounded-md shadow-md'&amp;gt; &amp;lt;h1 className='text-4xl font-bold text-gray-800 mb-6'&amp;gt; Welcome to My Awesome Next.js App! &amp;lt;/h1&amp;gt; &amp;lt;p className='text-lg text-gray-600 mb-4'&amp;gt; You have successfully installed and configured Tailwind CSS. 🚀 &amp;lt;/p&amp;gt; &amp;lt;ul className='list-disc list-inside'&amp;gt; &amp;lt;li className='mb-2'&amp;gt; &amp;lt;p className='flex items-center text-green-600'&amp;gt; &amp;lt;span className='mr-2'&amp;gt;🐈&amp;lt;/span&amp;gt; You should see a walking cat! &amp;lt;/p&amp;gt; &amp;lt;/li&amp;gt; &amp;lt;li className='mb-2'&amp;gt; &amp;lt;p className='bg-yellow-500 text-white p-2 rounded'&amp;gt; This has a yellow background color. &amp;lt;/p&amp;gt; &amp;lt;/li&amp;gt; &amp;lt;/ul&amp;gt; &amp;lt;/section&amp;gt; &amp;lt;/main&amp;gt; &amp;lt;/&amp;gt; );}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you've added a utility class to an existing component, your browser should display that class. Otherwise, it will appear like the index.js file:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fruvk5mx2ld16caj1gr6h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fruvk5mx2ld16caj1gr6h.png" width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;In wrapping up our exploration of integrating Tailwind CSS with Next.js, let's recap the key steps and fundamental concepts covered in this comprehensive guide. We initiated the process by creating a Next.js project with Tailwind CSS using the &lt;code&gt;npx create-next-app&lt;/code&gt; command with the &lt;code&gt;with-tailwindcss&lt;/code&gt; example. We then delved into configuring, installing, and seamlessly integrating Tailwind CSS into our Next.js application, embracing its utility-first approach for efficient and modular styling.&lt;/p&gt;

&lt;p&gt;Throughout the guide, we explored the significance of Tailwind CSS in modern web development, emphasizing its role in promoting rapid prototyping, consistent design language, and streamlined collaboration. By understanding the importance of Tailwind CSS within the context of Next.js, we unlocked the potential for building scalable, performant, and visually appealing web applications.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>A Guide to Developing an Account Manager with React Router DOM.</title>
      <dc:creator>Daniel Musembi</dc:creator>
      <pubDate>Sun, 21 Jan 2024 22:24:11 +0000</pubDate>
      <link>https://dev.to/danmusembi/a-guide-to-developing-an-account-manager-with-react-router-dom-291i</link>
      <guid>https://dev.to/danmusembi/a-guide-to-developing-an-account-manager-with-react-router-dom-291i</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Web applications revolve around account management, which allows users to access, interact with, and customize their digital experiences. An efficient account manager is critical for guaranteeing a safe and smooth user experience, whether it's getting access to unique content, protecting personal data, or customizing preferences. Improving user authentication, streamlining navigation, and laying a solid groundwork for personalized interactions within web apps are some of the topics covered in this article as it dives into the complexities of building an account manager using React Router DOM. Come along as we explore what goes into making an account management system that is both user-friendly and dynamic.&lt;/p&gt;

&lt;h2&gt;
  
  
  Importance of React Router DOM for navigation
&lt;/h2&gt;

&lt;p&gt;React Router DOM is a vital library for React application navigation. Here are several reasons React Router DOM is crucial for navigation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Declarative Routing: React Router DOM lets developers define application navigation using components like Route and Link. Declarative coding improves readability and maintainability.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The module supports React's philosophy by enabling component-based routing. Each route represents a React component, facilitating code modularization and structure.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;For complicated applications like account managers, React Router DOM allows hierarchical routing. This functionality lets you organize account views and sections hierarchically.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The React Router DOM simplifies the extraction of URL parameters and the management of dynamic data in the application. This is necessary for user account management and URL-based information display.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The library allows developers to guide users based on specific conditions or activities. This helps implement user authentication and account management operations.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In React Router DOM, navigation functions mirror standard browser behavior. It makes forward and backward navigation easy and smooth.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Integration with React Ecosystem: React Router DOM works flawlessly with other React frameworks and tools. Compatibility enables a seamless development experience and lets developers use the React ecosystem.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Setting Up the Project
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Installing React Router DOM
&lt;/h3&gt;

&lt;p&gt;Open the Visual Studio code editor and run the following command on the terminal to create a new React app. &lt;strong&gt;account-manager-app&lt;/strong&gt; is the name of the application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app account-manager-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fj50dfczvcilqn33aezco.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fj50dfczvcilqn33aezco.png" width="623" height="227"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Navigate to our app directory&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd account-manager-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To launch our React app in VS Code, type the following command while in the directory:.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Let's install the dependencies we need. For instructions on how to configure tailwind CSS, check &lt;a href="https://dev.to/danmusembi/a-comprehensive-guide-on-using-tailwind-css-with-react-icons-to-create-an-engaging-contact-page-bfh-temp-slug-1395006"&gt;here&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install react-router-domnpm install -D tailwindcssnpm install react-icons --save
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the application to make sure everything is working well.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;The picture below shows our app running well.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F84dnyyry5i2rzn6nboz3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F84dnyyry5i2rzn6nboz3.png" width="800" height="551"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Project structure and organization
&lt;/h3&gt;

&lt;p&gt;In the src folder, create components folder and in it, create Sidebar.js file&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F6tabxznf6i74twc1tyqu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F6tabxznf6i74twc1tyqu.png" width="191" height="138"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Add this code to the sidebar. js: A responsive sidebar with user information and navigation links is defined by this React component. We use Tailwind CSS styles, FontAwesome icons, and custom pictures to dynamically render links with sub-items (changing colors and styles based on conditions).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';import { FaBitbucket, FaRegUser } from 'react-icons/fa';import { CiCreditCard1, CiHeart, CiLogout } from 'react-icons/ci';const Sidebar = () =&amp;gt; { return ( &amp;lt;aside className="hidden lg:block w-64 bg-gray-900 text-white p-4"&amp;gt; &amp;lt;div className="border-b border-gray-700 py-5"&amp;gt; &amp;lt;div className="flex items-center"&amp;gt; &amp;lt;img width="40px" height="40px" className="rounded-full object-cover mr-3" src="https://i.imgur.com/m79BpQy.jpg" alt="Red woman portrait" /&amp;gt; &amp;lt;div&amp;gt; &amp;lt;p className="text-gray-500 text-sm"&amp;gt;Hello,&amp;lt;/p&amp;gt; &amp;lt;p className="font-bold"&amp;gt;alice&amp;lt;/p&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;/div&amp;gt; {sidebarItems.map((item, index) =&amp;gt; ( &amp;lt;div key={index} className="flex border-b py-4"&amp;gt; &amp;lt;div className="flex w-full"&amp;gt; &amp;lt;div className="flex flex-col gap-2"&amp;gt; {item.subItems ? ( &amp;lt;div&amp;gt; &amp;lt;a href={item.link} className={`flex items-center gap-2 font-medium ${ item.icon ? 'text-yellow-400' : '' } ${index === 0 ? 'text-blue-500' : 'text-gray-400'} hover:text-yellow-400`} &amp;gt; {item.icon &amp;amp;&amp;amp; item.icon} {item.text} &amp;lt;/a&amp;gt; &amp;lt;div className="ml-8 flex flex-col gap-2"&amp;gt; {item.subItems.map((subItem, subIndex) =&amp;gt; ( &amp;lt;a key={subIndex} href={subItem.link} className="text-gray-500 hover:text-yellow-400" &amp;gt; {subItem.text} &amp;lt;/a&amp;gt; ))} &amp;lt;/div&amp;gt; &amp;lt;/div&amp;gt; ) : ( &amp;lt;a href={item.link} className={`flex items-center gap-2 font-medium ${ item.icon ? 'text-yellow-400' : '' } ${index === 0 ? 'text-blue-500' : 'text-gray-400'} hover:text-yellow-400`} &amp;gt; {item.icon &amp;amp;&amp;amp; item.icon} {item.text} &amp;lt;/a&amp;gt; )} &amp;lt;/div&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;/div&amp;gt; ))} &amp;lt;/aside&amp;gt; );};export default Sidebar;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the components folder, create another file and name it Account.js.&lt;/p&gt;

&lt;p&gt;The following React component illustrates an account page with sidebar navigation and choice cards for profile, shipping, and billing address management. Users can access and change their account information by dynamically rendering account choices and fields. A sidebar for navigation and grid-based choice cards make the responsive layout work on small and large screens. Insert it into Account.js.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';import SideBar from './Sidebar';const accountOptions = [{ title: 'Personal Profile', link: 'profile-information', fields: [ { label: 'Name', value: 'Sarah Johnson' }, { label: 'Email', value: 'sarah@yandex.com' }, { label: 'Postal Code', value: '20371' }, { label: 'Card Number', value: '1223 3432 3344 0082' },], }, { title: 'Shipping Address', link: 'manage-adresse', fields: [{ label: 'Name', value: 'Sarah Johnson' }, { label: 'Location', value: 'Belgrade, Serbia' }, { label: 'Postal Code', value: '20371' }, { label: 'Card Number', value: '1223 3432 3344 0082' },], }, { title: 'Billing Address', link: '#', fields: [{ label: 'Name', value: 'Sarah Johnson' }, { label: 'Location', value: 'Belgrade, Serbia' }, { label: 'Postal Code', value: '20371' }, { label: 'Card Number', value: '1223 3432 3344 0082' },], },];const Account = () =&amp;gt; { return ( &amp;lt;section className="container mx-auto w-full flex-grow max-w-screen-xl border-b py-8 lg:flex lg:flex-row lg:py-16"&amp;gt; {/* sidebar */} &amp;lt;SideBar /&amp;gt; {/* /sidebar */} {/* option cards */} &amp;lt;div className="mb-8 flex items-center justify-between px-8 md:hidden"&amp;gt; &amp;lt;div className="flex gap-3"&amp;gt; &amp;lt;div className="py-5"&amp;gt; &amp;lt;div className="flex items-center"&amp;gt; &amp;lt;img width="48px" height="48px" className="rounded-full object-cover" src="https://raw.githubusercontent.com/bbulakh/tailwind-ecommerce/2.x/src/assets/images/avatar-photo.png" alt="Red woman portrait" /&amp;gt; &amp;lt;div className="ml-5"&amp;gt; &amp;lt;p className="font-medium text-gray-600 text-lg"&amp;gt;Hello,&amp;lt;/p&amp;gt; &amp;lt;p className="font-bold"&amp;gt;Sarah Johnson&amp;lt;/p&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;div className="flex gap-3"&amp;gt; &amp;lt;button className="border bg-amber-400 py-2 px-3 rounded"&amp;gt; Profile settings &amp;lt;/button&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;section className="grid w-full max-w-screen-xl grid-cols-1 gap-6 px-8 pb-16 lg:grid-cols-3"&amp;gt; {accountOptions.map((option, index) =&amp;gt; ( &amp;lt;div key={index} className=""&amp;gt; &amp;lt;div className="border py-6 shadow-md"&amp;gt; &amp;lt;div className="flex justify-between px-6 pb-6"&amp;gt; &amp;lt;p className="font-bold text-lg"&amp;gt;{option.title}&amp;lt;/p&amp;gt; &amp;lt;a className="text-sm text-purple-800" href={option.link}&amp;gt; Edit &amp;lt;/a&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;div className="px-6"&amp;gt; {option.fields.map((field, fieldIndex) =&amp;gt; ( &amp;lt;p key={fieldIndex} className="text-gray-600"&amp;gt; {field.label}: {field.value} &amp;lt;/p&amp;gt; ))} &amp;lt;/div&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;/div&amp;gt; ))} &amp;lt;/section&amp;gt; &amp;lt;/section&amp;gt; );};export default Account;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the components folder, create another file and call it Adresse.js.&lt;/p&gt;

&lt;p&gt;The following React component, "ManageAdresse," manages address information. It has a sidebar for navigation and a form for updating the nation, city, and zip code. The form is responsive and has validation indicators and a save button. Put it in the Adresse.js file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Sidebar from './Sidebar';const ManageAdresse = () =&amp;gt; { return ( &amp;lt;section className="container flex-grow mx-auto max-w-screen-xl border-b py-8 lg:flex lg:flex-row lg:py-16"&amp;gt; &amp;lt;Sidebar /&amp;gt; &amp;lt;section className="grid w-full max-w-screen-xl grid-cols-1 gap-6 px-8 pb-16"&amp;gt; &amp;lt;div className="py-6"&amp;gt; &amp;lt;div className="w-full" /&amp;gt; &amp;lt;form className="flex w-full flex-col gap-4" action=""&amp;gt; &amp;lt;div className="flex w-full flex-col"&amp;gt; &amp;lt;label className="flex items-center text-gray-600" htmlFor="country"&amp;gt; Country &amp;lt;span className="block text-sm font-medium text-red-500 ml-1"&amp;gt;*&amp;lt;/span&amp;gt; &amp;lt;/label&amp;gt; &amp;lt;input className="w-full border px-4 py-2 lg:w-1/2" type="text" placeholder="Serbia" /&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;div className="flex w-full flex-col"&amp;gt; &amp;lt;label className="flex items-center text-gray-600" htmlFor="city"&amp;gt; City &amp;lt;span className="block text-sm font-medium text-red-500 ml-1"&amp;gt;*&amp;lt;/span&amp;gt; &amp;lt;/label&amp;gt; &amp;lt;input className="w-full border px-4 py-2 lg:w-1/2" type="text" placeholder="Belgrade" /&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;div className="flex flex-col"&amp;gt; &amp;lt;label className="text-gray-600" htmlFor="zipCode"&amp;gt;Zip Code&amp;lt;/label&amp;gt; &amp;lt;input x-mask={999999} className="w-full border px-4 py-2 lg:w-1/2" placeholder={240116} /&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;button className="mt-4 w-40 bg-violet-900 px-4 py-2 text-white"&amp;gt; Save changes &amp;lt;/button&amp;gt; &amp;lt;/form&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;/section&amp;gt;&amp;lt;/section&amp;gt; );};export default ManageAdresse;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The next step is to proceed by making a new file called ChangePassword.js in the components folder.&lt;/p&gt;

&lt;p&gt;Users can change their password on the &lt;strong&gt;ChangePassword&lt;/strong&gt; page. It has a sidebar for navigation and a form area with fields for the current password, the new password, and the new password again. Visual cues on the form include labels that show which fields are needed and a button that lets you save your changes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Sidebar from './Sidebar';const ChangePassword = () =&amp;gt; { return ( &amp;lt;section className="container mx-auto flex-grow max-w-screen-xl border-b py-8 lg:flex lg:flex-row lg:py-16"&amp;gt; &amp;lt;Sidebar /&amp;gt; &amp;lt;section className="grid w-full max-w-screen-xl grid-cols-1 gap-6 px-8 pb-16"&amp;gt; &amp;lt;div className="py-6"&amp;gt; &amp;lt;div className="w-full" /&amp;gt; &amp;lt;form className="flex w-full flex-col gap-4" action=""&amp;gt; &amp;lt;div className="flex w-full flex-col"&amp;gt; &amp;lt;label className="flex items-center text-gray-600" htmlFor="currentPassword"&amp;gt; Current Password &amp;lt;span className="block text-sm font-medium text-red-500 ml-1"&amp;gt;*&amp;lt;/span&amp;gt; &amp;lt;/label&amp;gt; &amp;lt;input className="w-full border px-4 py-2 lg:w-1/2" type="password" placeholder="" /&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;div className="flex w-full flex-col"&amp;gt; &amp;lt;label className="flex items-center text-gray-600" htmlFor="newPassword"&amp;gt; New Password &amp;lt;span className="block text-sm font-medium text-red-500 ml-1"&amp;gt;*&amp;lt;/span&amp;gt; &amp;lt;/label&amp;gt; &amp;lt;input className="w-full border px-4 py-2 lg:w-1/2" type="password" placeholder="" /&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;div className="flex flex-col"&amp;gt; &amp;lt;label className="flex items-center text-gray-600" htmlFor="repeatNewPassword"&amp;gt; Repeat New Password &amp;lt;span className="block text-sm font-medium text-red-500 ml-1"&amp;gt;*&amp;lt;/span&amp;gt; &amp;lt;/label&amp;gt; &amp;lt;input className="w-full border px-4 py-2 lg:w-1/2" type="password" placeholder="" /&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;button className="mt-4 w-40 bg-violet-900 px-4 py-2 text-white"&amp;gt; Save Changes &amp;lt;/button&amp;gt; &amp;lt;/form&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;/section&amp;gt; &amp;lt;/section&amp;gt; );};export default ChangePassword;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will now make the last component, which is called profile. Make a file in the components folder and name it Profile.js.&lt;/p&gt;

&lt;p&gt;The Profile React component displays a user profile page with an avatar image, editable first, last, and bio fields, and a "Save changes" button.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import SideBar from './Sidebar';const Profile = () =&amp;gt; { return ( &amp;lt;section className="container flex-grow mx-auto max-w-[1200px] border-b py-5 lg:flex lg:flex-row lg:py-10"&amp;gt; &amp;lt;SideBar /&amp;gt; &amp;lt;section className="grid w-full max-w-[1200px] grid-cols-1 gap-3 px-5 pb-10"&amp;gt; &amp;lt;div className="py-5"&amp;gt; &amp;lt;div className="w-full"&amp;gt; &amp;lt;h2 className="text-2xl font-bold mb-4"&amp;gt;Avatar image&amp;lt;/h2&amp;gt; &amp;lt;div className="mx-auto mb-5 flex flex-row items-center bg-neutral-100 py-5 lg:mx-0 lg:w-1/2"&amp;gt; &amp;lt;img className="ml-5 h-20 w-20 rounded-full" src="https://imgur.com/JdFbwHZ" alt="John Doe" /&amp;gt; &amp;lt;form&amp;gt; &amp;lt;div&amp;gt; &amp;lt;label className="block"&amp;gt; &amp;lt;span className="sr-only"&amp;gt;Choose profile photo&amp;lt;/span&amp;gt; &amp;lt;input type="file" className="mx-auto ml-5 flex w-full justify-center border-yellow-400 text-sm outline-none file:mr-4 file:bg-amber-400 file:py-2 file:px-4 file:text-sm file:font-semibold" /&amp;gt; &amp;lt;/label&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;/form&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;form className="flex w-full flex-col gap-3" action=""&amp;gt; &amp;lt;div className="flex w-full flex-col"&amp;gt; &amp;lt;label className="flex" htmlFor="name"&amp;gt; First Name &amp;lt;span className="block text-sm font-medium text-slate-700 after:ml-0.5 after:text-red-500 after:content-['*']" /&amp;gt; &amp;lt;/label&amp;gt; &amp;lt;input className="w-full border px-4 py-2 lg:w-1/2" type="text" placeholder="Sarah" /&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;div className="flex w-full flex-col"&amp;gt; &amp;lt;label className="flex" htmlFor="name"&amp;gt; Last Name &amp;lt;span className="block text-sm font-medium text-slate-700 after:ml-0.5 after:text-red-500 after:content-['*']" /&amp;gt; &amp;lt;/label&amp;gt; &amp;lt;input className="w-full border px-4 py-2 lg:w-1/2" type="text" placeholder="Johnson" /&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;div className="flex flex-col"&amp;gt; &amp;lt;label htmlFor=""&amp;gt;Bio&amp;lt;/label&amp;gt; &amp;lt;textarea className="w-full border px-4 py-2 text-gray-500 outline-none lg:w-1/2" name="" id="" cols={30} rows={10} defaultValue={' CEO, MayBell Inc.'} /&amp;gt; &amp;lt;button className="mt-4 w-40 bg-violet-900 px-4 py-2 text-white"&amp;gt; Save changes &amp;lt;/button&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;/form&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;/section&amp;gt; {/* /form */} &amp;lt;/section&amp;gt; );};export default Profile;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lastly, update the App.jsx file with the following code: This code creates React Router routes for Account, Profile, ManageAdresse, and ChangePassword in a React BrowserRouter.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import './App.css';import { BrowserRouter, Routes, Route } from 'react-router-dom';import Profile from './components/Profile';import Account from './components/Account';import Adresse from './components/Adresse';import ChangePassword from './components/ChangePassword';export default function App() { return ( &amp;lt;BrowserRouter&amp;gt; &amp;lt;Routes&amp;gt; &amp;lt;Route path="/" element={&amp;lt;Account /&amp;gt;}&amp;gt;&amp;lt;/Route&amp;gt; &amp;lt;Route path="/profile-information" element={&amp;lt;Profile /&amp;gt;}&amp;gt;&amp;lt;/Route&amp;gt;{' '} &amp;lt;Route path="/manage-adresse" element={&amp;lt;Adresse /&amp;gt;}&amp;gt;&amp;lt;/Route&amp;gt; &amp;lt;Route path="/change-password" element={&amp;lt;ChangePassword /&amp;gt;}&amp;gt;&amp;lt;/Route&amp;gt; &amp;lt;/Routes&amp;gt; &amp;lt;/BrowserRouter&amp;gt; );}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the application using the following command:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;We have successfully constructed a multi-page account manager using React Router, which is fully responsive, as shown in the images below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F19tqpuhdosj7xo30tcok.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F19tqpuhdosj7xo30tcok.png" width="800" height="370"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F6mpqcp074fr5vni95q0h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F6mpqcp074fr5vni95q0h.png" width="415" height="630"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;We built an Account Manager app using the React Router DOM in this post. We demonstrated the organization of routes and components for seamless navigation by covering major components such as Profile, Account, Manage Address, and Change Password.&lt;/p&gt;

&lt;p&gt;As you go on with your Account Manager development, think about adding more complex functionalities, investigating more style possibilities, and researching new features. With React Router DOM as your base, you can build a robust navigation system and modify the app to fit your needs. In order to build a powerful yet intuitive account management system, you need to keep trying new things and honing your abilities. Have fun with the code!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Making a RESTful API using Express, MongoDB, and Node.js: A Complete Guide</title>
      <dc:creator>Daniel Musembi</dc:creator>
      <pubDate>Sat, 20 Jan 2024 23:21:52 +0000</pubDate>
      <link>https://dev.to/danmusembi/making-a-restful-api-using-express-mongodb-and-nodejs-a-complete-guide-eie</link>
      <guid>https://dev.to/danmusembi/making-a-restful-api-using-express-mongodb-and-nodejs-a-complete-guide-eie</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;The Representational State Transfer REST architecture style is a way of building applications that may be used in a network. Conceptually, it lays down the rules for how web services should talk to each other using the Hypertext Transfer Protocol&lt;code&gt;HTTP&lt;/code&gt;. Resources in RESTful &lt;code&gt;APIs&lt;/code&gt; are accessible by distinct URLs, and operations on these resources are carried out using the normal HTTP methods &lt;code&gt;GET&lt;/code&gt; &lt;code&gt;POST&lt;/code&gt;, &lt;code&gt;PUT&lt;/code&gt;, and &lt;code&gt;DELETE&lt;/code&gt;. Because of the design's emphasis on stateless communication, all the necessary information to interpret and process a client request is included inside it. With &lt;code&gt;RESTful APIs&lt;/code&gt;, web service development is standardised and scalable, facilitating client-server resource access and manipulation and facilitating system-to-system interoperability.&lt;/p&gt;

&lt;p&gt;When it comes to creating RESTful APIs, a popular and powerful stack for web application development is &lt;code&gt;Express&lt;/code&gt;, &lt;code&gt;MongoDB&lt;/code&gt;, and &lt;code&gt;Node.js&lt;/code&gt;. A quick rundown of why each part is important is this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Node.js:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Devoid of blocking Regarding input/output, &lt;code&gt;Node.js&lt;/code&gt; is well-known for efficiently processing concurrent requests due to its event-driven, non-blocking architecture. To construct performant, scalable apps, this is essential.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Express:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Built on top of &lt;code&gt;Node.js&lt;/code&gt;, &lt;code&gt;Express&lt;/code&gt; is a web application framework that allows for creating powerful &lt;code&gt;APIs&lt;/code&gt; and web apps with minimum and adaptable code. Routing, middleware integration, and &lt;code&gt;HTTP&lt;/code&gt; request handling are just a few of the typical operations that it streamlines.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;MongoDB:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;NoSQL database: One well-known NoSQL database is &lt;code&gt;MongoDB&lt;/code&gt;, which uses adaptable documents similar to &lt;code&gt;JSON&lt;/code&gt; to store data. Agile development and easy scalability are made possible by its schema-less nature. When dealing with massive volumes of unstructured data, it shines.&lt;/p&gt;

&lt;p&gt;If you have &lt;code&gt;Visual Studio Code&lt;/code&gt;, &lt;code&gt;MongoDB&lt;/code&gt;, &lt;code&gt;Node.js&lt;/code&gt;, and &lt;code&gt;npm&lt;/code&gt; installed, then you should be good to go.&lt;/p&gt;

&lt;p&gt;Make sure to check the following if you haven't already:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://nodejs.org/en" rel="noopener noreferrer"&gt;Installing Nodejs&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.mongodb.com/try/download/community" rel="noopener noreferrer"&gt;Download MongoDB&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://code.visualstudio.com/download" rel="noopener noreferrer"&gt;Download VS code&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting Up the Environment
&lt;/h2&gt;

&lt;p&gt;To begin, let's make a new folder for the project. Launch your terminal and execute each command individually.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir restful-apicd restful-api
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On your computer, this will make a new directory.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fw9qjde5aj7dsri8znk0l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fw9qjde5aj7dsri8znk0l.png" width="386" height="175"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Run the following command in the terminal to initialize our project. Make sure you're in the folder where you just created it.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;The command above will show you how to make a package.json file. Type in the question's information, then press "Enter" to move on to the next query.&lt;/p&gt;

&lt;p&gt;Once you finish setting up the &lt;code&gt;package.json&lt;/code&gt; file, enter the following command to launch your application on &lt;code&gt;VS code&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Installing Dependencies
&lt;/h3&gt;

&lt;p&gt;Installation of project packages is done with the following terminal command. We install nodemon to restart the server after file changes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install express mongoose dotenvnpm install -g nodemon
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will install the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Express&lt;/code&gt;: To construct the web server.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Mongoose&lt;/code&gt; is an object data modelling (ODM) library that is compatible with &lt;code&gt;MongoDB&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To load environment variables from &lt;code&gt;a.env&lt;/code&gt; file, use the &lt;code&gt;dotenv&lt;/code&gt; variable.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Following the execution of the command described above, the structure of your project folder should look like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fx0gd2r8l614phgff42fs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fx0gd2r8l614phgff42fs.png" width="171" height="138"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Setting up a simple Express app
&lt;/h3&gt;

&lt;p&gt;Begin by creating a file in your root directory that is called index.js.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F1viub7a19678ell5h05u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F1viub7a19678ell5h05u.png" width="170" height="152"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To understand how to set up an Express app, head to this website &lt;a href="http://expressjs.com" rel="noopener noreferrer"&gt;expressjs.com&lt;/a&gt;, Then, after clicking on Getting Started, select the Hello World option. You are going to find the code for the basic example of Hello World.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fynq0jkvt9eozii1o6ldm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fynq0jkvt9eozii1o6ldm.png" width="649" height="354"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Copy the code and paste it into the index.js file we just made. you can change &lt;code&gt;Example app listening on port ${port}&lt;/code&gt; to &lt;code&gt;Server is listening on port ${port}&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require("express");const app = express();const port = 3000;app.get("/", (req, res) =&amp;gt; { res.send("Hello World!");});app.listen(port, () =&amp;gt; { console.log(`Server is listening on port ${port}`);});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the server with the following command&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Output on the terminal&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fo8kxutwnybwjg27mffeh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fo8kxutwnybwjg27mffeh.png" width="569" height="111"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A page saying &lt;strong&gt;Hello world!&lt;/strong&gt; will load in your browser when you navigate to &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;&lt;code&gt;localhost:3000&lt;/code&gt;&lt;/a&gt;. Which means our server is running properly.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F8jcxxc496rdmxlkdxzyg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F8jcxxc496rdmxlkdxzyg.png" width="294" height="90"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating a MongoDB database
&lt;/h3&gt;

&lt;p&gt;We will create a Database on MongoDB Atlas in this step.&lt;/p&gt;

&lt;p&gt;To begin, visit &lt;a href="https://account.mongodb.com/account/login" rel="noopener noreferrer"&gt;MongoDB Atlas&lt;/a&gt; and either register for an account or log in with the one you currently have.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;username&lt;/strong&gt; and &lt;strong&gt;password&lt;/strong&gt; are required when making a new account, so be sure to fill them out. After that, enter your &lt;code&gt;IP address&lt;/code&gt; and select &lt;em&gt;My Local Environment&lt;/em&gt; in the second step. Select &lt;strong&gt;Finish and Close&lt;/strong&gt; from the bottom menu.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fpg0uy6nithrz4rmmvvvs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fpg0uy6nithrz4rmmvvvs.png" width="800" height="457"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After that, locate Database in the left-hand menu and, on the right-hand side of the screen, click the Create button. create a &lt;strong&gt;free Shared Cluster&lt;/strong&gt;. To join the cluster, go to its homepage and look for the "connect" option.&lt;/p&gt;

&lt;p&gt;The following window will appear and then click on the Compass link.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fvcxy4yhe8w3w3ond3szs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fvcxy4yhe8w3w3ond3szs.png" width="765" height="624"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Make a copy of the connection string and substitute it with the password you've already generated. If you haven't already, make sure to install the &lt;code&gt;MongoDB Compass&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is how your connection string ought to appear.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mongodb+srv://dandev20:&amp;lt;password&amp;gt;@cluster0.m56pnex.mongodb.net/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Creating a &lt;code&gt;.env&lt;/code&gt; file
&lt;/h3&gt;

&lt;p&gt;We can create a new file in the root directory named &lt;code&gt;.env&lt;/code&gt; to secure our &lt;code&gt;mongoDB&lt;/code&gt; connection string.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F3ux6k2pdmsnz9u2tck9i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F3ux6k2pdmsnz9u2tck9i.png" width="174" height="177"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Put this code into the.env file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MONGODB_URL=&amp;lt;Your MongoDB Connection String&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Put the connection string that you got from MongoDB Atlas instead of. After that, your.env file should match this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mongodb+srv://dandev20: ****** @cluster0.m56pnex.mongodb.net/my-restful-api
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At the end of the connection string I have added &lt;strong&gt;my-restful-api&lt;/strong&gt; which is my database name, feel free to customize your database name.&lt;/p&gt;

&lt;h3&gt;
  
  
  Connecting to MongoDB using Mongoose
&lt;/h3&gt;

&lt;p&gt;The following &lt;code&gt;Node.js&lt;/code&gt; script connects to a &lt;code&gt;MongoDB&lt;/code&gt; database using the &lt;code&gt;Mongoose&lt;/code&gt; module. The &lt;code&gt;dotenv&lt;/code&gt; module, which handles environment variables, and &lt;code&gt;Mongoose&lt;/code&gt;, which interacts with &lt;code&gt;MongoDB&lt;/code&gt;, are among the required dependencies imported at the beginning. The connection is established using the &lt;code&gt;mongoose.connect()&lt;/code&gt; method, which retrieves the &lt;code&gt;MongoDB&lt;/code&gt; connection URL from the&lt;code&gt;.env&lt;/code&gt; file. A message confirming the successful connection to MongoDB is printed to the console when the connection is successful. To link to the MongoDB, include the following code in your index.js file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;require("dotenv").config(); //for using variables from .env file.const app = express();const port = 3000;mongoose.connect(process.env.MONGODB_URL).then(() =&amp;gt; { console.log("MongoDB is connected!");});app.get("/", (req, res) =&amp;gt; { res.send("Hello World!");});app.listen(port, () =&amp;gt; { console.log(`Server is listening on port ${port}`);});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the expected terminal output after restarting the server with the node index.js command.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F2pgrt4p3ocm3vnakos3h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F2pgrt4p3ocm3vnakos3h.png" width="594" height="139"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you don't see that &lt;code&gt;MongoDB&lt;/code&gt; is connected, make sure you restart the server and check to see if you added the &lt;code&gt;IP address&lt;/code&gt; to &lt;code&gt;MongoDB's network access&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Define simple schema
&lt;/h3&gt;

&lt;p&gt;We begin by creating a basic project schema. To accomplish this, in your project's root directory, make a new folder called &lt;strong&gt;model&lt;/strong&gt; and then in that folder, create a file called &lt;strong&gt;todo.js.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The following snippet of code uses the &lt;code&gt;Mongoose&lt;/code&gt; module to define a &lt;strong&gt;Task schema&lt;/strong&gt; in &lt;code&gt;MongoDB&lt;/code&gt;. It is written in &lt;code&gt;Node.js&lt;/code&gt;. The schema is comprised of two fields: &lt;strong&gt;title&lt;/strong&gt; and &lt;strong&gt;description&lt;/strong&gt; both of type String. In order to organize the data model, the Schema class from &lt;code&gt;Mongoose&lt;/code&gt; is utilized. To communicate with &lt;code&gt;MongoDB&lt;/code&gt; documents that adhere to this schema, a &lt;code&gt;Mongoose&lt;/code&gt; model called &lt;strong&gt;Task&lt;/strong&gt; is generated using &lt;code&gt;mongoose.model()&lt;/code&gt; after the schema is defined. Last but not least, other components of the app can make use of the &lt;strong&gt;Task&lt;/strong&gt; model by exporting it to the &lt;code&gt;MongoDB&lt;/code&gt; database. Add the code to the &lt;code&gt;task.js&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mongoose = require("mongoose");const { Schema } = mongoose;const taskSchema = new Schema({ title: { type: String, }, description: { type: String, },});const Task = mongoose.model("Task", taskSchema);module.exports = Task;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's import this model in our &lt;strong&gt;index.js&lt;/strong&gt; file&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Flaw151fy5yq0phxgfurb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Flaw151fy5yq0phxgfurb.png" width="374" height="123"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementing CRUD Operations
&lt;/h2&gt;

&lt;p&gt;Create, Read, Update, and Delete (CRUD) are the four cornerstones of database administration and data processing. To manage and interact with data inside a system or application, these activities are crucial. Every action has its distinct function:&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating resources (POST)
&lt;/h3&gt;

&lt;p&gt;During this process, further records will be added to the database. It typically refers to adding a new document to an existing collection when discussing &lt;code&gt;MongoDB&lt;/code&gt; and &lt;code&gt;Mongoose&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In the following code;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;/tasks/create&lt;/strong&gt; endpoint is an &lt;code&gt;HTTP POST&lt;/code&gt; method for adding a new task to the list. The code within the async function is executed whenever a &lt;code&gt;POST&lt;/code&gt; request is made to /tasks/create. The data in the request body &lt;code&gt;req.body&lt;/code&gt; is used to create a new task item using the Task.create method. The newly created task is sent in JSON format along with a &lt;code&gt;status code 201&lt;/code&gt; if the creation is successful. A &lt;code&gt;500 status code&lt;/code&gt; and the words &lt;strong&gt;Internal Server Error&lt;/strong&gt; are the responses it gives if an error occurs during the operation. Add the code to the &lt;code&gt;index.js&lt;/code&gt; file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Create an task itemapp.post("/task/create", async (req, res) =&amp;gt; { try { const newTask = await Task.create(req.body); res.status(201).json(newTask); } catch (error) { res.status(500).json({ error: "Internal Server Error" }); }});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Read resources (GET)
&lt;/h3&gt;

&lt;p&gt;Reading is getting information out of a database using queries and retrieval. You can use this operation to retrieve data according to specific criteria, like whether you need to get one document, a few documents, or the entire collection.&lt;/p&gt;

&lt;p&gt;The following Express.js route handler handles &lt;strong&gt;/tasks&lt;/strong&gt; &lt;code&gt;GET&lt;/code&gt; requests. &lt;code&gt;Todo.find()&lt;/code&gt; fetches all todos from &lt;code&gt;MongoDB&lt;/code&gt; and returns a &lt;code&gt;JSON&lt;/code&gt; response with a 200 status code on success. The process logs errors and sends a &lt;code&gt;JSON&lt;/code&gt; response with a 500 status code for internal server errors. add the code to the &lt;code&gt;index.js&lt;/code&gt; file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Read all tasksapp.get("/tasks", async (req, res) =&amp;gt; { try { const tasks = await Task.find(); res.status(200).json(tasks); } catch (error) { console.log(error); res.status(500).json({ error: "Internal Server Error" }); }});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Updating resources (PUT/PATCH)
&lt;/h3&gt;

&lt;p&gt;Updating includes changing database records. It can involve changing document fields or replacing the whole thing.&lt;/p&gt;

&lt;p&gt;The following &lt;code&gt;Express.js&lt;/code&gt; route handler updates tasks by ID via a &lt;code&gt;PUT&lt;/code&gt; request at "/tasks/:id". The Task.&lt;code&gt;findByIdAndUpdate&lt;/code&gt; method finds the task by ID and updates it with request body data. Updated tasks are provided as &lt;code&gt;JSON&lt;/code&gt; responses with a &lt;strong&gt;200 status code&lt;/strong&gt; on success and a &lt;strong&gt;500 status code&lt;/strong&gt; on failure, signalling an internal server fault. Add the code to the &lt;code&gt;index.js&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Update a task by IDapp.put("/tasks/:id", async (req, res) =&amp;gt; { try { const updatedTask = await Task.findByIdAndUpdate(req.params.id, req.body, { new: true, }); res.status(200).json(updatedTask); } catch (error) { res.status(500).json({ error: "Internal Server Error" }); }});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Deleting resources (DELETE)
&lt;/h3&gt;

&lt;p&gt;The delete procedure removes database records. This usually implies removing documents from a MongoDB collection.&lt;/p&gt;

&lt;p&gt;This &lt;code&gt;Node.js&lt;/code&gt; and Express code define a route handler for handling HTTP DELETE requests to "/tasks/:id". It uses MongoDB/Mongoose to delete a task by its ID, responding with a status code of 204 if successful, or 500 with an error message if an internal server error occurs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.delete("/tasks/:id", async (req, res) =&amp;gt; { try { await Task.findByIdAndDelete(req.params.id); res.status(204).send(); } catch (error) { res.status(500).json({ error: "Internal Server Error" }); }});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since we will be reading the data in JSON format, we must add the following line of code to our index.js file before testing our API.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//middleware provided by Express to parse incoming JSON requests.app.use(express.json());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;index.js&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express')const mongoose = require("mongoose");const Task = require("./model/task");require("dotenv").config(); //for using variables from .env file.const app = express()const port = 3000//middleware provided by Express to parse incoming JSON requests.app.use(express.json()); mongoose.connect(process.env.MONGODB_URL).then(() =&amp;gt; { console.log("MongoDB is connected!"); });app.get('/', (req, res) =&amp;gt; { res.send('Hello World!')})// Create an task itemapp.post("/tasks/create", async (req, res) =&amp;gt; { try { const newTask = await Todo.create(req.body); res.status(201).json(newTask); } catch (error) { res.status(500).json({ error: "Internal Server Error" }); }});// Read all todosapp.get("/tasks", async (req, res) =&amp;gt; { try { const tasks = await Task.find(); res.status(200).json(tasks); } catch (error) { console.log(error); res.status(500).json({ error: "Internal Server Error" }); }});// Update a task by IDapp.put("/tasks/:id", async (req, res) =&amp;gt; { try { const updatedTask = await Task.findByIdAndUpdate(req.params.id, req.body, { new: true, }); res.status(200).json(updatedTask); } catch (error) { res.status(500).json({ error: "Internal Server Error" }); }});// Delete a todo by IDapp.delete("/tasks/:id", async (req, res) =&amp;gt; { try { await Task.findByIdAndDelete(req.params.id); res.status(204).send(); } catch (error) { res.status(500).json({ error: "Internal Server Error" }); }});// Delete a todo by IDapp.delete("/tasks/:id", async (req, res) =&amp;gt; { try { await Task.findByIdAndDelete(req.params.id); res.status(204).send(); } catch (error) { res.status(500).json({ error: "Internal Server Error" }); }});app.listen(port, () =&amp;gt; { console.log(`Server is listening on port ${port}`)})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Testing the API
&lt;/h2&gt;

&lt;p&gt;Run your server using this command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nodemon index.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2F9ssg6mp5ky8x5yhx9l6s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F9ssg6mp5ky8x5yhx9l6s.png" width="579" height="233"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When it comes to testing the API, I'm making use of the &lt;strong&gt;Thunder Client&lt;/strong&gt; extension for Visual Studio Code.&lt;/p&gt;

&lt;p&gt;To install the Thunder Client extension, click on manage icon on bottom left corner and select extensions.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fvw44gfbl89sj66q7d6vu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fvw44gfbl89sj66q7d6vu.png" width="800" height="426"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Use the search bar to look for it, and then install it. After installing a thunder client icon will apper on the left screen, click on it and then click on new request on top to start working with it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating a new Task item using POST request
&lt;/h3&gt;

&lt;p&gt;Make sure that the Content-Type is set to application/json in the &lt;strong&gt;Headers&lt;/strong&gt; tab, then select POST and write the URL along the lines as in the image below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F8rp1gbn6ugiknijbd2f6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F8rp1gbn6ugiknijbd2f6.png" width="580" height="290"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Make sure to include a title and description of the Task item in the &lt;strong&gt;Body&lt;/strong&gt; tab, just like we see in the image.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Ffbw9j12hov3ihnhn7pq8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Ffbw9j12hov3ihnhn7pq8.png" width="568" height="352"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The following response will be displayed after you click the Send button.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fu73j8kiyjxrpiwo2qgg0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fu73j8kiyjxrpiwo2qgg0.png" width="569" height="222"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We completed our first duty. You can create more task items with different titles and descriptions using the same approach. The MongoDB compass app can also display these items after connecting with your connection string, as shown below.&lt;/p&gt;

&lt;p&gt;Select compass as the tool to connect to MongoDB compass by clicking connet on MongoDB. substitute with your own password and insert it into the campass connection string.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fjnefi2ejxnziq0r55x0e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fjnefi2ejxnziq0r55x0e.png" width="800" height="433"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Get all tasks using a GET request
&lt;/h3&gt;

&lt;p&gt;To access the tasks, select GET, copy the URL (as seen in the figure below), and then hit the Send button.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fsiqm9pim3oqlvduehrd0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fsiqm9pim3oqlvduehrd0.png" width="566" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As can be seen in the image, you will receive the response.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F1x6307810x4ke0zoeizm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F1x6307810x4ke0zoeizm.png" width="571" height="272"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Update task using PUT request
&lt;/h3&gt;

&lt;p&gt;We are going to require the id of the task item in order to properly update the task. It is therefore necessary for you to copy the id of the task that you obtained in the previous response. To write the URL, select the PUT option and write it down as seen in the image.&lt;/p&gt;

&lt;p&gt;Now, change the title and description in the Body tab.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Ffgd31w2z414gck2lsrgy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Ffgd31w2z414gck2lsrgy.png" width="560" height="377"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you click "Send," you'll get the message below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F8ntw3i8u4ilkkwz5dhzh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F8ntw3i8u4ilkkwz5dhzh.png" width="570" height="248"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Delete a task item using the DELETE request
&lt;/h3&gt;

&lt;p&gt;Prior to deleting the task, we will need to know its ID. You need to copy the ID that you got in the last response . Select "DELETE" and write down the URL, as shown in the picture below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fyz28g1y4hib3yrrbwzet.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fyz28g1y4hib3yrrbwzet.png" width="569" height="261"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is what you'll get after hitting the "Send" button.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F4eowj9f6xmk9k6pfz0nm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F4eowj9f6xmk9k6pfz0nm.png" width="560" height="238"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusions
&lt;/h2&gt;

&lt;p&gt;Using Express, MongoDB, and Node.js, this detailed article has provided you with a full walkthrough of the main processes and concepts involved in the process of developing a RESTful application programming interface (API). We went over the fundamental components of establishing routes, interacting with MongoDB by means of Mongoose, and putting in place important HTTP methods. Developing a robust and scalable application programming interface (API) is ensured by placing a strong emphasis on error handling, the utilization of middleware, and adherence to best practices.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>A Comprehensive Guide on Using Tailwind CSS with React Icons to Create an Engaging Contact Page</title>
      <dc:creator>Daniel Musembi</dc:creator>
      <pubDate>Wed, 17 Jan 2024 10:34:01 +0000</pubDate>
      <link>https://dev.to/danmusembi/a-comprehensive-guide-on-using-tailwind-css-with-react-icons-to-create-an-engaging-contact-page-25gm</link>
      <guid>https://dev.to/danmusembi/a-comprehensive-guide-on-using-tailwind-css-with-react-icons-to-create-an-engaging-contact-page-25gm</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;One of the most important places for customers to reach out to companies online is the contact page. Beyond mere aesthetics, a visually appealing contact page improves the user experience, establishes trust, and promotes meaningful engagement. A contact page in the ever-changing world of web design is more than simply a static form; it's a portal to significant user interactions. Users' impressions of a company or brand are frequently shaped at the contact page, which is a crucial point of interaction when navigating through websites. An attractive contact page may greatly affect user engagement and the public's perception of your business, so you must take the time to design one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction to the technologies used: Tailwind CSS and React Icons
&lt;/h2&gt;

&lt;p&gt;Two robust technologies, &lt;a href="https://tailwindcss.com/" rel="noopener noreferrer"&gt;Tailwind CSS&lt;/a&gt; and &lt;a href="https://react-icons.github.io/react-icons/" rel="noopener noreferrer"&gt;React Icons&lt;/a&gt;, are essential in our pursuit of making an aesthetically appealing contact page. The complementary qualities of each element help to ensure that form and function are well-coordinated. Let's take a quick look at these technologies and see why they work so well together to make interesting user experiences.&lt;/p&gt;

&lt;h3&gt;
  
  
  Tailwind CSS: Unleashing Utility-First Styling
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Tailwind&lt;/code&gt; Regarding web development, &lt;code&gt;CSS&lt;/code&gt; is a game-changer since it is a utility-first framework. &lt;code&gt;Tailwind&lt;/code&gt; gives a large collection of utility classes that developers may use to compose styles directly in the &lt;code&gt;HTML&lt;/code&gt; markup, rather than depending on pre-defined styles. The unprecedented efficiency and adaptability of this method make it possible to build cutting-edge, user-responsive interfaces in a flash. some of the key features are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Putting Utility First:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Tailwind is utility-first, letting developers apply styles directly. This granular approach improves agility and reduces bespoke style.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Responsive Design Made Simple:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Tailwind makes responsive design easy. Responsive utility classes provide screen size adaptation without media queries in the framework.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Large Component Library:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Tailwind has a large pre-built component library. With these components and utility classes, developers can focus on creating great user experiences faster.&lt;/p&gt;

&lt;h3&gt;
  
  
  React Icons: Dynamic Iconography in React
&lt;/h3&gt;

&lt;p&gt;Many developers turn to the widely used React Icons package for easy icon integration into React apps. A wide variety of well-known font sets, including Material Design, &lt;code&gt;Font Awesome&lt;/code&gt;, and more, are available for customization in its extensive icon library. Using &lt;code&gt;React Icons&lt;/code&gt;, adding dynamic and aesthetically pleasing icons to your &lt;code&gt;React&lt;/code&gt; components is a breeze. some of the key features are as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Flexible Icons:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;React Icons&lt;/code&gt; provides developers with a variety of icon sets for different settings and design preferences.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Simple Integration:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Adding icons to &lt;code&gt;React&lt;/code&gt; components is easy with &lt;code&gt;React Icons&lt;/code&gt;. Icons can be imported and used directly in &lt;code&gt;React&lt;/code&gt; code, making apps more visually appealing.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Customizing &amp;amp; Theming:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;React Icons&lt;/code&gt; makes icon customisation and theming easy. Developers can match icon size, colour, and other styling to the design language.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting Up the Project
&lt;/h2&gt;

&lt;p&gt;To begin, open Visual Studio Code and run the following command in the console to create a new React app. Give it the name contact form and navigate to it using the second command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app contact-formcd contact-form
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Install the necessary dependencies by running the following command&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Installing Tailwind CSS in a React project
&lt;/h3&gt;

&lt;p&gt;Run the following command to install &lt;code&gt;tailwind&lt;/code&gt; CSS:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install -D tailwindcss postcss autoprefixer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Configuring Tailwind CSS to suit project requirements
&lt;/h3&gt;

&lt;p&gt;To turn the tailwind configuration file run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx tailwindcss init -p
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Image showing &lt;code&gt;tailwind&lt;/code&gt; CSS configuration file&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fqhoi9kpl8o3m8zlcy7m5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fqhoi9kpl8o3m8zlcy7m5.png" width="159" height="231"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The next step is to add the paths to all of your template files into the &lt;code&gt;tailwind.config.js&lt;/code&gt; file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'./index.html', './src/**/*.{js,ts,jsx,tsx}',
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Image showing where to add the paths in &lt;code&gt;tailwind.config.js&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F9fxroc0nmw2bqpks478m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F9fxroc0nmw2bqpks478m.png" width="513" height="186"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lastly, add &lt;code&gt;tailwind directives&lt;/code&gt; to the &lt;strong&gt;index.css&lt;/strong&gt; file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@import 'tailwindcss/base';@import 'tailwindcss/components';@import 'tailwindcss/utilities';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Installing React Icons
&lt;/h3&gt;

&lt;p&gt;Install react icons using the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install react-icons --save
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Designing the Contact Form
&lt;/h2&gt;

&lt;p&gt;In the &lt;code&gt;src&lt;/code&gt; folder, create a folder and name its &lt;strong&gt;components&lt;/strong&gt;. In the components folder create a file and name it &lt;strong&gt;ContactPage.jsx&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The following code is a contact page with classified contact information and an interactive form for user inquiries is provided by the ContactPage &lt;code&gt;React&lt;/code&gt; component. It dynamically generates a visually pleasing grid structure with the contact details for various departments, including Careers, Delivery, and Support. The form has email and full name input fields, a &lt;strong&gt;drop-down menu&lt;/strong&gt; for selecting a category, a text field for comments, and a checkbox for the terms and conditions agreement. The component, which has been styled with &lt;code&gt;Tailwind CSS&lt;/code&gt;, has a contemporary and responsive design.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
JavaScript import React from "react";import { MdEmail } from "react-icons/md";import { FaPhoneAlt, FaMessage } from "react-icons/fa";const ContactPage = () =&amp;gt; { const ContactInfo = [{ title: "Delivery", email: "&lt;a href="mailto:delivery@gmail.com"&gt;delivery@gmail.com&lt;/a&gt;", phone: "+254 780341267", message: "+254 789065432", }, { title: "Support", email: "&lt;a href="mailto:support@gmail.com"&gt;support@gmail.com&lt;/a&gt;", phone: "+254 765890231", message: "+254 756421190", }, { title: "Careers", email: "&lt;a href="mailto:careers@gmail.com"&gt;careers@gmail.com&lt;/a&gt;", phone: "+254 768905413", message: "+254 711748900", },]; return ( &lt;/p&gt;  &lt;h1&gt;Contact Us&lt;/h1&gt; &lt;p&gt; Have questions? We're here to help! &lt;/p&gt;   {ContactInfo.map((info, index) =&amp;gt; (  &lt;h2&gt;{info.title}&lt;/h2&gt;  &lt;a href="{"&gt;  {info.email} &lt;/a&gt; &lt;a href="{"&gt;  {info.phone} &lt;/a&gt; &lt;a href="{"&gt;  {info.message} &lt;/a&gt;   ))}   &lt;h2&gt;Have another question?&lt;/h2&gt; &lt;p&gt;Fill the form below&lt;/p&gt;          Please choose a category  Delivery Support Profile Careers Another category      I have read and agree with &lt;a href="/terms-and-conditions"&gt; {" "} terms &amp;amp; conditions &lt;/a&gt;    Send Message     );};export default ContactPage;&lt;br&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
## Enhancing User Experience with Icons

- Visual Representation: The `react-icons` library is utilized to provide icons like `MdEmail`, `FaPhoneAlt`, and `FaMessage`. Users will find it easier to recognize and comprehend the material when these symbols are used to visually represent the various sorts of contact information.

- Differentiation by Category: The contact information is set up in groups, such as **Careers** , **Delivery** and **Support.** An icon is used to represent each group, which makes it easier for users to find the information they need.

- Accessibility and Readability: Icons come with text information, which makes them easy for people who use screen readers to reach. Putting icons and words together makes things look better and easier to read.

- Styling for Visual Appeal: To keep the look of the contact page uniform, icons are styled with certain colours (text-violet-700). This helps with the overall look and feel of the business.

## Integrating into the App

Import and return the `ContactPage` component in the `app.js`

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

&lt;/div&gt;


&lt;p&gt;import "./App.css";import ContactPage from "./components/ContactPage";export default function App() { return ( &lt;/p&gt;   );}&lt;br&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
The following code will run the app in the terminal and show the contact form we just made in the browser.

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

&lt;/div&gt;


&lt;p&gt;npm start&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
An image displaying the contact form on the web browser

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705487052488/7bdc431b-c19f-4a59-bb79-f4deb16b6a86.png)

## Animating Icons for Engagement

You can make your user interface more interesting and interactive using animated icons. Using the supplied React component for a contact page, you can add basic icon movements with Tailwind CSS. Here's a way to make animations more engaging for users:

- Hover Effects: You can add styles explicitly on hover using the hover classes provided by Tailwind CSS. Add a professional touch by combining them with transition classes. Here, the icon's size and colour will vary as you hover over it.

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

&lt;/div&gt;



&lt;p&gt;;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
- Apply Animation Classes: Tailwind CSS provides utility classes for various animations. You can apply these classes to your icons to create animation effects. For example, you can use the `animate-bounce` class for a bouncing effect.

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

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
- Add Transitions: Apply transition classes to create smooth transitions for hover effects. For example, you can use the transition-transform class to smoothly transition the transform property. This will scale the icon to 110% on hover with a smooth transition.

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

&lt;/div&gt;



&lt;p&gt;;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
- Balancing Aesthetics: Find the sweet spot between visual appeal and user engagement by experimenting with various transition and animation classes. Pick animations that go with the aesthetic motif of your contact page while keeping the entire design in mind.

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

&lt;/div&gt;



&lt;p&gt;;&lt;/p&gt;



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


Use these ways to bring your icons to life, making the contact page more visually appealing and engaging. Test and iterate to find the optimal animation styles for your app's design and user experience.

## Optimizing for Accessibility

Web development requires accessibility to make digital material usable by all. This comprehensive guide optimizes icon and form element accessibility to meet and exceed accessibility standards. Follow these best practices to make the web more inclusive.

1. Icons Accessibility

2. Focus on ARIA Roles

3. Form Elements Accessibility

4. Testing for Accessibility

## Troubleshooting and Debugging

Building a contact page with `Tailwind CSS` and `React` icons will cause complications. Let's discuss troubleshooting and debugging basics:

### Missing Styles or Classes

- Make sure `Tailwind CSS` and `React Icons` are installed and set up.

- Imported Statements: Recheck your project's import statements for both libraries.

- Check your `Tailwind CSS` configuration for mistakes and missing settings.

- Adjust `PurgeCSS` parameters to avoid accidentally removing styles.

### Browser Developer Tools

- Use browser developer tools to inspect components and detect styles.

- Examine computed styles to understand how styles cascade and affect components.

- The `Tailwind CSS DevTools` extension provides browser-specific debugging capabilities.

- Interactive Mode: Interactive mode lets you test utility classes in real time.

### Cross-Browser Compatibility

- Find anomalies by testing your contact page on different browsers.

- Vendor Prefixes: Address vendor-prefix issues for certain CSS properties that may vary across browsers.

- Versions of Libraries: Check that your `React icons` work with the latest version of `React`.

- Make sure you're using the `React Icons` components correctly in your JSX by checking their usage.

### Optimizing Styles

- Maximize the efficiency of your build process by adjusting [PurgeCSS](https://purgecss.com/) to delete unnecessary styles.

- Prioritize the loading of vital styles to speed up the initial page rendering in critical `CSS`.

- Use `cache-busting techniques` on `CSS` files to ensure users always see the most recent styles.

- Versioning Your Libraries: To Take Advantage of Performance and Bug Fix Updates, Always Use the Most Recent Versions of Your Libraries.

## Conclusion

The focus is on accessibility optimization when creating an interesting contact page with `Tailwind` CSS and `React` Icons. The use of accessible icons and form elements must be strictly enforced. You may ensure a smooth and accessible user experience by using ARIA properties that are made expressly for screen readers. Your contact page will be both aesthetically pleasing and usable for all users, regardless of their skills, thanks to thorough testing of accessibility elements and subsequent improvements. The significance of making user-friendly and accessible web content is shown by this dedication to accessibility.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
    </item>
  </channel>
</rss>
