<?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: Nandhakumar</title>
    <description>The latest articles on DEV Community by Nandhakumar (@nandhakumar).</description>
    <link>https://dev.to/nandhakumar</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%2F535555%2Fdcffe7e4-7de4-4295-9b64-4e649d4cdf2f.png</url>
      <title>DEV Community: Nandhakumar</title>
      <link>https://dev.to/nandhakumar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nandhakumar"/>
    <language>en</language>
    <item>
      <title>How To Add Custom Font in Flutter</title>
      <dc:creator>Nandhakumar</dc:creator>
      <pubDate>Fri, 17 Feb 2023 23:36:19 +0000</pubDate>
      <link>https://dev.to/nandhakumar/how-to-add-custom-font-in-flutter-1e2o</link>
      <guid>https://dev.to/nandhakumar/how-to-add-custom-font-in-flutter-1e2o</guid>
      <description>&lt;p&gt;Recently, I was developing a clothing store app using flutter. And I was about to use a custom font, so researched flutter docs to find a way to add custom fonts.&lt;/p&gt;

&lt;p&gt;Here's how I did 👇&lt;/p&gt;

&lt;h2&gt;
  
  
  Download your fonts
&lt;/h2&gt;

&lt;p&gt;As a first step download the required fonts. Flutter supports &lt;code&gt;.ttc&lt;/code&gt;,&lt;code&gt;.ttf&lt;/code&gt; and &lt;code&gt;.otf&lt;/code&gt; fonts. It doesn't support &lt;code&gt;.woff&lt;/code&gt; and &lt;code&gt;.woff2&lt;/code&gt; fonts for all platforms.&lt;/p&gt;

&lt;h2&gt;
  
  
  Add Fonts To Your Project
&lt;/h2&gt;

&lt;p&gt;Create &lt;code&gt;assets/fonts&lt;/code&gt; in the root level of the project&lt;/p&gt;

&lt;p&gt;Add all your fonts.&lt;/p&gt;

&lt;p&gt;In my case, I am adding all styles of &lt;code&gt;EncodeSans&lt;/code&gt; Font to &lt;code&gt;Fonts&lt;/code&gt; directory&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0gsy6wt967hpode31zf5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0gsy6wt967hpode31zf5.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Declare Fonts In Pubspec.yml
&lt;/h2&gt;

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

fonts:
    - family: EncodeSans
      fonts:
        - asset: assets/fonts/EncodeSans-Black.ttf
          weight: 900
        - asset: assets/fonts/EncodeSans-ExtraBold.ttf
          weight: 800
        - asset: assets/fonts/EncodeSans-Bold.ttf
          weight: 700
        - asset: assets/fonts/EncodeSans-SemiBold.ttf
          weight: 600
        - asset: assets/fonts/EncodeSans-Medium.ttf
          weight: 500
        - asset: assets/fonts/EncodeSans-Regular.ttf
          weight: 400
        - asset: assets/fonts/EncodeSans-Thin.ttf
          weight: 300
        - asset: assets/fonts/EncodeSans-Light.ttf
          weight: 200
        - asset: assets/fonts/EncodeSans-ExtraLight.ttf
          weight: 100


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

&lt;/div&gt;
&lt;h2&gt;
  
  
  Create a Font Family Typography Class
&lt;/h2&gt;

&lt;p&gt;Creating a common Typography class that holds font family styles would help in using font family throughout the application&lt;/p&gt;

&lt;p&gt;Add &lt;code&gt;constant/typography.dart&lt;/code&gt; file under &lt;code&gt;lib&lt;/code&gt; directory&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// typography.dart&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="s"&gt;'package:flutter/material.dart'&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;_FONT_FAMILY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'EncodeSans'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Typogaphy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="n"&gt;TextStyle&lt;/span&gt; &lt;span class="n"&gt;Black&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
      &lt;span class="n"&gt;TextStyle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;fontFamily:&lt;/span&gt; &lt;span class="n"&gt;_FONT_FAMILY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;fontWeight:&lt;/span&gt; &lt;span class="n"&gt;FontWeight&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;w900&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="n"&gt;TextStyle&lt;/span&gt; &lt;span class="n"&gt;ExtraBold&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
      &lt;span class="n"&gt;TextStyle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;fontFamily:&lt;/span&gt; &lt;span class="n"&gt;_FONT_FAMILY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;fontWeight:&lt;/span&gt; &lt;span class="n"&gt;FontWeight&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;w800&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="n"&gt;TextStyle&lt;/span&gt; &lt;span class="n"&gt;Bold&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
      &lt;span class="n"&gt;TextStyle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;fontFamily:&lt;/span&gt; &lt;span class="n"&gt;_FONT_FAMILY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;fontWeight:&lt;/span&gt; &lt;span class="n"&gt;FontWeight&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;w700&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="n"&gt;TextStyle&lt;/span&gt; &lt;span class="n"&gt;SemiBold&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
      &lt;span class="n"&gt;TextStyle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;fontFamily:&lt;/span&gt; &lt;span class="n"&gt;_FONT_FAMILY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;fontWeight:&lt;/span&gt; &lt;span class="n"&gt;FontWeight&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;w600&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="n"&gt;TextStyle&lt;/span&gt; &lt;span class="n"&gt;Medium&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
      &lt;span class="n"&gt;TextStyle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;fontFamily:&lt;/span&gt; &lt;span class="n"&gt;_FONT_FAMILY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;fontWeight:&lt;/span&gt; &lt;span class="n"&gt;FontWeight&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;w500&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="n"&gt;TextStyle&lt;/span&gt; &lt;span class="n"&gt;Regular&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
      &lt;span class="n"&gt;TextStyle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;fontFamily:&lt;/span&gt; &lt;span class="n"&gt;_FONT_FAMILY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;fontWeight:&lt;/span&gt; &lt;span class="n"&gt;FontWeight&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;w400&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="n"&gt;TextStyle&lt;/span&gt; &lt;span class="n"&gt;Thin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
      &lt;span class="n"&gt;TextStyle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;fontFamily:&lt;/span&gt; &lt;span class="n"&gt;_FONT_FAMILY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;fontWeight:&lt;/span&gt; &lt;span class="n"&gt;FontWeight&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;w300&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="n"&gt;TextStyle&lt;/span&gt; &lt;span class="n"&gt;Light&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
      &lt;span class="n"&gt;TextStyle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;fontFamily:&lt;/span&gt; &lt;span class="n"&gt;_FONT_FAMILY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;fontWeight:&lt;/span&gt; &lt;span class="n"&gt;FontWeight&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;w200&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="n"&gt;TextStyle&lt;/span&gt; &lt;span class="n"&gt;ExtraLight&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
      &lt;span class="n"&gt;TextStyle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;fontFamily:&lt;/span&gt; &lt;span class="n"&gt;_FONT_FAMILY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;fontWeight:&lt;/span&gt; &lt;span class="n"&gt;FontWeight&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;w100&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;



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

&lt;/div&gt;
&lt;h2&gt;
  
  
  Using Font Family From The Typography Class
&lt;/h2&gt;

&lt;p&gt;To use the font family declared in the typography class, &lt;br&gt;
use &lt;code&gt;copyWith&lt;/code&gt; method to merge typography style with text-specific style.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;

&lt;span class="n"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'This is a Semi Bold Typo'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                  &lt;span class="nl"&gt;textAlign:&lt;/span&gt; &lt;span class="n"&gt;TextAlign&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;center&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                  &lt;span class="nl"&gt;style:&lt;/span&gt; &lt;span class="n"&gt;Typogaphy&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;SemiBold&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;copyWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                    &lt;span class="nl"&gt;color:&lt;/span&gt; &lt;span class="n"&gt;Colors&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;black&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="nl"&gt;fontSize:&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                  &lt;span class="p"&gt;))&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;Here's my main.dart file looks like&lt;/p&gt;

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

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="s"&gt;'package:clothing_store/constant/typography.dart'&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="s"&gt;'package:flutter/material.dart'&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;runApp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;MyApp&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyApp&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="n"&gt;StatelessWidget&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;MyApp&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="nd"&gt;@override&lt;/span&gt;
  &lt;span class="n"&gt;Widget&lt;/span&gt; &lt;span class="n"&gt;build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;BuildContext&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;MaterialApp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="nl"&gt;title:&lt;/span&gt; &lt;span class="s"&gt;'Flutter Custom Typo'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nl"&gt;theme:&lt;/span&gt; &lt;span class="n"&gt;ThemeData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="nl"&gt;primarySwatch:&lt;/span&gt; &lt;span class="n"&gt;Colors&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;blue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;),&lt;/span&gt;
      &lt;span class="nl"&gt;home:&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;MyHomePage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;title:&lt;/span&gt; &lt;span class="s"&gt;'Flutter Demo Home Page'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyHomePage&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="n"&gt;StatefulWidget&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;MyHomePage&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;required&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nd"&gt;@override&lt;/span&gt;
  &lt;span class="n"&gt;State&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;MyHomePage&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;createState&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;_MyHomePageState&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;_MyHomePageState&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="n"&gt;State&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;MyHomePage&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nd"&gt;@override&lt;/span&gt;
  &lt;span class="n"&gt;Widget&lt;/span&gt; &lt;span class="n"&gt;build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;BuildContext&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Scaffold&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="nl"&gt;appBar:&lt;/span&gt; &lt;span class="n"&gt;AppBar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="nl"&gt;title:&lt;/span&gt; &lt;span class="n"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;widget&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="nl"&gt;style:&lt;/span&gt; &lt;span class="n"&gt;Typogaphy&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;SemiBold&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;copyWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;color:&lt;/span&gt; &lt;span class="n"&gt;Colors&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;white&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
      &lt;span class="p"&gt;),&lt;/span&gt;
      &lt;span class="nl"&gt;body:&lt;/span&gt; &lt;span class="n"&gt;Center&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="nl"&gt;child:&lt;/span&gt; &lt;span class="n"&gt;Padding&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
          &lt;span class="nl"&gt;padding:&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;EdgeInsets&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;8.0&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
          &lt;span class="nl"&gt;child:&lt;/span&gt; &lt;span class="n"&gt;Column&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="nl"&gt;mainAxisAlignment:&lt;/span&gt; &lt;span class="n"&gt;MainAxisAlignment&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;center&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="nl"&gt;children:&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Widget&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;[&lt;/span&gt;
              &lt;span class="n"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'This is a Semi Bold Typo'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                  &lt;span class="nl"&gt;textAlign:&lt;/span&gt; &lt;span class="n"&gt;TextAlign&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;center&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                  &lt;span class="nl"&gt;style:&lt;/span&gt; &lt;span class="n"&gt;Typogaphy&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;SemiBold&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;copyWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                    &lt;span class="nl"&gt;color:&lt;/span&gt; &lt;span class="n"&gt;Colors&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;black&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="nl"&gt;fontSize:&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                  &lt;span class="p"&gt;))&lt;/span&gt;
            &lt;span class="p"&gt;],&lt;/span&gt;
          &lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="p"&gt;),&lt;/span&gt;
      &lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;For simplicity, I have created a basic typography class with font families alone. Based on your application requirement, you can extend it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Final Result
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvsthe8yb19nmidaj5zgk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvsthe8yb19nmidaj5zgk.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;In this post, you have learned how to add a custom font, what fonts are supported by the flutter and how to reuse font family throughout the application.&lt;/p&gt;




&lt;h2&gt;
  
  
  Thanks For Reading!
&lt;/h2&gt;

&lt;p&gt;Hope you have learned something new today 😊.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Originally Posted On - &lt;a href="https://www.nandhakumar.io/post/how-to-add-custom-font-in-flutter" rel="noopener noreferrer"&gt;https://www.nandhakumar.io/post/how-to-add-custom-font-in-flutter&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://dev.to/nandhakumar"&gt;Follow me&lt;/a&gt; to get notified of all upcoming posts.&lt;/p&gt;

&lt;p&gt;Follow and connect with me on &lt;a href="https://twitter.com/nandhakumar_io" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;, &lt;a href="https://www.instagram.com/nandhakumar.io/" rel="noopener noreferrer"&gt;Instagram&lt;/a&gt;, &lt;a href="//mailto:contact@nandhakumar.io"&gt;Email&lt;/a&gt; and &lt;a href="https://www.linkedin.com/in/nandhakumar-io/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; for more interesting stuff like this.&lt;/p&gt;

</description>
      <category>flutter</category>
      <category>dart</category>
      <category>mobileapp</category>
      <category>typography</category>
    </item>
    <item>
      <title>Regex #1: Understanding Regex, Regex-Based String Methods and Regex Flags</title>
      <dc:creator>Nandhakumar</dc:creator>
      <pubDate>Wed, 04 Jan 2023 12:45:38 +0000</pubDate>
      <link>https://dev.to/nandhakumar/regex-1-understanding-regex-regex-based-string-methods-and-regex-flags-533p</link>
      <guid>https://dev.to/nandhakumar/regex-1-understanding-regex-regex-based-string-methods-and-regex-flags-533p</guid>
      <description>&lt;p&gt;Hi There! 👋&lt;/p&gt;

&lt;p&gt;In this post, you will learn the fundamentals of regex.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Originally Posted On - &lt;a href="https://www.nandhakumar.io/post/regex-1-understanding-regex-methods-and-flags" rel="noopener noreferrer"&gt;https://www.nandhakumar.io/post/regex-1-understanding-regex-methods-and-flags&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Why Regex?
&lt;/h1&gt;

&lt;p&gt;The most common feature every application has is user registration and login. &lt;/p&gt;

&lt;p&gt;Nowadays, Libraries(Eg: Class Validator NPM Package) that you use for form validation has a direct method to validate input value like &lt;code&gt;isEmail()&lt;/code&gt;, etc...&lt;/p&gt;

&lt;p&gt;Which is very helpful, But each application has its own use case. So, these direct methods won't be helpful in that case.&lt;/p&gt;

&lt;p&gt;For Example, Not all application has the same rules for password.&lt;/p&gt;

&lt;p&gt;In such scenarios, you can use Regex Pattern to validate the user input values.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Regex is not limited to use only for Input Validation&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Most of the time I used to find the required Regex pattern from Google.&lt;/p&gt;

&lt;p&gt;At some point, I thought why can't we try to understand regex instead of searching on Google? &lt;/p&gt;

&lt;p&gt;Just gave try!&lt;/p&gt;

&lt;p&gt;After understanding some basics, Regex was interesting. And I regret that I didn't learn Regex so far.&lt;/p&gt;

&lt;p&gt;That's How I started learning Regex in depth.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Examples I share in this post are based on &lt;br&gt;
javascript, but the concepts can be applied to any &lt;br&gt;
programming language&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Defining a Regex Pattern?
&lt;/h1&gt;

&lt;p&gt;When you add a pattern between &lt;code&gt;/&lt;/code&gt;, it is considered a regex pattern&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;/[regex pattern]/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; There shouldn't be any &lt;code&gt;'&lt;/code&gt; or &lt;code&gt;"&lt;/code&gt; around the &lt;code&gt;/&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Two Ways to test Regex Pattern
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Using Regex Methods
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;.test()&lt;/code&gt; - It will return a boolean value if the pattern matches the string
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// regex Pattern&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;pattern&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/world/&lt;/span&gt; 

&lt;span class="c1"&gt;// string to be tested against the pattern&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello world&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; 

&lt;span class="c1"&gt;// returns `true` as the `world` is present in the message&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;[Test Method] - &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; 
&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;// output
[Test Method] - true 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;.exec()&lt;/code&gt; - It will return an array with the index and additional info on the matching pattern
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// regex Pattern&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;pattern&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/world/&lt;/span&gt; 

&lt;span class="c1"&gt;// string to be tested against the pattern&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello world&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; 

&lt;span class="c1"&gt;// returns Array with index and additional info&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;[Exec Method] - &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; 
&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;// output
[Exec Method] - [ 'world', index: 6, input: 'Hello world', groups: undefined ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Using String Methods
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;.match()&lt;/code&gt; - It will return an array with the index and additional info on the matching pattern (similar to &lt;code&gt;.exec()&lt;/code&gt; method) if only one match is found. Otherwise, it will return all the string that matches the pattern as an array.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// regex Pattern&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;pattern&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/world/&lt;/span&gt; 

&lt;span class="c1"&gt;// string to be tested against the pattern&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello world&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; 

&lt;span class="c1"&gt;// returns Array with index and additional info&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;[Exec Match] - &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; 
&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;// output
[Exec Match] - [ 'world', index: 6, input: 'Hello world', groups: undefined ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;.replace()&lt;/code&gt; -  It will replace the string that matches the pattern with the string that is to be replaced.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// regex Pattern&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;pattern&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/world/&lt;/span&gt; 

&lt;span class="c1"&gt;// string to be tested against the pattern&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello world&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; 

&lt;span class="c1"&gt;// returns Array with index and additional info&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;[Exec Replace] - &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Word&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&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;// output
[Exec Replace] - Hello Word
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;.split()&lt;/code&gt; -  It will split the text into an array based on the matching pattern
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// regex Pattern&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;pattern&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/world/&lt;/span&gt; 

&lt;span class="c1"&gt;// string to be tested against the pattern&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello world&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; 

&lt;span class="c1"&gt;// returns Array with index and additional info&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;[Exec Split] - &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&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;// output
[Exec Split] - [ 'Hello ', '' ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Regex Flags
&lt;/h1&gt;

&lt;p&gt;There are three 3 flags in Regex&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;g&lt;/code&gt; - Global&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Usually, when you try to match a pattern, it will match only the first occurrence in the string.&lt;/p&gt;

&lt;p&gt;By adding the &lt;code&gt;Global&lt;/code&gt; flag to the Regex pattern, it can match multiple occurrences of the pattern in the string&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// regex Pattern with global flag&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;pattern&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/the/g&lt;/span&gt;

&lt;span class="c1"&gt;// string to be tested against the pattern&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Get the Work Done and the work will pay off.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="c1"&gt;// Returns Array of strings matching the pattern&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;[Exec Global Flag] - &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&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;// output
[Exec Global Flag] - [ 'the', 'the' ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;i&lt;/code&gt; - Case Insensitive&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By default, matching string with regex is case-sensitive.&lt;/p&gt;

&lt;p&gt;By adding the &lt;code&gt;Case Insensitive&lt;/code&gt; flag to the Regex pattern, it can match the string with Case Insensitive&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// regex Pattern with global and Case Insensitive flag&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;pattern&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/work/gi&lt;/span&gt;

&lt;span class="c1"&gt;// string to be tested against the pattern&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Get the Work Done and the work will pay off.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="c1"&gt;// Returns Array of strings matching the pattern&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;[Exec Global &amp;amp; Case Insensitive Flag] - &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&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;// output
[Exec Global &amp;amp; Case Insensitive Flag] - [ 'Work', 'work' ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;m&lt;/code&gt; - Multi Line&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When you try to match a pattern with a multi-line string, By default the match is found only in the first line even though we have a match in the second line&lt;/p&gt;

&lt;p&gt;By adding the &lt;code&gt;Multi Line&lt;/code&gt; flag to the Regex pattern, it can find the matches in multiple lines.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// regex Pattern with global and Multi-Line flag&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;pattern&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/^The/gm&lt;/span&gt; &lt;span class="c1"&gt;// starts with "The"&lt;/span&gt;

&lt;span class="c1"&gt;// string to be tested against the pattern&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`The the Work Done.
The work will pay off.`&lt;/span&gt;

&lt;span class="c1"&gt;// Returns Array of strings matching the pattern&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;[Exec Global &amp;amp; Multi Line Flag] - &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&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;// output
[Exec Global &amp;amp; New Line Flag] - [ 'The', 'The' ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Thanks For Reading!
&lt;/h2&gt;

&lt;p&gt;Hope you have learned something new today 😊.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/nandhakumar"&gt;Follow me&lt;/a&gt; to get notified of all upcoming posts.&lt;/p&gt;

&lt;p&gt;Follow and connect with me on &lt;a href="https://twitter.com/nandhakumar_io" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;, &lt;a href="https://www.instagram.com/nandhakumar.io/" rel="noopener noreferrer"&gt;Instagram&lt;/a&gt;, &lt;a href="//mailto:contact@nandhakumar.io"&gt;Email&lt;/a&gt; and &lt;a href="https://www.linkedin.com/in/nandhakumar-io/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; for more interesting stuff like this.&lt;/p&gt;

</description>
      <category>regex</category>
      <category>javascript</category>
      <category>ts</category>
    </item>
    <item>
      <title>Nest JS Tutorial #3 - Query &amp; Route Params</title>
      <dc:creator>Nandhakumar</dc:creator>
      <pubDate>Thu, 15 Dec 2022 05:35:08 +0000</pubDate>
      <link>https://dev.to/nandhakumar/nest-js-tutorial-3-query-route-params-3gi4</link>
      <guid>https://dev.to/nandhakumar/nest-js-tutorial-3-query-route-params-3gi4</guid>
      <description>&lt;p&gt;Hi There! 👋&lt;/p&gt;

&lt;p&gt;In this post, you will learn what is route &amp;amp; query param, when to use it and how to use it in &lt;a href="https://nestjs.com/" rel="noopener noreferrer"&gt;Nest.JS&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This post is a part Nest JS tutorial series, Do check out my other post on nest js to know more about it.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Originally Posted On - &lt;a href="https://www.nandhakumar.io/post/nest-js-tutorial-3-route-and-query-param" rel="noopener noreferrer"&gt;https://www.nandhakumar.io/post/nest-js-tutorial-3-route-and-query-param&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's Get Started Now! 🚀&lt;/p&gt;

&lt;h1&gt;
  
  
  Prerequisite
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript" rel="noopener noreferrer"&gt;Javascript&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.typescriptlang.org/" rel="noopener noreferrer"&gt;Typescript&lt;/a&gt; (Optional)&lt;/li&gt;
&lt;li&gt;Basics Of OOPS&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.postman.com/" rel="noopener noreferrer"&gt;Postman&lt;/a&gt;  (For API Testing)&lt;/li&gt;
&lt;li&gt;&lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer"&gt;Code Editor&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://nodejs.org/en/" rel="noopener noreferrer"&gt;Node.JS&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Understanding Route and Query param
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Route Param
&lt;/h3&gt;

&lt;p&gt;Route Params are used to get specific data.&lt;/p&gt;

&lt;p&gt;Example Scenario,&lt;br&gt;
Let's say you want to get a user detail by &lt;code&gt;ID&lt;/code&gt;, so &lt;code&gt;ID&lt;/code&gt; can be will be passed as a route param like this 👇&lt;/p&gt;

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

&lt;span class="s2"&gt;"http://api.com/5689"&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;Here, &lt;code&gt;5689&lt;/code&gt; is the &lt;code&gt;ID&lt;/code&gt; of the user&lt;/p&gt;

&lt;h3&gt;
  
  
  Query Param
&lt;/h3&gt;

&lt;p&gt;Query params are used when you want to filter, sort and paginate the data.&lt;/p&gt;

&lt;p&gt;Example Scenario,&lt;br&gt;
Let's say you want to get a list of users that matches a name. so, here name can be passed as a query param like this 👇&lt;/p&gt;

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

&lt;span class="s2"&gt;"http://api.com?name=tamilan"&lt;/span&gt; 


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

&lt;/div&gt;

&lt;p&gt;Here, the name &lt;code&gt;tamilan&lt;/code&gt; is passed as a query param. To get results matching the name.&lt;/p&gt;

&lt;p&gt;You can also pass multiple query param.&lt;/p&gt;

&lt;p&gt;In addition to filter by name, we can also add a sort query with the existing name query like this 👇&lt;/p&gt;

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

http://api.com?name&lt;span class="o"&gt;=&lt;/span&gt;tamilan&amp;amp;sort&lt;span class="o"&gt;=&lt;/span&gt;name 


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

&lt;/div&gt;

&lt;p&gt;That's a short brief on route and query param!&lt;/p&gt;

&lt;p&gt;Now let's see how to capture these query and route param in nest js.&lt;/p&gt;

&lt;h1&gt;
  
  
  Using Route and Query Param in Nest JS
&lt;/h1&gt;

&lt;p&gt;To get started, Create a new nest project using nest cli. If you are not sure how to create a new project, check out this &lt;strong&gt;&lt;a href="https://dev.to/nandhakumar/nest-js-part-1-creating-your-first-api-5f2a"&gt;post&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I have created a new nest project, with its starter code.&lt;/p&gt;

&lt;p&gt;Here's the project structure I have now,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fexw7umiwr6o7xfawgw3o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fexw7umiwr6o7xfawgw3o.png" alt="Nest JS Starter Project Structure"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Requirement
&lt;/h3&gt;

&lt;p&gt;We need two routes,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;/user&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It should be a GET request&lt;/li&gt;
&lt;li&gt;It should return all the users when there is no query 
param&lt;/li&gt;
&lt;li&gt;It should return all the users that match the name, 
when the name query param is passed&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;code&gt;/user/:id&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It should be a GET request&lt;/li&gt;
&lt;li&gt;It should return the user that matches the id passed as a 
route param&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Let's do the implementation step by step,&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1:
&lt;/h3&gt;

&lt;p&gt;Add some mock user data to the service file.&lt;/p&gt;

&lt;p&gt;As we are using typescript it would be nice to add interface for the user.&lt;/p&gt;

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

&lt;span class="c1"&gt;// app.service.ts&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Injectable&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// user interface&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;IUser&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Injectable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AppService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// list of users&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;IUser&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Tamilan&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Nandha&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;24&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Nandha Kumar&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;24&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;



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

&lt;/div&gt;
&lt;h3&gt;
  
  
  Step 2:
&lt;/h3&gt;

&lt;p&gt;Create a method &lt;code&gt;findUsersByName&lt;/code&gt; that returns users matching the 'name' query or all the users when there is no name query.&lt;/p&gt;

&lt;p&gt;Add this method to the same service file&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

  &lt;span class="nf"&gt;findUsersByName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nameToBeMatched&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;IUser&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;nameToBeMatched&lt;/span&gt;
      &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
          &lt;span class="c1"&gt;// return users with name matching&lt;/span&gt;
          &lt;span class="c1"&gt;// 'name' query param&lt;/span&gt;
          &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nameToBeMatched&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="c1"&gt;// returns all the users if the&lt;/span&gt;
        &lt;span class="c1"&gt;// 'name' query param is 'null' or ''&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;


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

&lt;/div&gt;
&lt;h3&gt;
  
  
  Step 3:
&lt;/h3&gt;

&lt;p&gt;Create another method &lt;code&gt;findUserById&lt;/code&gt; that returns a user matching the 'id' route param&lt;/p&gt;

&lt;p&gt;Add this method to the same service file&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

  &lt;span class="nf"&gt;findUserById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;IUser&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// return user with id matching 'id' route param&lt;/span&gt;
    &lt;span class="c1"&gt;// By Default when you get value from query or route&lt;/span&gt;
    &lt;span class="c1"&gt;// param it will be string, But in our case user id is of&lt;/span&gt;
    &lt;span class="c1"&gt;// type number. So we are making a type conversion for id&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;


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

&lt;/div&gt;
&lt;h3&gt;
  
  
  Step 3:
&lt;/h3&gt;

&lt;p&gt;Update the base route of the controller to &lt;code&gt;/user&lt;/code&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// app.controller.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;AppService&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;IUser&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./app.service&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/user&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// update the base route to '/user'&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AppController&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;appService&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;AppService&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

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



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

&lt;/div&gt;
&lt;h3&gt;
  
  
  Step 4:
&lt;/h3&gt;

&lt;p&gt;Implementing the first requirement.&lt;/p&gt;

&lt;p&gt;Create a method &lt;code&gt;getUsers&lt;/code&gt; with &lt;code&gt;@Get()&lt;/code&gt; decorator.&lt;/p&gt;

&lt;p&gt;And it should take &lt;code&gt;@Query()&lt;/code&gt; decorator as its argument. So that whenever we add the query to the URL, those params will be captured by this argument.&lt;/p&gt;

&lt;p&gt;In the implementation pass the query params to the service method(&lt;code&gt;findUsersByName&lt;/code&gt;) that we have already implemented in the service&lt;/p&gt;

&lt;p&gt;Finally, return the data&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;AppService&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;IUser&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./app.service&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/user&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AppController&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;appService&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;AppService&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nf"&gt;getUsers&lt;/span&gt;&lt;span class="p"&gt;(@&lt;/span&gt;&lt;span class="nd"&gt;Query&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;IUser&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;appService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findUsersByName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;



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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Pro Tip&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you want a specific property from the query, you can specify the property &lt;code&gt;name&lt;/code&gt; in the &lt;code&gt;@Query()&lt;/code&gt; decorator &lt;br&gt;
like this 👇&lt;/p&gt;

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

 &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nf"&gt;getUsers&lt;/span&gt;&lt;span class="p"&gt;(@&lt;/span&gt;&lt;span class="nd"&gt;Query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;IUser&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;appService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findUsersByName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;


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

&lt;/div&gt;
&lt;h3&gt;
  
  
  Step 4:
&lt;/h3&gt;

&lt;p&gt;Implementing the second requirement.&lt;/p&gt;

&lt;p&gt;Create a method &lt;code&gt;getUserById&lt;/code&gt; with &lt;code&gt;@Get(':id')&lt;/code&gt; decorator.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: &lt;br&gt;
Here we have defined ':id' route param in the &lt;code&gt;@Get()&lt;/code&gt; &lt;br&gt;
decorator.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And it should take &lt;code&gt;@Param()&lt;/code&gt; decorator as its argument. So that the route param will be captured by this argument.&lt;/p&gt;

&lt;p&gt;In the implementation pass the query params to the service method(&lt;code&gt;findUserById&lt;/code&gt;) that we have already implemented in the service&lt;/p&gt;

&lt;p&gt;Finally, return the data&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// app.controller.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Param&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Query&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;AppService&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;IUser&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./app.service&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/user&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AppController&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;appService&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;AppService&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nf"&gt;getUsers&lt;/span&gt;&lt;span class="p"&gt;(@&lt;/span&gt;&lt;span class="nd"&gt;Query&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;IUser&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;appService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findUsersByName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;:id&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nf"&gt;getUserById&lt;/span&gt;&lt;span class="p"&gt;(@&lt;/span&gt;&lt;span class="nd"&gt;Param&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;IUser&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;appService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findUserById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Pro Tip&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Similar to the &lt;code&gt;@Query()&lt;/code&gt; decorator you can specify the property &lt;code&gt;id&lt;/code&gt; in the &lt;code&gt;@Param()&lt;/code&gt; decorator &lt;br&gt;
like this 👇&lt;/p&gt;

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

 &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nf"&gt;getUsers&lt;/span&gt;&lt;span class="p"&gt;(@&lt;/span&gt;&lt;span class="nd"&gt;Param&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;id&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;IUser&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;appService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findUsersByName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;That's it!&lt;/p&gt;

&lt;p&gt;As per the requirement, you have implemented the API.&lt;/p&gt;




&lt;h1&gt;
  
  
  Testing
&lt;/h1&gt;

&lt;p&gt;Let's test the implementation now!&lt;/p&gt;

&lt;p&gt;Start the server by executing&lt;/p&gt;

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

npm start:dev


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

&lt;/div&gt;

&lt;p&gt;Open the server URL &lt;code&gt;http://localhost:3000&lt;/code&gt; in &lt;a href="https://www.postman.com/" rel="noopener noreferrer"&gt;Postman&lt;/a&gt; or any API Testing Tool&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Test Case 1:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Getting all Users, without query params&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffhqdj2ojjdvaoba9xhr4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffhqdj2ojjdvaoba9xhr4.png" alt="Nest JS Query and Route Param, Test Case - 1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And we got all the users. &lt;/p&gt;

&lt;p&gt;✅ Test Case Passed!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Test Case 2:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Getting all Users, with &lt;code&gt;name&lt;/code&gt; query param&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcqxetgp9tcqvst7gr15w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcqxetgp9tcqvst7gr15w.png" alt="Nest JS Query and Route Param, Test Case - 2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And we got all the users matching the name passed in the query param. &lt;/p&gt;

&lt;p&gt;✅ Test Case Passed!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Test Case 3:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Getting User by id, with &lt;code&gt;id&lt;/code&gt; route param&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fghhc4ajem3yedxdjj8vw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fghhc4ajem3yedxdjj8vw.png" alt="Nest JS Query and Route Param, Test Case - 3"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And we got the user with the id we passed in the route&lt;/p&gt;

&lt;p&gt;✅ Test Case Passed!&lt;/p&gt;




&lt;p&gt;Try implementing different scenarios with multiple &lt;code&gt;Query&lt;/code&gt; and &lt;code&gt;Route&lt;/code&gt; params.&lt;/p&gt;

&lt;p&gt;Congratulation! 👏&lt;/p&gt;

&lt;p&gt;You have successfully implemented APIs with &lt;code&gt;Query&lt;/code&gt; and &lt;code&gt;Route&lt;/code&gt; param in Nest.JS.&lt;/p&gt;

&lt;p&gt;Get the completed source code from &lt;a href="https://github.com/nandhakumar-rs/nest-js-tutorial-series" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Thanks For Reading!
&lt;/h2&gt;

&lt;p&gt;Hope you have learned something new today 😊.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/nandhakumar"&gt;Follow me&lt;/a&gt; to get notified of all upcoming posts on this series.&lt;/p&gt;

&lt;p&gt;Follow and connect with me on &lt;a href="https://twitter.com/nandhakumar_io" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;, &lt;a href="https://www.instagram.com/nandhakumar.io/" rel="noopener noreferrer"&gt;Instagram&lt;/a&gt;, &lt;a href="//mailto:contact@nandhakumar.io"&gt;Email&lt;/a&gt; and &lt;a href="https://www.linkedin.com/in/nandhakumar-io/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; for more interesting stuff like this.&lt;/p&gt;

</description>
      <category>api</category>
      <category>javascript</category>
      <category>typescript</category>
      <category>nestjs</category>
    </item>
    <item>
      <title>Nest JS Tutorial #2: HTTP Request &amp; Data Validation</title>
      <dc:creator>Nandhakumar</dc:creator>
      <pubDate>Fri, 02 Dec 2022 04:49:03 +0000</pubDate>
      <link>https://dev.to/nandhakumar/nest-js-tutorial-2-http-request-data-validation-2bjn</link>
      <guid>https://dev.to/nandhakumar/nest-js-tutorial-2-http-request-data-validation-2bjn</guid>
      <description>&lt;p&gt;Hi There! 👋&lt;/p&gt;

&lt;p&gt;In this post, you will learn how to validate request data in &lt;a href="https://nestjs.com/" rel="noopener noreferrer"&gt;Nest JS&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you don't know what is Nest JS, If you don't know what is Nest JS, Check out &lt;a href="https://dev.to/nandhakumar/nest-js-part-1-creating-your-first-api-5f2a"&gt;my previous post&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Originally Posted On - &lt;a href="https://www.nandhakumar.io/post/nest-js-tutorial-2-http-request-data-validation" rel="noopener noreferrer"&gt;https://www.nandhakumar.io/post/nest-js-tutorial-2-http-request-data-validation&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Prerequisite
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript" rel="noopener noreferrer"&gt;Javascript&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.typescriptlang.org/" rel="noopener noreferrer"&gt;Typescript&lt;/a&gt; (Optional)&lt;/li&gt;
&lt;li&gt;Basics Of OOPS&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.postman.com/" rel="noopener noreferrer"&gt;Postman&lt;/a&gt;  (For API Testing)&lt;/li&gt;
&lt;li&gt;&lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer"&gt;Code Editor&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://nodejs.org/en/" rel="noopener noreferrer"&gt;Node.JS&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  What is an HTTP Request?
&lt;/h1&gt;

&lt;p&gt;HTTP Request is sent from the client machine(web browser or any other client application) to access or get information from the server.&lt;/p&gt;

&lt;p&gt;These Requests are sent with the help of URLs like this 👇&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://yourapp.com/api/v1/users
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Three parts of HTTP Request
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Methods&lt;/strong&gt; - Defines the type of the request&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Headers&lt;/strong&gt; - Will have the information about the request and sender(client), you can add custom parameters as well. Usually, Authorization Cookies will be attached to Headers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Body&lt;/strong&gt; - Will have the requested data. For example, which registering as a new user, you have to pass user information as request data to store it in the server &lt;/p&gt;

&lt;h3&gt;
  
  
  HTTP Request Methods
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Methods&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;GET&lt;/td&gt;
&lt;td&gt;To get Data&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;POST&lt;/td&gt;
&lt;td&gt;To create or add data&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PATCH&lt;/td&gt;
&lt;td&gt;To update data partially&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PUT&lt;/td&gt;
&lt;td&gt;To update the entire data&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;DELETE&lt;/td&gt;
&lt;td&gt;To delete data&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;That's a short brief on HTTP Requests.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;To know more about HTTP Request, &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods" rel="noopener noreferrer"&gt;Click Here!&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Request Data Validation?
&lt;/h2&gt;

&lt;p&gt;When the HTTP Request reaches the server, Getting the Body data directly without validating is not good. Some of you may get malicious data. So, you have to validate your Body data always. &lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding Nest.JS Validation
&lt;/h2&gt;

&lt;p&gt;Let's first understand how validation works behind the scene in Nest.JS&lt;/p&gt;

&lt;p&gt;In Nest.JS, We have something called a validation pipe. Every time when a request body is passed through this pipe and validated. If there are any invalid data, it will throw an error.&lt;/p&gt;

&lt;p&gt;Before moving further you need to understand what is DTO(Data Transfer Object)&lt;/p&gt;

&lt;p&gt;DTO is a class where you will define the rules for validation. If you are not sure about this no worries, you'll understand this when implementing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two Parts Of Validation Pipe
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Class Transformer&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Class Transformer helps to transform your request body into a class instance of DTO and then the instance will be passed to class validation&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Class Validator&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Class Validator will validate the request body based on the rules defined in the DTO and throws an error if there are any invalid data&lt;/p&gt;




&lt;p&gt;Now let's start the implementation for the example scenario given below 👇&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scenario&lt;/strong&gt;: Need to add a new user and the user should have email, age, name, and country( Country is optional).&lt;/p&gt;

&lt;p&gt;As per the scenario, it's clear that we need to add data to the server&lt;/p&gt;

&lt;p&gt;So, it is an HTTP POST Method and we need to pass user data in the request body.&lt;/p&gt;

&lt;p&gt;Hope you would have already created a Nest.JS Project, If not follow my previous &lt;a href="https://dev.to/nandhakumar/nest-js-part-1-creating-your-first-api-5f2a"&gt;tutorial&lt;/a&gt; to create one.&lt;/p&gt;

&lt;p&gt;Let's do the implementation step by step,&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1:
&lt;/h3&gt;

&lt;p&gt;Install class validator and class transformer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm i class-validator class-transformer &lt;span class="nt"&gt;-s&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 2:
&lt;/h3&gt;

&lt;p&gt;Generate User Controller.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;nest g controller user
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;note: to execute nest commands you should have installed&lt;br&gt;
nest cli globally&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Step 3:
&lt;/h3&gt;

&lt;p&gt;Generate User Module&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;nest g module user/user &lt;span class="nt"&gt;--flat&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note: As we have already created a user folder in Step 2,&lt;br&gt;
To avoid creating additional folders --flag is used&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Step 4:
&lt;/h3&gt;

&lt;p&gt;Add User Controller to User Module&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// user.module.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Module&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;UserController&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./user.controller&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;imports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt;
  &lt;span class="na"&gt;providers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt;
  &lt;span class="na"&gt;controllers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;UserController&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="c1"&gt;// add user controller&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserModule&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 5:
&lt;/h3&gt;

&lt;p&gt;Add User Module to App Module&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app.module.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Module&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;UserModule&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./user/user.module&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;imports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;UserModule&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="c1"&gt;// add user module&lt;/span&gt;
  &lt;span class="na"&gt;controllers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt;
  &lt;span class="na"&gt;providers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AppModule&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 6:
&lt;/h3&gt;

&lt;p&gt;Create a dto folder under the user folder and create a file called user.dto.ts&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%2F5bz9as4lek43gk0m2zoa.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%2F5bz9as4lek43gk0m2zoa.png" alt="Project Structure - USER DTO" width="365" height="343"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 7:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Create UserDTO Class &lt;/li&gt;
&lt;li&gt;Add the user properties to UserDTO as per the scenario
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// user.dto.ts&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserDTO&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="nl"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nl"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nl"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nl"&gt;country&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 8:
&lt;/h3&gt;

&lt;p&gt;Nest.JS uses &lt;a href="https://www.typescriptlang.org/" rel="noopener noreferrer"&gt;Typescript&lt;/a&gt; decorators extensively.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: &lt;a href="https://www.typescriptlang.org/docs/handbook/decorators.html" rel="noopener noreferrer"&gt;Decorators&lt;/a&gt; are a simple function that helps to modify &lt;br&gt;
the targeted value&lt;br&gt;
Decorators are defined like &lt;code&gt;@[decoratorname]()&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Coming back to the UserDTO,&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Class-Validator&lt;/strong&gt; has a list of validation functions which can be used in the form of a decorator&lt;/p&gt;

&lt;p&gt;Now, Let's try to add those validation decorators to the class properties&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;IsEmail&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;IsNumber&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;IsOptional&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;IsString&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;class-validator&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserDTO&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;IsEmail&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;IsNumber&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;IsString&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// add @IsOptional to make the country property optional&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;IsOptional&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; 
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;IsString&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nx"&gt;country&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 9:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;create a controller to add a user&lt;/li&gt;
&lt;li&gt;Add UserDTO as the type of the request body
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Post&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;UserDTO&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./dto/user.dto&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;user&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserController&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;addUser&lt;/span&gt;&lt;span class="p"&gt;(@&lt;/span&gt;&lt;span class="nd"&gt;Body&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UserDTO&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note: &lt;br&gt;
I have added &lt;a class="mentioned-user" href="https://dev.to/post"&gt;@post&lt;/a&gt; Decorator to the function addUser as we &amp;gt; are dealing with Post HTTP Request&lt;br&gt;
Also, the &lt;a class="mentioned-user" href="https://dev.to/body"&gt;@body&lt;/a&gt; Decorator to get the request body&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's it we have added validation to the &lt;code&gt;addUser&lt;/code&gt; request body&lt;/p&gt;




&lt;h1&gt;
  
  
  Testing
&lt;/h1&gt;

&lt;p&gt;Let's test the implementation now!&lt;/p&gt;

&lt;p&gt;Start the server by executing&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Open the server URL &lt;code&gt;http://localhost:3000&lt;/code&gt; in &lt;a href="https://www.postman.com/" rel="noopener noreferrer"&gt;Postman&lt;/a&gt; or any API Testing Tool&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Test Case 1:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Passing all the user properties with an invalid email&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%2F4ark6dxnw3c3x4ajx5g3.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%2F4ark6dxnw3c3x4ajx5g3.png" alt="Nest JS Request Body Validation - Test Case 1" width="671" height="598"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And the validation works!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Test Case 2:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Since the country property has been set to optional, Now let's try to send the request without the country property&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%2Fwptt1ahr6qkkpnxl9pfe.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%2Fwptt1ahr6qkkpnxl9pfe.png" alt="Nest JS Request Body Validation - Test Case 2" width="671" height="598"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we got success response, even without the country.&lt;/p&gt;

&lt;p&gt;Try testing with different scenarios by passing invalid data.&lt;/p&gt;

&lt;p&gt;Congratulation! 👏&lt;/p&gt;

&lt;p&gt;You have successfully validated the request body in Nest.JS&lt;/p&gt;




&lt;h2&gt;
  
  
  Thanks For Reading!
&lt;/h2&gt;

&lt;p&gt;Hope you have learned something new today 😊.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/nandhakumar"&gt;Follow me&lt;/a&gt; to get notified of all upcoming posts on this series.&lt;/p&gt;

&lt;p&gt;Follow and connect with me on &lt;a href="https://twitter.com/nandhakumar_io" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;, &lt;a href="https://www.instagram.com/nandhakumar.io/" rel="noopener noreferrer"&gt;Instagram&lt;/a&gt;, &lt;a href="//mailto:contact@nandhakumar.io"&gt;Email&lt;/a&gt; and &lt;a href="https://www.linkedin.com/in/nandhakumar-io/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; for more interesting stuff like this.&lt;/p&gt;

</description>
      <category>api</category>
      <category>javascript</category>
      <category>typescript</category>
      <category>nestjs</category>
    </item>
    <item>
      <title>Nest JS Tutorial #1: Create Your First API</title>
      <dc:creator>Nandhakumar</dc:creator>
      <pubDate>Thu, 24 Nov 2022 11:25:06 +0000</pubDate>
      <link>https://dev.to/nandhakumar/nest-js-part-1-creating-your-first-api-5f2a</link>
      <guid>https://dev.to/nandhakumar/nest-js-part-1-creating-your-first-api-5f2a</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Originally Posted On - &lt;a href="https://www.nandhakumar.io/post/nest-js-series-create-your-first-api" rel="noopener noreferrer"&gt;https://www.nandhakumar.io/post/nest-js-series-create-your-first-api&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hi There! 👋&lt;/p&gt;

&lt;p&gt;It's been a couple of months working with &lt;a href="https://nestjs.com/" rel="noopener noreferrer"&gt;Nest.JS&lt;/a&gt; and I love the way the framework build to develop scalable server-side applications&lt;/p&gt;

&lt;p&gt;I am still trying to learn more about Nest.JS, So thought of sharing my learning in the form of tutorial series from Beginner to Advanced.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/nandhakumar"&gt;Follow me&lt;/a&gt; to get notified of all upcoming posts on this series.&lt;/p&gt;

&lt;p&gt;Let's Get Started Now! 🚀&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisite
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript" rel="noopener noreferrer"&gt;Javascript&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.typescriptlang.org/" rel="noopener noreferrer"&gt;Typescript&lt;/a&gt; (Optional)&lt;/li&gt;
&lt;li&gt;Basics Of OOPS&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.postman.com/" rel="noopener noreferrer"&gt;Postman&lt;/a&gt;  (For API Testing)&lt;/li&gt;
&lt;li&gt;&lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer"&gt;Code Editor&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://nodejs.org/en/" rel="noopener noreferrer"&gt;Node.JS&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What is Nest.JS?
&lt;/h2&gt;

&lt;p&gt;Nest.JS is a framework to build highly scalable Node.JS server-side applications. It comes with the full support of typescript(It also allows us to build the app with pure JS as well). &lt;/p&gt;

&lt;p&gt;It combines OOP(Object Oriented Programming) and Functional Programming to write clean and reusable code.&lt;/p&gt;

&lt;p&gt;If you have ever built a server-side application, you would have heard about &lt;a href="https://expressjs.com/" rel="noopener noreferrer"&gt;Express.JS&lt;/a&gt; and &lt;a href="https://www.fastify.io/" rel="noopener noreferrer"&gt;Fastify&lt;/a&gt; libraries.&lt;/p&gt;

&lt;p&gt;Under the hood, Nest.JS uses Express.JS as its default HTTP Server. If you prefer to use Fastify, you can switch easily &lt;a href="https://docs.nestjs.com/first-steps" rel="noopener noreferrer"&gt;(More about this on official docs)&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's a short brief on Nest.JS&lt;/p&gt;




&lt;h2&gt;
  
  
  Generating Nest.JS Project
&lt;/h2&gt;

&lt;p&gt;Before generating the project, make sure you have installed the latest version of Node.JS on your system&lt;/p&gt;

&lt;p&gt;I am using Node.JS Version &lt;code&gt;v18.12.1&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To generate a Nest.JS Project first, you need to install &lt;a href="https://docs.nestjs.com/cli/overview" rel="noopener noreferrer"&gt;Nest Cli&lt;/a&gt;&lt;/p&gt;

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

npm i &lt;span class="nt"&gt;-g&lt;/span&gt; @nestjs/cli


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

&lt;/div&gt;

&lt;p&gt;Now generate a new Nest.JS project&lt;/p&gt;

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

npm i &lt;span class="nt"&gt;-g&lt;/span&gt; @nestjs/cli


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

&lt;/div&gt;

&lt;p&gt;It will ask for,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Project name (Give as you wish)&lt;/li&gt;
&lt;li&gt;Which package manager (&lt;a href="https://www.npmjs.com/" rel="noopener noreferrer"&gt;npm&lt;/a&gt;, &lt;a href="https://yarnpkg.com/" rel="noopener noreferrer"&gt;yarn&lt;/a&gt;, &lt;a href="https://pnpm.io/" rel="noopener noreferrer"&gt;pnpm&lt;/a&gt;) to use? (I choose yarn, you can choose as you wish)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now open the project on your code editor&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding Nest.JS Project Structure
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7d11o7dru54dgkw3z4f1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7d11o7dru54dgkw3z4f1.png" alt="Nest JS Project Structure"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;node_modules&lt;/strong&gt; - Contains all the libraries that are required for your project.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;src&lt;/strong&gt; - Contains your application source code, Under this directory Nest.JS has generated a starter code. Let's explore what they are,

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;app.controller.spec.ts&lt;/strong&gt; - Contains unit test cases for each controller implementation&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;app.controller.ts&lt;/strong&gt; - Contains the implementation of the &lt;a href="https://en.wikipedia.org/wiki/API" rel="noopener noreferrer"&gt;API&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;app.service.ts&lt;/strong&gt; - Contains business logic which will  be used in the controller&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;app.module.ts&lt;/strong&gt; - Contains Imports, Exports and DI(Dependency Injection). More on this later...&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;main.ts&lt;/strong&gt; - Contains implementation to bootstrap the application. This is the starting point of your application &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;test&lt;/strong&gt; - Contains E2E test case implementations&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;.eslintrc.js&lt;/strong&gt; - Contains lint configuration that helps to identify incorrect syntax, typescript standards and more ...&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;.gitignore&lt;/strong&gt; - Contains files &amp;amp; directories to ignore when pushing the code to GitHub&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;.prettierrc&lt;/strong&gt; - Contains prettier config to format your code&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;nest-cli.json&lt;/strong&gt; - Basic Nest.JS Configuration&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;package.json&lt;/strong&gt; - Contains dependency, scripts, project version and more required for your project&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;README.md&lt;/strong&gt; - Contains documentation of your project&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;tsconfig.json&lt;/strong&gt; - Contains typescript configurations&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Basic Understanding Of API(You can skip this section if you know what is an API)
&lt;/h2&gt;

&lt;p&gt;If you are a complete beginner in API development.&lt;/p&gt;

&lt;p&gt;In simple terms,&lt;br&gt;
Let's say you have a raw cloth material and you want it to be stitched. &lt;/p&gt;

&lt;p&gt;So you will go to a tailor and give your cloth to stitch. You will also give some instructions on how the design should be.&lt;/p&gt;

&lt;p&gt;Considering the above example, In the computer world frontend application(Apps with user interfaces) will request certain data with some parameters(raw cloth material and instruction to design the cloth) to the server(tailor) in return server will send you the requested data as a response(stitched cloth).&lt;/p&gt;

&lt;p&gt;These requests are sent using an API URL.&lt;/p&gt;

&lt;p&gt;An API(Application programming interface) will look like this 👇&lt;/p&gt;

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

&lt;span class="c1"&gt;// Example API&lt;/span&gt;
&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://google.com/api/users&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="c1"&gt;// returns a list of users&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;If you are still confused. Don't worry, going forward you will figure it out.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating Your First API
&lt;/h2&gt;

&lt;p&gt;Let's start the server first&lt;/p&gt;

&lt;p&gt;Open your terminal &lt;/p&gt;

&lt;p&gt;Navigate to your project directory&lt;/p&gt;

&lt;p&gt;And Run&lt;/p&gt;

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

yarn start:dev


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

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;Replace &lt;code&gt;yarn&lt;/code&gt; with &lt;code&gt;npm&lt;/code&gt; or &lt;code&gt;pnpm&lt;/code&gt; if you have configured those package managers initially&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now your server should have been started on &lt;/p&gt;

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

http://localhost:3000


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

&lt;/div&gt;

&lt;p&gt;To test the starter code generated by Nest.JS&lt;/p&gt;

&lt;p&gt;Open Postman(tool to test the API) &lt;/p&gt;

&lt;p&gt;Choose &lt;strong&gt;GET&lt;/strong&gt; Method and Add the &lt;strong&gt;API URL(&lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt;)&lt;/strong&gt; then hit &lt;strong&gt;Send&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmh6bqxytzlncjyju8u79.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmh6bqxytzlncjyju8u79.png" alt="API Testing With Postman"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You will see the response as "Hello World!"&lt;/p&gt;

&lt;p&gt;Great!&lt;/p&gt;

&lt;p&gt;You have executed your first API.&lt;/p&gt;

&lt;p&gt;Now let's try to customise this API in a way to get a list of user names&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1
&lt;/h3&gt;

&lt;p&gt;Open &lt;strong&gt;app.controller.ts&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add &lt;strong&gt;/user&lt;/strong&gt; route to &lt;strong&gt;@Controller()&lt;/strong&gt; decorator &lt;/li&gt;
&lt;li&gt;Change the function name under &lt;strong&gt;@Get()&lt;/strong&gt; decorator to  &lt;strong&gt;getUsers&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Change return type from &lt;strong&gt;string&lt;/strong&gt; to &lt;strong&gt;string[]&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Change the App Service method name to &lt;strong&gt;getUsers&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Code Snippet 👇&lt;/p&gt;

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

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Get&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;AppService&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./app.service&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/users&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Add '/user' route&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AppController&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;appService&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;AppService&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

  &lt;span class="c1"&gt;// Change the function name to getUsers&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nf"&gt;getUsers&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// Change return type to string[](stirng Array) &lt;/span&gt;
     &lt;span class="c1"&gt;// Change the service method name to getUsers&lt;/span&gt;
     &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;appService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getUsers&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; 
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;



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

&lt;/div&gt;
&lt;h3&gt;
  
  
  Step 2
&lt;/h3&gt;

&lt;p&gt;Open &lt;strong&gt;app.service.ts&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Change the function name &lt;strong&gt;getHello()&lt;/strong&gt; inside AppService Class to &lt;strong&gt;getUsers()&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Change return type from &lt;strong&gt;string&lt;/strong&gt; to &lt;strong&gt;string[]&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Change the return value from &lt;strong&gt;'Hello World!'&lt;/strong&gt; to &lt;code&gt;['userOne','userTwo']&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Code Snippet 👇&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Injectable&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Injectable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AppService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Change function name to getUsers&lt;/span&gt;
  &lt;span class="nf"&gt;getUsers&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// Change return type to string[](stirng Array) &lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;userOne&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;userTwo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;



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

&lt;/div&gt;

&lt;p&gt;That's it your user API is ready now.&lt;/p&gt;

&lt;p&gt;Let's test it in Postman &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Open Postman&lt;/li&gt;
&lt;li&gt;Choose &lt;strong&gt;GET&lt;/strong&gt; Method and Add the &lt;strong&gt;API URL(&lt;a href="http://localhost:3000/users" rel="noopener noreferrer"&gt;http://localhost:3000/users&lt;/a&gt;)&lt;/strong&gt; then hit &lt;strong&gt;Send&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4zi9b0oytvztskqjgtnf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4zi9b0oytvztskqjgtnf.png" alt="Testing Get Users API Developed in Nest JS"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you got a response as a list of user names.&lt;/p&gt;

&lt;p&gt;Congratulation! 👏&lt;/p&gt;

&lt;p&gt;You have created your first API in Nest.JS&lt;/p&gt;




&lt;h2&gt;
  
  
  Thanks For Reading!
&lt;/h2&gt;

&lt;p&gt;Hope you have learned something new today 😊.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/nandhakumar"&gt;Follow me&lt;/a&gt; to get notified of all upcoming posts on this series.&lt;/p&gt;

&lt;p&gt;Follow and connect with me on &lt;a href="https://twitter.com/nandhakumar_io" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;, &lt;a href="https://www.instagram.com/nandhakumar.io/" rel="noopener noreferrer"&gt;Instagram&lt;/a&gt;, &lt;a href="//mailto:rsnk2013@gmail.com"&gt;Email&lt;/a&gt; and &lt;a href="https://www.linkedin.com/in/nandhakumar-io/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; for more interesting stuff like this.&lt;/p&gt;

</description>
      <category>nestjs</category>
      <category>javascript</category>
      <category>typescript</category>
      <category>backend</category>
    </item>
    <item>
      <title>Step By Step Guide To Dockerize React App Created Using Vite</title>
      <dc:creator>Nandhakumar</dc:creator>
      <pubDate>Fri, 18 Nov 2022 11:04:21 +0000</pubDate>
      <link>https://dev.to/nandhakumar/step-by-step-guide-to-dockerize-react-app-created-using-vite-2jg3</link>
      <guid>https://dev.to/nandhakumar/step-by-step-guide-to-dockerize-react-app-created-using-vite-2jg3</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Originally Posted On - &lt;a href="https://www.nandhakumar.io/post/step-by-step-guide-to-dockerize-vite-react-app-in-dev-environment" rel="noopener noreferrer"&gt;https://www.nandhakumar.io/post/step-by-step-guide-to-dockerize-vite-react-app-in-dev-environment&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hey Folks!&lt;/p&gt;

&lt;p&gt;In this post, you'll learn how to dockerize your react app created using the Vite tool for the development environment&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Basic knowledge of React&lt;/li&gt;
&lt;li&gt;Basic understanding of what is docker (I'll give a brief on it as well)&lt;/li&gt;
&lt;li&gt;Docker on your local system&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What is Docker?
&lt;/h2&gt;

&lt;p&gt;Docker is a tool that helps to create a common environment(called as &lt;strong&gt;Container&lt;/strong&gt; in the docker world) for development, testing and production.&lt;/p&gt;

&lt;p&gt;Let's say you are developing a react app on your local environment(development)&lt;/p&gt;

&lt;p&gt;Now, once you are done with the development you need to hand over the source code to the testing team and if the testing team doesn't have the same environment as your local environment there might be some issues which would be a blocker for testing.&lt;/p&gt;

&lt;p&gt;Similar scenario for production as well.&lt;/p&gt;

&lt;p&gt;So How can we ship the environment to all the places where ever you run the application? &lt;/p&gt;

&lt;p&gt;And here comes Docker, It will create a virtual environment for your app to run and you can create and use the environment where ever you need to run your app.&lt;/p&gt;

&lt;p&gt;Awesome right? &lt;/p&gt;

&lt;p&gt;That's a short brief on Docker.&lt;/p&gt;

&lt;p&gt;If you want to know more about it check out the &lt;a href="https://www.docker.com/" rel="noopener noreferrer"&gt;Official Docker Site&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now Let's see how to Dockerize your React app.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 1: Create React App Using Vite (Skip this step if you already have a react app)
&lt;/h2&gt;

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

npm create vite@latest


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

&lt;/div&gt;

&lt;p&gt;You'll be asked for&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;App Name&lt;/li&gt;
&lt;li&gt;Which Framework to use like React, Angular, or Vue? Choose React&lt;/li&gt;
&lt;li&gt;Then, Typescript or Javascript. Choose as you wish&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1668678866236%2FJzJ25nfLJ.png" class="article-body-image-wrapper"&gt;&lt;img alt="desktop" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1668678866236%2FJzJ25nfLJ.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now switch to the project directory&lt;/p&gt;

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

&lt;span class="nb"&gt;cd&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;your project name]


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

&lt;/div&gt;
&lt;h2&gt;
  
  
  Step 2: Update &lt;code&gt;vite.config&lt;/code&gt; File
&lt;/h2&gt;

&lt;p&gt;This step is required to map the port between Docker container and your React app&lt;/p&gt;

&lt;p&gt;Now replace this code snippet in &lt;code&gt;vite.config&lt;/code&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nf"&gt;defineConfig&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;react&lt;/span&gt;&lt;span class="p"&gt;()],&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;


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

&lt;/div&gt;

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

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

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nf"&gt;defineConfig&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;react&lt;/span&gt;&lt;span class="p"&gt;()],&lt;/span&gt;
  &lt;span class="na"&gt;server&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;watch&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;usePolling&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;host&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// needed for the Docker Container port mapping to work&lt;/span&gt;
    &lt;span class="na"&gt;strictPort&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;port&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5173&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// you can replace this port with any port&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;


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

&lt;/div&gt;
&lt;h2&gt;
  
  
  Step 3: Create a Dockerfile
&lt;/h2&gt;

&lt;p&gt;Create a file called &lt;code&gt;Dockerfile&lt;/code&gt; in the root of your project directory like this 👇&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1668679231923%2Fp-97mBMMq.png" class="article-body-image-wrapper"&gt;&lt;img alt="desktop" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1668679231923%2Fp-97mBMMq.png"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 4: Add Commands to Dockerfile
&lt;/h2&gt;

&lt;p&gt;Copy these commands to your Dockerfile&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

FROM node

WORKDIR /app

COPY package.json &lt;span class="nb"&gt;.&lt;/span&gt;
RUN npm i

COPY &lt;span class="nb"&gt;.&lt;/span&gt; &lt;span class="nb"&gt;.&lt;/span&gt;

&lt;span class="c"&gt;## EXPOSE [Port you mentioned in the vite.config file]&lt;/span&gt;

EXPOSE 5173

CMD &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"npm"&lt;/span&gt;, &lt;span class="s2"&gt;"run"&lt;/span&gt;, &lt;span class="s2"&gt;"dev"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;If you don't know what are these commands, read the explanation 👇&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;FROM node&lt;/code&gt; - Will create a node environment in the container&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;WORKDIR /app&lt;/code&gt; - Will create a directory app and switch to that directory&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;COPY package.json .&lt;/code&gt; - Copies package.json file to /app directory&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;RUN npm i&lt;/code&gt; - Runs &lt;code&gt;npm install&lt;/code&gt; to create node_modules for your app&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;COPY . .&lt;/code&gt; - Copies the source code to /app directory&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;EXPOSE 5173&lt;/code&gt; - Exposes the port to access the app from outside the container i.e from the browser&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;CMD ["npm", "run", "dev"]&lt;/code&gt; - Executes &lt;code&gt;npm run dev&lt;/code&gt; to start the server&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 5: Build the Dockerfile
&lt;/h2&gt;

&lt;p&gt;In the terminal navigate to your project directory, then execute&lt;/p&gt;

&lt;p&gt;&lt;code&gt;!! Remove the [ ] when executing&lt;/code&gt;&lt;/p&gt;

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

docker build &lt;span class="nt"&gt;-t&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;any name] &lt;span class="nb"&gt;.&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Flag used in the command&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-t&lt;/code&gt; - To tag the container (This will be your container name)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After the build completion, a docker image will be created.&lt;/p&gt;

&lt;p&gt;Docker Image is a template to run the container.&lt;/p&gt;

&lt;p&gt;To check your Docker Image, execute&lt;/p&gt;

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

docker images


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

&lt;/div&gt;

&lt;p&gt;It will show the list of images available on your system&lt;/p&gt;

&lt;p&gt;Check whether the name of the image you have given when running the &lt;code&gt;docker build&lt;/code&gt; command is available in the list.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 6: Run the Docker Container
&lt;/h2&gt;

&lt;p&gt;Execute this command in the terminal 👇&lt;/p&gt;

&lt;p&gt;&lt;code&gt;!! Remove the [ ] when executing&lt;/code&gt;&lt;/p&gt;

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

docker run &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="nt"&gt;--rm&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; 5173:5173 &lt;span class="nt"&gt;--name&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;name of the container] &lt;span class="o"&gt;[&lt;/span&gt;your docker image name]


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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Flag used in the command&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-d&lt;/code&gt; - To run the container in the background (Detach Mode )&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--rm&lt;/code&gt; - To delete the container, when you stop the container&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-p&lt;/code&gt; - Port Mapping between container and outside world.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;5173:5173&lt;/code&gt; - [Port access from Browser]: [Port exposed from the container]&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To check whether your docker container is running or not, execute&lt;/p&gt;

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

docker ps


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

&lt;/div&gt;

&lt;p&gt;It will show the list of containers running on your system&lt;/p&gt;

&lt;p&gt;Check whether the name of the container you have given when running the &lt;code&gt;docker run&lt;/code&gt; command is available in the list.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 7: Open the App in the Browser
&lt;/h2&gt;

&lt;p&gt;Open the Browser and access &lt;code&gt;http://localhost:[Port you mentioned in the docker run command]&lt;/code&gt; as per the configuration we did so far it should be &lt;code&gt;http://localhost:5173&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1668682778580%2FowixBZ78D.png" class="article-body-image-wrapper"&gt;&lt;img alt="desktop" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1668682778580%2FowixBZ78D.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Thanks For Reading!
&lt;/h2&gt;

&lt;p&gt;Hope you have learned something new today 😊.&lt;/p&gt;

&lt;p&gt;Follow and connect with me on &lt;a href="https://twitter.com/nandhakumar_io" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;, &lt;a href="https://www.instagram.com/nandhakumar.io/" rel="noopener noreferrer"&gt;Instagram&lt;/a&gt;, &lt;a href="//mailto:rsnk2013@gmail.com"&gt;Email&lt;/a&gt; and &lt;a href="https://www.linkedin.com/in/nandhakumar-dev/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; for more interesting stuff like this.&lt;/p&gt;

&lt;p&gt;My Personal Blog &lt;a href="https://www.nandhakumar.io" rel="noopener noreferrer"&gt;nandhakumar.io&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Cheers ✌️&lt;/p&gt;

</description>
      <category>react</category>
      <category>vite</category>
      <category>docker</category>
      <category>javascript</category>
    </item>
    <item>
      <title>How to get last element of an array or string using JS ES13 Feature</title>
      <dc:creator>Nandhakumar</dc:creator>
      <pubDate>Mon, 05 Sep 2022 19:52:58 +0000</pubDate>
      <link>https://dev.to/nandhakumar/how-to-get-last-element-of-an-array-or-string-using-js-es13-feature-2342</link>
      <guid>https://dev.to/nandhakumar/how-to-get-last-element-of-an-array-or-string-using-js-es13-feature-2342</guid>
      <description>&lt;p&gt;Javascript ES13 has released in June 2022 and it has pretty cool new features one of them is at() method that can be used with strings and arrays.&lt;/p&gt;

&lt;p&gt;So, before ES13 if you want to find the last element of an array or string.&lt;/p&gt;

&lt;p&gt;You need to subtract 1 from the length of the array and string and the calculated value will be passed within [] to get the last element like this 👇&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CGSczM4h--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9oxhgqk2rs4olufa1kw9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CGSczM4h--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9oxhgqk2rs4olufa1kw9.png" alt="Before ES13" width="801" height="345"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But now, after ES13 you can use at() method with negative indexes to get the elements in reverse order of a string or array like this 👇&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--C2-cjnN0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/36xfqtg68iego2k04ib0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--C2-cjnN0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/36xfqtg68iego2k04ib0.png" alt="After ES13" width="880" height="462"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Simple right? &lt;/p&gt;

&lt;p&gt;Thanks for reading this article.&lt;/p&gt;

&lt;p&gt;If you find this post helpful&lt;/p&gt;

&lt;p&gt;Follow me on &lt;a href="https://twitter.com/nandhakumar_dev"&gt;Twitter&lt;/a&gt; and &lt;a href="https://www.instagram.com/nandhakumar.dev"&gt;Instagram&lt;/a&gt; to show your support&lt;/p&gt;

&lt;p&gt;Cheers ✌️&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>softwaredeveloper</category>
      <category>es13</category>
    </item>
  </channel>
</rss>
