<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Bliss Abhademere</title>
    <description>The latest articles on Forem by Bliss Abhademere (@blissfelix3).</description>
    <link>https://forem.com/blissfelix3</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%2F1022893%2F78619cc8-7d6f-418c-848b-4e1a53277d42.png</url>
      <title>Forem: Bliss Abhademere</title>
      <link>https://forem.com/blissfelix3</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/blissfelix3"/>
    <language>en</language>
    <item>
      <title>How To Get Started From Express To NestJS: Complete Guide</title>
      <dc:creator>Bliss Abhademere</dc:creator>
      <pubDate>Mon, 05 Feb 2024 12:01:36 +0000</pubDate>
      <link>https://forem.com/blissfelix3/how-to-get-started-from-express-to-nestjs-complete-guide-3hdh</link>
      <guid>https://forem.com/blissfelix3/how-to-get-started-from-express-to-nestjs-complete-guide-3hdh</guid>
      <description>&lt;h2&gt;
  
  
  Introduction:
&lt;/h2&gt;

&lt;p&gt;Backend Developers like myself in the Node.js space looking to transition from Express.js a flexible and often straight forward framework to Nest.js a more structured and organized architecture to enhance the maintainability and scalability of your application. In this guide, we'll explore the key concepts of Nest.js, providing detailed examples and code snippets along the way.&lt;/p&gt;

&lt;h2&gt;
  
  
  Advantages of NestJS over Express:
&lt;/h2&gt;

&lt;p&gt;NestJS offers several benefits that makes it an appealing alternative choice for developers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Modularity&lt;/strong&gt;: NestJS is built with modularity in mind, allowing you to organize your code into manageable modules.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Dependency Injection&lt;/strong&gt;: NestJS also comes with built-in dependency which simplifies code organization and promotes testability.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Decorators and Metadata&lt;/strong&gt;: NestJS use of decorators and metadata simplifies the creation of controllers, services, and modules.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Typescript and Microservices&lt;/strong&gt;: NestJS is built with &lt;a href="https://hashnode.com/post/getting-started-with-typescript-ckpa0jge00u7gvws1cwajd5j5" rel="noopener noreferrer"&gt;Typescript&lt;/a&gt; and &lt;a href="https://medium.com/widle-studio/mastering-microservices-in-nest-js-eb143a6b9639" rel="noopener noreferrer"&gt;Microservices&lt;/a&gt; in mind, providing strong typing and enhanced developer experience in using microservices like RabbitMQ, Kafka.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Resources
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://nodejs.org/en/docs" rel="noopener noreferrer"&gt;Node.js Documentation&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://nestjs.com/" rel="noopener noreferrer"&gt;Nest.js Documentation&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://expressjs.com/" rel="noopener noreferrer"&gt;Express.js Documentation&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  NestJS Project Setup
&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%2Fwzaabinvsj82pdsko58c.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%2Fwzaabinvsj82pdsko58c.png" alt="NestJS welcome page"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Installing the NestJS CLI
&lt;/h3&gt;

&lt;p&gt;To get started, install the NestJS CLI globally:&lt;/p&gt;

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

npm install -g @nestjs/cli


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

&lt;/div&gt;

&lt;p&gt;The NestJS CLI simplies nest project creation and component generation, providing a seamless development experience.&lt;/p&gt;

&lt;h3&gt;
  
  
  NestJS Project Creation
&lt;/h3&gt;

&lt;p&gt;Next, we create a new NestJS project named "my-nest-app":&lt;/p&gt;

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

nest new my-nest-app


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

&lt;/div&gt;

&lt;p&gt;After this command is executed on your CLI you get a prompt asking your preferred package manager:&lt;/p&gt;

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

⚡  We will scaffold your app &lt;span class="k"&gt;in &lt;/span&gt;a few seconds..

? Which package manager would you ❤️  to use? &lt;span class="o"&gt;(&lt;/span&gt;Use arrow keys&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; npm
  yarn
  pnpm


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

&lt;/div&gt;

&lt;p&gt;Now once your preference has been selected, the project structure and necessary dependencies installs. Navigate 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;my-nest-app


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

&lt;/div&gt;
&lt;h2&gt;
  
  
  Application Structures: NestJS vs. Express
&lt;/h2&gt;

&lt;p&gt;Let's compare the directory structures of NestJS and Express: &lt;strong&gt;NestJS Project Structure&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

src/
|-- controllers/
|   |-- app.controller.ts
|-- modules/
|   |-- app.module.ts
|-- services/
|   |-- app.service.ts
|-- main.ts


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

&lt;/div&gt;

&lt;p&gt;Express Project Structure&lt;/p&gt;

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

routes/
|-- appRoutes.js
|-- userRoutes.js
|-- ...
models/
|-- userModel.js
|-- ...
app.js


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

&lt;/div&gt;

&lt;p&gt;In NestJS, we organize our code into modules, controllers, and services, promoting a modular and scalable architecture.&lt;/p&gt;

&lt;h2&gt;
  
  
  Controllers in NestJS vs. Routes in Express
&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%2Ffxgb7nm1td1v6rej46lc.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%2Ffxgb7nm1td1v6rej46lc.png" alt="Controllers flow"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Introduction to Controllers
&lt;/h3&gt;

&lt;p&gt;In NestJS, controllers play a crucial role in handling incoming requests and shaping the application's behavior. They're similar to Express routes but provide additional features.&lt;/p&gt;

&lt;h3&gt;
  
  
  Comparing Controllers to Routes
&lt;/h3&gt;

&lt;p&gt;Let's consider a CRUD example to illustrate the differences between controllers in NestJS and routes in Express.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NestJS Controller&lt;/strong&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;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;Post&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="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;CatsService&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;./cats.service&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;Cat&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;./interfaces/cat.interface&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;cats&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;CatsController&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;catsService&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;CatsService&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;findAll&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nx"&gt;Cat&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;catsService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findAll&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;Post&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nf"&gt;create&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;createCatDto&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="kr"&gt;string&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`This action adds a new cat: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;createCatDto&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="s2"&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;The &lt;code&gt;@Controller&lt;/code&gt; decorator specifies the base route for all the routes defined within the controller.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Express Routes&lt;/strong&gt;&lt;/p&gt;

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

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;router&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Router&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;catsService&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../services/cats.service&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&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;/&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="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;allCats&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;catsService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findAll&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;allCats&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&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="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newCat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&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;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`This action adds a new cat: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;newCat&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="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="kr"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;In Express, routes are defined using the &lt;code&gt;express.Router()&lt;/code&gt; and then exported.&lt;/p&gt;

&lt;h2&gt;
  
  
  Dependency Injection and Services
&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%2Fl2ka9w2yqptyfit7nwzk.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%2Fl2ka9w2yqptyfit7nwzk.png" alt="Service Injection"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Understanding Dependency Injection in NestJS
&lt;/h3&gt;

&lt;p&gt;NestJS embraces the &lt;a href="https://www.freecodecamp.org/news/a-quick-intro-to-dependency-injection-what-it-is-and-when-to-use-it-7578c84fa88f/#:~:text=In%20software%20engineering%2C%20dependency%20injection,be%20used%20(a%20service)." rel="noopener noreferrer"&gt;concept of dependency injection (DI) for managing and injecting dependencies into components&lt;/a&gt;. In contrast to Express, which often relies on singleton patterns, NestJS promotes the use of injectable services.&lt;/p&gt;

&lt;p&gt;Transitioning from Singleton Patterns to Injectable Services Let's refactor a simple Express service to a NestJS injectable service.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Express Singleton Service&lt;/strong&gt;&lt;/p&gt;

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

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CatService&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="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;cats&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="nf"&gt;findAll&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;cats&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="kr"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;CatService&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;In this example, the service is a singleton instance exported for global use.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NestJS Injectable Service&lt;/strong&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;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;CatsService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;cats&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;

  &lt;span class="nf"&gt;findAll&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="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;cats&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;By using the @Injectable decorator, we define a service that can be injected into controllers or other services.&lt;/p&gt;

&lt;h2&gt;
  
  
  ORM in NestJS
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Setting Up TypeORM Entity
&lt;/h3&gt;

&lt;p&gt;NestJS supports various &lt;a href="https://medium.com/@julianam.tyler/what-is-the-difference-between-odm-and-orm-267bbb7778b0" rel="noopener noreferrer"&gt;Object-Relational Mapping (ORM) and Object-Document Mapping (ODM) libraries&lt;/a&gt;. Let's set up a &lt;code&gt;TypeORM&lt;/code&gt; entity for PostgreSQL:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Install&lt;/strong&gt;&lt;code&gt;TypeORM&lt;/code&gt; and PostgreSQL Package&lt;/p&gt;

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

npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save&lt;/span&gt; @nestjs/typeorm typeorm pg


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

&lt;/div&gt;

&lt;p&gt;Configure the module in app.module.ts:&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;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;TypeOrmModule&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/typeorm&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;CatsModule&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;./cats/cats.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;CatsModule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;TypeOrmModule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forRoot&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;postgres&lt;/span&gt;&lt;span class="dl"&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;localhost&lt;/span&gt;&lt;span class="dl"&gt;'&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;5432&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;nestuser&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;nestpassword&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;database&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;nestjs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;synchronize&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;entities&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/**/*.entity{.ts,.js}&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;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;p&gt;&lt;strong&gt;Define a TypeORM entity in cats/cat.entity.ts&lt;/strong&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;Entity&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Column&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;PrimaryGeneratedColumn&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;typeorm&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;Entity&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;Cat&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;PrimaryGeneratedColumn&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;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Column&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="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Column&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;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Column&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nx"&gt;breed&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This example sets up a PostgreSQL database with TypeORM and defines a Cat entity.&lt;/p&gt;

&lt;h2&gt;
  
  
  Data Validation and Validation Pipes
&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%2Fz18ydzqzcekvp5j9tr2e.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%2Fz18ydzqzcekvp5j9tr2e.png" alt="Active Validation Error Catch"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Class Validators and Global Validation Pipe
&lt;/h3&gt;

&lt;p&gt;NestJS simplifies data validation using class validators and global validation pipes. Let's explore how to use these features.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using Class Validators&lt;/strong&gt; Create a DTO (Data Transfer Object) in &lt;code&gt;cats/dto/create-cat.dto.ts&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="k"&gt;import&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="nx"&gt;IsInt&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;CreateCatDto&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="k"&gt;readonly&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="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;IsInt&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="k"&gt;readonly&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;number&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="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;breed&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;In the corresponding controller, use the DTO with class validation&lt;/strong&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;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="nx"&gt;Body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;UsePipes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ValidationPipe&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;CreateCatDto&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/create-cat.dto&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;CatsService&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;./cats.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;cats&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;CatsController&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;catsService&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;CatsService&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;Post&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;UsePipes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ValidationPipe&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;transform&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="nf"&gt;create&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;createCatDto&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;CreateCatDto&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;catsService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;createCatDto&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`Cat &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;createCatDto&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="s2"&gt; created successfully`&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;Here, the @UsePipes(new ValidationPipe({ transform: true })) decorator applies the global validation pipe to the create route, validating incoming data against the CreateCatDto schema. The transform: true option automatically transforms incoming payload data into instances of the DTO.&lt;/p&gt;

&lt;h3&gt;
  
  
  Global Validation Pipe Configuration
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Configure the global validation pipe in main.ts&lt;/strong&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;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;NestFactory&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/core&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;AppModule&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.module&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;ValidationPipe&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;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;bootstrap&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;NestFactory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;AppModule&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;useGlobalPipes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ValidationPipe&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;transform&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="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;bootstrap&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;In this configuration, useGlobalPipes applies the validation pipe globally for all routes, ensuring that data is validated and transformed consistently across the application.&lt;/p&gt;

&lt;p&gt;Now, every incoming request to your NestJS application will undergo validation against the defined DTO schemas, providing a robust and centralized approach to data validation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing in NestJS
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Writing Tests for NestJS Applications
&lt;/h3&gt;

&lt;p&gt;NestJS includes built-in testing utilities that make it easy to write unit and integration tests for your application. Let's explore testing approaches using Jest in NestJS compared to testing in Express.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setting Up Jest
&lt;/h3&gt;

&lt;p&gt;NestJS Jest is a popular testing framework that works seamlessly with NestJS. &lt;strong&gt;Install the required packages&lt;/strong&gt;:&lt;/p&gt;

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

npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save-dev&lt;/span&gt; @nestjs/testing jest @nestjs/schematics


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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Configure Jest in&lt;/strong&gt;&lt;code&gt;jest.config.js&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="kr"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;preset&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ts-jest&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;testEnvironment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;node&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Writing a Basic Unit Test
&lt;/h3&gt;

&lt;p&gt;Let's write a simple unit test for a service in &lt;code&gt;cats/cats.service.spec.ts&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="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Test&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;TestingModule&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/testing&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;CatsService&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;./cats.service&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nf"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;CatsService&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="na"&gt;service&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;CatsService&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;beforeEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="na"&gt;module&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;TestingModule&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;Test&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createTestingModule&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="nx"&gt;CatsService&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nf"&gt;compile&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="nx"&gt;service&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kr"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;CatsService&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;CatsService&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;should be defined&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;service&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toBeDefined&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;should return an array of cats&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cats&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;service&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findAll&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cats&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nf"&gt;toBe&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="p"&gt;});&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;In this example, we're testing the CatsService to ensure it returns an array of cats.&lt;/p&gt;

&lt;h3&gt;
  
  
  Running Tests
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Execute the tests with the following command&lt;/strong&gt;:&lt;/p&gt;

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

npm run &lt;span class="nb"&gt;test&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;Jest will run the tests and provide feedback on their success or failure.&lt;/p&gt;

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

&lt;p&gt;Migrating from Express to NestJS might be overwhelming at first, but the benefits of a structured and modular architecture will greatly enhance your maintainability and scalability of your application.&lt;/p&gt;

&lt;p&gt;Here’s a link to my main blog site: &lt;a href="https://blissfelix.info" rel="noopener noreferrer"&gt;Bliss Articles&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/BlissFelix" rel="noopener noreferrer"&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%2Ftwve1hh3j8ewl5aowo7r.png" alt="Buy Me A Coffee"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I appreciate you taking the time to read this😁. Please think about giving it a ❤️ if you found it useful and instructive and bookmarking✅ for later use. Please post your queries and remarks in the comments box if you have any. I'm eager to hear your thoughts. Up until then!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>beginners</category>
      <category>nestjs</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Exciting projects to build using Socket.IO Node.js Express</title>
      <dc:creator>Bliss Abhademere</dc:creator>
      <pubDate>Wed, 22 Mar 2023 20:06:36 +0000</pubDate>
      <link>https://forem.com/blissfelix3/exciting-projects-to-build-using-socketio-nodejs-express-7mn</link>
      <guid>https://forem.com/blissfelix3/exciting-projects-to-build-using-socketio-nodejs-express-7mn</guid>
      <description>&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%2Farn3jzsq4zbpzwbxq63f.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%2Farn3jzsq4zbpzwbxq63f.png" alt="Build with socket io"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Socket.io is a popular library for real-time web applications that enables bidirectional communication between the client and server. Node.js and Express are popular back-end frameworks often used with Socket.io to build scalable and robust real-time applications. In this article we'll explore some exciting projects to develop using Socket.io, Node.js, and Express. These projects can help you hone your abilities and improve your portfolio as a novice or an experienced developer. &lt;br&gt;
So let's get started and investigate some intriguing project ideas!&lt;/p&gt;

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

&lt;h3&gt;
  
  
  Frameworks
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Node.js&lt;/li&gt;
&lt;li&gt;npm&lt;/li&gt;
&lt;li&gt;Express&lt;/li&gt;
&lt;li&gt;Socket.io&lt;/li&gt;
&lt;li&gt;VS Code&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Resources
&lt;/h3&gt;

&lt;p&gt;Before diving into the projects, it is essential to have a basic understanding of Node.js, Express.js, and Socket.io. Here are some resources to get you started:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://nodejs.org/en/docs" rel="noopener noreferrer"&gt;Node.js Documentation&lt;/a&gt;&lt;br&gt;
&lt;a href="https://expressjs.com/" rel="noopener noreferrer"&gt;Express.js Documentation&lt;/a&gt;&lt;br&gt;
&lt;a href="https://socket.io/docs/v4/" rel="noopener noreferrer"&gt;Socket.io Documentation&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Project 1: Real-time Chat Application
&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%2Frbp9e2854ehw3famfl50.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%2Frbp9e2854ehw3famfl50.png" alt="Real time flow"&gt;&lt;/a&gt;&lt;br&gt;
The first project is a real-time chat application that allows users to send and receive messages in real time. The application will set up a two-way communication channel between the server and the client using Socket.io and serve the web pages using Express.js.&lt;br&gt;
Here is a code snippet that demonstrates how to create a Socket.io server and listen for incoming connections:&lt;/p&gt;

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

const express = require('express');
const http = require('http');
const socketIo = require('socket.io');

const app = express();
const server = http.createServer(app);
const io = socketIo(server);

io.on('connection', (socket) =&amp;gt; {
  console.log('A user connected');

  socket.on('disconnect', () =&amp;gt; {
    console.log('A user disconnected');
  });
});


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

&lt;/div&gt;

&lt;p&gt;This code creates an HTTP server using the http module, an Express.js application using the socket.io module, and an HTTP server using the socket.io module. The &lt;code&gt;io.on()&lt;/code&gt; method listens for incoming connections and logs a message when a user connects or disconnects.&lt;/p&gt;

&lt;p&gt;We can use the socket to transmit and receive messages using &lt;code&gt;socket.on()&lt;/code&gt; and &lt;code&gt;emit()&lt;/code&gt; method. Here is an example of how to broadcast a message to all connected clients:&lt;/p&gt;

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

socket.on('chat message', (msg) =&amp;gt; {
  io.emit('chat message', msg);
});


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

&lt;/div&gt;

&lt;p&gt;This code listens for a chat message event from the client and then broadcasts the message to all connected clients using the &lt;code&gt;io.emit()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;Here is a link to a complete tutorial on how to build a real-time chat application with Socket.io, Node.js, and Express.js: &lt;a href="https://socket.io/get-started/chat/" rel="noopener noreferrer"&gt;Building a Real-time Chat Application with Socket.io, Node.js, and Express.js&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Project 2: Real-time Dashboard Application
&lt;/h2&gt;

&lt;p&gt;The second project is a real-time dashboard application that displays real-time data from a data source, such as a database or an API. The application will use Socket.io to stream the data to the client in real-time and Express.js to serve the web pages.&lt;/p&gt;

&lt;p&gt;Here is a code snippet that demonstrates how to stream data using Socket.io:&lt;/p&gt;

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

const io = socketIo(server);

io.on('connection', (socket) =&amp;gt; {
  console.log('A user connected');

  const interval = setInterval(() =&amp;gt; {
    // Get data from the data source
    const data = getData();

    // Send the data to the client
    socket.emit('data', data);
  }, 1000);

  socket.on('disconnect', () =&amp;gt; {
    console.log('A user disconnected');
    clearInterval(interval);
  });
});


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

&lt;/div&gt;

&lt;p&gt;This code listens for incoming connections and then streams data to the client every second using the &lt;code&gt;socket.emit()&lt;/code&gt; method. The &lt;code&gt;setInterval()&lt;/code&gt; method is used to continuously fetch data from the data source and emit it to the client.&lt;/p&gt;

&lt;p&gt;Here is a link to a complete tutorial on how to build a real-time dashboard application with Socket.io, Node.js, and Express.js: &lt;a href="https://www.freecodecamp.org/news/build-a-realtime-chat-app-with-react-express-socketio-and-harperdb/" rel="noopener noreferrer"&gt;Building a Real-time Dashboard Application with Socket.io, Node.js, and Express.js&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Project 3: Multiplayer Game
&lt;/h2&gt;

&lt;p&gt;The third project is a multiplayer game that allows multiple players to play together in real-time. Socket.io and Express.js will be used by the game to synchronize game state between the server and clients and to serve web pages.&lt;/p&gt;

&lt;p&gt;Here is a code snippet that demonstrates how to synchronize the game state using Socket.io:&lt;/p&gt;

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

io.on('connection', (socket) =&amp;gt; {
console.log('A user connected');

// Add the player to the game
socket.on('add player', (player) =&amp;gt; {
// Add the player to the game state
gameState.addPlayer(player);

// Send the updated game state to all connected clients
io.emit('game state', gameState);
});

// Move the player in the game
socket.on('move player', (player) =&amp;gt; {
// Move the player in the game state
gameState.movePlayer(player);

// Send the updated game state to all connected clients
io.emit('game state', gameState);
});

// Remove the player from the game
socket.on('remove player', (player) =&amp;gt; {
// Remove the player from the game state
gameState.removePlayer(player);

// Send the updated game state to all connected clients
io.emit('game state', gameState);
});

socket.on('disconnect', () =&amp;gt; {
console.log('A user disconnected');

// Remove the player from the game
gameState.removePlayer(socket.id);

// Send the updated game state to all connected clients
io.emit('game state', gameState);
});
});


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

&lt;/div&gt;

&lt;p&gt;This code listens for incoming connections and then handles events such as adding a player, moving a player, and removing a player from the game state. Socket.io synchronizes the server and clients' game sessions using the gameState object, which serves as a game's state storage.&lt;/p&gt;

&lt;p&gt;Here is a link to a complete tutorial on how to build a multiplayer game with Socket.io, Node.js, and Express.js: &lt;a href="https://dev.to/denctl/how-to-use-socket-io-not-the-chat-3l21"&gt;Building a Multiplayer Game with Socket.io, Node.js, and Express.js&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;Socket.io, Node.js, and Express.js are essential tools for building real-time applications that require low latency and high throughput. This article covered some sample projects software engineers can create using these tools. You can create your real-time applications and benefit from Socket.io, Node.js, and Express.js by following the sample code snippets and tutorials.&lt;/p&gt;

&lt;p&gt;Here’s a link to my main blog site: &lt;a href="https://blissfelix.info" rel="noopener noreferrer"&gt;Bliss Articles&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/BlissFelix" rel="noopener noreferrer"&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%2Ftwve1hh3j8ewl5aowo7r.png" alt="Buy Me A Coffee"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I appreciate you taking the time to read this😁. Please think about giving it a ❤️ if you found it useful and instructive and bookmarking✅ for later use. Please post your queries and remarks in the comments box if you have any. I'm eager to hear your thoughts. Up until then!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>tutorial</category>
      <category>node</category>
      <category>community</category>
    </item>
    <item>
      <title>Real-Time Applications with Node.js and WebSockets</title>
      <dc:creator>Bliss Abhademere</dc:creator>
      <pubDate>Fri, 17 Feb 2023 09:02:06 +0000</pubDate>
      <link>https://forem.com/blissfelix3/real-time-applications-with-nodejs-and-websockets-1fcc</link>
      <guid>https://forem.com/blissfelix3/real-time-applications-with-nodejs-and-websockets-1fcc</guid>
      <description>&lt;p&gt;Real-time web applications have gained popularity in recent years. These programs provide real-time data flow without the need to repeatedly refresh web pages by facilitating smooth communication between clients and servers. Due to their scalability, speed, and user-friendliness, Node.js and related technologies, like Express and WebSockets, are preferred options for developing real-time applications.&lt;/p&gt;

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

&lt;p&gt;WebSockets is a protocol for two-way communication between a client and a server, allowing data to be sent and received in real time. WebSockets overcome the limitations of traditional HTTP communication, which requires establishing a new connection for each request. WebSockets enable the establishment of a persistent connection, which allows data to be delivered and received at any time, without the need to initiate a new connection.&lt;br&gt;
Building a Real-Time Application with Node.js and WebSockets&lt;br&gt;
In this article, we'll use Node.js, Express, and WebSockets to create a straightforward real-time chat application. The application will allow multiple users to communicate in real time.&lt;/p&gt;
&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;Before we begin, ensure that you have the following installed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Node.js&lt;/li&gt;
&lt;li&gt;npm&lt;/li&gt;
&lt;li&gt;Express&lt;/li&gt;
&lt;li&gt;Socket.IO&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Setting up the Project&lt;br&gt;
First, create a new directory for your project and initialize it with npm:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir real-time-chat
cd real-time-chat
npm init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Install the required packages:&lt;/p&gt;

&lt;p&gt;npm install express socket.io --save&lt;/p&gt;

&lt;p&gt;Create a new file named &lt;code&gt;server.js&lt;/code&gt; and add the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express');
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);

app.use(express.static(__dirname + '/public'));

app.get('/', (req, res) =&amp;gt; {
  res.sendFile(__dirname + '/public/index.html');
});

io.on('connection', (socket) =&amp;gt; {
  console.log('a user connected');

  socket.on('disconnect', () =&amp;gt; {
    console.log('user disconnected');
  });

  socket.on('chat message', (msg) =&amp;gt; {
    console.log('message: ' + msg);
    io.emit('chat message', msg);
  });
});

server.listen(3000, () =&amp;gt; {
  console.log('listening on *:3000');
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We import the needed package in this code., set up the Express app, and create a new server using the &lt;code&gt;http&lt;/code&gt; module. We also set up the Socket.IO server, which allows us to communicate in real-time with our clients.&lt;br&gt;
Static files are supplied from the specified directory by the &lt;code&gt;app.use()&lt;/code&gt; function. This instance is serving files from the &lt;code&gt;public&lt;/code&gt; directory.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;app.get()&lt;/code&gt; method sets up a route to the home page, which provides access to the public directory's &lt;code&gt;index.html&lt;/code&gt; file.&lt;br&gt;
A connection listener is established by the &lt;code&gt;io.on()&lt;/code&gt; method, which watches for connections from clients.&lt;/p&gt;

&lt;p&gt;When a client disconnects, an event called "disconnect" is released, and the &lt;code&gt;socket.on()&lt;/code&gt; procedure watches for it. We log a note to the console whenever a client disconnects.&lt;/p&gt;

&lt;p&gt;The "chat message" event, which is released whenever a client sends a message, is also monitored by the &lt;code&gt;socket.on()&lt;/code&gt; method.&lt;br&gt;
When a message is received, we emit it to all clients using &lt;code&gt;io.emit()&lt;/code&gt;and log it to the console.&lt;/p&gt;

&lt;p&gt;Finally, we launch the server and open port 3000 for listening.&lt;/p&gt;
&lt;h2&gt;
  
  
  Creating the Frontend
&lt;/h2&gt;

&lt;p&gt;Create a new directory called &lt;code&gt;public&lt;/code&gt;, and add a new file called &lt;code&gt;index.html&lt;/code&gt; to it. The code below should be added to this file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;title&amp;gt;Real-Time Chat Application&amp;lt;/title&amp;gt;
    &amp;lt;link rel="stylesheet" type="text/css" href="style.css"&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
    &amp;lt;div class="chat-container"&amp;gt;
      &amp;lt;header&amp;gt;
        &amp;lt;h1&amp;gt;Real-Time Chat Application&amp;lt;/h1&amp;gt;
      &amp;lt;/header&amp;gt;
      &amp;lt;main class="chat-window"&amp;gt;
        &amp;lt;ul class="message-list"&amp;gt;&amp;lt;/ul&amp;gt;
        &amp;lt;form id="message-form"&amp;gt;
          &amp;lt;input type="text" name="message" placeholder="Type your message here"&amp;gt;
          &amp;lt;button&amp;gt;Send&amp;lt;/button&amp;gt;
        &amp;lt;/form&amp;gt;
      &amp;lt;/main&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;script src="/socket.io/socket.io.js"&amp;gt;&amp;lt;/script&amp;gt;
    &amp;lt;script src="script.js"&amp;gt;&amp;lt;/script&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this HTML file, we create the structure for the chat interface. We have a header that displays the application's name, a "main" element with the chat window and a message-sending form, and a script tag that loads the socket.io client library and our client-side JavaScript file.&lt;br&gt;
Creating a form to send messages requires the use of the &lt;code&gt;&amp;lt;form&amp;gt;&lt;/code&gt; tag. We'll attach a listener to it when the form is submitted because it has an &lt;code&gt;id&lt;/code&gt; attribute with the value "message-form" set.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;placeholder&lt;/code&gt; attribute on the form's &lt;code&gt;&amp;lt;input&amp;gt;&lt;/code&gt; tag with the name "message" prompts the user to "Type your message here" as a hint.&lt;/p&gt;

&lt;p&gt;Additionally, there is a "Send"-labeled &lt;code&gt;&amp;lt;button&amp;gt;&lt;/code&gt; tag that the user can click to submit the form.&lt;/p&gt;

&lt;p&gt;In the &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag, we'll create a new instance of the socket.io client and attach event listeners for the form submission and receiving chat messages. Let's add the &lt;code&gt;script.js&lt;/code&gt; file and add the following code:&lt;br&gt;
&lt;/p&gt;

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

// Handle form submission
const form = document.getElementById('message-form');
form.addEventListener('submit', e =&amp;gt; {
  e.preventDefault();
  const messageInput = form.elements.message;
  const message = messageInput.value.trim();
  if (message) {
    socket.emit('chat message', message);
    messageInput.value = '';
  }
});

// Handle incoming chat messages
const messageList = document.querySelector('.message-list');
socket.on('chat message', message =&amp;gt; {
  const li = document.createElement('li');
  li.textContent = message;
  messageList.appendChild(li);
});

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

&lt;/div&gt;



&lt;p&gt;In this client-side JavaScript code, we first create a new instance of the socket.io client using &lt;code&gt;io()&lt;/code&gt;. Then, we attach an event listener for the form submission using &lt;code&gt;addEventListener()&lt;/code&gt;. Inside the event listener, we prevent the default form submission behavior using &lt;code&gt;e.preventDefault()&lt;/code&gt;, get the message input using &lt;code&gt;form.elements.message&lt;/code&gt;, and emit a &lt;code&gt;chat message&lt;/code&gt; event with the message to the server using &lt;code&gt;socket.emit()&lt;/code&gt;. We also clear the message input value using &lt;code&gt;messageInput.value = " "&lt;/code&gt;.&lt;br&gt;
We also attach an event listener for receiving chat messages using &lt;code&gt;socket.on()&lt;/code&gt;. Inside the event listener, we create a new list item element using &lt;code&gt;document.createElement()&lt;/code&gt; and add the received message as text content using &lt;code&gt;li.textContent&lt;/code&gt;. Then, we append the list item to the message list using &lt;code&gt;messageList.appendChild(li)&lt;/code&gt;.&lt;/p&gt;

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

&lt;p&gt;With this code, we have a functional real-time chat application that allows users to send and receive messages in real-time. We can start the server using node &lt;code&gt;index.js&lt;/code&gt; and navigate to &lt;code&gt;http://localhost:3000&lt;/code&gt; in a web browser to test it out.&lt;br&gt;
Congratulations on building your first real-time application with Node.js, Express, and WebSockets!&lt;/p&gt;

</description>
      <category>gratitude</category>
    </item>
    <item>
      <title>Integrate your Node.js and Express.js App with Socket.IO</title>
      <dc:creator>Bliss Abhademere</dc:creator>
      <pubDate>Wed, 08 Feb 2023 10:44:42 +0000</pubDate>
      <link>https://forem.com/blissfelix3/integrating-nodejs-and-expressjs-with-socketio-gn</link>
      <guid>https://forem.com/blissfelix3/integrating-nodejs-and-expressjs-with-socketio-gn</guid>
      <description>&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%2Flcphqx51cptrbazk8xla.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%2Flcphqx51cptrbazk8xla.png" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Integrating Node.js, Express.js, and Socket.IO is required when creating real-time online applications. These technologies provide server-side web development projects with quick, scalable, and user-friendly solutions.&lt;/p&gt;

&lt;p&gt;You will discover how to combine these technologies in this tutorial to create a real-time chat application.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Basic knowledge of Node.js, Express.js and JavaScript.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Node.js and npm installed on your machine.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 1: Install dependencies
&lt;/h2&gt;

&lt;p&gt;Before starting, install the essential dependencies, including Node.js, Express.js, and Socket.IO. Use npm to accomplish this:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 2: Set up a basic Express.js server
&lt;/h2&gt;

&lt;p&gt;Your chat application will be built on a simple Express.js server that you create in this phase. The code below creates an HTTP server by calling require("http") on an Express.js server. Server (app), which listens on a certain port:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express');
const app = express();
const server = require('http').Server(app);
const port = process.env.PORT || 3000;

server.listen(port, () =&amp;gt; {
  console.log(`Server started on port ${port}`);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 3: Integrate Socket.IO
&lt;/h2&gt;

&lt;p&gt;You must incorporate Socket.IO if you want to provide your chat application real-time capabilities. Add the following code to your Express.js server to accomplish this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const io = require('socket.io')(server);

io.on('connection', socket =&amp;gt; {
  console.log('User connected');

  socket.on('disconnect', () =&amp;gt; {
    console.log('User disconnected');
  });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By supplying the server to require("socket.io") in this case, io is constructed. When a client connects to the server, the code then waits for a "connection" event to be produced. The code logs a message to the console to show that a connection has been formed when one is made. A "disconnect" event is released when a user disconnects, and the code writes a message to the console to let the user know that they have disconnected.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Add chat functionality
&lt;/h2&gt;

&lt;p&gt;You can now give your chat application conversation features after integrating Socket.IO. You can achieve this by issuing a chat message event to all connected clients after listening for a chat message event from the client.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;io.on('connection', socket =&amp;gt; {
  console.log('User connected');

  socket.on('chat message', message =&amp;gt; {
    io.emit('chat message', message);
  });

  socket.on('disconnect', () =&amp;gt; {
    console.log('User disconnected');
  });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the code uses &lt;code&gt;socket.on('chat message', message =&amp;gt; ... )&lt;/code&gt; to monitor the client for a "chat message" event. The code utilizes the &lt;code&gt;io.emit()&lt;/code&gt; function to send out a "chat message" event to all connected clients whenever a "chat message" event is received. In this manner, the message will be delivered to all connected clients, who will then be able to see it in the conversation.&lt;/p&gt;

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

&lt;p&gt;Now that Node.js, Express.js, and Socket.IO have been integrated, you have the ideal basis for a real-time online application. On top of this foundation, you can keep expanding and investigate the fascinating possibilities of real-time web development.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/BlissFelix" rel="noopener noreferrer"&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%2Ftwve1hh3j8ewl5aowo7r.png" alt="Buy Me A Coffee" width="434" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I appreciate you taking the time to read this😁. Please think about giving it a ❤️ if you found it useful and instructive and bookmarking✅ for later use. Please post your queries and remarks in the comments box if you have any. I'm eager to hear your thoughts. Up until then!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>tutorial</category>
      <category>node</category>
      <category>community</category>
    </item>
    <item>
      <title>Modernizing Authentication with JWT Cookies</title>
      <dc:creator>Bliss Abhademere</dc:creator>
      <pubDate>Wed, 08 Feb 2023 08:50:51 +0000</pubDate>
      <link>https://forem.com/blissfelix3/modernizing-authentication-with-jwt-cookies-18b3</link>
      <guid>https://forem.com/blissfelix3/modernizing-authentication-with-jwt-cookies-18b3</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Verifying a user's identity in a web application using authentication. Modern web applications now frequently use JSON Web Tokens (JWTs) as a means of authentication.&lt;/p&gt;

&lt;p&gt;JWTs are distinctive because they:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Stateless (don't require server storage)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Self-contained (all necessary information is included in the token) (all necessary information is included in the token)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Simple to transfer (can be easily passed between client and server)&lt;br&gt;
JWTs can be used by being kept in HTTP cookies, for example. In this post, we'll examine the advantages JWT cookies have over other authentication options and why they're a popular choice.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Security
&lt;/h2&gt;

&lt;p&gt;Enhanced security is among the major advantages of using JWT cookies. JWTs are not kept on the server, in contrast to conventional session-based authentication. Instead, they are provided along with every request, saving the server from having to keep track of active session databases. Due to the fact that critical data is not kept in a single, easily accessible area, the danger of security breaches is decreased.&lt;/p&gt;

&lt;p&gt;In addition to being digitally signed and encrypted, JWTs allow for the encryption of the data they contain and the verification of their integrity and authenticity. This makes it far more difficult for an attacker to alter a JWT's contents and pass themselves off as a user.&lt;/p&gt;

&lt;h2&gt;
  
  
  Scalability
&lt;/h2&gt;

&lt;p&gt;Scalability is a benefit of JWT cookies as well. With conventional session-based authentication, the server must handle an increasing number of active sessions as the number of users rises. As the server must manage a huge volume of data processing and storage, this might soon become a scalability bottleneck.&lt;/p&gt;

&lt;p&gt;Since JWT cookies are stored on the client and sent with each request, the server does not need to keep track of active sessions. This lessens the server's workload and makes it simpler to scale the application as necessary.&lt;/p&gt;

&lt;h2&gt;
  
  
  Interdisciplinary compatibility
&lt;/h2&gt;

&lt;p&gt;Because JWT cookies are stored in HTTP cookies and are easily transferable between domains, they are compatible with cross-domain queries. Due to the ease with which authentication data can be sent across other domains and services, it is now simpler to develop applications that employ numerous domains and services.&lt;/p&gt;

&lt;h2&gt;
  
  
  Portability
&lt;/h2&gt;

&lt;p&gt;Additionally portable, JWT cookies make it simple to move authentication data from one application to another. As a result, it is simpler to develop apps that utilize a variety of services and platforms because the authentication data can be readily transferred between them.&lt;/p&gt;

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

&lt;p&gt;As a result of their improved security, scalability, cross-domain compatibility, and portability, JWT cookies are a popular solution for authentication in contemporary web applications. Compared to conventional session-based authentication, they have a number of benefits and are a good fit for contemporary web development. The advantages of JWT cookies and how they can be used to enhance your authentication process should be taken into account whether you are developing a new application or updating an existing one.&lt;/p&gt;

&lt;p&gt;I appreciate you taking the time to read this😁. Please think about giving it a ❤️ if you found it useful and instructive and bookmarking✅ for later use. Please post your queries and remarks in the comments box if you have any. I'm eager to hear your thoughts. Up until then!&lt;/p&gt;

&lt;h2&gt;
  
  
  Further Reading
&lt;/h2&gt;

&lt;p&gt;Consult the following sites for a fuller understanding of JWT cookies and how they are used in contemporary web development:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://auth0.com/learn/json-web-tokens"&gt;Auth0 - Understanding JSON Web Tokens (JWT)&lt;/a&gt;&lt;br&gt;
&lt;a href="https://auth0.com/blog/refresh-tokens-what-are-they-and-when-to-use-them/"&gt;JWT Cookies vs Session Cookies&lt;/a&gt;&lt;br&gt;
&lt;a href="https://jwt.io/"&gt;JWT.io - JSON Web Token for modern applications&lt;/a&gt;&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>webdev</category>
      <category>security</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
