<?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: renam singla</title>
    <description>The latest articles on DEV Community by renam singla (@renam_singla_fb52a400f07e).</description>
    <link>https://dev.to/renam_singla_fb52a400f07e</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%2F3510494%2F026be079-3407-41a3-86dd-83bf6c7a45fa.png</url>
      <title>DEV Community: renam singla</title>
      <link>https://dev.to/renam_singla_fb52a400f07e</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/renam_singla_fb52a400f07e"/>
    <language>en</language>
    <item>
      <title>WEBSOCKETS</title>
      <dc:creator>renam singla</dc:creator>
      <pubDate>Wed, 15 Oct 2025 21:09:49 +0000</pubDate>
      <link>https://dev.to/renam_singla_fb52a400f07e/websockets-2ehb</link>
      <guid>https://dev.to/renam_singla_fb52a400f07e/websockets-2ehb</guid>
      <description>&lt;p&gt;The &lt;strong&gt;WebSocket API&lt;/strong&gt; makes it possible to open a two-way interactive communication session between the user's browser and a server. With this API, you can send messages to a server and receive responses without having to poll the server for a reply&lt;/p&gt;

&lt;p&gt;The WebSocket protocol is the underlying communication standard, while the WebSocket API is the interface that developers use in client-side applications (like web browsers) to leverage the capabilities of that protocol.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The client and the server can communicate without the handshake.&lt;/li&gt;
&lt;li&gt;The communication needs to started by the client generally.&lt;/li&gt;
&lt;li&gt;WebSocket servers are used for real time communication (provide full duplex communication).&lt;/li&gt;
&lt;li&gt;It is persistent in nature, once connection made, it remains.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Sockets are -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In-build&lt;/li&gt;
&lt;li&gt;A library called Socket.IO&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  SOCKET.IO
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It is build on WebSocket only.&lt;/li&gt;
&lt;li&gt;Socket.IO is a library that enables &lt;strong&gt;low-latency&lt;/strong&gt;, &lt;strong&gt;bidirectional&lt;/strong&gt; and &lt;strong&gt;event-based&lt;/strong&gt; communication between a client and a server.&lt;/li&gt;
&lt;li&gt;In this http also works along with the sockets functionality.&lt;/li&gt;
&lt;li&gt;Socket.IO internally uses sockets creating WebSocket connections and using eventListeners for its working. That is-
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Create WebSocket connection.&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;socket&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;WebSocket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ws://localhost:8080&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Connection opened&lt;/span&gt;
&lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;open&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;event&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="nx"&gt;socket&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello Server!&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="c1"&gt;// Listen for messages&lt;/span&gt;
&lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;message&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;event&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="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="s2"&gt;Message from server &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&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;
  
  
  Socket.IO Working-
&lt;/h2&gt;

&lt;p&gt;Client and server connects through a pipeline, not requiring handshake, where server can send data to the client and client can send data to server as well.&lt;/p&gt;

&lt;p&gt;Hence, not requiring &lt;code&gt;get&lt;/code&gt; or &lt;code&gt;post&lt;/code&gt; request for communication&lt;/p&gt;

&lt;p&gt;And that very pipeline is called  &lt;strong&gt;&lt;em&gt;SOCKET&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each &lt;code&gt;socket&lt;/code&gt; has an &lt;code&gt;id&lt;/code&gt; to refer to the client connected to the server.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;On the server side,&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;server manages 1) request response cycle and 2) sockets as well.&lt;/p&gt;

&lt;p&gt;To use sockets on server side-&lt;br&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;socket.io
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The socket(pipeline) is used on the client and the server side with the help of the &lt;code&gt;object&lt;/code&gt; of socket having their &lt;code&gt;id&lt;/code&gt; &lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;&lt;em&gt;IO&lt;/em&gt;&lt;/strong&gt; -The group of sockets on server side is called &lt;code&gt;io&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;Hence , when message is send to the &lt;code&gt;io&lt;/code&gt; , all the connected cllients receives the message with different id , called BROADCASTING.&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%2F5usx6k41btm32c3vr0a9.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%2F5usx6k41btm32c3vr0a9.png" alt="system" width="800" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE-&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When we use sockets, we cannot start express server i.e. &lt;code&gt;app.listen()&lt;/code&gt; as it creates a new HTTP server&lt;/p&gt;

&lt;h3&gt;
  
  
  INITIALIZING SERVER (backend)-
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&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="s2"&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createServer&lt;/span&gt; &lt;span class="p"&gt;}&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="s2"&gt;http&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;//require http&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Server&lt;/span&gt; &lt;span class="p"&gt;}&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="s2"&gt;socket.io&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;   &lt;span class="c1"&gt;//require server from socket.io&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="nf"&gt;express&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;httpServer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createServer&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="c1"&gt;//http gets a refrence of the app&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;io&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;Server&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;httpServer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* options */&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;io&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;connection&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;socket&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="c1"&gt;// this function works when client is connected to the server&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;httpServer&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;-Whenever a client connects to the server , it triggers the event &lt;code&gt;connection&lt;/code&gt; , hence using-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;io&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;connection&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,(&lt;/span&gt;&lt;span class="nx"&gt;socket&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="c1"&gt;//we get an object of socket&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="s2"&gt;client connected&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;
  
  
  USE SOCKET.IO ON FRONTEND-
&lt;/h3&gt;

&lt;p&gt;By default, the Socket.IO server exposes a client bundle at &lt;code&gt;/socket.io/socket.io.js&lt;/code&gt; when the server is started.&lt;/p&gt;

&lt;p&gt;STEPS-&lt;/p&gt;

&lt;p&gt;1- Create a &lt;code&gt;public&lt;/code&gt; folder and make &lt;code&gt;index.html&lt;/code&gt; inside it.&lt;br&gt;
2- Send the file on the server-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;path&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;path&lt;/span&gt;&lt;span class="dl"&gt;'&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;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;static&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;public&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;p&gt;3- The first initiation is to de done by the client by adding the bundle to the &lt;code&gt;index.html&lt;/code&gt; file in the &lt;code&gt;script&lt;/code&gt; tag by-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/socket.io/socket.io.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;4- &lt;code&gt;io&lt;/code&gt; named function now will be available to the browser&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%2Fupa2cmxtky0akcxqogoo.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%2Fupa2cmxtky0akcxqogoo.png" alt="browser" width="533" height="414"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;5- &lt;strong&gt;To connect the client to server&lt;/strong&gt;- &lt;code&gt;io()&lt;/code&gt; function needs to be called by the browser, meaning a pipeline is established between the client and the server &lt;br&gt;
6- To do the above step we made &lt;code&gt;js&lt;/code&gt; file in the &lt;code&gt;public&lt;/code&gt; folder for the frontend.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;//in script.js file in the public folder(frontend)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;io&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;socket&lt;/code&gt; can be accessed from the backend and the frontend as well, they would have same &lt;code&gt;socket.id&lt;/code&gt; hence establishing the pipeline.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sockets Work through Event Triggering from both ends-
&lt;/h3&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%2Frz17qs3npe2g3viahbjb.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%2Frz17qs3npe2g3viahbjb.png" alt="doc" width="800" height="289"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;(source- documentation of socket.io)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;emit&lt;/code&gt; function is used to send the message with the name of the event&lt;/p&gt;

&lt;p&gt;&lt;code&gt;on&lt;/code&gt; function is used to receive (or capturing the event) the message by matching the event name&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;All the event trigger written in backend are coded inside the &lt;code&gt;io.on()&lt;/code&gt; function only where we made &lt;code&gt;"connection"&lt;/code&gt; .&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  CREATE CHAT APPLICATION-
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;ON FRONTEND- create an input and send message button-&lt;/li&gt;
&lt;/ul&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%2Fua5gcrbq391wrexa9a85.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%2Fua5gcrbq391wrexa9a85.png" alt="frontend" width="415" height="148"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In &lt;code&gt;script.js&lt;/code&gt; file of frontend send this input message to the server when button is clicked-
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;btn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,(&lt;/span&gt;&lt;span class="nx"&gt;ev&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="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;chat&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,{&lt;/span&gt;
        &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="c1"&gt;//here msg is the id of the input&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;ul&gt;
&lt;li&gt;To receive this message on the server-
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;io&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;connection&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;socket&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="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="s2"&gt;client connected&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;socket&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="c1"&gt;//   all the event triggering is written in the io.on() function&lt;/span&gt;

    &lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;chat&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,(&lt;/span&gt;&lt;span class="nx"&gt;chatdata&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="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="nx"&gt;chatdata&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&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;ul&gt;
&lt;li&gt;To send this message on all the clients including from where it was received we use-&lt;code&gt;io.emit()&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;io&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;grpchat&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;chatdata&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and the frontend can receive the message using &lt;code&gt;socket.on()&lt;/code&gt; and match the event name and display on the viewport using &lt;code&gt;ul&lt;/code&gt; and &lt;code&gt;li&lt;/code&gt; -&lt;code&gt;DOM&lt;/code&gt; concept.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To &lt;strong&gt;broadcast&lt;/strong&gt; this message to all the clients except from where it was send to the server, we use- &lt;code&gt;socket.broadcast.emit()&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;broadcast&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;grpchat&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;chatdata&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&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;OUPTUT FOR BROADCAST-&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjnx0qrie6nfruukc1gh1.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%2Fjnx0qrie6nfruukc1gh1.png" alt="chat" width="494" height="753"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To disconnect from client side, create a button that would fire the given command on click of it to disconnect the user and the server- &lt;code&gt;socket.disconnect();&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To view the full code, visit GitHub- &lt;a href="https://github.com/renamsingla/WebSockets" rel="noopener noreferrer"&gt;https://github.com/renamsingla/WebSockets&lt;/a&gt;&lt;/p&gt;

</description>
      <category>express</category>
      <category>webdev</category>
      <category>socket</category>
    </item>
    <item>
      <title>Session Management- Cookies, Session and JWT</title>
      <dc:creator>renam singla</dc:creator>
      <pubDate>Fri, 03 Oct 2025 17:48:14 +0000</pubDate>
      <link>https://dev.to/renam_singla_fb52a400f07e/session-management-cookies-session-and-jwt-554h</link>
      <guid>https://dev.to/renam_singla_fb52a400f07e/session-management-cookies-session-and-jwt-554h</guid>
      <description>&lt;p&gt;Since every request to a server is a new request, so for the server to identify whether the request  came from the same browser/ user, we use SESSION MANAGEMENT.&lt;/p&gt;

&lt;p&gt;In order to identify the user, we use the concept of &lt;strong&gt;&lt;em&gt;Cookies, Session, and JWT&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  COOKIES
&lt;/h2&gt;

&lt;p&gt;The cookies , is a way to identify whether the user is a new user or has visited the website before.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When a client sends a “sign in” request to the server using a form lets say, in response to that server generates the personalised or generic response .&lt;/li&gt;
&lt;li&gt;With the response, the server sends a “cookie”, an additional data to the client which stores information about the user which helps to distinguish the user.&lt;/li&gt;
&lt;li&gt;Next time , whenever the user makes a &lt;code&gt;get&lt;/code&gt; request to the server for that &lt;code&gt;PORT&lt;/code&gt; it will also give its &lt;code&gt;cookie&lt;/code&gt; with the &lt;code&gt;request&lt;/code&gt; to the server , which says, &lt;em&gt;i am the same user that signed in before , hence helps for identification of the authenticated client.&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Basically cookie is a medium through which a user can remain logged in as server has information of the user through cookie.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How to set a Cookie?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Cookie stores the user information key-value format, &lt;em&gt;the cookie can be set by&lt;/em&gt;-
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;//user, id and info is an object of cookie&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;cookie&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;123&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,{&lt;/span&gt;
                &lt;span class="na"&gt;httpOnly&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;//it allows access to the javascript&lt;/span&gt;
                &lt;span class="na"&gt;secure&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;//allow over HTTP&lt;/span&gt;
                &lt;span class="na"&gt;sameSite&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Strict&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="na"&gt;maxAfe&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;65480000&lt;/span&gt; &lt;span class="c1"&gt;//in sec, it gives the expire time of the cookie&lt;/span&gt;
                &lt;span class="p"&gt;});&lt;/span&gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;The another way to set a cookie is-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This is an object of user-data containing the information of the user
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userdata&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nx"&gt;username&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;123&lt;/span&gt;
                &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;This &lt;code&gt;userdata&lt;/code&gt;  object then further is &lt;code&gt;JSON.stringify&lt;/code&gt; to store is a cookie with the help of function &lt;code&gt;res.cookie()&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cookie&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="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userdata&lt;/span&gt;&lt;span class="p"&gt;),{&lt;/span&gt;
                &lt;span class="na"&gt;httpOnly&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;//it allows access to the javascript&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 object of javascript needs to be changed as a string in order to store it in cookie, as it is not a primitive data type.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;SO, LETS SEE HOW WILL COOKIES WORK IN CONTEXT OF AN APPLICATION!!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Start a Server -
&lt;/h3&gt;

&lt;p&gt;To start a server , we need to install &lt;code&gt;npm&lt;/code&gt; package and express and further use &lt;code&gt;app.listen()&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;For in detail understanding refer to the previous blog- &lt;a href="https://dev.to/renam_singla_fb52a400f07e/expressjs-for-beginners-get-post-serving-static-files-577d"&gt;https://dev.to/renam_singla_fb52a400f07e/expressjs-for-beginners-get-post-serving-static-files-577d&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  2.  Declaring the Functionality of the Application
&lt;/h3&gt;

&lt;p&gt;It is important to know what the application will do and understanding of its flow before building it. So let’s start with declaring our functionality, request and the working of the app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;//we need a log-in page, which will give a .html page&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;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;/login&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="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;//this will set the cookie, with the information send by the user&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;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;/login&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="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;//we need a profile page, which is accessible to only users having the cookie&lt;/span&gt;
&lt;span class="c1"&gt;//and help in their identification&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;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;/profile&lt;/span&gt;&lt;span class="dl"&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="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;//a log-out page which will delete the cookie&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;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;/logout&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="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;FLOW OF APPLICATION-&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwiw7yd7nei47lff63g4z.jpeg" 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%2Fwiw7yd7nei47lff63g4z.jpeg" alt="flow" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Create a "LOGIN" page -
&lt;/h3&gt;

&lt;p&gt;Through this page, client can &lt;code&gt;sign in&lt;/code&gt; to the application using &lt;code&gt;post&lt;/code&gt; request and with it  a cookie will be created and send to the browser by the server.&lt;/p&gt;

&lt;p&gt;To access this page on browser, we create a &lt;code&gt;GET&lt;/code&gt; REQUEST on the server , which will send this &lt;code&gt;file&lt;/code&gt; in response using &lt;code&gt;res.sendFile&lt;/code&gt;  and &lt;code&gt;path.join()&lt;/code&gt; function&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%2F67mp2qinfbpt50en7amu.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%2F67mp2qinfbpt50en7amu.png" alt="login" width="715" height="109"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, this &lt;code&gt;get&lt;/code&gt; request will give a &lt;code&gt;html&lt;/code&gt; file in the same directory.&lt;/p&gt;

&lt;p&gt;This &lt;code&gt;inhex.html&lt;/code&gt; page contains a &lt;code&gt;form&lt;/code&gt; tag, which lets the user “sign in” by giving the asked information .&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;COOKIES&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;form&lt;/span&gt; &lt;span class="na"&gt;action=&lt;/span&gt;&lt;span class="s"&gt;"/login"&lt;/span&gt; &lt;span class="na"&gt;method=&lt;/span&gt;&lt;span class="s"&gt;"POST"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"username"&lt;/span&gt; &lt;span class="na"&gt;placeholder=&lt;/span&gt;&lt;span class="s"&gt;"add username"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"submit"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;login&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Further, submitting the form, will send a &lt;code&gt;post&lt;/code&gt; request through browser, through which later a cookie will be set.&lt;/p&gt;

&lt;p&gt;So , our &lt;code&gt;login&lt;/code&gt; page will look like-&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%2Fl96bfnsnubtticnf4e48.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%2Fl96bfnsnubtticnf4e48.png" alt="outputcookie" width="800" height="420"&gt;&lt;/a&gt;&lt;br&gt;
But , if cookie already exists, then user must be redirected to the profile-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;if &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;cookies&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="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&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;redirect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/profile&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;
  
  
  4. Set a Cookie-
&lt;/h3&gt;

&lt;p&gt;To set a cookie, we first need to install the cookie package from &lt;code&gt;npm&lt;/code&gt; , here we are going to use &lt;code&gt;cookie-parser&lt;/code&gt; package.&lt;br&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;cookie-parser
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then, require it in the &lt;code&gt;app.js&lt;/code&gt; file&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faq6tbk0samw9nacf732a.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%2Faq6tbk0samw9nacf732a.png" alt="set cookie" width="791" height="211"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As discussed initially , to set a cookie, we use &lt;code&gt;res.cookie()&lt;/code&gt; function.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now, we will first require the “username” send by the user by the form.&lt;/li&gt;
&lt;li&gt;Then, create an object for cookie data using that username.&lt;/li&gt;
&lt;li&gt;Which will further help to set a cookie after changing it to a string&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;…when the &lt;code&gt;post&lt;/code&gt; request was made on the submission of the &lt;code&gt;form&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz0oqgpy54m69k62uk1ib.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%2Fz0oqgpy54m69k62uk1ib.png" alt="post" width="743" height="295"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And since the cookie is set now, the user can have access to &lt;code&gt;/profile&lt;/code&gt; URL path &lt;/p&gt;

&lt;h3&gt;
  
  
  5. Access to “/profile” and Working of it-
&lt;/h3&gt;

&lt;p&gt;Firstly for any user to access &lt;code&gt;'profile&lt;/code&gt; path , it is mandatory to have a cookie, which means they have logged in , otherwise the user should be redirected to the &lt;code&gt;/login&lt;/code&gt; path&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;req.cookies&lt;/code&gt; : command helps to read the cookie, by the name- user
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&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;/profile&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="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&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;cookies&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;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;redirect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/login&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;If cookie exists-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We can have the username on the profile with number of times the user visited/ refreshed the profile url path&lt;/li&gt;
&lt;li&gt;For &lt;code&gt;js&lt;/code&gt; to require the username from the cookie, we need to &lt;code&gt;parse&lt;/code&gt; the cookie&lt;/li&gt;
&lt;li&gt;With the above response in &lt;code&gt;res.send()&lt;/code&gt;, we will also send a &lt;code&gt;logout&lt;/code&gt; button (having &lt;code&gt;/logout&lt;/code&gt; path ) if user wants to log out from the application&lt;/li&gt;
&lt;/ul&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%2Fu7evvh2p1asjztn6z2x8.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%2Fu7evvh2p1asjztn6z2x8.png" alt="profile" width="743" height="467"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;OUTPUT-&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%2F89xepn68bf94rbrb61pn.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%2F89xepn68bf94rbrb61pn.png" alt="cookie output" width="800" height="431"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  6. Logout from the Application-
&lt;/h3&gt;

&lt;p&gt;When clicked on the &lt;code&gt;logout&lt;/code&gt; BUTTON, the &lt;code&gt;'/logout'&lt;/code&gt; path will be requested due to &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; tag.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Where we simply empty the cookie string.&lt;/li&gt;
&lt;li&gt;Cookie can not be deleted as once set, it cannot be modified from server side, as it is stored by the browser&lt;/li&gt;
&lt;li&gt;And since the cookie is empty, the profile page won’t be accessed in future as it requires &lt;code&gt;username&lt;/code&gt; and further redirect to the &lt;code&gt;/login&lt;/code&gt; .&lt;/li&gt;
&lt;/ul&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%2Fu3x8iy6uyjsblfdj51y5.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%2Fu3x8iy6uyjsblfdj51y5.png" alt="logout" width="604" height="76"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With the cookies, there is an issue that we have not addressed yet, that they can be manipulated y anyone and hence the user can be changed without any restriction.&lt;/p&gt;

&lt;p&gt;To stop this, we use &lt;strong&gt;SESSION.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  SESSION
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The session has a “session map” which will store the object of the session-data, and it will send an “id” in respond which will be stored in cookie rather than the data.&lt;/li&gt;
&lt;li&gt;Therefore, no one will have access to the authentication data and it cannot be manipulated anymore.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;session-id&lt;/code&gt; will be generated automatically&lt;/li&gt;
&lt;li&gt;Hence the session data is stored in the server side and session-Id is only accessible by the cookie.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;NOTE- session can be stored in the database as well&lt;/em&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  1. How to use express-session?
&lt;/h3&gt;

&lt;p&gt;Firstly install &lt;code&gt;express-session&lt;/code&gt;  npm package, in the terminal-&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 express-session
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then require it in the &lt;code&gt;js&lt;/code&gt; file-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;session&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-session&lt;/span&gt;&lt;span class="dl"&gt;'&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;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;session&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;secret&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;notknownbytheuser&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;//for id generation, secret is required&lt;/span&gt;
    &lt;span class="na"&gt;resave&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;//do not change the data&lt;/span&gt;
    &lt;span class="na"&gt;saveUninitialized&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;  
    &lt;span class="c1"&gt;//irrespective of requirement everyone will get a session ID&lt;/span&gt;
&lt;span class="p"&gt;}))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Secret should not be shared with anyone&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;saveUninitialized&lt;/code&gt; will create an id even before the user has login as default&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;express-session&lt;/code&gt; do not need to use parser, it manages it by itself&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Set a session-
&lt;/h3&gt;

&lt;p&gt;On our &lt;code&gt;login&lt;/code&gt; page, to set a session-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The user provides a username and the object of that is then required by the session using the &lt;code&gt;POST&lt;/code&gt; request.&lt;/li&gt;
&lt;li&gt;To set the session and to read as well we use- &lt;code&gt;req.session.user&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;And in return it creates an unique id by itself
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&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;/login&lt;/span&gt;&lt;span class="dl"&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="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;}&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;userData&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// setting the session- putting userData in the session&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;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;userData&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;redirect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/profile&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;p&gt;The Output in the browser,  will only show the “id”&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%2Fefdq86g1qdh25si01ucu.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%2Fefdq86g1qdh25si01ucu.png" alt="session" width="800" height="384"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For the profile &lt;code&gt;get&lt;/code&gt; request-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&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;/profile&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="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&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;session&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="k"&gt;return&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;redirect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/login&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;session&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;count&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;userData&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;session&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;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;`welcome &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;userData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; with the count: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;userData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;
        &amp;lt;br&amp;gt; &amp;lt;br&amp;gt;
        &amp;lt;a href='/logout'&amp;gt;
            &amp;lt;button&amp;gt;logout&amp;lt;/button&amp;gt;
        &amp;lt;/a&amp;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;
  
  
  3. Delete a session-
&lt;/h3&gt;

&lt;p&gt;When clicked on the “logout” button, &lt;code&gt;/logout&lt;/code&gt; path is requested-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&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;/logout&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;destroy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&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;redirect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/login&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;To delete, we use &lt;code&gt;req.session.destroy(function(error))&lt;/code&gt; function which is an error first callback function. &lt;/p&gt;

&lt;p&gt;If there is no error it will simply does perform the function otherwise will give an error &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;LASTLY,&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;if the server is refreshed, session map will loose its data, hence it is better to store sessions in a database.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;JWT- JSON WEB TOKEN&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;JWT comprises of three things-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Header: type and algorithm&lt;/li&gt;
&lt;li&gt;Payload: user information&lt;/li&gt;
&lt;li&gt;Signature: having header, payload and secret&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We also need JWT secret.&lt;/p&gt;

&lt;p&gt;JWT does not go with each request by itself like cookie, it needs to be send with the request.&lt;/p&gt;

&lt;p&gt;Here, we will also be requiring cookie to store “token”, hence install that by referring to the above blog.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Install JWT-
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;jwt&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;jsonwebtoken&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;p&gt;JWT generates a “token” when provided the payload and the JWT secret&lt;/p&gt;

&lt;p&gt;Therefore, we have to create a JWT secret as well-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;JWT_SECRET&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;decneicnjvbjrgvd&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;h3&gt;
  
  
  2. Generate a JWT -
&lt;/h3&gt;

&lt;p&gt;In the &lt;code&gt;post&lt;/code&gt; request of the form  when submitted the form, we first create a token by-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;token&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;jwt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sign&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userData&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;JWT_SECRET&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then this token is stored in the cookie using &lt;code&gt;CookieParser&lt;/code&gt; as we did before&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cookie&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;token&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;token&lt;/span&gt;&lt;span class="p"&gt;,{&lt;/span&gt; 
        &lt;span class="na"&gt;httpOnly&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the &lt;code&gt;POST&lt;/code&gt; request for &lt;code&gt;/login&lt;/code&gt; looks like-&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%2Fn1pqolnd96x206vnkboo.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%2Fn1pqolnd96x206vnkboo.png" alt="post login" width="798" height="285"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Verification of the token -
&lt;/h3&gt;

&lt;p&gt;Before giving access to the profile, it is important to verify that the token exists and if exists , does it matches the user.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;first we require the token from the cookie, if it exists then we proceed&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;userData&lt;/code&gt; then is verified using the “token” and the “secret key”&lt;/li&gt;
&lt;li&gt;If it does not matches , the request will be redirected to the  &lt;code&gt;/login&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;This is done using either error first callback function or using try-catch syntax&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, after the token is verified, we proceed to show the profile to the client.&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%2F1ybm6wd1fzazykhu2rjo.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%2F1ybm6wd1fzazykhu2rjo.png" alt="profile jwt" width="676" height="496"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;OUTPUT ON THE BROWSER AND THE TOKEN IN THE COOKIE-&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%2Fas71x2s6x8irbxs16qqv.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%2Fas71x2s6x8irbxs16qqv.png" alt="output jwt" width="800" height="468"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Logout -
&lt;/h3&gt;

&lt;p&gt;To logout, we simply empty the cookie, which establishes that the token does not exists&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&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;/logout&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cookie&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;token&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="p"&gt;,{&lt;/span&gt;
        &lt;span class="na"&gt;httpOnly&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="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;redirect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/login&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;p&gt;And then , at &lt;code&gt;/login&lt;/code&gt; get request , we first check if the token exists or not.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;if the token exists the client is redirected to the profile page only-&lt;/li&gt;
&lt;/ul&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%2Fe3m6lzwbrd72qvfthw4g.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%2Fe3m6lzwbrd72qvfthw4g.png" alt="get login" width="526" height="421"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With this the working of JWT is completed!!&lt;/p&gt;

&lt;p&gt;Anyone interested to view the whole code can check out the GitHub link-&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/renamsingla/Session_Management" rel="noopener noreferrer"&gt;https://github.com/renamsingla/Session_Management&lt;/a&gt;&lt;/p&gt;

</description>
      <category>express</category>
      <category>cookie</category>
      <category>webdev</category>
      <category>jwt</category>
    </item>
    <item>
      <title>Full-Stack TodoApp: Node.js, Express, MongoDB, and HBS Guide</title>
      <dc:creator>renam singla</dc:creator>
      <pubDate>Tue, 23 Sep 2025 16:41:04 +0000</pubDate>
      <link>https://dev.to/renam_singla_fb52a400f07e/full-stack-todoapp-nodejs-express-mongodb-and-ejs-guide-5dj2</link>
      <guid>https://dev.to/renam_singla_fb52a400f07e/full-stack-todoapp-nodejs-express-mongodb-and-ejs-guide-5dj2</guid>
      <description>&lt;p&gt;A Todo Application is the easiest web based application that usually each web developer starts their journey with, yet most of the candidates struggle with it.&lt;/p&gt;

&lt;p&gt;This simplest application holds many important topics and concept that one needs to understand before diving in the complexity of developing web application for masses.&lt;/p&gt;

&lt;p&gt;Through this application we are going to focus on concepts like-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Mongoose (MongoDB)- for storing the data&lt;/li&gt;
&lt;li&gt;Handlers- for server side rendering&lt;/li&gt;
&lt;li&gt;module.exports concept&lt;/li&gt;
&lt;li&gt;Basic CRUD operation functionality to make the app working&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;Before starting with coding part, it is important to know the working and features of the app-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Adding a todo to the todo app&lt;/li&gt;
&lt;li&gt;Getting all the todos on the browser&lt;/li&gt;
&lt;li&gt;Deleting a todo from the todos&lt;/li&gt;
&lt;li&gt;Getting one todo on the browser&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The application needs to have a database to store the todos we will add to our application, for that we will use &lt;strong&gt;MongoDB- *mongoose&lt;/strong&gt;*&lt;/p&gt;

&lt;h3&gt;
  
  
  MongoDB
&lt;/h3&gt;

&lt;p&gt;MongoDB is a NOSQL database product for storing data as a BSON, a JSON like-format.&lt;/p&gt;

&lt;p&gt;To store data it is important to have in our system, one can download it from its official side according to their system compatibility.&lt;/p&gt;

&lt;p&gt;Next,&lt;/p&gt;

&lt;p&gt;Before we are able to store the data, it is important to have the mongodb server setup&lt;/p&gt;

&lt;p&gt;It requires us to create a folder in our project directory, lets name it &lt;code&gt;data&lt;/code&gt;  and then in terminal write-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;mongod&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;dbpath&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After running this command, once check if mongodb is running or not-&lt;/p&gt;

&lt;p&gt;Go to the browser and give url- &lt;a href="http://localhost:27017" rel="noopener noreferrer"&gt;&lt;code&gt;http://localhost:27017&lt;/code&gt;&lt;/a&gt; ,it should show-&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%2Fyqb3majdl2yw6ply4w88.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%2Fyqb3majdl2yw6ply4w88.png" alt="mongodb" width="708" height="140"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  MONGOOSE
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Mongoose&lt;/strong&gt; is a JavaScript object-oriented programming library that creates a connection between MongoDB and the Node.js JavaScript runtime environment&lt;/p&gt;

&lt;p&gt;Basically, it is an ODM- Object Data Modelling, in which all things are stored as an object.&lt;/p&gt;

&lt;p&gt;To start the server, In terminal-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;npm&lt;/span&gt; &lt;span class="nx"&gt;init&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;
&lt;span class="nx"&gt;npm&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; 
&lt;span class="nx"&gt;npm&lt;/span&gt; &lt;span class="nx"&gt;install&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make a javascript file in the directory&lt;/p&gt;

&lt;p&gt;So now, we need to create a connection between Mongodb and our javascript file&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9kalunj3o48sqa6dl999.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%2F9kalunj3o48sqa6dl999.png" alt="server" width="708" height="388"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To use mongoose, we need models(collections) , which are made with the help of schema&lt;/p&gt;

&lt;p&gt;Everything in Mongoose starts with a Schema. Each schema maps to a MongoDB collection and defines the shape of the documents within that collection.&lt;/p&gt;

&lt;p&gt;In our application we are going to create a schema for a Todo which will have-&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;taskName&lt;/li&gt;
&lt;li&gt;Description&lt;/li&gt;
&lt;li&gt;Id (it is automatically generated by the MongoDB , hence no need to specify)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It is usually better to segregate different functionality of the project , hence we are going to make schemas in new javascript file which is stored in a folder by the name &lt;code&gt;model&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv6swoihjvbzpnvdphm3c.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%2Fv6swoihjvbzpnvdphm3c.png" alt="mongoose" width="708" height="392"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;here , in schema object we declared an object of data in form of key:value pair where each key having an object with its type and  true for required, which means that it is mandatory to give the data , otherwise it will give error and &lt;/p&gt;

&lt;p&gt;Now at the end , you must have thought what does &lt;strong&gt;&lt;em&gt;module.exports&lt;/em&gt;&lt;/strong&gt; mean?&lt;/p&gt;

&lt;h3&gt;
  
  
  module.exports
&lt;/h3&gt;

&lt;p&gt;In Node.js, &lt;code&gt;module.exports&lt;/code&gt; is a special object available within every JavaScript file,&lt;/p&gt;

&lt;p&gt;it allows you to expose specific functionalities (like functions, objects, classes, or primitive values) from a module, making them accessible to other modules in your application.&lt;/p&gt;

&lt;p&gt;Hence we can use the &lt;code&gt;Todos.js&lt;/code&gt;  file and &lt;code&gt;mongoose.model&lt;/code&gt; in any other javascript file where it is required.&lt;/p&gt;

&lt;p&gt;As mentioned earlier, it is better to use different files for handling different work just like we do with functions.&lt;/p&gt;

&lt;p&gt;therefore, in order to separate functionality of each CRUD operation from the routing (working) of it , we make a separate javascript file - &lt;code&gt;todoFun.js&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;Now, here , we are going to &lt;code&gt;require&lt;/code&gt; the &lt;code&gt;Todos.js&lt;/code&gt; file that we exported&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%2Fsngw82vjy94v4w9s5ea8.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%2Fsngw82vjy94v4w9s5ea8.png" alt="require" width="708" height="76"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We are almost set to work on our application, but one thing that we are yet to handle is what we will show on the browser???&lt;/p&gt;

&lt;p&gt;For that we will be using &lt;strong&gt;handlebars- hbs&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;HANDLEBARS- hbs&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;While using serving static file, the data is send to the browser in the chunky manner (i.e. little data is send according to the need) with the help of &lt;code&gt;app.use(express.static())&lt;/code&gt; middleware, but it is not efficient to write backend code&lt;/p&gt;

&lt;p&gt;Hence , we use SERVER SIDE RENDERING - sending the whole file at once with the help of handlebars&lt;/p&gt;

&lt;p&gt;It helps to throw the &lt;code&gt;index&lt;/code&gt;  file at once and it is used as &lt;code&gt;.hbs&lt;/code&gt; file as with this, HTML file is just not only made on backend but we can use javascript variables as well in the file&lt;/p&gt;

&lt;p&gt;For this , install hbs&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;npm&lt;/span&gt; &lt;span class="nx"&gt;install&lt;/span&gt; &lt;span class="nx"&gt;hbs&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;WE can access &lt;code&gt;.hbs&lt;/code&gt; in js file with the help of &lt;code&gt;res.render&lt;/code&gt; , it is a response where client gets &lt;code&gt;index.hbs&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;index&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;p&gt;In order to use handlebars in our javascript file, we need to set it by-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;view engine&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;hbs&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;p&gt;Now, in &lt;code&gt;package.json&lt;/code&gt; file, dependencies will look like-&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%2F5u0l1mwligwznccjd9nm.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%2F5u0l1mwligwznccjd9nm.png" alt="dependencies" width="187" height="122"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, create a folder in the project folder by the name &lt;code&gt;views&lt;/code&gt; and it will hold our all &lt;code&gt;.hbs&lt;/code&gt; files&lt;/p&gt;

&lt;p&gt;Now, make &lt;code&gt;index.hbs&lt;/code&gt; file which will be send to the client and an &lt;code&gt;error.hbs&lt;/code&gt; file if there is something wrong on the backend&lt;/p&gt;

&lt;p&gt;At the same time, make a &lt;code&gt;static&lt;/code&gt; folder to have &lt;code&gt;style.css&lt;/code&gt; file &lt;/p&gt;

&lt;p&gt;And, in order to use all of the files, we must require them-&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%2F9v82k738u4lpi3u9xxao.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%2F9v82k738u4lpi3u9xxao.png" alt="hbs" width="777" height="105"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now your project set up will look something like this-&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5qok2z50fr3bw7jkfrw5.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%2F5qok2z50fr3bw7jkfrw5.png" alt="project" width="224" height="364"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  FRONTEND OF THE APPLICATION
&lt;/h3&gt;

&lt;p&gt;One can create their application with as much creativity as they like, here, we are going to keep it simple to focus more on the functionality&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the &lt;code&gt;index.hbs&lt;/code&gt; file will will create a simple template for the app, using &lt;code&gt;&amp;lt;form&amp;gt;&lt;/code&gt; tag which will help us later to add new todos and &lt;code&gt;&amp;lt;ul&amp;gt;&lt;/code&gt; tag to show the created todos on the browser.&lt;/li&gt;
&lt;li&gt;Here the &lt;code&gt;&amp;lt;form&amp;gt;&lt;/code&gt; tag has &lt;code&gt;&amp;lt;input&amp;gt;&lt;/code&gt; that will help us to add the task name and the description as an input on frontend&lt;/li&gt;
&lt;li&gt;The submit  &lt;code&gt;&amp;lt;button&amp;gt;&lt;/code&gt; will add the todo to our database by the &lt;code&gt;action&lt;/code&gt; attribute in the form routing to the given url path.&lt;/li&gt;
&lt;/ul&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%2Fn7tm4z54li23dblt8ekk.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%2Fn7tm4z54li23dblt8ekk.png" alt="frontend" width="800" height="444"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With the above code of templating and styling, our webpage looks like this- &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fglzr0t33bqflvp28m6oa.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%2Fglzr0t33bqflvp28m6oa.png" alt="app" width="800" height="255"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But the application is a Static application without being able to do any functionality&lt;/p&gt;

&lt;p&gt;So, here we start with application CRUD operation functionality making it usable&lt;/p&gt;

&lt;h3&gt;
  
  
  BACKEND OF THE APPLICATION
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Adding a todo to the todo app-
&lt;/h3&gt;

&lt;p&gt;WHEN WE PRESS “ADD TODO” on our webpage after adding task name and its description what happens is-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To add any kind of data, we use &lt;code&gt;POST&lt;/code&gt; request&lt;/li&gt;
&lt;li&gt;We will add data with the help of form that we made in &lt;code&gt;index.hbs&lt;/code&gt; file, which had &lt;code&gt;action&lt;/code&gt;  that helps us to get a path of post request&lt;/li&gt;
&lt;li&gt;This path will have a POST request written in our &lt;code&gt;app.js&lt;/code&gt; file&lt;/li&gt;
&lt;li&gt;For post request in &lt;code&gt;app.js&lt;/code&gt; we call for &lt;code&gt;todoFun.js&lt;/code&gt; asynchronously for &lt;code&gt;addTodo&lt;/code&gt; function&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;addTodo&lt;/code&gt; will perform the function of adding a todo in our &lt;code&gt;Todos&lt;/code&gt; database and further return the message if successfully conducted.&lt;/li&gt;
&lt;li&gt;Now, in &lt;code&gt;app.js&lt;/code&gt; and performing the async function, the request is redirected to the base url.&lt;/li&gt;
&lt;li&gt;Lastly, the newTodo will be added to our database that we can see with the help of “MongoDB Compass”&lt;/li&gt;
&lt;/ul&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%2Fccsf2w6hvbvvl2qphqcp.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%2Fccsf2w6hvbvvl2qphqcp.png" alt="add" width="800" height="230"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we can see, we are directed to base path, so what will it have, can we see the added todo?&lt;/p&gt;

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

&lt;p&gt;As we have written a functionality to get all the todos that has been added explained below.&lt;/p&gt;

&lt;h3&gt;
  
  
  Getting all the todos on the browser-
&lt;/h3&gt;

&lt;p&gt;To get all the todos , we simply needs to get all the todos in our database and then send it to the &lt;code&gt;index.hbs&lt;/code&gt; page to help us show on the page with the help of the list&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When a &lt;code&gt;GET&lt;/code&gt; request is made on the base url (’/’), the &lt;code&gt;getTodo()&lt;/code&gt; function is called in &lt;code&gt;todoFun.js&lt;/code&gt; file  asynchronously.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;getTodo()&lt;/code&gt; function will &lt;code&gt;find()&lt;/code&gt; all the todos from database using mongoose ‘find’ function and later return it to the get request where it was called from.&lt;/li&gt;
&lt;li&gt;Then the todos that we received will be send to &lt;code&gt;index.hbs&lt;/code&gt; file as an object using &lt;code&gt;res.render&lt;/code&gt; request.&lt;/li&gt;
&lt;/ul&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%2Fh29bi7nxb1t8wq79kzy1.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%2Fh29bi7nxb1t8wq79kzy1.png" alt="get" width="800" height="144"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;index.hbs&lt;/code&gt; file, which just not shows a list but let us use javascript variables and working like “for each loops” and “if conditions” with the help of its “moustache” syntax.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We iterate through each todo as &lt;code&gt;t&lt;/code&gt; and if &lt;code&gt;t.taskName&lt;/code&gt; exists , each todo task-name and description will be added to the &lt;code&gt;li&lt;/code&gt; of list in a &lt;code&gt;span&lt;/code&gt; tag&lt;/li&gt;
&lt;/ul&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%2Fmkt9ckljskawy0knrvzz.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%2Fmkt9ckljskawy0knrvzz.png" alt="index" width="490" height="359"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On client side, this would look like-&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%2Fvxlnb2kq850gbm8ki210.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%2Fvxlnb2kq850gbm8ki210.png" alt="application" width="800" height="252"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One can apply &lt;code&gt;css&lt;/code&gt; as they would like to have their content styled on the webpage&lt;/p&gt;

&lt;p&gt;You must be thinking what does this delete button do? Or will we be able to actually delete?&lt;/p&gt;

&lt;h3&gt;
  
  
  Deleting a todo from the todos-
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;When we click on the delete button, the &lt;code&gt;get&lt;/code&gt; request would be directed to the “delete path”&lt;/li&gt;
&lt;li&gt;With the help of &lt;code&gt;req.params&lt;/code&gt; we de-structure the &lt;code&gt;id&lt;/code&gt; which is unique for each todo&lt;/li&gt;
&lt;li&gt;Then we call the &lt;code&gt;delTodo(id)&lt;/code&gt; function from &lt;code&gt;todoFun.js&lt;/code&gt; file .&lt;/li&gt;
&lt;li&gt;This function will delete the todo by using &lt;code&gt;Todos.deleteOne()&lt;/code&gt; mongoose function by matching the id .&lt;/li&gt;
&lt;li&gt;And later the request is directed to the base url path.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;with the help of anchor tag &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; and we will see all the content except that todo that was deleted.&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%2Fqonwrqmoy9xl92s1qxas.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%2Fqonwrqmoy9xl92s1qxas.png" alt="delete" width="800" height="212"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Getting one todo on the browser-
&lt;/h3&gt;

&lt;p&gt;Now, to get just one todo from all the todos,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;when we click on the todo of our choice, it will redirect us to a new page having that todo only&lt;/li&gt;
&lt;li&gt;This is possible as every todo has unique id provided by the MongoDB, which helps server to identify the todo client need&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;what we are doing is-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When clicked on the todo (as all todos are stored in anchor tag, they have a &lt;code&gt;href&lt;/code&gt; path), it will direct the &lt;code&gt;get&lt;/code&gt; request at the &lt;code&gt;/todo/:id&lt;/code&gt; path.&lt;/li&gt;
&lt;li&gt;Here we will take the id of that one todo using &lt;code&gt;req.params&lt;/code&gt; and further call the &lt;code&gt;getTodo&lt;/code&gt; function from &lt;code&gt;todoFun&lt;/code&gt; file&lt;/li&gt;
&lt;li&gt;In that function, it will asynchronously( using async await syntax) will find that one todo by &lt;code&gt;Todos.findOne()&lt;/code&gt; mongoose function in the database having same &lt;code&gt;id&lt;/code&gt; and return it.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;GET&lt;/code&gt; request then &lt;code&gt;render&lt;/code&gt; to the &lt;code&gt;index&lt;/code&gt; file with that one todo in the object&lt;/li&gt;
&lt;/ul&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%2Fpcvq588atq2cyr7f95cj.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%2Fpcvq588atq2cyr7f95cj.png" alt="getone" width="800" height="203"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;OUTPUT-&lt;/p&gt;

&lt;p&gt;Here, we can notice the url path has changed , hence we are redirected to the new page containing only that one todo.&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%2Fgd58ysodtm3phpxzar18.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%2Fgd58ysodtm3phpxzar18.png" alt="one todo" width="800" height="235"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Hence, our Todo Application is ready with a permanent database helping us to perform basic crud operation so we can keep a check on our todo list.&lt;/p&gt;

&lt;p&gt;Hopefully , this was helpful ,if you want to view the whole code -&lt;/p&gt;

&lt;p&gt;Here is the GitHub link-  &lt;a href="https://github.com/renamsingla/TodoApp" rel="noopener noreferrer"&gt;https://github.com/renamsingla/TodoApp&lt;/a&gt;&lt;/p&gt;

</description>
      <category>express</category>
      <category>mongodb</category>
      <category>webdev</category>
      <category>fullstack</category>
    </item>
    <item>
      <title>Express.js for Beginners: GET, POST &amp; Serving Static Files</title>
      <dc:creator>renam singla</dc:creator>
      <pubDate>Thu, 18 Sep 2025 07:18:56 +0000</pubDate>
      <link>https://dev.to/renam_singla_fb52a400f07e/expressjs-for-beginners-get-post-serving-static-files-577d</link>
      <guid>https://dev.to/renam_singla_fb52a400f07e/expressjs-for-beginners-get-post-serving-static-files-577d</guid>
      <description>&lt;p&gt;When building web apps with Node.js, handling requests and sending responses manually can get messy. That’s where &lt;strong&gt;Express.js&lt;/strong&gt; comes in — a lightweight framework that makes it simple to define routes, handle GET and POST requests, and even serve static files like HTML, CSS, and JavaScript.&lt;/p&gt;

&lt;p&gt;Express is a &lt;code&gt;node.js&lt;/code&gt; web  framework(un-opinionated).&lt;/p&gt;

&lt;p&gt;It is required:-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Write handlers for requests with different HTTP verbs at different URL paths (routes).&lt;/li&gt;
&lt;li&gt;For setting up servers for web applications&lt;/li&gt;
&lt;li&gt;To use middleware for tasks like parsing request bodies&lt;/li&gt;
&lt;li&gt;Serving static files efficiently&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In a traditional data-driven website, a web application waits for HTTP requests from the web browser (or other client). When a request is received the application works out what action is needed based on the URL pattern and possibly associated information contained in &lt;code&gt;POST&lt;/code&gt; data or &lt;code&gt;GET&lt;/code&gt; data. Depending on what is required it may then read or write information from a database or perform other tasks required to satisfy the request. The application will then return a response to the web browser, often dynamically creating an HTML page for the browser to display by inserting the retrieved data into placeholders in an HTML template.&lt;/p&gt;

&lt;h2&gt;
  
  
  CREATING A SERVER-
&lt;/h2&gt;

&lt;p&gt;in the terminal of the right directory&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm init &lt;span class="nt"&gt;--y&lt;/span&gt;
npm i express

//install nodemon&lt;span class="o"&gt;(&lt;/span&gt;just once&lt;span class="o"&gt;)&lt;/span&gt;
npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-g&lt;/span&gt; nodemon

//create a js file- app.js &lt;span class="k"&gt;in &lt;/span&gt;the same directory
//to start the server

nodemon app.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After installing, in order to create a server in our &lt;code&gt;app.js&lt;/code&gt; file, we require(import) express module and create an express application.&lt;/p&gt;

&lt;p&gt;This app starts a server and listen to the port- 4444 for connection.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&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;app&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&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;PORT&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4444&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;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="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="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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hello world&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;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="nx"&gt;PORT&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="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="s2"&gt;`http://localhost:`&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nx"&gt;PORT&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;
  
  
  ROUTE handlers-
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Routing&lt;/em&gt;&lt;/strong&gt; refers to how an application’s endpoints (URIs) respond to client requests. For an introduction to routing, see &lt;a href="https://expressjs.com/en/starter/basic-routing.html" rel="noopener noreferrer"&gt;Basic routing&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You define routing using methods of the Express &lt;code&gt;app&lt;/code&gt; object that correspond to HTTP methods; for example, &lt;code&gt;app.get()&lt;/code&gt; to handle GET requests and &lt;code&gt;app.post&lt;/code&gt; to handle POST requests. For a full list, see &lt;a href="https://expressjs.com/en/5x/api.html#app.METHOD" rel="noopener noreferrer"&gt;app.METHOD&lt;/a&gt;. You can also use &lt;a href="https://expressjs.com/en/5x/api.html#app.all" rel="noopener noreferrer"&gt;app.all()&lt;/a&gt; to handle all HTTP methods and &lt;a href="https://expressjs.com/en/5x/api.html#app.use" rel="noopener noreferrer"&gt;app.use()&lt;/a&gt; to specify middleware as the callback function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;METHOD&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;PATH&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;HANDLER&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;app&lt;/code&gt; is an instance of &lt;code&gt;express&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;METHOD&lt;/code&gt; is an &lt;a href="https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods" rel="noopener noreferrer"&gt;HTTP request method&lt;/a&gt;, in lowercase.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;PATH&lt;/code&gt; is a path on the server.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;HANDLER&lt;/code&gt; is the function executed when the route is matched.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  GET REQUEST (res.send, req.query, req.params)-
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;IT IS USED TO READ DATA&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The middle part of the code (the three lines starting with &lt;code&gt;app.get&lt;/code&gt;) shows a &lt;em&gt;route definition&lt;/em&gt;. The &lt;code&gt;app.get()&lt;/code&gt; method specifies a callback function that will be invoked whenever there is an HTTP &lt;code&gt;GET&lt;/code&gt; request with a path (&lt;code&gt;'/'&lt;/code&gt;) relative to the site root. The callback function takes a request and a response object as arguments, and calls &lt;a href="https://expressjs.com/en/5x/api.html#res.send" rel="noopener noreferrer"&gt;&lt;code&gt;send()&lt;/code&gt;&lt;/a&gt; on the response to return the string "Hello World!"&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;res.send-&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the app, we have written a &lt;code&gt;GET&lt;/code&gt; request- a GET request will read the data on the specified URL path (for ‘/’ means the base path) &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Here, res- response and req- request.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;res.send&lt;/code&gt; is the respond send by the server to the client when client go the url path mentioned&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;OUTPUT we got when we visited the url(&lt;code&gt;http://localhost:4444/&lt;/code&gt;) mentioned-&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%2Feh6lljf2z8pejr0z808r.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%2Feh6lljf2z8pejr0z808r.png" alt="send" width="800" height="116"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;req.query -&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;req.query&lt;/code&gt;  is the request made to the browser to send the data to the server.&lt;/li&gt;
&lt;li&gt;Data comes in the object of request(req).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;req.query&lt;/code&gt; is an object that stores data sent in the URL after a &lt;code&gt;?&lt;/code&gt;. Example: &lt;code&gt;/greet?name=John&lt;/code&gt; → &lt;code&gt;{ name: 'John' }&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;And then to access the url in browser we have to give &lt;code&gt;res.send&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&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;/greet&lt;/span&gt;&lt;span class="dl"&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="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="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;query&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="c1"&gt;//we have destructured the name from the url path using query parameter&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;`hii after requesting from browser to send
         name- "&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;" to the server`&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;OUTPUT after “getting” data from the url path using &lt;code&gt;req.query&lt;/code&gt; and showing it on the browser using &lt;code&gt;res.send&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fglo3v7c9b9i6l2vdqy6w.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%2Fglo3v7c9b9i6l2vdqy6w.png" alt="query" width="800" height="129"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;req.params-&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Now, the another way to get the data be requesting is- &lt;code&gt;req.params&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;it takes  “key/:value”  like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;//in browser url path key/value are written without ':' unlike mentioned &lt;/span&gt;
&lt;span class="c1"&gt;//in the get request&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;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;/movies/:movie&lt;/span&gt;&lt;span class="dl"&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="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;movie&lt;/span&gt;&lt;span class="p"&gt;}&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;params&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;`the data requested using req.params is -&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;movie&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;OUTPUT-&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv7g83io36n50n55f4aim.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%2Fv7g83io36n50n55f4aim.png" alt="output" width="800" height="129"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;GET REQUEST IMPLEMENTATION AFTER STARTING A SERVER USING EXPRESS.JS&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpvlhxc8830214pryaeij.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%2Fpvlhxc8830214pryaeij.png" alt="code" width="800" height="481"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  POST REQUEST (req.body,  middleware)-
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;IT IS USED TO GIVE SOME DATA (CREATE/ ADD)&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The data in &lt;code&gt;POST&lt;/code&gt; request is hidden from user as it not shown in the url.&lt;/li&gt;
&lt;li&gt;To use post request we used &lt;em&gt;POSTMAN, and the res.send is shown in postman only as it is an alternative for browser&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Data of post request is called “Payload”&lt;/li&gt;
&lt;li&gt;In backend the data of payload is seen in “body” in postman&lt;/li&gt;
&lt;li&gt;Whenever the server is restarted , we lost the added data (by post) hence we need databases.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&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;/add&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;cost&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="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&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;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="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;`new person- &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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;In the above code, we are using a &lt;code&gt;POST&lt;/code&gt; request, at &lt;code&gt;url&lt;/code&gt; ”localhost:4444/add” .If this url exists, its callback function is called.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;em&gt;MIDDLEWARE&lt;/em&gt;-
&lt;/h3&gt;

&lt;p&gt;Middleware that only parses urlencoded bodies and only looks at requests where the &lt;code&gt;Content-Type&lt;/code&gt; header matches the &lt;code&gt;type&lt;/code&gt; option. This parser accepts only UTF-8 encoding of the body and supports automatic inflation of &lt;code&gt;gzip&lt;/code&gt; and &lt;code&gt;deflate&lt;/code&gt; encodings.&lt;/p&gt;

&lt;p&gt;-Functions that run between a request and a response, e.g., parsing request body, authentication, logging.&lt;/p&gt;

&lt;p&gt;-Hence it is like a layer or barrier that will run before the user gets the data from the server&lt;/p&gt;

&lt;p&gt;-the data added using post was an encoded data send in the request body, hence it needs to be decoded by server.&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%2Fcdyhig4xvgr4ao4dqnwu.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%2Fcdyhig4xvgr4ao4dqnwu.png" alt="middleware" width="671" height="140"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;req.body-&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;it is used with post request, it requests the data from the body that was added using postman&lt;/li&gt;
&lt;li&gt;req.body will also has an object of name and age&lt;/li&gt;
&lt;li&gt;Here , the &lt;code&gt;res.send&lt;/code&gt; will show us the response in the body of the postman.&lt;/li&gt;
&lt;/ul&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%2Flrt5wd6qltmxqybv7gs0.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%2Flrt5wd6qltmxqybv7gs0.png" alt="postman" width="800" height="486"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, to get the data , that was added with the help of the post request, we use-&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;get request&lt;/li&gt;
&lt;li&gt;a way to add data in a data structure (or DOM manipulation, so the data won’t get lost once server restarted)&lt;/li&gt;
&lt;li&gt;work in the callback function of post and get request to give and acquire data respectively&lt;/li&gt;
&lt;/ol&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%2F6noru3rrh6qygveph447.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%2F6noru3rrh6qygveph447.png" alt="result" width="695" height="628"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;OUTPUT IN POSTMAN FOR GET REQUEST- &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%2Fg1gai397jimzhpgk11jl.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%2Fg1gai397jimzhpgk11jl.png" alt="postman" width="800" height="465"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;OUTPUT IN TERMINAL- &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5an5262szvob9t31xaa5.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%2F5an5262szvob9t31xaa5.png" alt="terminal" width="338" height="82"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;OUTPUT IN BROWSER FOR GET -&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%2Fmniw1xrvivttg5c4s0gc.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%2Fmniw1xrvivttg5c4s0gc.png" alt="output" width="800" height="92"&gt;&lt;/a&gt;&lt;/p&gt;





&lt;p&gt;ERROR-&lt;/p&gt;

&lt;p&gt;BUT, once the server is restarted it gives error, because the data get lost that was added using post request, therefore, we require databases&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%2Fe45ea44ku5d4iv9cd0gy.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%2Fe45ea44ku5d4iv9cd0gy.png" alt="error" width="800" height="202"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  SEND FILE AS A RESPONSE TO A PATH
&lt;/h3&gt;

&lt;p&gt;In order to send a file as a response to the browser (client) , we first need the path to that file on the server. For that we use a built-in module of &lt;code&gt;Node.js&lt;/code&gt; called &lt;code&gt;path&lt;/code&gt; .&lt;/p&gt;

&lt;p&gt;“PATH” module is utilised for working with file and directory path , making it easier to write code independent of system used upon.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;path&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;path&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;p&gt;The function that we will use from path module is- &lt;code&gt;path.join&lt;/code&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It joins the path of different file and directory, without making it difficult to connect through concatenation and making it platform independent&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;path.join&lt;/code&gt;  function takes the path and file name and join it
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;pathOfFile&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/index.html&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;p&gt;here the &lt;code&gt;__dirname&lt;/code&gt; is the path to the present directory of the system and joining it with the &lt;code&gt;index.html&lt;/code&gt; file present in same directory.&lt;/p&gt;

&lt;p&gt;Here, this &lt;code&gt;pathOfFile&lt;/code&gt; will help us to locate the file and further &lt;code&gt;send&lt;/code&gt; it as a &lt;code&gt;res&lt;/code&gt; .&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;res.sendFile-&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To send a file we &lt;em&gt;cannot&lt;/em&gt; simply use &lt;code&gt;res.send&lt;/code&gt; as it sends a single response but not any file, image or documents ,etc. .&lt;/p&gt;

&lt;p&gt;Instead we use an Express.js function - &lt;code&gt;res.sendFile&lt;/code&gt; that helps to send static files provided a HTTP response to the client.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&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;/file&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sendFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/index.html);
}
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As a response on the browser we get-&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%2Flby3akx0cmh7p4y01otz.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%2Flby3akx0cmh7p4y01otz.png" alt="response" width="800" height="402"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is what we wrote to get this response-&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdqbn37n0hjdnqq02dcli.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%2Fdqbn37n0hjdnqq02dcli.png" alt="code" width="800" height="181"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;⚠️&lt;/p&gt;

&lt;p&gt;&lt;em&gt;But what if our index.html file had &lt;code&gt;style.css&lt;/code&gt; file and &lt;code&gt;script .js&lt;/code&gt; file linked. Will those also work with in relation to &lt;code&gt;index.html&lt;/code&gt; file being requested from our server ?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;—NO, because a &lt;code&gt;js&lt;/code&gt; and &lt;code&gt;css&lt;/code&gt; file, in-fact any file is also a request on the server, hence they also need to be inclusive of the response of the &lt;code&gt;GET&lt;/code&gt; request when requested at their path route.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F76v0ruqk36y7oya6499g.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%2F76v0ruqk36y7oya6499g.png" alt="issue" width="773" height="357"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Therefore, we use &lt;code&gt;GET&lt;/code&gt; request for all the files that we need as a response using &lt;code&gt;res.sendFile&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyeqcitrctg5awbv99d7d.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%2Fyeqcitrctg5awbv99d7d.png" alt="request" width="773" height="357"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;em&gt;But what if there are hundreds of files that we want to send to the browser? It doesn't make sense to write a request for each file that needs to be sent, as it's time-consuming and makes it difficult to track which files have been handled.&lt;/em&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Therefore we have- SERVING STATIC FILES&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  SERVING STATIC FILES-
&lt;/h2&gt;

&lt;p&gt;To serve static files such as images, CSS files, and JavaScript files, we use the  &lt;code&gt;express.static&lt;/code&gt; built-in middleware function in Express.&lt;/p&gt;

&lt;p&gt;-Firstly a folder (usually under the name ‘static’) needs to be made in the directory.&lt;/p&gt;

&lt;p&gt;-The static folder contains all the files that needs to be requested by the client&lt;/p&gt;

&lt;p&gt;-It should have an &lt;code&gt;.html&lt;/code&gt;  file in order to show the response on the browser&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%2Fnraggnfupcy7cc2alr5a.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%2Fnraggnfupcy7cc2alr5a.png" alt="static" width="322" height="209"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Middleware-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;static&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;static&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;p&gt;This middleware is a header, which means, we cannot send a GET request at path (’/’) , because &lt;code&gt;express.static&lt;/code&gt; did that for us.&lt;/p&gt;

&lt;p&gt;Hence, when we go the &lt;code&gt;http://localhost: ${PORT}&lt;/code&gt; , it would launch a &lt;code&gt;.html&lt;/code&gt; file that was present in the &lt;em&gt;“static”&lt;/em&gt; folder and other linked files like javascript and CSS file.&lt;/p&gt;

&lt;p&gt;OUTPUT - &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%2Fusf8tiqtq638e6mckhhe.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%2Fusf8tiqtq638e6mckhhe.png" alt="result" width="800" height="405"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, with the help of above knowledge we can create a basic todo app :)&lt;/p&gt;

</description>
      <category>express</category>
      <category>node</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
