<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[BackendWithShubham]]></title><description><![CDATA[BackendWithShubham]]></description><link>https://backendwithshubham.hashnode.dev</link><image><url>https://cdn.hashnode.com/res/hashnode/image/upload/v1593680282896/kNC7E8IR4.png</url><title>BackendWithShubham</title><link>https://backendwithshubham.hashnode.dev</link></image><generator>RSS for Node</generator><lastBuildDate>Tue, 13 Oct 2026 16:11:24 GMT</lastBuildDate><atom:link href="https://backendwithshubham.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[What is Middleware in Express and How It Works]]></title><description><![CDATA[What Middleware Is in Express
One of the most important concepts in Express.js is:
Middleware

Almost every real-world Express application depends heavily on middleware.
Without middleware:

authentic]]></description><link>https://backendwithshubham.hashnode.dev/what-is-middleware-in-express-and-how-it-works</link><guid isPermaLink="true">https://backendwithshubham.hashnode.dev/what-is-middleware-in-express-and-how-it-works</guid><dc:creator><![CDATA[Shubham Bhardwaj]]></dc:creator><pubDate>Sun, 10 May 2026 15:43:45 GMT</pubDate><content:encoded><![CDATA[<h3>What Middleware Is in Express</h3>
<p>One of the most important concepts in Express.js is:</p>
<pre><code class="language-text">Middleware
</code></pre>
<p>Almost every real-world Express application depends heavily on middleware.</p>
<p>Without middleware:</p>
<ul>
<li><p>authentication becomes difficult</p>
</li>
<li><p>request validation becomes repetitive</p>
</li>
<li><p>logging becomes messy</p>
</li>
<li><p>route handling becomes harder to organize</p>
</li>
</ul>
<p>Middleware is basically:</p>
<blockquote>
<p>A function that runs between receiving a request and sending a response.</p>
</blockquote>
<h3>Understanding Middleware Simply</h3>
<p>Whenever client sends request:</p>
<pre><code class="language-text">Client → Server → Response
</code></pre>
<p>middleware sits in the middle and can:</p>
<ul>
<li><p>inspect request</p>
</li>
<li><p>modify request</p>
</li>
<li><p>stop request</p>
</li>
<li><p>continue request</p>
</li>
<li><p>send response early</p>
</li>
</ul>
<h3>Real Thinking Behind Middleware</h3>
<p>Think of middleware like:</p>
<blockquote>
<p>checkpoints in an airport.</p>
</blockquote>
<p>Before reaching airplane:</p>
<ul>
<li><p>security checks happen</p>
</li>
<li><p>passport verification happens</p>
</li>
<li><p>luggage validation happens</p>
</li>
</ul>
<p>Middleware works similarly for requests.</p>
<h3>Where Middleware Sits in Request Lifecycle</h3>
<p>This is extremely important to understand.</p>
<h3>Basic Request Lifecycle</h3>
<pre><code class="language-text">Client Request
      ↓
Middleware
      ↓
Route Handler
      ↓
Response
</code></pre>
<h3>More Realistic Flow</h3>
<pre><code class="language-text">Request
   ↓
Logging Middleware
   ↓
Authentication Middleware
   ↓
Validation Middleware
   ↓
Route Controller
   ↓
Response
</code></pre>
<p>Each middleware gets chance to:</p>
<ul>
<li><p>process request</p>
</li>
<li><p>decide what happens next</p>
</li>
</ul>
<h3>Important Understanding</h3>
<p>Middleware executes:</p>
<blockquote>
<p>before final route response.</p>
</blockquote>
<p>That is why middleware is extremely powerful.</p>
<h3>Basic Middleware Example</h3>
<p>Example:</p>
<pre><code class="language-js">app.use((req, res, next) =&gt; {
  console.log("Middleware running");

  next();
});
</code></pre>
<h3>Understanding This</h3>
<h3><code>req</code></h3>
<p>Incoming request object.</p>
<h3><code>res</code></h3>
<p>Outgoing response object.</p>
<h3><code>next</code></h3>
<p>Function that passes control to next middleware.</p>
<h3>What <code>app.use()</code> Does</h3>
<p>Registers middleware globally.</p>
<p>Meaning:</p>
<ul>
<li>middleware runs for incoming requests.</li>
</ul>
<h3>Types of Middleware</h3>
<p>Express supports different middleware types.</p>
<p>Main types:</p>
<ul>
<li><p>Application-level middleware</p>
</li>
<li><p>Router-level middleware</p>
</li>
<li><p>Built-in middleware</p>
</li>
</ul>
<h3>Application-Level Middleware</h3>
<p>Application-level middleware applies to:</p>
<blockquote>
<p>entire Express application.</p>
</blockquote>
<h3>Example</h3>
<pre><code class="language-js">app.use((req, res, next) =&gt; {
  console.log("Global middleware");

  next();
});
</code></pre>
<h3>What Happens Here</h3>
<p>Every incoming request passes through this middleware.<br />Examples:</p>
<ul>
<li><p>logging</p>
</li>
<li><p>authentication</p>
</li>
<li><p>request parsing</p>
</li>
</ul>
<h3>Why Application-Level Middleware Matters</h3>
<p>Useful for:</p>
<ul>
<li><p>shared logic</p>
</li>
<li><p>global validation</p>
</li>
<li><p>request tracking</p>
</li>
<li><p>security checks</p>
</li>
</ul>
<h3>Router-Level Middleware</h3>
<p>Router-level middleware applies only to:</p>
<blockquote>
<p>specific router or routes.</p>
</blockquote>
<h3>Example</h3>
<pre><code class="language-js">const router = express.Router();

router.use((req, res, next) =&gt; {
  console.log("Router middleware");

  next();
});
</code></pre>
<h3>Why Router Middleware Is Useful</h3>
<p>Suppose application contains:</p>
<ul>
<li><p>admin routes</p>
</li>
<li><p>user routes</p>
</li>
<li><p>product routes</p>
</li>
</ul>
<p>Different middleware may apply to different sections.</p>
<h3>Example</h3>
<pre><code class="language-text">/admin → admin authentication
/api → API validation
/users → user-specific logic
</code></pre>
<h3>Built-In Middleware</h3>
<p>Express already provides some middleware internally.</p>
<h3>Example — JSON Middleware</h3>
<p>Example:</p>
<pre><code class="language-js">app.use(express.json());
</code></pre>
<h3>What This Does</h3>
<p>Parses incoming JSON request body.</p>
<p>Without this:</p>
<ul>
<li><code>req.body</code> may not work properly for JSON requests.</li>
</ul>
<h3>Example Request</h3>
<pre><code class="language-json">{
  "name": "Shubham"
}
</code></pre>
<p>Built-in middleware converts this into usable JavaScript object.</p>
<h3>Other Common Built-In Middleware</h3>
<p>Examples:</p>
<ul>
<li><p><code>express.static()</code></p>
</li>
<li><p><code>express.urlencoded()</code></p>
</li>
</ul>
<h3>Static Middleware Example</h3>
<p>Example:</p>
<pre><code class="language-js">app.use(express.static("public"));
</code></pre>
<p>Used for serving:</p>
<ul>
<li><p>images</p>
</li>
<li><p>CSS</p>
</li>
<li><p>frontend files</p>
</li>
</ul>
<h3>Execution Order of Middleware</h3>
<p>This is one of the most important middleware concepts.<br />Middleware executes:</p>
<blockquote>
<p>in the order it is defined.</p>
</blockquote>
<h3>Example</h3>
<pre><code class="language-js">app.use((req, res, next) =&gt; {
  console.log("First");

  next();
});

app.use((req, res, next) =&gt; {
  console.log("Second");

  next();
});
</code></pre>
<p>Output:</p>
<pre><code class="language-text">First
Second
</code></pre>
<h3>Why Order Matters</h3>
<p>Suppose authentication middleware comes AFTER protected route.<br />Then:</p>
<ul>
<li>protection fails</li>
</ul>
<p>Correct order becomes critical in large applications.</p>
<h3>Real Example</h3>
<p>Correct flow:</p>
<pre><code class="language-text">Request
   ↓
Authentication
   ↓
Validation
   ↓
Controller
</code></pre>
<p>Wrong ordering can create:</p>
<ul>
<li><p>security issues</p>
</li>
<li><p>broken logic</p>
</li>
<li><p>unexpected bugs</p>
</li>
</ul>
<h3>Role of <code>next()</code> Function</h3>
<p>This is the heart of middleware flow.</p>
<h3>What <code>next()</code> Does</h3>
<p><code>next()</code> tells Express:</p>
<blockquote>
<p>“Move to next middleware or route handler.”</p>
</blockquote>
<h3>Example</h3>
<pre><code class="language-js">app.use((req, res, next) =&gt; {
  console.log("Middleware");

  next();
});
</code></pre>
<p>Without <code>next()</code>:</p>
<ul>
<li>request stops there</li>
</ul>
<h3>Important Understanding</h3>
<p>Middleware must either:</p>
<ul>
<li><p>send response OR</p>
</li>
<li><p>call <code>next()</code></p>
</li>
</ul>
<p>Otherwise request hangs forever.</p>
<h3>Example of Hanging Request</h3>
<p>Wrong example:</p>
<pre><code class="language-js">app.use((req, res, next) =&gt; {
  console.log("Oops");
});
</code></pre>
<p>Here:</p>
<ul>
<li><p>no response</p>
</li>
<li><p>no next()</p>
</li>
</ul>
<p>Client keeps waiting.</p>
<h3>Middleware Chain Concept</h3>
<p>Middleware creates chain-like execution. Example:</p>
<pre><code class="language-text">Middleware 1
   ↓
Middleware 2
   ↓
Middleware 3
   ↓
Route Handler
</code></pre>
<p>Each middleware decides:</p>
<ul>
<li><p>continue</p>
</li>
<li><p>stop</p>
</li>
<li><p>reject request</p>
</li>
</ul>
<h3>Real-World Examples</h3>
<p>This is where middleware becomes truly powerful.</p>
<h3>Logging Middleware</h3>
<p>Logging tracks incoming requests.</p>
<p>Example:</p>
<pre><code class="language-js">app.use((req, res, next) =&gt; {
  console.log(req.method, req.url);

  next();
});
</code></pre>
<h3>Why Logging Matters</h3>
<p>Useful for:</p>
<ul>
<li><p>debugging</p>
</li>
<li><p>monitoring traffic</p>
</li>
<li><p>tracking APIs</p>
</li>
<li><p>server analysis</p>
</li>
</ul>
<h3>Example Output</h3>
<pre><code class="language-text">GET /users
POST /login
</code></pre>
<h3>Authentication Middleware</h3>
<p>Authentication middleware checks:</p>
<blockquote>
<p>is user logged in?</p>
</blockquote>
<h3>Example</h3>
<pre><code class="language-js">function auth(req, res, next) {
  const token = req.headers.authorization;

  if (!token) {
    return res.status(401).send("Unauthorized");
  }

  next();
}
</code></pre>
<h3>What Happens Here</h3>
<h3>If Token Exists</h3>
<p>Request continues.</p>
<h3>If Token Missing</h3>
<p>Access blocked.</p>
<h3>Why Authentication Middleware Matters</h3>
<p>Without it:</p>
<ul>
<li>private routes become publicly accessible</li>
</ul>
<h3>Protected Route Example</h3>
<p>Example:</p>
<pre><code class="language-js">app.get("/dashboard", auth, (req, res) =&gt; {
  res.send("Private Dashboard");
});
</code></pre>
<p>Now route becomes protected.</p>
<h3>Request Validation Middleware</h3>
<p>Validation middleware checks:</p>
<blockquote>
<p>is incoming data valid?</p>
</blockquote>
<h3>Example</h3>
<pre><code class="language-js">function validate(req, res, next) {
  if (!req.body.name) {
    return res.status(400).send("Name required");
  }

  next();
}
</code></pre>
<h3>Why Validation Matters</h3>
<p>Without validation:</p>
<ul>
<li><p>invalid data enters database</p>
</li>
<li><p>APIs become unstable</p>
</li>
<li><p>security risks increase</p>
</li>
</ul>
<h3>Example Route</h3>
<p>Example:</p>
<pre><code class="language-js">app.post("/users", validate, (req, res) =&gt; {
  res.send("User created");
});
</code></pre>
<h3>Middleware in Real Applications</h3>
<p>Real-world Express applications may contain middleware for:</p>
<ul>
<li><p>logging</p>
</li>
<li><p>authentication</p>
</li>
<li><p>validation</p>
</li>
<li><p>file uploads</p>
</li>
<li><p>rate limiting</p>
</li>
<li><p>security</p>
</li>
<li><p>error handling</p>
</li>
<li><p>CORS handling</p>
</li>
</ul>
<p>Middleware becomes backbone of backend architecture.</p>
<h3>Understanding Middleware as Reusable Logic</h3>
<p>One major benefit:</p>
<ul>
<li><p>write logic once</p>
</li>
<li><p>reuse everywhere</p>
</li>
</ul>
<p>Example:</p>
<ul>
<li><p>one authentication middleware</p>
</li>
<li><p>used across many routes</p>
</li>
</ul>
<p>This improves:</p>
<ul>
<li><p>maintainability</p>
</li>
<li><p>scalability</p>
</li>
<li><p>cleaner code structure</p>
</li>
</ul>
<h3>Common Beginner Mistake</h3>
<p>Many beginners forget:</p>
<pre><code class="language-js">next()
</code></pre>
<p>This causes requests to freeze.</p>
<h3>Another Common Mistake</h3>
<p>People often confuse:</p>
<ul>
<li><p>middleware</p>
</li>
<li><p>route handlers</p>
</li>
</ul>
<h3>Important Difference</h3>
<h4>Middleware</h4>
<p>Usually preprocesses requests.</p>
<h4>Route Handler</h4>
<p>Usually sends final response.</p>
<h3>Why Middleware Is So Important in Express</h3>
<p>Middleware is one of the biggest reasons Express.js became so flexible and powerful.</p>
<p>It allows developers to:</p>
<ul>
<li><p>organize logic cleanly</p>
</li>
<li><p>reuse functionality</p>
</li>
<li><p>separate concerns properly</p>
</li>
</ul>
<h3>Final Understanding</h3>
<p>Middleware in Express.js are functions that execute during the request-response lifecycle before the final route handler.</p>
<p>Middleware can:</p>
<ul>
<li><p>inspect requests</p>
</li>
<li><p>modify requests</p>
</li>
<li><p>authenticate users</p>
</li>
<li><p>validate data</p>
</li>
<li><p>log activity</p>
</li>
<li><p>stop requests</p>
</li>
<li><p>continue requests using <code>next()</code></p>
</li>
</ul>
<p>Express supports:</p>
<ul>
<li><p>application-level middleware</p>
</li>
<li><p>router-level middleware</p>
</li>
<li><p>built-in middleware</p>
</li>
</ul>
<p>And because middleware executes in sequence, execution order becomes extremely important for building scalable and maintainable backend applications.</p>
]]></content:encoded></item><item><title><![CDATA[Handling File Uploads in Express with Multer]]></title><description><![CDATA[Why File Uploads Need Middleware
Whenever users upload files like:

profile images

PDFs

videos

resumes

product photos


the browser sends file data differently compared to normal JSON requests.Nor]]></description><link>https://backendwithshubham.hashnode.dev/handling-file-uploads-in-express-with-multer</link><guid isPermaLink="true">https://backendwithshubham.hashnode.dev/handling-file-uploads-in-express-with-multer</guid><dc:creator><![CDATA[Shubham Bhardwaj]]></dc:creator><pubDate>Sun, 10 May 2026 15:39:58 GMT</pubDate><content:encoded><![CDATA[<h3>Why File Uploads Need Middleware</h3>
<p>Whenever users upload files like:</p>
<ul>
<li><p>profile images</p>
</li>
<li><p>PDFs</p>
</li>
<li><p>videos</p>
</li>
<li><p>resumes</p>
</li>
<li><p>product photos</p>
</li>
</ul>
<p>the browser sends file data differently compared to normal JSON requests.<br />Normal API requests often look like:</p>
<pre><code class="language-json">{
  "name": "Shubham"
}
</code></pre>
<p>But file uploads use:</p>
<pre><code class="language-text">multipart/form-data
</code></pre>
<p>format.</p>
<h3>The Real Problem</h3>
<p>By default, :contentReference[oaicite:0]{index=0} cannot properly understand uploaded file data automatically.</p>
<p>Meaning:</p>
<ul>
<li><p><code>req.body</code> alone is not enough</p>
</li>
<li><p>file binary data needs special handling</p>
</li>
</ul>
<p>This is why middleware becomes necessary.</p>
<h3>What Middleware Does Here</h3>
<p>Middleware sits between:</p>
<ul>
<li><p>request</p>
</li>
<li><p>route handler</p>
</li>
</ul>
<p>and processes uploaded file data before route logic executes.</p>
<h3>Real Thinking Behind Upload Middleware</h3>
<p>Imagine receiving a parcel. Before using contents:</p>
<ul>
<li><p>someone must unpack</p>
</li>
<li><p>organize</p>
</li>
<li><p>validate</p>
</li>
</ul>
<p>Middleware performs similar preprocessing for uploaded files.</p>
<h3>What Multer Is</h3>
<p>One of the most popular upload middleware libraries for NodeJS is:</p>
<p>Multer is basically:</p>
<blockquote>
<p>Middleware for handling multipart/form-data file uploads in Express applications.</p>
</blockquote>
<h3>Why Multer Became Popular</h3>
<p>Because it simplifies:</p>
<ul>
<li><p>file handling</p>
</li>
<li><p>upload parsing</p>
</li>
<li><p>storage configuration</p>
</li>
<li><p>multiple uploads</p>
</li>
<li><p>file access</p>
</li>
</ul>
<p>Without Multer:</p>
<ul>
<li>handling uploads manually becomes much harder.</li>
</ul>
<h3>Installing Multer</h3>
<p>Example:</p>
<pre><code class="language-bash">npm install multer
</code></pre>
<h3>Importing Multer</h3>
<p>Example:</p>
<pre><code class="language-js">const multer = require("multer");
</code></pre>
<p>Now upload handling becomes possible.</p>
<h3>Understanding Multer Simply</h3>
<p>When upload request arrives:</p>
<ul>
<li><p>Multer reads uploaded files</p>
</li>
<li><p>processes file data</p>
</li>
<li><p>stores files</p>
</li>
<li><p>attaches information to request object</p>
</li>
</ul>
<h3>Basic Upload Flow</h3>
<pre><code class="language-text">Client uploads file
      ↓
Multer processes file
      ↓
File stored
      ↓
Route handler receives file info
</code></pre>
<h3>Handling Single File Upload</h3>
<p>One of the most common cases:</p>
<blockquote>
<p>uploading one file.</p>
</blockquote>
<p>Example:</p>
<ul>
<li><p>profile image</p>
</li>
<li><p>resume</p>
</li>
<li><p>single document</p>
</li>
</ul>
<h3>Basic Single Upload Setup</h3>
<p>Example:</p>
<pre><code class="language-js">const express = require("express");
const multer = require("multer");

const app = express();

const upload = multer({
  dest: "uploads/"
});
</code></pre>
<h3>Understanding This</h3>
<h3><code>multer()</code></h3>
<p>Creates upload middleware.</p>
<h3><code>dest</code></h3>
<p>Specifies upload folder.</p>
<p>Example:</p>
<pre><code class="language-text">uploads/
</code></pre>
<p>Uploaded files get stored there.</p>
<h3>Single File Route</h3>
<p>Example:</p>
<pre><code class="language-js">app.post("/upload", upload.single("image"), (req, res) =&gt; {
  res.send("File uploaded");
});
</code></pre>
<h3>Understanding <code>single()</code></h3>
<p>Example:</p>
<pre><code class="language-js">upload.single("image")
</code></pre>
<p>means:</p>
<blockquote>
<p>accept one uploaded file from field named <code>"image"</code>.</p>
</blockquote>
<h3>Important Understanding</h3>
<p>Field name must match frontend form field.</p>
<p>Example:</p>
<pre><code class="language-html">&lt;input type="file" name="image"&gt;
</code></pre>
<h3>Accessing Uploaded File</h3>
<p>Multer stores uploaded file info inside:</p>
<pre><code class="language-js">req.file
</code></pre>
<h3>Example</h3>
<pre><code class="language-js">app.post("/upload", upload.single("image"), (req, res) =&gt; {
  console.log(req.file);

  res.send("Uploaded");
});
</code></pre>
<h3>Example File Info</h3>
<p>Example:</p>
<pre><code class="language-js">{
  filename: "abc123.jpg",
  path: "uploads/abc123.jpg"
}
</code></pre>
<h3>Handling Multiple File Uploads</h3>
<p>Applications often need:</p>
<ul>
<li><p>gallery uploads</p>
</li>
<li><p>multiple images</p>
</li>
<li><p>document collections</p>
</li>
</ul>
<p>Multer supports multiple uploads easily.</p>
<h3>Using <code>array()</code></h3>
<p>Example:</p>
<pre><code class="language-js">app.post("/upload", upload.array("images", 5), (req, res) =&gt; {
  res.send("Files uploaded");
});
</code></pre>
<h3>Understanding This</h3>
<h3><code>"images"</code></h3>
<p>Field name.<br /><code>5</code> Maximum allowed files.</p>
<h3>Accessing Multiple Files</h3>
<p>Multiple uploads become available in:</p>
<pre><code class="language-js">req.files
</code></pre>
<h3>Example</h3>
<pre><code class="language-js">console.log(req.files);
</code></pre>
<p>This returns array of uploaded file objects.</p>
<h3>Example Frontend Field</h3>
<p>Example:</p>
<pre><code class="language-html">&lt;input type="file" name="images" multiple&gt;
</code></pre>
<h3>Why Multiple Upload Support Matters</h3>
<p>Modern applications frequently upload:</p>
<ul>
<li><p>product galleries</p>
</li>
<li><p>social media images</p>
</li>
<li><p>multiple documents</p>
</li>
<li><p>media collections</p>
</li>
</ul>
<h3>Storage Configuration Basics</h3>
<p>By default:</p>
<ul>
<li>Multer stores files with random names.</li>
</ul>
<p>But developers usually want more control.</p>
<p>Examples:</p>
<ul>
<li><p>custom filenames</p>
</li>
<li><p>custom folders</p>
</li>
<li><p>validation</p>
</li>
<li><p>file filtering</p>
</li>
</ul>
<h3>Using Disk Storage</h3>
<p>Example:</p>
<pre><code class="language-js">const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, "uploads/");
  },

  filename: function (req, file, cb) {
    cb(null, Date.now() + "-" + file.originalname);
  }
});
</code></pre>
<h3>Understanding <code>destination</code></h3>
<p>Controls:</p>
<blockquote>
<p>where file gets stored.</p>
</blockquote>
<h3>Understanding <code>filename</code></h3>
<p>Controls:</p>
<blockquote>
<p>uploaded filename.</p>
</blockquote>
<h3>Creating Upload Middleware</h3>
<p>Example:</p>
<pre><code class="language-js">const upload = multer({
  storage: storage
});
</code></pre>
<p>Now Multer uses custom storage settings.</p>
<h3>Why Custom Storage Matters</h3>
<p>Without custom names:</p>
<ul>
<li><p>duplicate files may overwrite</p>
</li>
<li><p>filenames become messy</p>
</li>
<li><p>organization becomes difficult</p>
</li>
</ul>
<h3>Example Custom Filename</h3>
<p>Example:</p>
<pre><code class="language-text">1712345678-profile.jpg
</code></pre>
<p>Timestamp helps uniqueness.</p>
<h3>File Validation Basics</h3>
<p>Multer also allows filtering uploads.</p>
<p>Example:</p>
<ul>
<li><p>only images</p>
</li>
<li><p>file size limits</p>
</li>
</ul>
<h3>Example File Filter</h3>
<p>Example:</p>
<pre><code class="language-js">fileFilter: function (req, file, cb) {
  if (file.mimetype.startsWith("image")) {
    cb(null, true);
  } else {
    cb(new Error("Only images allowed"));
  }
}
</code></pre>
<h3>Why Validation Matters</h3>
<p>Without validation:</p>
<ul>
<li><p>dangerous files may upload</p>
</li>
<li><p>malware risks increase</p>
</li>
<li><p>storage abuse becomes possible</p>
</li>
</ul>
<h3>Serving Uploaded Files</h3>
<p>After uploading:</p>
<ul>
<li>frontend usually needs access to files.</li>
</ul>
<p>Examples:</p>
<ul>
<li><p>display profile image</p>
</li>
<li><p>show uploaded documents</p>
</li>
<li><p>render product photos</p>
</li>
</ul>
<p>Express serves uploaded files using static middleware.</p>
<h3>Basic Static Serving</h3>
<p>Example:</p>
<pre><code class="language-js">app.use("/uploads", express.static("uploads"));
</code></pre>
<h3>What This Means</h3>
<p>Anything inside:</p>
<pre><code class="language-text">uploads/
</code></pre>
<p>becomes publicly accessible through URL.</p>
<h3>Example</h3>
<p>Suppose file:</p>
<pre><code class="language-text">uploads/photo.jpg
</code></pre>
<p>Now accessible at:</p>
<pre><code class="language-text">http://localhost:3000/uploads/photo.jpg
</code></pre>
<h3>Why Serving Files Matters</h3>
<p>Frontend applications need URLs for:</p>
<ul>
<li><p>image rendering</p>
</li>
<li><p>downloads</p>
</li>
<li><p>previews</p>
</li>
<li><p>media access</p>
</li>
</ul>
<h3>Real Upload Architecture</h3>
<p>Typical upload flow:</p>
<pre><code class="language-text">User uploads file
      ↓
Multer processes upload
      ↓
File stored
      ↓
Path/URL generated
      ↓
URL saved in database
      ↓
Frontend accesses file
</code></pre>
<h3>Important Security Understanding</h3>
<p>Public uploads should be handled carefully.</p>
<p>Because attackers may upload:</p>
<ul>
<li><p>malware</p>
</li>
<li><p>fake files</p>
</li>
<li><p>huge files</p>
</li>
<li><p>dangerous scripts</p>
</li>
</ul>
<h3>Common Security Practices</h3>
<p>Examples:</p>
<ul>
<li><p>validate file types</p>
</li>
<li><p>limit file sizes</p>
</li>
<li><p>sanitize filenames</p>
</li>
<li><p>restrict executable files</p>
</li>
</ul>
<h3>Common Beginner Mistake</h3>
<p>Many beginners forget:</p>
<pre><code class="language-js">enctype="multipart/form-data"
</code></pre>
<p>inside forms.</p>
<p>Without it:</p>
<ul>
<li>uploads fail</li>
</ul>
<h3>Correct Form Example</h3>
<p>Example:</p>
<pre><code class="language-html">&lt;form enctype="multipart/form-data"&gt;
</code></pre>
<h3>Another Common Mistake</h3>
<p>People often confuse:</p>
<ul>
<li><p><code>req.body</code></p>
</li>
<li><p><code>req.file</code></p>
</li>
<li><p><code>req.files</code></p>
</li>
</ul>
<h3>Important Difference</h3>
<h4><code>req.body</code></h4>
<p>Normal text fields.</p>
<h4><code>req.file</code></h4>
<p>Single uploaded file.</p>
<h4><code>req.files</code></h4>
<p>Multiple uploaded files.</p>
<h3>Why Multer Became Industry Standard</h3>
<p>:contentReference[oaicite:3]{index=3} became extremely popular because it simplifies:</p>
<ul>
<li><p>upload handling</p>
</li>
<li><p>storage management</p>
</li>
<li><p>multipart parsing</p>
</li>
<li><p>Express integration</p>
</li>
</ul>
<p>without requiring complex manual processing.</p>
<h3>Final Understanding</h3>
<p>File uploads require middleware because uploaded files use:</p>
<pre><code class="language-text">multipart/form-data
</code></pre>
<p>instead of normal JSON requests.</p>
<p>:contentReference[oaicite:4]{index=4} is a middleware library that helps Express applications handle uploaded files efficiently.</p>
<p>Multer supports:</p>
<ul>
<li><p>single file uploads</p>
</li>
<li><p>multiple file uploads</p>
</li>
<li><p>custom storage configuration</p>
</li>
<li><p>file validation</p>
</li>
</ul>
<p>and uploaded files can later be served publicly using Express static middleware for frontend access.</p>
]]></content:encoded></item><item><title><![CDATA[REST API Design Made Simple with Express.js]]></title><description><![CDATA[What REST API Means
Modern applications constantly communicate with servers.Examples:

frontend requesting products

mobile app fetching user data

dashboard updating information

backend services com]]></description><link>https://backendwithshubham.hashnode.dev/rest-api-design-made-simple-with-express-js</link><guid isPermaLink="true">https://backendwithshubham.hashnode.dev/rest-api-design-made-simple-with-express-js</guid><dc:creator><![CDATA[Shubham Bhardwaj]]></dc:creator><pubDate>Sun, 10 May 2026 15:36:49 GMT</pubDate><content:encoded><![CDATA[<h3>What REST API Means</h3>
<p>Modern applications constantly communicate with servers.<br />Examples:</p>
<ul>
<li><p>frontend requesting products</p>
</li>
<li><p>mobile app fetching user data</p>
</li>
<li><p>dashboard updating information</p>
</li>
<li><p>backend services communicating together</p>
</li>
</ul>
<p>Most of this communication happens using:</p>
<pre><code class="language-text">REST APIs
</code></pre>
<p>REST stands for:</p>
<pre><code class="language-text">Representational State Transfer
</code></pre>
<p>Even though the full name sounds complex, the core idea is actually simple.</p>
<p>REST API is basically:</p>
<blockquote>
<p>A structured way for applications to communicate over HTTP.</p>
</blockquote>
<h3>Understanding REST in Simple Terms</h3>
<p>REST defines rules for:</p>
<ul>
<li><p>requesting data</p>
</li>
<li><p>creating data</p>
</li>
<li><p>updating data</p>
</li>
<li><p>deleting data</p>
</li>
</ul>
<p>using standard HTTP methods and URLs.</p>
<h3>Real Thinking Behind REST</h3>
<p>Without structure, every backend developer could create APIs differently.<br />Example:</p>
<pre><code class="language-text">/getAllUsersNow
/createNewUserFast
/removeUserFinally
</code></pre>
<p>This would become messy and inconsistent.<br />REST introduced:</p>
<ul>
<li><p>predictable structure</p>
</li>
<li><p>standard conventions</p>
</li>
<li><p>cleaner APIs</p>
</li>
</ul>
<h3>Why REST Became Popular</h3>
<p>Because REST APIs are:</p>
<ul>
<li><p>simple</p>
</li>
<li><p>scalable</p>
</li>
<li><p>readable</p>
</li>
<li><p>predictable</p>
</li>
<li><p>easy to integrate</p>
</li>
</ul>
<p>Almost every modern application interacts with REST APIs in some way.</p>
<h3>Resources in REST Architecture</h3>
<p>One of the core ideas in REST is:</p>
<pre><code class="language-text">Resources
</code></pre>
<p>A resource means:</p>
<blockquote>
<p>any piece of data managed by server.</p>
</blockquote>
<p>Examples:</p>
<ul>
<li><p>users</p>
</li>
<li><p>products</p>
</li>
<li><p>posts</p>
</li>
<li><p>orders</p>
</li>
<li><p>comments</p>
</li>
</ul>
<h3>Understanding Resources Simply</h3>
<p>Suppose application manages users.</p>
<p>Then:</p>
<pre><code class="language-text">users
</code></pre>
<p>becomes resource. REST APIs focus heavily on resources instead of actions.</p>
<h3>Example Resources</h3>
<p>Examples:</p>
<pre><code class="language-text">/users
/products
/orders
/posts
</code></pre>
<p>Each resource usually gets:</p>
<ul>
<li><p>its own routes</p>
</li>
<li><p>its own CRUD operations</p>
</li>
</ul>
<h3>Real Thinking Behind Resource-Based Design</h3>
<p>REST says:</p>
<blockquote>
<p>Focus on nouns (resources), not verbs (actions).</p>
</blockquote>
<p>Good REST APIs look like:</p>
<pre><code class="language-text">/users
/products
/orders
</code></pre>
<p>instead of:</p>
<pre><code class="language-text">/getUsers
/createProducts
/deleteOrders
</code></pre>
<h3>HTTP Methods</h3>
<p>REST APIs heavily depend on HTTP methods. Different methods represent different actions. Main methods:</p>
<ul>
<li><p>GET</p>
</li>
<li><p>POST</p>
</li>
<li><p>PUT</p>
</li>
<li><p>DELETE</p>
</li>
</ul>
<h3>GET</h3>
<p>GET is used for:</p>
<blockquote>
<p>Fetching data.</p>
</blockquote>
<h3>Example</h3>
<p>Example route:</p>
<pre><code class="language-text">GET /users
</code></pre>
<p>Meaning:</p>
<blockquote>
<p>fetch all users.</p>
</blockquote>
<h3>Another Example</h3>
<pre><code class="language-text">GET /users/10
</code></pre>
<p>Meaning:</p>
<blockquote>
<p>fetch user with ID 10.</p>
</blockquote>
<h3>Important Understanding</h3>
<p>GET should:</p>
<ul>
<li><p>not modify data</p>
</li>
<li><p>only retrieve information</p>
</li>
</ul>
<h3>POST</h3>
<p>POST is used for:</p>
<blockquote>
<p>Creating new data.</p>
</blockquote>
<h3>Example</h3>
<p>Example:</p>
<pre><code class="language-text">POST /users
</code></pre>
<p>Meaning:</p>
<blockquote>
<p>create new user.</p>
</blockquote>
<h3>Example Request Body</h3>
<p>Example:</p>
<pre><code class="language-json">{
  "name": "Shubham",
  "email": "test@gmail.com"
}
</code></pre>
<p>Server receives this data and creates user.</p>
<h3>PUT</h3>
<p>PUT is used for:</p>
<blockquote>
<p>Updating existing data.</p>
</blockquote>
<h3>Example</h3>
<p>Example:</p>
<pre><code class="language-text">PUT /users/10
</code></pre>
<p>Meaning:</p>
<blockquote>
<p>update user with ID 10.</p>
</blockquote>
<h3>Example Request Body</h3>
<p>Example:</p>
<pre><code class="language-json">{
  "name": "Updated Name"
}
</code></pre>
<h3>Important Understanding</h3>
<p>PUT usually replaces or updates resource data.</p>
<h3>DELETE</h3>
<p>DELETE is used for:</p>
<blockquote>
<p>Removing data.</p>
</blockquote>
<h3>Example</h3>
<p>Example:</p>
<pre><code class="language-text">DELETE /users/10
</code></pre>
<p>Meaning:</p>
<blockquote>
<p>delete user with ID 10.</p>
</blockquote>
<h3>Understanding CRUD Operations</h3>
<p>REST APIs usually map methods to CRUD operations.</p>
<h3>Create</h3>
<pre><code class="language-text">POST
</code></pre>
<h3>Read</h3>
<pre><code class="language-text">GET
</code></pre>
<h3>Update</h3>
<pre><code class="language-text">PUT
</code></pre>
<h3>Delete</h3>
<pre><code class="language-text">DELETE
</code></pre>
<h3>Status Codes Basics</h3>
<p>Whenever server responds, it sends:</p>
<pre><code class="language-text">HTTP Status Codes
</code></pre>
<p>These codes tell client:</p>
<ul>
<li><p>success</p>
</li>
<li><p>failure</p>
</li>
<li><p>errors</p>
</li>
<li><p>authorization issues</p>
</li>
</ul>
<h3>Common Status Codes</h3>
<h3>200 OK</h3>
<p>Meaning:</p>
<blockquote>
<p>request successful.</p>
</blockquote>
<p>Example:</p>
<ul>
<li>successful GET request</li>
</ul>
<h3>201 Created</h3>
<p>Meaning:</p>
<blockquote>
<p>new resource created successfully.</p>
</blockquote>
<p>Example:</p>
<ul>
<li>successful POST request</li>
</ul>
<h3>400 Bad Request</h3>
<p>Meaning:</p>
<blockquote>
<p>invalid request from client.</p>
</blockquote>
<p>Examples:</p>
<ul>
<li><p>missing fields</p>
</li>
<li><p>invalid data</p>
</li>
</ul>
<h3>401 Unauthorized</h3>
<p>Meaning:</p>
<blockquote>
<p>authentication required.</p>
</blockquote>
<h3>403 Forbidden</h3>
<p>Meaning:</p>
<blockquote>
<p>user lacks permission.</p>
</blockquote>
<h3>404 Not Found</h3>
<p>Meaning:</p>
<blockquote>
<p>requested resource does not exist.</p>
</blockquote>
<h3>500 Internal Server Error</h3>
<p>Meaning:</p>
<blockquote>
<p>server-side issue occurred.</p>
</blockquote>
<h3>Why Status Codes Matter</h3>
<p>Without status codes:</p>
<ul>
<li><p>clients would not know what happened</p>
</li>
<li><p>debugging becomes difficult</p>
</li>
<li><p>frontend logic becomes messy</p>
</li>
</ul>
<h3>Example Response</h3>
<p>Example:</p>
<pre><code class="language-http">200 OK
</code></pre>
<p>or:</p>
<pre><code class="language-http">404 Not Found
</code></pre>
<h3>Designing Routes Using REST Principles</h3>
<p>This is where many beginners struggle.</p>
<p>REST encourages:</p>
<ul>
<li><p>clean routes</p>
</li>
<li><p>resource-focused URLs</p>
</li>
<li><p>predictable structure</p>
</li>
</ul>
<h3>Good REST Route Examples</h3>
<h3>Fetch All Users</h3>
<pre><code class="language-text">GET /users
</code></pre>
<h3>Fetch Single User</h3>
<pre><code class="language-text">GET /users/10
</code></pre>
<h3>Create User</h3>
<pre><code class="language-text">POST /users
</code></pre>
<h3>Update User</h3>
<pre><code class="language-text">PUT /users/10
</code></pre>
<h3>Delete User</h3>
<pre><code class="language-text">DELETE /users/10
</code></pre>
<h3>Why This Structure Is Powerful</h3>
<p>Because APIs become:</p>
<ul>
<li><p>easy to understand</p>
</li>
<li><p>consistent</p>
</li>
<li><p>predictable</p>
</li>
</ul>
<p>Even new developers can understand routes quickly.</p>
<h3>Bad REST Design Example</h3>
<p>Example:</p>
<pre><code class="language-text">/getAllUsers
/createUserNow
/removeUserById
</code></pre>
<p>These routes:</p>
<ul>
<li><p>mix actions into URL names</p>
</li>
<li><p>become inconsistent</p>
</li>
</ul>
<p>REST prefers:</p>
<pre><code class="language-text">resources + HTTP methods
</code></pre>
<h3>Example Resource: Users</h3>
<p>Let’s understand full REST design using:</p>
<pre><code class="language-text">users
</code></pre>
<p>resource.</p>
<h3>Fetch All Users</h3>
<pre><code class="language-http">GET /users
</code></pre>
<p>Response:</p>
<pre><code class="language-json">[
  {
    "id": 1,
    "name": "Shubham"
  }
]
</code></pre>
<h3>Fetch One User</h3>
<pre><code class="language-http">GET /users/1
</code></pre>
<p>Response:</p>
<pre><code class="language-json">{
  "id": 1,
  "name": "Shubham"
}
</code></pre>
<h3>Create User</h3>
<pre><code class="language-http">POST /users
</code></pre>
<p>Request body:</p>
<pre><code class="language-json">{
  "name": "Rahul"
}
</code></pre>
<h3>Update User</h3>
<pre><code class="language-http">PUT /users/1
</code></pre>
<p>Request body:</p>
<pre><code class="language-json">{
  "name": "Updated Rahul"
}
</code></pre>
<h3>Delete User</h3>
<pre><code class="language-http">DELETE /users/1
</code></pre>
<h3>Understanding Route Parameters</h3>
<p>Example:</p>
<pre><code class="language-text">/users/:id
</code></pre>
<p>Here:</p>
<ul>
<li><code>:id</code> becomes dynamic parameter</li>
</ul>
<p>This allows:</p>
<ul>
<li><p>fetching specific resources</p>
</li>
<li><p>updating specific resources</p>
</li>
<li><p>deleting specific resources</p>
</li>
</ul>
<h3>Why REST APIs Became Industry Standard</h3>
<p>REST APIs became popular because they provide:</p>
<ul>
<li><p>standardization</p>
</li>
<li><p>scalability</p>
</li>
<li><p>simplicity</p>
</li>
<li><p>interoperability</p>
</li>
</ul>
<p>Frontend, backend, mobile apps, and third-party systems can all communicate consistently.</p>
<h3>Real-World REST API Examples</h3>
<p>Examples:</p>
<ul>
<li><p>social media APIs</p>
</li>
<li><p>e-commerce APIs</p>
</li>
<li><p>payment systems</p>
</li>
<li><p>authentication systems</p>
</li>
<li><p>dashboard backends</p>
</li>
</ul>
<p>Almost every modern backend system exposes REST-style APIs.</p>
<h3>Common Beginner Mistake</h3>
<p>Many beginners confuse:</p>
<ul>
<li><p>routes</p>
</li>
<li><p>HTTP methods</p>
</li>
</ul>
<p>Example:</p>
<pre><code class="language-text">GET /users
</code></pre>
<p>and:</p>
<pre><code class="language-text">POST /users
</code></pre>
<p>may share same route path but perform completely different operations because method differs.</p>
<h3>Another Common Mistake</h3>
<p>People often put verbs directly inside routes:</p>
<pre><code class="language-text">/createUser
</code></pre>
<p>instead of using RESTful structure:</p>
<pre><code class="language-text">POST /users
</code></pre>
<h3>Why Understanding REST Matters</h3>
<p>REST concepts become foundation for:</p>
<ul>
<li><p>backend APIs</p>
</li>
<li><p>frontend-backend communication</p>
</li>
<li><p>mobile app integration</p>
</li>
<li><p>microservices</p>
</li>
<li><p>modern web architecture</p>
</li>
</ul>
<p>Without understanding REST:</p>
<ul>
<li>API design becomes inconsistent and difficult to scale.</li>
</ul>
<h3>Final Understanding</h3>
<p>REST APIs are structured systems for communication between clients and servers using HTTP.</p>
<p>REST focuses on:</p>
<ul>
<li><p>resources</p>
</li>
<li><p>standard HTTP methods</p>
</li>
<li><p>predictable route design</p>
</li>
</ul>
<p>Main HTTP methods include:</p>
<ul>
<li><p>GET → fetch data</p>
</li>
<li><p>POST → create data</p>
</li>
<li><p>PUT → update data</p>
</li>
<li><p>DELETE → remove data</p>
</li>
</ul>
<p>Servers also return HTTP status codes to indicate success or failure.</p>
<p>And by designing routes around resources like:</p>
<pre><code class="language-text">/users
/products
/orders
</code></pre>
<p>developers create scalable, readable, and industry-standard APIs.</p>
]]></content:encoded></item><item><title><![CDATA[Blocking vs Non-Blocking Code in Node.js]]></title><description><![CDATA[What Blocking Code Means
One of the most important backend concepts in :contentReference[oaicite:0]{index=0} is understanding:
Blocking vs Non-Blocking

This concept directly affects:

server speed

s]]></description><link>https://backendwithshubham.hashnode.dev/blocking-vs-non-blocking-code-in-node-js</link><guid isPermaLink="true">https://backendwithshubham.hashnode.dev/blocking-vs-non-blocking-code-in-node-js</guid><dc:creator><![CDATA[Shubham Bhardwaj]]></dc:creator><pubDate>Sun, 10 May 2026 15:34:17 GMT</pubDate><content:encoded><![CDATA[<h3>What Blocking Code Means</h3>
<p>One of the most important backend concepts in :contentReference[oaicite:0]{index=0} is understanding:</p>
<pre><code class="language-text">Blocking vs Non-Blocking
</code></pre>
<p>This concept directly affects:</p>
<ul>
<li><p>server speed</p>
</li>
<li><p>scalability</p>
</li>
<li><p>performance</p>
</li>
<li><p>user experience</p>
</li>
</ul>
<p>Let’s first understand blocking code. Blocking code means:</p>
<blockquote>
<p>Code execution stops and waits until current task fully finishes.</p>
</blockquote>
<p>During this waiting period:</p>
<ul>
<li><p>nothing else can continue</p>
</li>
<li><p>execution gets paused</p>
</li>
</ul>
<h3>Simple Example of Blocking Thinking</h3>
<p>Imagine one cashier in a shop. Suppose one customer takes:</p>
<pre><code class="language-text">10 minutes
</code></pre>
<p>to complete payment. If cashier refuses to serve anyone else during that time:</p>
<ul>
<li>entire queue waits</li>
</ul>
<p>That is blocking behavior.</p>
<h3>Understanding Blocking in Programming</h3>
<p>Suppose application needs:</p>
<ul>
<li><p>file reading</p>
</li>
<li><p>database query</p>
</li>
<li><p>API request</p>
</li>
</ul>
<p>If code waits completely before continuing:</p>
<ul>
<li>thread becomes blocked</li>
</ul>
<h3>Simple Conceptual Example</h3>
<p>Example:</p>
<pre><code class="language-js">const data = readFileSync("data.txt");

console.log(data);
console.log("Next task");
</code></pre>
<p>Here:</p>
<ul>
<li><p>execution waits until file reading finishes</p>
</li>
<li><p><code>"Next task"</code> cannot run earlier</p>
</li>
</ul>
<p>This is blocking behavior.</p>
<h3>Important Understanding</h3>
<p>Blocking itself is not always “bad.” Sometimes blocking operations are acceptable. But for servers handling many users:</p>
<ul>
<li>blocking becomes dangerous</li>
</ul>
<h3>What Non-Blocking Code Means</h3>
<p>Non-blocking code means:</p>
<blockquote>
<p>Program starts task and continues other work without waiting immediately.</p>
</blockquote>
<p>When task finishes:</p>
<ul>
<li>callback/promise/event handles result later</li>
</ul>
<h3>Real Thinking Behind Non-Blocking</h3>
<p>Instead of saying:</p>
<blockquote>
<p>“I’ll wait here until task finishes.”</p>
</blockquote>
<p>Node.js says:</p>
<blockquote>
<p>“Start task, notify me later when completed.”</p>
</blockquote>
<p>This creates asynchronous behavior.</p>
<h3>Simple Non-Blocking Example</h3>
<p>Example:</p>
<pre><code class="language-js">fs.readFile("data.txt", "utf8", (err, data) =&gt; {
  console.log(data);
});

console.log("Next task");
</code></pre>
<p>Output:</p>
<pre><code class="language-text">Next task
[file content later]
</code></pre>
<h3>What Happened Internally</h3>
<h3>Step 1</h3>
<p>File reading starts.</p>
<h3>Step 2</h3>
<p>Node.js delegates operation.</p>
<h3>Step 3</h3>
<p>Execution continues immediately.</p>
<h3>Step 4</h3>
<p>When file finishes:</p>
<ul>
<li>callback executes later</li>
</ul>
<p>This is non-blocking behavior.</p>
<h3>Why Blocking Slows Servers</h3>
<p>This is where scalability becomes important.</p>
<h3>Imagine Server with Blocking Code</h3>
<p>Suppose:</p>
<ul>
<li><p>User A requests database data</p>
</li>
<li><p>database takes 5 seconds</p>
</li>
</ul>
<p>If server blocks:</p>
<pre><code class="language-text">User A waits
↓
User B waits
↓
User C waits
</code></pre>
<p>Now entire server slows down.</p>
<h3>Real Problem with Blocking Servers</h3>
<p>Blocking creates:</p>
<ul>
<li><p>request queues</p>
</li>
<li><p>poor scalability</p>
</li>
<li><p>high response times</p>
</li>
<li><p>bad user experience</p>
</li>
</ul>
<p>Especially under heavy traffic.</p>
<h3>Example Scenario</h3>
<p>Suppose:</p>
<ul>
<li>10,000 users connect simultaneously</li>
</ul>
<p>If every request blocks server:</p>
<ul>
<li>performance collapses quickly</li>
</ul>
<h3>Why Node.js Avoids Blocking</h3>
<p>Node.js was designed mainly for:</p>
<pre><code class="language-text">high concurrency
</code></pre>
<p>Meaning:</p>
<ul>
<li><p>many users</p>
</li>
<li><p>many requests</p>
</li>
<li><p>many waiting operations</p>
</li>
</ul>
<p>Node.js solves this using:</p>
<ul>
<li><p>asynchronous processing</p>
</li>
<li><p>event loop</p>
</li>
<li><p>non-blocking I/O</p>
</li>
</ul>
<h3>Real Thinking Behind Node.js Architecture</h3>
<p>Most web applications spend large time:</p>
<ul>
<li><p>waiting for databases</p>
</li>
<li><p>waiting for APIs</p>
</li>
<li><p>waiting for files</p>
</li>
</ul>
<p>CPU is often NOT the bottleneck.</p>
<p>Waiting is.</p>
<p>Node.js optimizes this waiting behavior extremely well.</p>
<h3>Async Operations in Node.js</h3>
<p>Async means:</p>
<pre><code class="language-text">Asynchronous
</code></pre>
<p>Meaning:</p>
<blockquote>
<p>operations happen independently without blocking main execution flow.</p>
</blockquote>
<h3>Common Async Operations</h3>
<p>Examples:</p>
<ul>
<li><p>file reading</p>
</li>
<li><p>database queries</p>
</li>
<li><p>API calls</p>
</li>
<li><p>timers</p>
</li>
<li><p>network communication</p>
</li>
</ul>
<h3>Why These Operations Become Async</h3>
<p>Because these tasks may take:</p>
<ul>
<li><p>milliseconds</p>
</li>
<li><p>seconds</p>
</li>
<li><p>unpredictable time</p>
</li>
</ul>
<p>Blocking main thread for all of them would hurt scalability badly.</p>
<h3>How Node.js Handles Async Operations</h3>
<p>Node.js:</p>
<ol>
<li><p>starts async task</p>
</li>
<li><p>delegates task internally</p>
</li>
<li><p>continues handling other code</p>
</li>
<li><p>processes result later using:</p>
<ul>
<li><p>callbacks</p>
</li>
<li><p>promises</p>
</li>
<li><p>async/await</p>
</li>
</ul>
</li>
</ol>
<h3>Example with Timer</h3>
<p>Example:</p>
<pre><code class="language-js">setTimeout(() =&gt; {
  console.log("Timer completed");
}, 2000);

console.log("Running other code");
</code></pre>
<p>Output:</p>
<pre><code class="language-text">Running other code
Timer completed
</code></pre>
<h3>Important Understanding</h3>
<p>Node.js did NOT pause for 2 seconds.</p>
<p>It continued execution immediately.</p>
<h3>Async Operations and Event Loop</h3>
<p>Completed async tasks eventually return through:</p>
<pre><code class="language-text">Event Loop
</code></pre>
<p>The event loop checks:</p>
<ul>
<li><p>completed tasks</p>
</li>
<li><p>pending callbacks</p>
</li>
</ul>
<p>and executes them when main stack becomes free.</p>
<h3>Real-World Examples (File Read, DB Calls)</h3>
<h3>Example 1 — File Reading</h3>
<p>Suppose application reads uploaded file.</p>
<h3>Blocking Version</h3>
<p>Example:</p>
<pre><code class="language-js">const data = fs.readFileSync("file.txt");

console.log(data);
</code></pre>
<p>Problem:</p>
<ul>
<li>server waits during file reading</li>
</ul>
<h3>Non-Blocking Version</h3>
<p>Example:</p>
<pre><code class="language-js">fs.readFile("file.txt", "utf8", (err, data) =&gt; {
  console.log(data);
});
</code></pre>
<p>Now:</p>
<ul>
<li>server continues handling other requests</li>
</ul>
<h3>Why This Matters</h3>
<p>Suppose:</p>
<ul>
<li>thousands of users upload files</li>
</ul>
<p>Blocking file reads would slow entire server.</p>
<h3>Example 2 — Database Calls</h3>
<p>Database operations are usually slow compared to CPU execution.</p>
<p>Example:</p>
<pre><code class="language-js">const user = await User.findById(id);
</code></pre>
<p>Database may take:</p>
<ul>
<li><p>network latency</p>
</li>
<li><p>disk access time</p>
</li>
<li><p>query processing time</p>
</li>
</ul>
<h3>Why Async DB Calls Matter</h3>
<p>If every database query blocked server:</p>
<ul>
<li>APIs would become extremely slow under load</li>
</ul>
<p>Node.js handles DB operations asynchronously so:</p>
<ul>
<li>other requests continue meanwhile</li>
</ul>
<h3>Example 3 — External APIs</h3>
<p>Suppose backend calls payment service.</p>
<p>Example:</p>
<pre><code class="language-js">const response = await fetch(apiUrl);
</code></pre>
<p>External APIs may take:</p>
<ul>
<li><p>seconds</p>
</li>
<li><p>retries</p>
</li>
<li><p>network delays</p>
</li>
</ul>
<p>Non-blocking behavior allows server to stay responsive meanwhile.</p>
<h3>Example 4 — Chat Applications</h3>
<p>Real-time chat systems constantly wait for:</p>
<ul>
<li><p>incoming messages</p>
</li>
<li><p>socket events</p>
</li>
</ul>
<p>Node.js efficiently handles thousands of waiting connections using:</p>
<ul>
<li><p>event loop</p>
</li>
<li><p>non-blocking architecture</p>
</li>
</ul>
<h3>Why Non-Blocking Became So Important</h3>
<p>Modern internet applications are heavily:</p>
<pre><code class="language-text">I/O bound
</code></pre>
<p>Meaning:</p>
<ul>
<li>waiting dominates execution time</li>
</ul>
<p>Node.js became powerful because it handles waiting efficiently.</p>
<h3>Important Understanding About CPU Tasks</h3>
<p>Non-blocking works extremely well for:</p>
<ul>
<li>I/O-heavy operations</li>
</ul>
<p>But CPU-heavy calculations can still block main thread.</p>
<p>Examples:</p>
<ul>
<li><p>video rendering</p>
</li>
<li><p>heavy image processing</p>
</li>
<li><p>large mathematical computations</p>
</li>
</ul>
<h3>Common Beginner Mistake</h3>
<p>Many beginners think:</p>
<ul>
<li>async code means parallel JavaScript execution</li>
</ul>
<p>Not exactly.</p>
<p>JavaScript execution itself mainly stays single-threaded. Concurrency comes from:</p>
<ul>
<li><p>async delegation</p>
</li>
<li><p>event loop scheduling</p>
</li>
</ul>
<h3>Another Common Mistake</h3>
<p>People often overuse synchronous functions like:</p>
<pre><code class="language-js">readFileSync()
</code></pre>
<p>inside production servers. This can hurt scalability badly.</p>
<h3>Why Understanding Blocking vs Non-Blocking Matters</h3>
<p>This concept is one of the foundations of:</p>
<ul>
<li><p>backend scalability</p>
</li>
<li><p>API performance</p>
</li>
<li><p>Node.js architecture</p>
</li>
<li><p>server efficiency</p>
</li>
</ul>
<p>Without understanding it:</p>
<ul>
<li>developers struggle understanding async systems properly.</li>
</ul>
<h3>Final Understanding</h3>
<p>Blocking code pauses execution until a task fully finishes, while non-blocking code allows applications to continue doing other work meanwhile.</p>
<p>Blocking operations can severely slow servers because one slow request may delay many other users.</p>
<p>NodeJS solves this using:</p>
<ul>
<li><p>asynchronous operations</p>
</li>
<li><p>event loop</p>
</li>
<li><p>non-blocking I/O</p>
</li>
</ul>
<p>allowing operations like:</p>
<ul>
<li><p>file reading</p>
</li>
<li><p>database queries</p>
</li>
<li><p>API requests</p>
</li>
</ul>
<p>to execute efficiently without freezing the main execution flow.</p>
<p>And this non-blocking architecture is one of the biggest reasons Node.js scales so well for modern backend systems.</p>
]]></content:encoded></item><item><title><![CDATA[JWT Authentication in Node.js Explained Simply]]></title><description><![CDATA[What Authentication Means
Whenever users log into applications like:

social media platforms

banking apps

e-commerce websites

dashboards


the system needs a way to verify:

“Is this user really wh]]></description><link>https://backendwithshubham.hashnode.dev/jwt-authentication-in-node-js-explained-simply</link><guid isPermaLink="true">https://backendwithshubham.hashnode.dev/jwt-authentication-in-node-js-explained-simply</guid><dc:creator><![CDATA[Shubham Bhardwaj]]></dc:creator><pubDate>Sun, 10 May 2026 15:31:50 GMT</pubDate><content:encoded><![CDATA[<h3>What Authentication Means</h3>
<p>Whenever users log into applications like:</p>
<ul>
<li><p>social media platforms</p>
</li>
<li><p>banking apps</p>
</li>
<li><p>e-commerce websites</p>
</li>
<li><p>dashboards</p>
</li>
</ul>
<p>the system needs a way to verify:</p>
<blockquote>
<p>“Is this user really who they claim to be?”</p>
</blockquote>
<p>That verification process is called:</p>
<pre><code class="language-text">Authentication
</code></pre>
<p>Authentication basically means:</p>
<blockquote>
<p>Confirming user identity.</p>
</blockquote>
<h3>Real Purpose of Authentication</h3>
<p>Without authentication:</p>
<ul>
<li><p>anyone could access anyone’s account</p>
</li>
<li><p>private data would become exposed</p>
</li>
<li><p>systems would become insecure</p>
</li>
</ul>
<p>Authentication protects:</p>
<ul>
<li><p>user accounts</p>
</li>
<li><p>private information</p>
</li>
<li><p>restricted features</p>
</li>
</ul>
<h3>Simple Real-World Analogy</h3>
<p>Think about airport security. Before boarding:</p>
<ul>
<li><p>identity gets verified</p>
</li>
<li><p>ticket gets checked</p>
</li>
<li><p>access gets granted</p>
</li>
</ul>
<p>Authentication works similarly in software systems.</p>
<h3>Authentication vs Authorization</h3>
<p>Beginners often confuse these.</p>
<h3>Authentication</h3>
<pre><code class="language-text">Who are you?
</code></pre>
<h3>Authorization</h3>
<pre><code class="language-text">What are you allowed to access?
</code></pre>
<h3>Example</h3>
<p>Suppose user logs into admin panel.</p>
<h4>Authentication</h4>
<p>Checks:</p>
<blockquote>
<p>Is this really Shubham?</p>
</blockquote>
<h4>Authorization</h4>
<p>Checks:</p>
<blockquote>
<p>Is Shubham allowed to access admin dashboard?</p>
</blockquote>
<h3>What JWT Is</h3>
<p>JWT stands for:</p>
<pre><code class="language-text">JSON Web Token
</code></pre>
<p>JWT is one of the most popular modern authentication methods.</p>
<p>It is basically:</p>
<blockquote>
<p>A secure token used for verifying users between client and server.</p>
</blockquote>
<h3>Why JWT Became Popular</h3>
<p>Modern applications often have:</p>
<ul>
<li><p>separate frontend</p>
</li>
<li><p>separate backend</p>
</li>
<li><p>mobile apps</p>
</li>
<li><p>APIs</p>
</li>
<li><p>microservices</p>
</li>
</ul>
<p>Traditional session systems become harder to manage at scale.</p>
<p>JWT solved many of these problems using:</p>
<pre><code class="language-text">stateless authentication
</code></pre>
<h3>Understanding JWT Simply</h3>
<p>After login:</p>
<ul>
<li><p>server creates token</p>
</li>
<li><p>token gets sent to client</p>
</li>
<li><p>client sends token with future requests</p>
</li>
</ul>
<p>Server verifies token.</p>
<p>If valid:</p>
<ul>
<li>user becomes authenticated</li>
</ul>
<h3>Real Thinking Behind JWT</h3>
<p>Instead of server storing user login session:</p>
<ul>
<li>client carries authentication token</li>
</ul>
<p>This makes systems easier to scale.</p>
<h3>Structure of a JWT</h3>
<p>JWT contains three parts:</p>
<pre><code class="language-text">Header.Payload.Signature
</code></pre>
<p>Example:</p>
<pre><code class="language-text">xxxxx.yyyyy.zzzzz
</code></pre>
<p>Each part has different responsibility.</p>
<h3>Header</h3>
<p>Header contains metadata about token.</p>
<p>Usually includes:</p>
<ul>
<li><p>token type</p>
</li>
<li><p>signing algorithm</p>
</li>
</ul>
<p>Example:</p>
<pre><code class="language-json">{
  "alg": "HS256",
  "typ": "JWT"
}
</code></pre>
<h3>Understanding Header Simply</h3>
<p>Header says:</p>
<blockquote>
<p>“This is a JWT token and this algorithm was used.”</p>
</blockquote>
<h3>Payload</h3>
<p>Payload contains actual data.</p>
<p>Usually includes:</p>
<ul>
<li><p>user ID</p>
</li>
<li><p>email</p>
</li>
<li><p>role</p>
</li>
<li><p>expiration time</p>
</li>
</ul>
<p>Example:</p>
<pre><code class="language-json">{
  "userId": 10,
  "email": "test@gmail.com"
}
</code></pre>
<h3>Important Understanding About Payload</h3>
<p>Payload is:</p>
<pre><code class="language-text">encoded
</code></pre>
<p>NOT fully encrypted by default.</p>
<p>Meaning:</p>
<ul>
<li><p>people can decode it</p>
</li>
<li><p>but cannot safely modify it because of signature verification</p>
</li>
</ul>
<p>So sensitive information should not be stored carelessly.</p>
<h3>Signature</h3>
<p>Signature is the security part.</p>
<p>It helps verify:</p>
<blockquote>
<p>token was not modified.</p>
</blockquote>
<p>Server creates signature using:</p>
<ul>
<li><p>secret key</p>
</li>
<li><p>header</p>
</li>
<li><p>payload</p>
</li>
</ul>
<h3>Why Signature Matters</h3>
<p>Suppose attacker changes:</p>
<pre><code class="language-json">{
  "role": "admin"
}
</code></pre>
<p>Signature verification fails.</p>
<p>Server detects tampering.</p>
<h3>Real JWT Structure Flow</h3>
<pre><code class="language-text">Header
   ↓
Payload
   ↓
Signature
</code></pre>
<p>Together these form complete JWT token.</p>
<h3>Login Flow Using JWT</h3>
<p>This is one of the most important backend concepts.</p>
<h3>Step 1 — User Sends Login Request</h3>
<p>Example:</p>
<pre><code class="language-json">{
  "email": "test@gmail.com",
  "password": "123456"
}
</code></pre>
<h3>Step 2 — Server Verifies Credentials</h3>
<p>Backend checks:</p>
<ul>
<li><p>email exists</p>
</li>
<li><p>password is correct</p>
</li>
</ul>
<h3>Step 3 — Server Creates JWT</h3>
<p>If credentials are valid:</p>
<ul>
<li>server generates token</li>
</ul>
<p>Example:</p>
<pre><code class="language-js">jwt.sign(payload, secretKey);
</code></pre>
<h3>Step 4 — Token Sent to Client</h3>
<p>Example response:</p>
<pre><code class="language-json">{
  "token": "xxxxx.yyyyy.zzzzz"
}
</code></pre>
<h3>Step 5 — Client Stores Token</h3>
<p>Usually stored in:</p>
<ul>
<li><p>localStorage</p>
</li>
<li><p>cookies</p>
</li>
<li><p>memory</p>
</li>
</ul>
<p>depending on architecture.</p>
<h3>Step 6 — Client Sends Token With Future Requests</h3>
<p>Now every protected request includes token.</p>
<h3>Simple JWT Login Flow</h3>
<pre><code class="language-text">User logs in
   ↓
Server verifies credentials
   ↓
JWT generated
   ↓
Client stores token
   ↓
Token sent with future requests
</code></pre>
<h3>Sending Token With Requests</h3>
<p>After login:</p>
<ul>
<li>client must prove authentication repeatedly</li>
</ul>
<p>This is done using token.</p>
<h3>Most Common Method — Authorization Header</h3>
<p>Example:</p>
<pre><code class="language-http">Authorization: Bearer TOKEN
</code></pre>
<h3>Understanding This</h3>
<h4>Authorization</h4>
<p>HTTP header name.</p>
<h4>Bearer</h4>
<p>Means:</p>
<blockquote>
<p>token-based authentication.</p>
</blockquote>
<h4>TOKEN</h4>
<p>Actual JWT string.</p>
<h3>Example Request</h3>
<p>Example:</p>
<pre><code class="language-http">GET /profile
Authorization: Bearer xxxxx.yyyyy.zzzzz
</code></pre>
<h3>Why Tokens Are Sent Every Time</h3>
<p>Because HTTP is stateless. Server does not automatically remember previous requests. So token gets attached repeatedly.</p>
<h3>Protecting Routes Using Tokens</h3>
<p>Not every route should be public. Examples of protected routes:</p>
<ul>
<li><p>profile pages</p>
</li>
<li><p>dashboards</p>
</li>
<li><p>payments</p>
</li>
<li><p>admin panels</p>
</li>
</ul>
<p>JWT helps protect these routes.</p>
<h3>Basic Route Protection Flow</h3>
<h3>Step 1</h3>
<p>User sends token.</p>
<h3>Step 2</h3>
<p>Backend verifies token.</p>
<h3>Step 3</h3>
<p>If valid:</p>
<ul>
<li>request continues</li>
</ul>
<h3>Step 4</h3>
<p>If invalid:</p>
<ul>
<li>access denied</li>
</ul>
<h3>Example Middleware</h3>
<p>Example:</p>
<pre><code class="language-js">function auth(req, res, next) {
  const token = req.headers.authorization;

  next();
}
</code></pre>
<h3>What Middleware Does</h3>
<p>Middleware acts like:</p>
<blockquote>
<p>security checkpoint before route access.</p>
</blockquote>
<h3>Token Verification Example</h3>
<p>Example:</p>
<pre><code class="language-js">jwt.verify(token, secretKey);
</code></pre>
<p>Server checks:</p>
<ul>
<li><p>signature validity</p>
</li>
<li><p>expiration</p>
</li>
<li><p>token integrity</p>
</li>
</ul>
<h3>Protected Route Example</h3>
<p>Example:</p>
<pre><code class="language-js">app.get("/dashboard", auth, (req, res) =&gt; {
  res.send("Protected data");
});
</code></pre>
<p>Only authenticated users can access this route.</p>
<h3>What Happens If Token Is Missing</h3>
<p>Server usually responds:</p>
<pre><code class="language-text">401 Unauthorized
</code></pre>
<p>Meaning:</p>
<blockquote>
<p>authentication required.</p>
</blockquote>
<h3>What Happens If Token Is Invalid</h3>
<p>Examples:</p>
<ul>
<li><p>fake token</p>
</li>
<li><p>modified token</p>
</li>
<li><p>expired token</p>
</li>
</ul>
<p>Verification fails.</p>
<p>Access gets blocked.</p>
<h3>Why JWT Works Well for APIs</h3>
<p>Modern frontend applications often communicate with backend APIs separately. JWT works extremely well because:</p>
<ul>
<li><p>frontend and backend stay independent</p>
</li>
<li><p>mobile apps can use same token</p>
</li>
<li><p>servers remain stateless</p>
</li>
</ul>
<h3>Real-World JWT Use Cases</h3>
<p>Examples:</p>
<ul>
<li><p>React frontend + Node.js backend</p>
</li>
<li><p>mobile applications</p>
</li>
<li><p>REST APIs</p>
</li>
<li><p>microservices</p>
</li>
<li><p>cloud systems</p>
</li>
</ul>
<h3>Important Security Understanding</h3>
<p>JWT itself is not automatically secure. Security also depends on:</p>
<ul>
<li><p>HTTPS usage</p>
</li>
<li><p>token storage method</p>
</li>
<li><p>expiration handling</p>
</li>
<li><p>secret key protection</p>
</li>
</ul>
<h3>Common Beginner Mistake</h3>
<p>Many beginners think:</p>
<ul>
<li>JWT encrypts everything</li>
</ul>
<p>Not true.</p>
<p>JWT payload is usually:</p>
<pre><code class="language-text">encoded
</code></pre>
<p>not fully encrypted.</p>
<h3>Another Common Mistake</h3>
<p>People often store highly sensitive information directly inside token payload. This is dangerous because payload can be decoded.</p>
<h3>JWT Expiration</h3>
<p>JWTs commonly include expiration time. Example:</p>
<pre><code class="language-js">expiresIn: "1h"
</code></pre>
<p>This improves security.</p>
<h3>Why Expiration Matters</h3>
<p>If token gets stolen:</p>
<ul>
<li>attacker cannot use it forever</li>
</ul>
<h3>Why JWT Became So Popular</h3>
<p>JWT simplifies:</p>
<ul>
<li><p>scalable authentication</p>
</li>
<li><p>API security</p>
</li>
<li><p>cross-platform login systems</p>
</li>
</ul>
<p>Especially for modern full-stack applications.</p>
<h3>Final Understanding</h3>
<p>Authentication is the process of verifying user identity. JWT (JSON Web Token) is a popular authentication mechanism that allows clients and servers to securely exchange authentication information using tokens.</p>
<p>A JWT contains:</p>
<ul>
<li><p>Header</p>
</li>
<li><p>Payload</p>
</li>
<li><p>Signature</p>
</li>
</ul>
<p>During login:</p>
<ul>
<li><p>server verifies credentials</p>
</li>
<li><p>generates token</p>
</li>
<li><p>sends token to client</p>
</li>
</ul>
<p>Client then sends token with future requests using:</p>
<pre><code class="language-http">Authorization: Bearer TOKEN
</code></pre>
<p>Servers protect private routes by verifying token validity before allowing access.</p>
<p>And this token-based authentication approach became one of the most widely used authentication systems in modern API and full-stack development.</p>
]]></content:encoded></item><item><title><![CDATA[The Node.js Event Loop Explained]]></title><description><![CDATA[What the Event Loop Is
One of the most important concepts in :contentReference[oaicite:0]{index=0} is:
Event Loop

Almost every powerful feature of Node.js depends on it. Without the event loop:

asyn]]></description><link>https://backendwithshubham.hashnode.dev/the-node-js-event-loop-explained</link><guid isPermaLink="true">https://backendwithshubham.hashnode.dev/the-node-js-event-loop-explained</guid><dc:creator><![CDATA[Shubham Bhardwaj]]></dc:creator><pubDate>Sun, 10 May 2026 15:27:27 GMT</pubDate><content:encoded><![CDATA[<h3>What the Event Loop Is</h3>
<p>One of the most important concepts in :contentReference[oaicite:0]{index=0} is:</p>
<pre><code class="language-text">Event Loop
</code></pre>
<p>Almost every powerful feature of Node.js depends on it. Without the event loop:</p>
<ul>
<li><p>asynchronous programming would break</p>
</li>
<li><p>Node.js scalability would collapse</p>
</li>
<li><p>non-blocking behavior would not exist</p>
</li>
</ul>
<p>The event loop is basically:</p>
<blockquote>
<p>A system inside Node.js that manages asynchronous operations and decides what code should run next.</p>
</blockquote>
<h3>Understanding the Real Purpose of Event Loop</h3>
<p>JavaScript itself can only execute:</p>
<blockquote>
<p>one thing at a time.</p>
</blockquote>
<p>Meaning:</p>
<ul>
<li><p>one function executes</p>
</li>
<li><p>then next function</p>
</li>
<li><p>then next</p>
</li>
</ul>
<p>This sounds limiting. Because modern applications constantly deal with:</p>
<ul>
<li><p>APIs</p>
</li>
<li><p>databases</p>
</li>
<li><p>file systems</p>
</li>
<li><p>timers</p>
</li>
<li><p>network requests</p>
</li>
</ul>
<p>All these operations can take time. If JavaScript waited for every task to finish:</p>
<ul>
<li><p>applications would freeze</p>
</li>
<li><p>servers would become slow</p>
</li>
<li><p>scalability would suffer badly</p>
</li>
</ul>
<p>The event loop solves this problem.</p>
<h3>Simple Real-World Analogy</h3>
<p>Imagine restaurant manager. Instead of:</p>
<ul>
<li><p>personally cooking food</p>
</li>
<li><p>waiting beside oven</p>
</li>
<li><p>wasting time</p>
</li>
</ul>
<p>manager:</p>
<ul>
<li><p>delegates kitchen work</p>
</li>
<li><p>handles other customers meanwhile</p>
</li>
<li><p>returns when food becomes ready</p>
</li>
</ul>
<p>That is similar to how event loop works.</p>
<h3>Why Node.js Needs an Event Loop</h3>
<p>This is extremely important.</p>
<p>Node.js is mainly:</p>
<pre><code class="language-text">single-threaded
</code></pre>
<p>Meaning:</p>
<ul>
<li>JavaScript execution happens on one main thread</li>
</ul>
<p>Now imagine thousands of users:</p>
<ul>
<li><p>opening APIs</p>
</li>
<li><p>requesting database data</p>
</li>
<li><p>uploading files</p>
</li>
</ul>
<p>If Node.js blocked main thread for every operation:</p>
<ul>
<li>everything would slow down massively</li>
</ul>
<h3>Example Problem Without Event Loop</h3>
<p>Suppose database query takes:</p>
<pre><code class="language-text">5 seconds
</code></pre>
<p>Without event loop:</p>
<pre><code class="language-text">Request A starts
↓
Server waits 5 seconds
↓
Request B waits
↓
Request C waits
</code></pre>
<p>This would be terrible for scalability.</p>
<h3>Event Loop Solves This</h3>
<p>Instead:</p>
<ul>
<li><p>slow tasks get delegated</p>
</li>
<li><p>Node.js continues handling other work</p>
</li>
<li><p>completed operations later return through callbacks/promises</p>
</li>
</ul>
<p>This creates:</p>
<pre><code class="language-text">non-blocking behavior
</code></pre>
<h3>Real Thinking Behind Event Loop</h3>
<p>The event loop allows Node.js to say:</p>
<blockquote>
<p>“Start this task, and notify me when it finishes. Meanwhile I’ll continue doing other work.”</p>
</blockquote>
<p>That single idea changed backend scalability massively.</p>
<h3>Task Queue vs Call Stack (Conceptual Only)</h3>
<p>To understand event loop properly, two concepts matter:</p>
<ul>
<li><p>Call Stack</p>
</li>
<li><p>Task Queue</p>
</li>
</ul>
<h3>What Is Call Stack?</h3>
<p>Call stack is where:</p>
<blockquote>
<p>JavaScript executes functions.</p>
</blockquote>
<p>Whenever a function runs:</p>
<ul>
<li><p>it enters stack</p>
</li>
<li><p>executes</p>
</li>
<li><p>gets removed</p>
</li>
</ul>
<h3>Simple Example</h3>
<p>Example:</p>
<pre><code class="language-js">function one() {
  console.log("One");
}

function two() {
  console.log("Two");
}

one();
two();
</code></pre>
<p>Execution order:</p>
<pre><code class="language-text">one() enters stack
one() finishes
two() enters stack
two() finishes
</code></pre>
<h3>Important Understanding</h3>
<p>JavaScript can execute:</p>
<blockquote>
<p>only one stack operation at a time.</p>
</blockquote>
<p>That is why JavaScript itself is synchronous by default.</p>
<h3>What Is Task Queue?</h3>
<p>Task queue stores:</p>
<blockquote>
<p>completed asynchronous callbacks waiting to execute.</p>
</blockquote>
<p>Example:</p>
<ul>
<li><p>timer completed</p>
</li>
<li><p>file read completed</p>
</li>
<li><p>API response returned</p>
</li>
</ul>
<p>Callbacks wait inside queue until stack becomes empty.</p>
<h3>Simple Mental Model</h3>
<h4>Call Stack</h4>
<p>Currently executing work.</p>
<h4>Task Queue</h4>
<p>Waiting work ready for execution.</p>
<h3>How Event Loop Connects Them</h3>
<p>The event loop continuously checks:</p>
<pre><code class="language-text">Is call stack empty?
</code></pre>
<p>If yes:</p>
<ul>
<li><p>it takes next callback from task queue</p>
</li>
<li><p>pushes it into stack</p>
</li>
</ul>
<p>This cycle repeats continuously.</p>
<h3>Simple Flow</h3>
<pre><code class="language-text">Call Stack empty?
      ↓
Yes
      ↓
Take callback from queue
      ↓
Execute callback
      ↓
Repeat forever
</code></pre>
<p>That repeating cycle is:</p>
<pre><code class="language-text">the event loop
</code></pre>
<h3>How Async Operations Are Handled</h3>
<p>This is where Node.js becomes powerful.</p>
<h3>Example</h3>
<p>Suppose code:</p>
<pre><code class="language-js">setTimeout(() =&gt; {
  console.log("Done");
}, 2000);

console.log("Hello");
</code></pre>
<h3>What Beginners Expect</h3>
<p>Many beginners think:</p>
<pre><code class="language-text">Wait 2 seconds
Then print everything
</code></pre>
<p>But actual output:</p>
<pre><code class="language-text">Hello
Done
</code></pre>
<h3>What Actually Happened Internally</h3>
<h3>Step 1</h3>
<p><code>setTimeout</code> starts timer.</p>
<h3>Step 2</h3>
<p>Timer gets delegated outside main thread.</p>
<h3>Step 3</h3>
<p>Main thread immediately continues.</p>
<pre><code class="language-js">console.log("Hello");
</code></pre>
<p>executes instantly.</p>
<h3>Step 4</h3>
<p>After 2 seconds:</p>
<ul>
<li>callback enters task queue</li>
</ul>
<h3>Step 5</h3>
<p>When stack becomes empty:</p>
<ul>
<li>event loop moves callback into stack</li>
</ul>
<h3>Step 6</h3>
<pre><code class="language-js">console.log("Done");
</code></pre>
<p>executes.</p>
<h3>Important Understanding</h3>
<p>JavaScript never sat idle waiting for timer. That is the power of asynchronous handling.</p>
<h3>Async Operations Usually Delegated</h3>
<p>Node.js delegates operations like:</p>
<ul>
<li><p>file reading</p>
</li>
<li><p>database queries</p>
</li>
<li><p>network communication</p>
</li>
<li><p>timers</p>
</li>
<li><p>DNS lookups</p>
</li>
</ul>
<p>while event loop keeps running.</p>
<h3>Why This Creates Scalability</h3>
<p>Suppose 10,000 users request APIs simultaneously.</p>
<p>Node.js:</p>
<ul>
<li><p>starts async operations</p>
</li>
<li><p>continues handling other users</p>
</li>
<li><p>processes completed callbacks later</p>
</li>
</ul>
<p>without creating massive thread overhead.</p>
<h3>Timers vs I/O Callbacks (High Level)</h3>
<p>Different async operations enter event loop differently.</p>
<p>At high level:</p>
<ul>
<li><p>timers</p>
</li>
<li><p>I/O callbacks</p>
</li>
</ul>
<p>are handled in different phases internally.</p>
<h3>Timers</h3>
<p>Examples:</p>
<ul>
<li><p><code>setTimeout</code></p>
</li>
<li><p><code>setInterval</code></p>
</li>
</ul>
<p>These callbacks execute after timer duration completes.</p>
<p>Example:</p>
<pre><code class="language-js">setTimeout(() =&gt; {
  console.log("Timer");
}, 1000);
</code></pre>
<h3>I/O Callbacks</h3>
<p>Examples:</p>
<ul>
<li><p>file reading</p>
</li>
<li><p>database responses</p>
</li>
<li><p>network requests</p>
</li>
</ul>
<p>Example:</p>
<pre><code class="language-js">fs.readFile("file.txt", () =&gt; {
  console.log("File loaded");
});
</code></pre>
<p>These callbacks execute when I/O operation completes.</p>
<h3>Important Understanding</h3>
<p>Both eventually reach:</p>
<ul>
<li><p>task queue</p>
</li>
<li><p>event loop processing</p>
</li>
</ul>
<p>But internally Node.js organizes them differently for efficiency.</p>
<h3>Real Difference</h3>
<h4>Timers</h4>
<p>Triggered by time completion.</p>
<h4>I/O Callbacks</h4>
<p>Triggered by operation completion.</p>
<h3>Role of Event Loop in Scalability</h3>
<p>This is the heart of Node.js performance.</p>
<h3>Traditional Blocking Systems</h3>
<p>Suppose every request blocks one thread.</p>
<p>Now imagine:</p>
<ul>
<li><p>100,000 users</p>
</li>
<li><p>100,000 threads</p>
</li>
</ul>
<p>Huge:</p>
<ul>
<li><p>memory usage</p>
</li>
<li><p>CPU overhead</p>
</li>
<li><p>thread scheduling complexity</p>
</li>
</ul>
<h3>Node.js Approach</h3>
<p>Node.js mainly uses:</p>
<ul>
<li><p>one JavaScript thread</p>
</li>
<li><p>asynchronous delegation</p>
</li>
<li><p>event loop coordination</p>
</li>
</ul>
<p>This dramatically reduces overhead.</p>
<h3>Why Event Loop Makes Node.js Scalable</h3>
<p>Because while one request waits for:</p>
<ul>
<li><p>database</p>
</li>
<li><p>API</p>
</li>
<li><p>file system</p>
</li>
</ul>
<p>Node.js continues serving:</p>
<ul>
<li><p>other users</p>
</li>
<li><p>other requests</p>
</li>
<li><p>other callbacks</p>
</li>
</ul>
<p>This creates very high concurrency.</p>
<h3>Real Applications Benefiting from Event Loop</h3>
<p>Examples:</p>
<ul>
<li><p>chat applications</p>
</li>
<li><p>streaming platforms</p>
</li>
<li><p>APIs</p>
</li>
<li><p>live notifications</p>
</li>
<li><p>multiplayer systems</p>
</li>
</ul>
<h3>Example — Chat App</h3>
<p>Thousands of users connected simultaneously. Most connections spend time:</p>
<ul>
<li>waiting for messages</li>
</ul>
<p>Node.js efficiently manages these waiting operations using:</p>
<ul>
<li><p>event loop</p>
</li>
<li><p>asynchronous sockets</p>
</li>
</ul>
<h3>Example — APIs</h3>
<p>Most APIs spend significant time waiting for:</p>
<ul>
<li><p>database responses</p>
</li>
<li><p>external services</p>
</li>
</ul>
<p>Node.js handles these waiting periods extremely efficiently.</p>
<h3>Important Understanding About Scalability</h3>
<p>The event loop does NOT make CPU-heavy calculations faster.</p>
<p>It mainly improves:</p>
<pre><code class="language-text">I/O concurrency
</code></pre>
<p>Meaning:</p>
<ul>
<li>many waiting operations can coexist efficiently.</li>
</ul>
<h3>Common Beginner Mistake</h3>
<p>Many beginners think:</p>
<ul>
<li>event loop itself performs async work</li>
</ul>
<p>Not exactly.</p>
<p>Actual async operations are often handled by:</p>
<ul>
<li><p>operating system</p>
</li>
<li><p>background workers</p>
</li>
<li><p>system APIs</p>
</li>
</ul>
<p>Event loop mainly:</p>
<blockquote>
<p>coordinates completed callbacks.</p>
</blockquote>
<h3>Another Common Mistake</h3>
<p>People often think:</p>
<ul>
<li>async code runs magically in parallel inside JavaScript thread</li>
</ul>
<p>But JavaScript execution itself still mainly happens sequentially. Concurrency comes from:</p>
<ul>
<li><p>delegation</p>
</li>
<li><p>callback scheduling</p>
</li>
<li><p>event loop orchestration</p>
</li>
</ul>
<h3>Why Event Loop Is One of the Most Important Backend Concepts</h3>
<p>Once developers understand:</p>
<ul>
<li><p>call stack</p>
</li>
<li><p>task queue</p>
</li>
<li><p>async delegation</p>
</li>
<li><p>callback execution</p>
</li>
</ul>
<p>many “magic” behaviors of Node.js suddenly become understandable. This concept becomes foundation for:</p>
<ul>
<li><p>async/await</p>
</li>
<li><p>promises</p>
</li>
<li><p>APIs</p>
</li>
<li><p>backend scalability</p>
</li>
<li><p>real-time systems</p>
</li>
</ul>
<h3>Final Understanding</h3>
<p>The event loop is the core mechanism that allows :contentReference[oaicite:1]{index=1} to handle asynchronous operations efficiently.</p>
<p>It continuously coordinates between:</p>
<ul>
<li><p>call stack</p>
</li>
<li><p>task queue</p>
</li>
<li><p>completed async operations</p>
</li>
</ul>
<p>allowing Node.js to remain non-blocking even though JavaScript mainly runs on a single thread. By delegating slow operations like:</p>
<ul>
<li><p>timers</p>
</li>
<li><p>file reading</p>
</li>
<li><p>database communication</p>
</li>
<li><p>network requests</p>
</li>
</ul>
<p>Node.js can continue handling other requests simultaneously, which is one of the major reasons it scales so efficiently for modern backend applications.</p>
]]></content:encoded></item><item><title><![CDATA[URL Parameters vs Query Strings in Express.js]]></title><description><![CDATA[What URL Parameters Are
Whenever websites or APIs need dynamic values inside URLs, they often use:
URL Parameters

URL parameters are values placed directly inside the route path.
Example:
/users/10

]]></description><link>https://backendwithshubham.hashnode.dev/url-parameters-vs-query-strings-in-express-js</link><guid isPermaLink="true">https://backendwithshubham.hashnode.dev/url-parameters-vs-query-strings-in-express-js</guid><dc:creator><![CDATA[Shubham Bhardwaj]]></dc:creator><pubDate>Sun, 10 May 2026 15:12:20 GMT</pubDate><content:encoded><![CDATA[<h3>What URL Parameters Are</h3>
<p>Whenever websites or APIs need dynamic values inside URLs, they often use:</p>
<pre><code class="language-text">URL Parameters
</code></pre>
<p>URL parameters are values placed directly inside the route path.</p>
<p>Example:</p>
<pre><code class="language-text">/users/10
</code></pre>
<p>Here:</p>
<ul>
<li><code>10</code> is dynamic value</li>
</ul>
<p>Usually it represents:</p>
<ul>
<li><p>user ID</p>
</li>
<li><p>product ID</p>
</li>
<li><p>post ID</p>
</li>
<li><p>resource identifier</p>
</li>
</ul>
<h3>Understanding URL Parameters Simply</h3>
<p>Think of URL parameters like:</p>
<blockquote>
<p>placeholders inside URL paths.</p>
</blockquote>
<p>Example route:</p>
<pre><code class="language-text">/users/:id
</code></pre>
<p>Here:</p>
<ul>
<li><code>:id</code> is parameter placeholder</li>
</ul>
<p>If user visits:</p>
<pre><code class="language-text">/users/25
</code></pre>
<p>then:</p>
<pre><code class="language-text">id = 25
</code></pre>
<h3>Why URL Parameters Exist</h3>
<p>Applications constantly need dynamic routes.</p>
<p>Examples:</p>
<ul>
<li><p>specific user profile</p>
</li>
<li><p>single blog post</p>
</li>
<li><p>single product page</p>
</li>
<li><p>specific order details</p>
</li>
</ul>
<p>Instead of creating separate routes manually:</p>
<pre><code class="language-text">/user1
/user2
/user3
</code></pre>
<p>developers use dynamic parameters.</p>
<h3>Real Example</h3>
<p>Example route:</p>
<pre><code class="language-text">/products/:productId
</code></pre>
<p>Possible URLs:</p>
<pre><code class="language-text">/products/101
/products/205
/products/999
</code></pre>
<p>Same route handles everything dynamically.</p>
<h3>What Query Parameters Are</h3>
<p>Query parameters are values added after:</p>
<pre><code class="language-text">?
</code></pre>
<p>inside URL.</p>
<p>Example:</p>
<pre><code class="language-text">/products?category=mobile
</code></pre>
<p>Here:</p>
<ul>
<li><code>category=mobile</code> is query parameter</li>
</ul>
<h3>Structure of Query Parameters</h3>
<p>Basic structure:</p>
<pre><code class="language-text">?key=value
</code></pre>
<p>Multiple query parameters:</p>
<pre><code class="language-text">/products?category=mobile&amp;price=1000
</code></pre>
<h3>Understanding Query Parameters Simply</h3>
<p>Query parameters are mainly used for:</p>
<ul>
<li><p>filtering</p>
</li>
<li><p>searching</p>
</li>
<li><p>sorting</p>
</li>
<li><p>pagination</p>
</li>
</ul>
<p>They provide extra optional information.</p>
<h3>Real Example</h3>
<p>Example:</p>
<pre><code class="language-text">/products?page=2
</code></pre>
<p>This may mean:</p>
<blockquote>
<p>fetch second page of products.</p>
</blockquote>
<h3>Differences Between URL Params and Query Params</h3>
<p>This is one of the most important beginner concepts.</p>
<h3>URL Parameters</h3>
<p>Usually identify:</p>
<blockquote>
<p>specific resource.</p>
</blockquote>
<p>Example:</p>
<pre><code class="language-text">/users/10
</code></pre>
<p>Meaning:</p>
<blockquote>
<p>user with ID 10.</p>
</blockquote>
<h3>Query Parameters</h3>
<p>Usually modify:</p>
<blockquote>
<p>how data should be returned.</p>
</blockquote>
<p>Example:</p>
<pre><code class="language-text">/users?sort=asc
</code></pre>
<p>Meaning:</p>
<blockquote>
<p>return users sorted ascending.</p>
</blockquote>
<h3>Simple Thinking Difference</h3>
<h4>Params</h4>
<p>Identify WHAT.</p>
<h4>Query</h4>
<p>Modify HOW.</p>
<h3>URL Structure Comparison</h3>
<h3>Params Example</h3>
<pre><code class="language-text">/products/25
</code></pre>
<p>Specific product.</p>
<h3>Query Example</h3>
<pre><code class="language-text">/products?category=electronics
</code></pre>
<p>Filter products.</p>
<h3>Accessing Params in Express</h3>
<p>:contentReference[oaicite:0]{index=0} provides access to URL parameters using:</p>
<pre><code class="language-js">req.params
</code></pre>
<h3>Basic Example</h3>
<p>Example:</p>
<pre><code class="language-js">app.get("/users/:id", (req, res) =&gt; {
  console.log(req.params);

  res.send("User route");
});
</code></pre>
<p>Suppose URL:</p>
<pre><code class="language-text">/users/10
</code></pre>
<p>Output:</p>
<pre><code class="language-js">{
  id: "10"
}
</code></pre>
<h3>Accessing Specific Param</h3>
<p>Example:</p>
<pre><code class="language-js">app.get("/users/:id", (req, res) =&gt; {
  console.log(req.params.id);

  res.send("Done");
});
</code></pre>
<p>Output:</p>
<pre><code class="language-text">10
</code></pre>
<h3>Multiple Params Example</h3>
<p>Example:</p>
<pre><code class="language-js">app.get("/users/:userId/posts/:postId", (req, res) =&gt; {
  console.log(req.params);
});
</code></pre>
<p>URL:</p>
<pre><code class="language-text">/users/5/posts/20
</code></pre>
<p>Output:</p>
<pre><code class="language-js">{
  userId: "5",
  postId: "20"
}
</code></pre>
<h3>Why Params Are Powerful</h3>
<p>Params allow:</p>
<ul>
<li><p>dynamic routing</p>
</li>
<li><p>reusable endpoints</p>
</li>
<li><p>scalable APIs</p>
</li>
</ul>
<p>Without them:</p>
<ul>
<li>backend routes become repetitive</li>
</ul>
<h3>Accessing Query Strings in Express</h3>
<p>Express provides query parameters using:</p>
<pre><code class="language-js">req.query
</code></pre>
<h3>Basic Query Example</h3>
<p>Example:</p>
<pre><code class="language-js">app.get("/products", (req, res) =&gt; {
  console.log(req.query);

  res.send("Products");
});
</code></pre>
<p>URL:</p>
<pre><code class="language-text">/products?category=mobile
</code></pre>
<p>Output:</p>
<pre><code class="language-js">{
  category: "mobile"
}
</code></pre>
<h3>Accessing Specific Query Parameter</h3>
<p>Example:</p>
<pre><code class="language-js">app.get("/products", (req, res) =&gt; {
  console.log(req.query.category);
});
</code></pre>
<p>URL:</p>
<pre><code class="language-text">/products?category=mobile
</code></pre>
<p>Output:</p>
<pre><code class="language-text">mobile
</code></pre>
<h3>Multiple Query Parameters</h3>
<p>Example URL:</p>
<pre><code class="language-text">/products?category=mobile&amp;page=2&amp;sort=asc
</code></pre>
<p>Output:</p>
<pre><code class="language-js">{
  category: "mobile",
  page: "2",
  sort: "asc"
}
</code></pre>
<h3>Why Query Parameters Matter</h3>
<p>Modern applications heavily use query strings for:</p>
<ul>
<li><p>searching</p>
</li>
<li><p>pagination</p>
</li>
<li><p>sorting</p>
</li>
<li><p>filtering</p>
</li>
</ul>
<h3>Example — Search API</h3>
<p>Example:</p>
<pre><code class="language-text">/products?search=iphone
</code></pre>
<h3>Example — Pagination</h3>
<p>Example:</p>
<pre><code class="language-text">/products?page=3
</code></pre>
<h3>Example — Sorting</h3>
<p>Example:</p>
<pre><code class="language-text">/products?sort=price
</code></pre>
<h3>When to Use Params vs Query</h3>
<p>This is one of the biggest practical backend decisions.</p>
<h3>Use Params When</h3>
<p>Use URL params for:</p>
<blockquote>
<p>identifying specific resources.</p>
</blockquote>
<p>Examples:</p>
<ul>
<li><p>user ID</p>
</li>
<li><p>product ID</p>
</li>
<li><p>post ID</p>
</li>
</ul>
<h3>Good Param Examples</h3>
<pre><code class="language-text">/users/10
/posts/5
/products/101
</code></pre>
<h3>Use Query When</h3>
<p>Use query parameters for:</p>
<blockquote>
<p>optional filtering or modification.</p>
</blockquote>
<p>Examples:</p>
<ul>
<li><p>search</p>
</li>
<li><p>sorting</p>
</li>
<li><p>pagination</p>
</li>
<li><p>filtering</p>
</li>
</ul>
<h3>Good Query Examples</h3>
<pre><code class="language-text">/products?page=2
/products?sort=asc
/products?category=electronics
</code></pre>
<h3>Real API Thinking</h3>
<p>Suppose API:</p>
<pre><code class="language-text">/users/5
</code></pre>
<p>This clearly identifies:</p>
<blockquote>
<p>one specific user.</p>
</blockquote>
<p>But:</p>
<pre><code class="language-text">/users?page=2
</code></pre>
<p>does not identify one user.</p>
<p>It changes:</p>
<blockquote>
<p>how user list should be returned.</p>
</blockquote>
<h3>Combining Params and Query Together</h3>
<p>Both can be used together.</p>
<p>Example:</p>
<pre><code class="language-text">/users/5/posts?page=2
</code></pre>
<p>Here:</p>
<ul>
<li><p><code>5</code> → param</p>
</li>
<li><p><code>page=2</code> → query</p>
</li>
</ul>
<h3>Express Example</h3>
<p>Example:</p>
<pre><code class="language-js">app.get("/users/:id/posts", (req, res) =&gt; {
  console.log(req.params.id);
  console.log(req.query.page);

  res.send("Done");
});
</code></pre>
<p>URL:</p>
<pre><code class="language-text">/users/5/posts?page=2
</code></pre>
<h3>Output</h3>
<pre><code class="language-text">5
2
</code></pre>
<h3>Common Beginner Mistake</h3>
<p>Many beginners confuse:</p>
<ul>
<li><p>params</p>
</li>
<li><p>query strings</p>
</li>
</ul>
<h3>Easy Trick to Remember</h3>
<h4>Params</h4>
<p>Inside route path.</p>
<h4>Query</h4>
<p>After <code>?</code></p>
<h3>Another Common Mistake</h3>
<p>People often try accessing query values using:</p>
<pre><code class="language-js">req.params
</code></pre>
<p>instead of:</p>
<pre><code class="language-js">req.query
</code></pre>
<p>Both are different.</p>
<h3>Why Understanding This Matters</h3>
<p>Modern APIs heavily depend on:</p>
<ul>
<li><p>dynamic routing</p>
</li>
<li><p>filtering</p>
</li>
<li><p>searching</p>
</li>
<li><p>pagination</p>
</li>
</ul>
<p>And URL/query handling becomes core backend knowledge.</p>
<h3>Final Understanding</h3>
<p>URL parameters are dynamic values placed directly inside route paths and are mainly used to identify specific resources.</p>
<p>Query parameters are optional key-value pairs added after <code>?</code> in URLs and are mainly used for:</p>
<ul>
<li><p>filtering</p>
</li>
<li><p>searching</p>
</li>
<li><p>sorting</p>
</li>
<li><p>pagination</p>
</li>
</ul>
<p>In :contentReference[oaicite:1]{index=1}:</p>
<ul>
<li><p>URL params are accessed using <code>req.params</code></p>
</li>
<li><p>Query params are accessed using <code>req.query</code></p>
</li>
</ul>
<p>And understanding when to use params vs query strings is extremely important for designing clean and scalable APIs.</p>
]]></content:encoded></item><item><title><![CDATA[Creating Routes and Handling Requests with Express]]></title><description><![CDATA[What Express.js Is
When developers start backend development using :contentReference[oaicite:0]{index=0}, they quickly realize something:Creating everything using raw Node.js becomes repetitive.Exampl]]></description><link>https://backendwithshubham.hashnode.dev/creating-routes-and-handling-requests-with-express</link><guid isPermaLink="true">https://backendwithshubham.hashnode.dev/creating-routes-and-handling-requests-with-express</guid><dc:creator><![CDATA[Shubham Bhardwaj]]></dc:creator><pubDate>Sun, 10 May 2026 15:10:35 GMT</pubDate><content:encoded><![CDATA[<h3>What Express.js Is</h3>
<p>When developers start backend development using :contentReference[oaicite:0]{index=0}, they quickly realize something:<br />Creating everything using raw Node.js becomes repetitive.<br />Example:</p>
<ul>
<li><p>handling routes manually</p>
</li>
<li><p>parsing requests</p>
</li>
<li><p>setting headers</p>
</li>
<li><p>managing responses</p>
</li>
</ul>
<p>This is where:<br />:contentReference[oaicite:1]{index=1}<br />comes in.</p>
<p>Express.js is:</p>
<blockquote>
<p>A lightweight web framework built on top of Node.js.</p>
</blockquote>
<p>It helps developers build:</p>
<ul>
<li><p>servers</p>
</li>
<li><p>APIs</p>
</li>
<li><p>backend applications</p>
</li>
</ul>
<p>much more easily.</p>
<h3>Real Purpose of Express.js</h3>
<p>Express simplifies backend development by providing:</p>
<ul>
<li><p>routing</p>
</li>
<li><p>middleware</p>
</li>
<li><p>request handling</p>
</li>
<li><p>response handling</p>
</li>
<li><p>cleaner server architecture</p>
</li>
</ul>
<p>Without Express:</p>
<ul>
<li>developers write much more boilerplate code</li>
</ul>
<h3>Why Express Became So Popular</h3>
<p>Because it is:</p>
<ul>
<li><p>simple</p>
</li>
<li><p>minimal</p>
</li>
<li><p>flexible</p>
</li>
<li><p>beginner friendly</p>
</li>
<li><p>extremely fast for development</p>
</li>
</ul>
<p>Many modern Node.js applications use Express internally.</p>
<h3>Why Express Simplifies Node.js Development</h3>
<p>Let’s compare raw Node.js first.</p>
<h3>Raw Node.js Example</h3>
<p>Example:</p>
<pre><code class="language-js">const http = require("http");

const server = http.createServer((req, res) =&gt; {
  if (req.url === "/") {
    res.end("Home Page");
  }
});

server.listen(3000);
</code></pre>
<p>This works.</p>
<p>But as applications grow:</p>
<ul>
<li><p>routes increase</p>
</li>
<li><p>logic increases</p>
</li>
<li><p>code becomes messy</p>
</li>
</ul>
<h3>Express Simplifies This</h3>
<p>Express provides cleaner syntax.</p>
<p>Example:</p>
<pre><code class="language-js">app.get("/", (req, res) =&gt; {
  res.send("Home Page");
});
</code></pre>
<p>Much easier to read.</p>
<h3>What Express Handles Internally</h3>
<p>Express internally manages many things automatically:</p>
<ul>
<li><p>routing</p>
</li>
<li><p>request parsing</p>
</li>
<li><p>middleware flow</p>
</li>
<li><p>headers</p>
</li>
<li><p>response utilities</p>
</li>
</ul>
<p>This saves huge development time.</p>
<h3>Installing Express</h3>
<p>Before using Express:</p>
<pre><code class="language-bash">npm install express
</code></pre>
<p>This installs framework inside project.</p>
<h3>Creating First Express Server</h3>
<p>After installation, developers create Express app.</p>
<h3>Basic Express Server</h3>
<p>Example:</p>
<pre><code class="language-js">const express = require("express");

const app = express();

app.listen(3000, () =&gt; {
  console.log("Server running");
});
</code></pre>
<h3>Understanding This Step-by-Step</h3>
<h3><code>require("express")</code></h3>
<p>Imports Express library.</p>
<h3><code>express()</code></h3>
<p>Creates Express application instance.</p>
<h3><code>app.listen()</code></h3>
<p>Starts server on specific port.</p>
<h3>Running Server</h3>
<p>Command:</p>
<pre><code class="language-bash">node app.js
</code></pre>
<p>Output:</p>
<pre><code class="language-text">Server running
</code></pre>
<h3>Accessing Server</h3>
<p>Open browser:</p>
<pre><code class="language-text">http://localhost:3000
</code></pre>
<h3>What Happens Internally</h3>
<p>Browser sends request. Express receives request. Server responds.</p>
<h3>Understanding Routes</h3>
<p>Routes define:</p>
<blockquote>
<p>what should happen for specific URLs.</p>
</blockquote>
<p>Example:</p>
<pre><code class="language-text">/            → home page
/about       → about page
/users       → users data
</code></pre>
<p>Express makes route creation extremely easy.</p>
<h3>Handling GET Requests</h3>
<p>GET requests are mainly used for:</p>
<blockquote>
<p>fetching data.</p>
</blockquote>
<p>Example:</p>
<ul>
<li><p>opening webpages</p>
</li>
<li><p>fetching API data</p>
</li>
<li><p>retrieving user information</p>
</li>
</ul>
<h3>Basic GET Route</h3>
<p>Example:</p>
<pre><code class="language-js">app.get("/", (req, res) =&gt; {
  res.send("Hello World");
});
</code></pre>
<h3>Understanding This</h3>
<h3><code>app.get()</code></h3>
<p>Handles GET requests.</p>
<h3><code>"/"</code></h3>
<p>Route path.</p>
<h3><code>(req, res)</code></h3>
<p>Request and response objects.</p>
<h3><code>res.send()</code></h3>
<p>Sends response back to client.</p>
<h3>Example Flow</h3>
<pre><code class="language-text">Browser → GET request → Express server
Express server → Response → Browser
</code></pre>
<h3>Another GET Example</h3>
<p>Example:</p>
<pre><code class="language-js">app.get("/about", (req, res) =&gt; {
  res.send("About Page");
});
</code></pre>
<p>Now:</p>
<pre><code class="language-text">localhost:3000/about
</code></pre>
<p>shows:</p>
<pre><code class="language-text">About Page
</code></pre>
<h3>Why GET Requests Matter</h3>
<p>Most frontend applications constantly use GET requests for:</p>
<ul>
<li><p>fetching products</p>
</li>
<li><p>loading users</p>
</li>
<li><p>retrieving posts</p>
</li>
<li><p>displaying data</p>
</li>
</ul>
<h3>Handling POST Requests</h3>
<p>POST requests are mainly used for:</p>
<blockquote>
<p>sending data to server.</p>
</blockquote>
<p>Examples:</p>
<ul>
<li><p>login forms</p>
</li>
<li><p>registration</p>
</li>
<li><p>creating posts</p>
</li>
<li><p>uploading data</p>
</li>
</ul>
<h3>Basic POST Route</h3>
<p>Example:</p>
<pre><code class="language-js">app.post("/user", (req, res) =&gt; {
  res.send("User created");
});
</code></pre>
<h3>Important Difference</h3>
<h4>GET</h4>
<p>Fetch data.</p>
<h4>POST</h4>
<p>Send/create data.</p>
<h3>Why POST Is Important</h3>
<p>Modern applications constantly send data:</p>
<ul>
<li><p>forms</p>
</li>
<li><p>authentication</p>
</li>
<li><p>APIs</p>
</li>
<li><p>uploads</p>
</li>
</ul>
<p>POST requests handle these operations.</p>
<h3>Understanding Request Body</h3>
<p>POST requests usually contain:</p>
<ul>
<li><p>JSON data</p>
</li>
<li><p>form data</p>
</li>
<li><p>uploaded files</p>
</li>
</ul>
<p>Express commonly uses middleware to parse request body.</p>
<h3>Example</h3>
<pre><code class="language-js">app.use(express.json());
</code></pre>
<p>This allows Express to read JSON data.</p>
<h3>Example POST with JSON</h3>
<p>Example:</p>
<pre><code class="language-js">app.post("/user", (req, res) =&gt; {
  console.log(req.body);

  res.send("Data received");
});
</code></pre>
<p>Suppose client sends:</p>
<pre><code class="language-json">{
  "name": "Shubham"
}
</code></pre>
<p>Server can access:</p>
<pre><code class="language-js">req.body.name
</code></pre>
<h3>Sending Responses</h3>
<p>Servers must respond to client requests. Express provides multiple response methods.</p>
<h3><code>res.send()</code></h3>
<p>Most common response method.</p>
<p>Example:</p>
<pre><code class="language-js">res.send("Hello");
</code></pre>
<p>Can send:</p>
<ul>
<li><p>text</p>
</li>
<li><p>HTML</p>
</li>
<li><p>JSON</p>
</li>
</ul>
<h3>Sending JSON Response</h3>
<p>Example:</p>
<pre><code class="language-js">res.json({
  name: "Shubham",
  age: 21
});
</code></pre>
<h3>Why JSON Matters</h3>
<p>Modern APIs mainly communicate using:</p>
<pre><code class="language-text">JSON
</code></pre>
<p>Frontend applications heavily depend on JSON responses.</p>
<h3>Sending Status Codes</h3>
<p>Example:</p>
<pre><code class="language-js">res.status(404).send("Page not found");
</code></pre>
<h3>Common HTTP Status Codes</h3>
<h4>200</h4>
<p>Success.</p>
<h4>404</h4>
<p>Not found.</p>
<h4>500</h4>
<p>Server error.</p>
<h3>Real API Example</h3>
<p>Example:</p>
<pre><code class="language-js">app.get("/user", (req, res) =&gt; {
  res.json({
    id: 1,
    name: "Shubham"
  });
});
</code></pre>
<p>This is how APIs commonly send data.</p>
<h3>Understanding the Bigger Picture</h3>
<p>Express simplifies backend architecture heavily.</p>
<p>Without Express:</p>
<ul>
<li><p>routing becomes difficult</p>
</li>
<li><p>middleware handling becomes repetitive</p>
</li>
<li><p>APIs become harder to organize</p>
</li>
</ul>
<p>Express provides clean structure for scalable applications.</p>
<h3>Why Express Is Beginner Friendly</h3>
<p>Because developers can focus on:</p>
<ul>
<li><p>application logic instead of:</p>
</li>
<li><p>low-level HTTP complexity</p>
</li>
</ul>
<h3>Common Beginner Mistake</h3>
<p>Many beginners forget:</p>
<pre><code class="language-js">app.listen()
</code></pre>
<p>Without it:</p>
<ul>
<li>server never starts</li>
</ul>
<h3>Another Common Mistake</h3>
<p>People often confuse:</p>
<ul>
<li><p>request</p>
</li>
<li><p>response</p>
</li>
</ul>
<h4>Request</h4>
<p>Comes from client.</p>
<h4>Response</h4>
<p>Sent by server.</p>
<h3>Why Express Became Industry Standard</h3>
<p>Even though many modern frameworks exist, Express remains extremely popular because:</p>
<ul>
<li><p>minimal learning curve</p>
</li>
<li><p>huge ecosystem</p>
</li>
<li><p>flexible architecture</p>
</li>
<li><p>easy API development</p>
</li>
</ul>
<p>Many backend systems still use Express heavily.</p>
<h3>Final Understanding</h3>
<p>:contentReference[oaicite:2]{index=2} is a lightweight framework built on top of Node.js that simplifies backend development.</p>
<p>It helps developers:</p>
<ul>
<li><p>create servers</p>
</li>
<li><p>manage routes</p>
</li>
<li><p>handle requests</p>
</li>
<li><p>send responses</p>
</li>
<li><p>build APIs</p>
</li>
</ul>
<p>Using Express, developers can easily handle:</p>
<ul>
<li><p>GET requests for fetching data</p>
</li>
<li><p>POST requests for sending data</p>
</li>
</ul>
<p>And because Express removes much of Node.js boilerplate complexity, it became one of the most widely used backend frameworks in modern JavaScript development.</p>
]]></content:encoded></item><item><title><![CDATA[How Node.js Handles Multiple Requests with a Single Thread]]></title><description><![CDATA[Single-Threaded Nature of Node.js
One of the first things developers hear about :contentReference[oaicite:0]{index=0} is:
Node.js is single-threaded

This confuses many beginners. Because they immedia]]></description><link>https://backendwithshubham.hashnode.dev/how-node-js-handles-multiple-requests-with-a-single-thread</link><guid isPermaLink="true">https://backendwithshubham.hashnode.dev/how-node-js-handles-multiple-requests-with-a-single-thread</guid><dc:creator><![CDATA[Shubham Bhardwaj]]></dc:creator><pubDate>Sun, 10 May 2026 15:07:08 GMT</pubDate><content:encoded><![CDATA[<h3>Single-Threaded Nature of Node.js</h3>
<p>One of the first things developers hear about :contentReference[oaicite:0]{index=0} is:</p>
<pre><code class="language-text">Node.js is single-threaded
</code></pre>
<p>This confuses many beginners. Because they immediately think:</p>
<blockquote>
<p>“If Node.js uses one thread, how can it handle thousands of users?”</p>
</blockquote>
<p>To understand this properly, we first need to understand what:</p>
<pre><code class="language-text">single-threaded
</code></pre>
<p>actually means.</p>
<h3>What Single-Threaded Means</h3>
<p>A thread is basically:</p>
<blockquote>
<p>a path of execution for code.</p>
</blockquote>
<p>When Node.js says:</p>
<pre><code class="language-text">single-threaded
</code></pre>
<p>it means:</p>
<ul>
<li>JavaScript code mainly executes on one main thread</li>
</ul>
<p>This thread executes:</p>
<ul>
<li><p>functions</p>
</li>
<li><p>callbacks</p>
</li>
<li><p>application logic</p>
</li>
</ul>
<p>one thing at a time.</p>
<h3>Important Understanding</h3>
<p>Node.js does NOT create:</p>
<ul>
<li><p>one thread per user</p>
</li>
<li><p>one thread per request</p>
</li>
</ul>
<p>like many traditional server systems.</p>
<p>Instead:</p>
<ul>
<li>one main thread handles JavaScript execution efficiently.</li>
</ul>
<h3>Real Thinking Behind Single Thread</h3>
<p>Imagine one extremely efficient manager handling many tasks intelligently instead of creating thousands of managers. That is closer to how Node.js works.</p>
<h3>Why Single Thread Sounds Scary at First</h3>
<p>Beginners often think:</p>
<pre><code class="language-text">One thread = only one request at a time
</code></pre>
<p>But that is NOT how Node.js actually behaves. Because Node.js heavily depends on:</p>
<ul>
<li><p>asynchronous processing</p>
</li>
<li><p>event loop</p>
</li>
<li><p>background workers</p>
</li>
</ul>
<h3>Event Loop Role in Concurrency</h3>
<p>The real power of Node.js comes from:</p>
<pre><code class="language-text">Event Loop
</code></pre>
<p>The event loop is one of the most important concepts in Node.js.</p>
<h3>What Event Loop Actually Does</h3>
<p>Event loop continuously checks:</p>
<ul>
<li><p>completed tasks</p>
</li>
<li><p>pending callbacks</p>
</li>
<li><p>incoming events</p>
</li>
<li><p>asynchronous operations</p>
</li>
</ul>
<p>and decides:</p>
<blockquote>
<p>what should execute next.</p>
</blockquote>
<h3>Understanding Event Loop Simply</h3>
<p>Think of event loop like:</p>
<blockquote>
<p>a smart task manager.</p>
</blockquote>
<p>It constantly watches:</p>
<ul>
<li><p>what tasks finished</p>
</li>
<li><p>what tasks are waiting</p>
</li>
<li><p>what callbacks should run</p>
</li>
</ul>
<h3>Why Event Loop Matters</h3>
<p>Without event loop:</p>
<ul>
<li><p>asynchronous behavior would not work properly</p>
</li>
<li><p>Node.js could not handle concurrency efficiently</p>
</li>
</ul>
<p>Event loop is what allows:</p>
<ul>
<li><p>one thread to still manage:</p>
</li>
<li><p>many operations simultaneously.</p>
</li>
</ul>
<h3>Real Example</h3>
<p>Suppose:</p>
<ul>
<li><p>User A requests database data</p>
</li>
<li><p>User B requests file read</p>
</li>
<li><p>User C opens API request</p>
</li>
</ul>
<p>Node.js does not block main thread waiting for each task. Instead:</p>
<ul>
<li><p>tasks are delegated</p>
</li>
<li><p>event loop continues processing other requests</p>
</li>
</ul>
<h3>Delegating Tasks to Background Workers</h3>
<p>This is where Node.js architecture becomes powerful.</p>
<h3>Important Understanding</h3>
<p>Even though JavaScript execution uses one main thread:</p>
<ul>
<li>Node.js internally uses background worker systems</li>
</ul>
<p>for slow operations.</p>
<h3>Examples of Delegated Tasks</h3>
<p>Tasks like:</p>
<ul>
<li><p>file reading</p>
</li>
<li><p>database operations</p>
</li>
<li><p>network requests</p>
</li>
<li><p>timers</p>
</li>
</ul>
<p>are usually delegated outside main thread.</p>
<h3>What Happens Internally</h3>
<p>Suppose application runs:</p>
<pre><code class="language-js">fs.readFile("data.txt", callback);
</code></pre>
<p>Node.js:</p>
<ol>
<li><p>sends file-reading task to background system</p>
</li>
<li><p>immediately continues other work</p>
</li>
<li><p>callback executes later when task finishes</p>
</li>
</ol>
<h3>Why This Is Powerful</h3>
<p>Instead of blocking:</p>
<ul>
<li><p>Node.js stays free</p>
</li>
<li><p>event loop continues handling requests</p>
</li>
</ul>
<p>This creates extremely efficient concurrency.</p>
<h3>Real Thinking Behind Delegation</h3>
<p>Imagine restaurant manager. Instead of personally cooking food:</p>
<ul>
<li><p>manager delegates kitchen work</p>
</li>
<li><p>continues handling customers</p>
</li>
</ul>
<p>Later:</p>
<ul>
<li>kitchen notifies when food is ready</li>
</ul>
<p>Node.js works similarly.</p>
<h3>Background Workers in Simple Terms</h3>
<p>Background workers handle:</p>
<ul>
<li><p>expensive waiting operations</p>
</li>
<li><p>slow I/O tasks</p>
</li>
</ul>
<p>while event loop keeps system responsive.</p>
<h3>Handling Multiple Client Requests</h3>
<p>This is where Node.js shines.</p>
<h3>Traditional Blocking Approach</h3>
<p>Suppose server handles one request like:</p>
<pre><code class="language-text">Request A → Wait 5 seconds
</code></pre>
<p>If blocking occurs:</p>
<ul>
<li><p>Request B must wait</p>
</li>
<li><p>Request C must wait</p>
</li>
</ul>
<p>Server responsiveness drops badly.</p>
<h3>Node.js Approach</h3>
<p>Node.js handles requests differently.</p>
<p>Suppose:</p>
<ul>
<li>Request A starts database query</li>
</ul>
<p>Instead of waiting:</p>
<ul>
<li>Node.js moves immediately to Request B</li>
</ul>
<p>Then:</p>
<ul>
<li><p>Request C</p>
</li>
<li><p>Request D</p>
</li>
<li><p>Request E</p>
</li>
</ul>
<p>all continue getting processed.</p>
<h3>Real Flow</h3>
<pre><code class="language-text">Request A → delegated
Request B → handled
Request C → handled
Request D → handled
</code></pre>
<p>Meanwhile:</p>
<ul>
<li>completed tasks later return through callbacks/promises/events</li>
</ul>
<h3>Why This Creates Concurrency</h3>
<p>Concurrency means:</p>
<blockquote>
<p>managing multiple tasks efficiently at same time.</p>
</blockquote>
<p>Node.js achieves concurrency NOT by:</p>
<ul>
<li>running many JavaScript threads</li>
</ul>
<p>but by:</p>
<ul>
<li><p>asynchronous delegation</p>
</li>
<li><p>non-blocking architecture</p>
</li>
<li><p>event loop scheduling</p>
</li>
</ul>
<h3>Example Scenario</h3>
<p>Suppose 10,000 users connect to chat server.</p>
<p>Node.js can efficiently manage:</p>
<ul>
<li><p>socket connections</p>
</li>
<li><p>messages</p>
</li>
<li><p>notifications</p>
</li>
</ul>
<p>without creating huge thread overhead.</p>
<h3>Why Node.js Scales Well</h3>
<p>Scalability means:</p>
<blockquote>
<p>handling increasing users or traffic efficiently.</p>
</blockquote>
<p>Node.js scales well mainly because:</p>
<ul>
<li><p>thread overhead remains low</p>
</li>
<li><p>memory usage stays efficient</p>
</li>
<li><p>non-blocking I/O avoids waiting</p>
</li>
</ul>
<h3>Traditional Thread Problem</h3>
<p>Some traditional systems create:</p>
<ul>
<li>one thread per request</li>
</ul>
<p>Now imagine:</p>
<ul>
<li><p>100,000 users</p>
</li>
<li><p>100,000 threads</p>
</li>
</ul>
<p>This consumes huge:</p>
<ul>
<li><p>memory</p>
</li>
<li><p>CPU scheduling overhead</p>
</li>
</ul>
<h3>Node.js Avoids This Problem</h3>
<p>Node.js mainly uses:</p>
<ul>
<li><p>one JavaScript execution thread</p>
</li>
<li><p>asynchronous delegation</p>
</li>
</ul>
<p>This dramatically reduces resource overhead.</p>
<h3>Why I/O-Heavy Systems Love Node.js</h3>
<p>Most modern web applications spend large time:</p>
<ul>
<li><p>waiting for database</p>
</li>
<li><p>waiting for APIs</p>
</li>
<li><p>waiting for files</p>
</li>
<li><p>waiting for network responses</p>
</li>
</ul>
<p>Node.js handles waiting extremely efficiently.</p>
<h3>Real Applications Where Node.js Performs Extremely Well</h3>
<p>Examples:</p>
<ul>
<li><p>chat systems</p>
</li>
<li><p>streaming</p>
</li>
<li><p>real-time dashboards</p>
</li>
<li><p>APIs</p>
</li>
<li><p>multiplayer games</p>
</li>
<li><p>collaborative applications</p>
</li>
</ul>
<h3>Example — Chat Application</h3>
<p>Suppose thousands of users send messages simultaneously. Node.js efficiently handles:</p>
<ul>
<li><p>socket events</p>
</li>
<li><p>asynchronous communication</p>
</li>
<li><p>concurrent connections</p>
</li>
</ul>
<p>without massive thread creation.</p>
<h3>Example — API Servers</h3>
<p>API requests often involve:</p>
<ul>
<li><p>database waiting</p>
</li>
<li><p>authentication</p>
</li>
<li><p>external API calls</p>
</li>
</ul>
<p>These are I/O-heavy operations. Node.js handles such workloads extremely efficiently.</p>
<h3>Important Understanding About Scalability</h3>
<p>Node.js scales well for:</p>
<blockquote>
<p>I/O-heavy workloads.</p>
</blockquote>
<p>Not necessarily for:</p>
<blockquote>
<p>CPU-heavy calculations.</p>
</blockquote>
<h3>CPU-Heavy Limitation</h3>
<p>Suppose application performs:</p>
<ul>
<li><p>video rendering</p>
</li>
<li><p>machine learning</p>
</li>
<li><p>large computations</p>
</li>
</ul>
<p>Main thread may become blocked. Because JavaScript execution itself still runs on one thread.</p>
<h3>Common Beginner Mistake</h3>
<p>Many beginners think:</p>
<ul>
<li>Node.js magically runs everything in parallel</li>
</ul>
<p>Not exactly. JavaScript itself mainly executes sequentially on one thread.<br />Concurrency comes from:</p>
<ul>
<li><p>asynchronous delegation</p>
</li>
<li><p>event loop coordination</p>
</li>
</ul>
<h3>Another Common Mistake</h3>
<p>People often think:</p>
<ul>
<li>single-threaded automatically means weak performance</li>
</ul>
<p>But Node.js proves:</p>
<blockquote>
<p>architecture matters more than raw thread count.</p>
</blockquote>
<h3>Why Understanding Event Loop Matters So Much</h3>
<p>Once developers understand:</p>
<ul>
<li><p>event loop</p>
</li>
<li><p>asynchronous delegation</p>
</li>
<li><p>non-blocking behavior</p>
</li>
</ul>
<p>many confusing Node.js concepts suddenly become clear. This is one of the biggest turning points in backend learning.</p>
<h3>Final Understanding</h3>
<p>:contentReference[oaicite:1]{index=1} mainly executes JavaScript on a single thread, but it achieves high concurrency using:</p>
<ul>
<li><p>event loop</p>
</li>
<li><p>asynchronous operations</p>
</li>
<li><p>background worker delegation</p>
</li>
</ul>
<p>Instead of blocking during slow tasks, Node.js delegates operations like:</p>
<ul>
<li><p>file reading</p>
</li>
<li><p>network requests</p>
</li>
<li><p>database communication</p>
</li>
</ul>
<p>allowing the event loop to continue handling other requests efficiently. This architecture makes Node.js highly scalable for:</p>
<ul>
<li><p>APIs</p>
</li>
<li><p>real-time systems</p>
</li>
<li><p>chat applications</p>
</li>
<li><p>I/O-heavy workloads</p>
</li>
</ul>
<p>and explains why Node.js became so popular for modern backend development.</p>
]]></content:encoded></item><item><title><![CDATA[Setting Up Your First Node.js Application Step-by-Step]]></title><description><![CDATA[Installing Node.js
Before writing backend applications using :contentReference[oaicite:0]{index=0}, Node.js must first be installed on the system. Node.js allows JavaScript to run outside the browser.]]></description><link>https://backendwithshubham.hashnode.dev/setting-up-your-first-node-js-application-step-by-step</link><guid isPermaLink="true">https://backendwithshubham.hashnode.dev/setting-up-your-first-node-js-application-step-by-step</guid><dc:creator><![CDATA[Shubham Bhardwaj]]></dc:creator><pubDate>Sun, 10 May 2026 15:04:09 GMT</pubDate><content:encoded><![CDATA[<h3>Installing Node.js</h3>
<p>Before writing backend applications using :contentReference[oaicite:0]{index=0}, Node.js must first be installed on the system. Node.js allows JavaScript to run outside the browser. Without Node.js:</p>
<ul>
<li><p>JavaScript mainly runs inside browsers</p>
</li>
<li><p>backend development becomes impossible</p>
</li>
</ul>
<h3>Why Installing Node.js Matters</h3>
<p>Installing Node.js provides:</p>
<ul>
<li><p>Node runtime</p>
</li>
<li><p>npm (Node Package Manager)</p>
</li>
<li><p>backend execution environment</p>
</li>
</ul>
<p>This allows developers to:</p>
<ul>
<li><p>run JavaScript from terminal</p>
</li>
<li><p>create servers</p>
</li>
<li><p>install packages</p>
</li>
<li><p>build backend applications</p>
</li>
</ul>
<h3>Downloading Node.js</h3>
<p>Node.js can be downloaded from the official website: <a href="https://nodejs.org?utm_source=chatgpt.com">Node.js Official Website</a> Usually two versions appear:</p>
<h3>LTS Version</h3>
<pre><code class="language-text">Long Term Support
</code></pre>
<p>Recommended for most developers. More stable.</p>
<h3>Current Version</h3>
<p>Contains latest features but may change more frequently. Beginners usually prefer:</p>
<blockquote>
<p>LTS version.</p>
</blockquote>
<h3>Installing Node.js on macOS</h3>
<p>Since you use macOS, installation is commonly done using:</p>
<ul>
<li><p>installer package</p>
</li>
<li><p>Homebrew</p>
</li>
</ul>
<h3>Using Homebrew</h3>
<p>Example:</p>
<pre><code class="language-bash">brew install node
</code></pre>
<p>This installs:</p>
<ul>
<li><p>node</p>
</li>
<li><p>npm</p>
</li>
</ul>
<h3>Checking Installation Using Terminal</h3>
<p>After installation, developers verify setup using terminal commands.</p>
<h3>Checking Node.js Version</h3>
<p>Command:</p>
<pre><code class="language-bash">node -v
</code></pre>
<p>Example output:</p>
<pre><code class="language-text">v22.1.0
</code></pre>
<p>This confirms:</p>
<ul>
<li>Node.js installed successfully</li>
</ul>
<h3>Checking npm Version</h3>
<p>Command:</p>
<pre><code class="language-bash">npm -v
</code></pre>
<p>Example output:</p>
<pre><code class="language-text">10.5.0
</code></pre>
<p>This confirms npm installation.</p>
<h3>Why Version Checking Matters</h3>
<p>Sometimes:</p>
<ul>
<li><p>installation fails</p>
</li>
<li><p>PATH variables break</p>
</li>
<li><p>wrong versions exist</p>
</li>
</ul>
<p>Checking versions helps verify environment setup correctly.</p>
<h3>Understanding Node REPL</h3>
<p>One of the most interesting beginner features in Node.js is:</p>
<pre><code class="language-text">REPL
</code></pre>
<p>REPL stands for:</p>
<pre><code class="language-text">Read
Eval
Print
Loop
</code></pre>
<h3>What REPL Actually Does</h3>
<p>REPL allows developers to:</p>
<ul>
<li><p>write JavaScript directly in terminal</p>
</li>
<li><p>execute code instantly</p>
</li>
<li><p>test small snippets quickly</p>
</li>
</ul>
<p>Without creating separate files.</p>
<h3>Starting REPL</h3>
<p>Command:</p>
<pre><code class="language-bash">node
</code></pre>
<p>After running:</p>
<ul>
<li>interactive Node environment opens</li>
</ul>
<p>Example:</p>
<pre><code class="language-bash">&gt;
</code></pre>
<p>Now JavaScript can be executed directly.</p>
<h3>Example Inside REPL</h3>
<p>Example:</p>
<pre><code class="language-js">2 + 3
</code></pre>
<p>Output:</p>
<pre><code class="language-text">5
</code></pre>
<h3>Another Example</h3>
<pre><code class="language-js">console.log("Hello")
</code></pre>
<p>Output:</p>
<pre><code class="language-text">Hello
</code></pre>
<h3>Why REPL Is Useful</h3>
<p>REPL is very useful for:</p>
<ul>
<li><p>testing JavaScript quickly</p>
</li>
<li><p>experimenting with code</p>
</li>
<li><p>debugging logic</p>
</li>
<li><p>learning Node basics</p>
</li>
</ul>
<h3>Understanding REPL Name</h3>
<h3>Read</h3>
<p>Reads input.</p>
<h3>Eval</h3>
<p>Executes code.</p>
<h3>Print</h3>
<p>Displays output.</p>
<h3>Loop</h3>
<p>Repeats continuously.</p>
<h3>Exiting REPL</h3>
<p>To exit:</p>
<pre><code class="language-bash">.exit
</code></pre>
<p>or press:</p>
<pre><code class="language-text">Ctrl + C
</code></pre>
<p>twice.</p>
<h3>Creating First JS File</h3>
<p>Real applications usually use JavaScript files instead of REPL.</p>
<h3>Step 1 — Create Project Folder</h3>
<p>Example:</p>
<pre><code class="language-bash">mkdir myapp
</code></pre>
<h3>Step 2 — Open Folder</h3>
<p>Example:</p>
<pre><code class="language-bash">cd myapp
</code></pre>
<h3>Step 3 — Create File</h3>
<p>Example:</p>
<pre><code class="language-bash">touch app.js
</code></pre>
<p>Now:</p>
<pre><code class="language-text">app.js
</code></pre>
<p>becomes your first Node.js file.</p>
<h3>Writing First JavaScript Code</h3>
<p>Inside:</p>
<pre><code class="language-js">console.log("Hello Node.js");
</code></pre>
<p>Very simple.</p>
<p>But extremely important first step.</p>
<h3>Running Script Using Node Command</h3>
<p>After writing code, Node.js executes file using:</p>
<pre><code class="language-bash">node filename
</code></pre>
<p>Example:</p>
<pre><code class="language-bash">node app.js
</code></pre>
<p>Output:</p>
<pre><code class="language-text">Hello Node.js
</code></pre>
<h3>Understanding What Happened</h3>
<p>Node.js:</p>
<ol>
<li><p>reads file</p>
</li>
<li><p>executes JavaScript</p>
</li>
<li><p>prints output</p>
</li>
</ol>
<p>This is how backend scripts run.</p>
<h3>Why Running Files Matters</h3>
<p>Real backend applications contain:</p>
<ul>
<li><p>APIs</p>
</li>
<li><p>servers</p>
</li>
<li><p>databases</p>
</li>
<li><p>authentication logic</p>
</li>
</ul>
<p>All of them eventually execute through Node runtime.</p>
<h3>Writing Hello World Server</h3>
<p>One of the most famous beginner examples in Node.js is creating first HTTP server.</p>
<h3>Why Server Matters</h3>
<p>Server is basically:</p>
<blockquote>
<p>a program that listens for requests and sends responses.</p>
</blockquote>
<p>Websites and APIs work using servers.</p>
<h3>Basic Hello World Server</h3>
<p>Example:</p>
<pre><code class="language-js">const http = require("http");

const server = http.createServer((req, res) =&gt; {
  res.end("Hello World");
});

server.listen(3000, () =&gt; {
  console.log("Server running on port 3000");
});
</code></pre>
<h3>Understanding This Code Step-by-Step</h3>
<h3><code>require("http")</code></h3>
<p>Imports built-in HTTP module.</p>
<h3><code>createServer()</code></h3>
<p>Creates web server.</p>
<h3><code>(req, res)</code></h3>
<p>Handles:</p>
<ul>
<li><p>incoming request</p>
</li>
<li><p>outgoing response</p>
</li>
</ul>
<h3><code>res.end()</code></h3>
<p>Sends response back to browser.</p>
<h3><code>listen(3000)</code></h3>
<p>Starts server on port 3000.</p>
<h3>Running the Server</h3>
<p>Command:</p>
<pre><code class="language-bash">node app.js
</code></pre>
<p>Terminal output:</p>
<pre><code class="language-text">Server running on port 3000
</code></pre>
<h3>Accessing the Server</h3>
<p>Open browser:</p>
<pre><code class="language-text">http://localhost:3000
</code></pre>
<p>Output:</p>
<pre><code class="language-text">Hello World
</code></pre>
<h3>Understanding <code>localhost</code></h3>
<p><code>localhost</code> means:</p>
<blockquote>
<p>your own computer.</p>
</blockquote>
<p>So browser communicates with server running on same machine.</p>
<h3>What Is a Port?</h3>
<p>Port is like a communication door.</p>
<p>Example:</p>
<pre><code class="language-text">localhost:3000
</code></pre>
<p>Means:</p>
<ul>
<li><p>computer → localhost</p>
</li>
<li><p>application → port 3000</p>
</li>
</ul>
<p>Different applications use different ports.</p>
<h3>Real Server Flow</h3>
<pre><code class="language-text">Browser
   ↓
Request
   ↓
Node.js Server
   ↓
Response
   ↓
Browser
</code></pre>
<p>This is the foundation of backend development.</p>
<h3>Common Beginner Mistake</h3>
<p>Many beginners forget saving file before running:</p>
<pre><code class="language-bash">node app.js
</code></pre>
<p>Then updated changes do not appear.</p>
<h3>Another Common Mistake</h3>
<p>Beginners often close terminal accidentally. If terminal closes:</p>
<ul>
<li>server also stops</li>
</ul>
<p>Because Node process terminates.</p>
<h3>Why First Server Is Important</h3>
<p>This simple server teaches core backend concepts:</p>
<ul>
<li><p>request</p>
</li>
<li><p>response</p>
</li>
<li><p>ports</p>
</li>
<li><p>HTTP communication</p>
</li>
<li><p>server listening</p>
</li>
</ul>
<p>Almost every backend framework builds on these same ideas.</p>
<h3>Understanding the Bigger Picture</h3>
<p>Even advanced frameworks like:</p>
<ul>
<li><p>:contentReference[oaicite:2]{index=2}</p>
</li>
<li><p>:contentReference[oaicite:3]{index=3}</p>
</li>
</ul>
<p>ultimately create HTTP servers internally.</p>
<p>So understanding raw Node server basics is extremely important.</p>
<h3>Final Understanding</h3>
<p>Installing :contentReference[oaicite:4]{index=4} allows JavaScript to run outside browsers and enables backend development.</p>
<p>Developers can:</p>
<ul>
<li><p>verify installation using terminal</p>
</li>
<li><p>experiment using Node REPL</p>
</li>
<li><p>create JavaScript files</p>
</li>
<li><p>execute scripts using node command</p>
</li>
</ul>
<p>And using Node’s built-in HTTP module, developers can create simple servers that listen for requests and send responses.</p>
<p>This becomes the foundation of modern backend development using JavaScript.</p>
]]></content:encoded></item><item><title><![CDATA[Storing Uploaded Files and Serving Them in Express]]></title><description><![CDATA[Where Uploaded Files Are Stored
Whenever users upload files like:

images

PDFs

videos

documents


those files need some place to live permanently. After upload, files are usually stored in:

local ]]></description><link>https://backendwithshubham.hashnode.dev/storing-uploaded-files-and-serving-them-in-express</link><guid isPermaLink="true">https://backendwithshubham.hashnode.dev/storing-uploaded-files-and-serving-them-in-express</guid><dc:creator><![CDATA[Shubham Bhardwaj]]></dc:creator><pubDate>Sun, 10 May 2026 15:01:46 GMT</pubDate><content:encoded><![CDATA[<h3>Where Uploaded Files Are Stored</h3>
<p>Whenever users upload files like:</p>
<ul>
<li><p>images</p>
</li>
<li><p>PDFs</p>
</li>
<li><p>videos</p>
</li>
<li><p>documents</p>
</li>
</ul>
<p>those files need some place to live permanently. After upload, files are usually stored in:</p>
<ul>
<li><p>local server storage</p>
</li>
<li><p>cloud/external storage systems</p>
</li>
</ul>
<h3>Understanding the Real Upload Flow</h3>
<p>When user uploads a file:</p>
<ol>
<li><p>browser sends file to backend</p>
</li>
<li><p>backend receives file</p>
</li>
<li><p>backend stores file somewhere</p>
</li>
<li><p>file path or URL is usually saved in database</p>
</li>
</ol>
<h3>Example</h3>
<p>Suppose user uploads:</p>
<pre><code class="language-text">profile.jpg
</code></pre>
<p>Backend may store:</p>
<ul>
<li><p>actual file in storage</p>
</li>
<li><p>file URL in database</p>
</li>
</ul>
<h3>Why File Storage Matters</h3>
<p>Applications constantly handle uploads:</p>
<ul>
<li><p>profile images</p>
</li>
<li><p>resumes</p>
</li>
<li><p>product images</p>
</li>
<li><p>videos</p>
</li>
<li><p>reports</p>
</li>
</ul>
<p>Without proper storage systems:</p>
<ul>
<li>uploaded data would disappear</li>
</ul>
<h3>Local Storage vs External Storage Concept</h3>
<p>There are mainly two common approaches.</p>
<h3>Local Storage</h3>
<p>Files are stored directly on server machine.</p>
<p>Example:</p>
<pre><code class="language-text">/project/uploads/image.jpg
</code></pre>
<h3>Understanding Local Storage</h3>
<p>Here:</p>
<ul>
<li>files physically exist inside backend server filesystem</li>
</ul>
<p>Very common for:</p>
<ul>
<li><p>small projects</p>
</li>
<li><p>learning projects</p>
</li>
<li><p>beginner applications</p>
</li>
</ul>
<h3>Example Local Folder Structure</h3>
<pre><code class="language-text">project/
 ├── uploads/
 │    ├── image1.jpg
 │    ├── file.pdf
</code></pre>
<h3>Advantages of Local Storage</h3>
<ul>
<li><p>simple setup</p>
</li>
<li><p>easy for beginners</p>
</li>
<li><p>fast local access</p>
</li>
<li><p>no external service required</p>
</li>
</ul>
<h3>Problems with Local Storage</h3>
<p>As applications grow:</p>
<ul>
<li><p>server storage becomes limited</p>
</li>
<li><p>scaling becomes difficult</p>
</li>
<li><p>files may disappear during deployments</p>
</li>
<li><p>multiple servers create synchronization problems</p>
</li>
</ul>
<h3>External Storage</h3>
<p>Instead of storing files on backend server directly:</p>
<ul>
<li>files get stored on external cloud services</li>
</ul>
<p>Examples:</p>
<ul>
<li><p>:contentReference[oaicite:0]{index=0} S3</p>
</li>
<li><p>:contentReference[oaicite:1]{index=1}</p>
</li>
<li><p>:contentReference[oaicite:2]{index=2}</p>
</li>
<li><p>:contentReference[oaicite:3]{index=3} Storage</p>
</li>
</ul>
<h3>Understanding External Storage</h3>
<p>Here:</p>
<ul>
<li><p>backend uploads files to cloud storage</p>
</li>
<li><p>cloud provider manages storage</p>
</li>
<li><p>backend stores returned file URLs</p>
</li>
</ul>
<h3>Example</h3>
<p>Instead of:</p>
<pre><code class="language-text">/uploads/image.jpg
</code></pre>
<p>you may get:</p>
<pre><code class="language-text">https://cloudservice.com/image123.jpg
</code></pre>
<h3>Why Large Applications Prefer External Storage</h3>
<p>Because cloud storage provides:</p>
<ul>
<li><p>scalability</p>
</li>
<li><p>CDN support</p>
</li>
<li><p>reliability</p>
</li>
<li><p>backups</p>
</li>
<li><p>distributed access</p>
</li>
</ul>
<h3>Real-World Thinking</h3>
<p>Suppose millions of users upload videos. Storing everything locally becomes very difficult. Cloud storage solves this problem efficiently.</p>
<h3>Serving Static Files in Express</h3>
<p>When files exist on server:</p>
<ul>
<li>users need access to them</li>
</ul>
<p>Express provides static file serving.</p>
<h3>What Are Static Files?</h3>
<p>Static files are files served directly without backend processing.</p>
<p>Examples:</p>
<ul>
<li><p>images</p>
</li>
<li><p>CSS files</p>
</li>
<li><p>JavaScript files</p>
</li>
<li><p>PDFs</p>
</li>
</ul>
<h3>Express Static Middleware</h3>
<p>Example:</p>
<pre><code class="language-js">app.use("/uploads", express.static("uploads"));
</code></pre>
<h3>Understanding This</h3>
<p>Suppose folder structure:</p>
<pre><code class="language-text">uploads/
 ├── image.jpg
</code></pre>
<p>Now file becomes accessible through:</p>
<pre><code class="language-text">http://localhost:3000/uploads/image.jpg
</code></pre>
<h3>Real Meaning of Static Serving</h3>
<p>Express basically says:</p>
<blockquote>
<p>“Anything inside this folder can be accessed publicly through URL.”</p>
</blockquote>
<h3>Accessing Uploaded Files via URL</h3>
<p>Once files are served statically:</p>
<ul>
<li>browsers can access them directly</li>
</ul>
<h3>Example</h3>
<p>Suppose uploaded file:</p>
<pre><code class="language-text">uploads/profile.jpg
</code></pre>
<p>If static middleware exists:</p>
<pre><code class="language-js">app.use("/uploads", express.static("uploads"));
</code></pre>
<p>Then URL becomes:</p>
<pre><code class="language-text">http://localhost:3000/uploads/profile.jpg
</code></pre>
<h3>Why File URLs Matter</h3>
<p>Frontend applications usually need URLs for:</p>
<ul>
<li><p>displaying images</p>
</li>
<li><p>downloading files</p>
</li>
<li><p>rendering videos</p>
</li>
</ul>
<p>So backend commonly stores:</p>
<ul>
<li><p>file path</p>
</li>
<li><p>public URL</p>
</li>
</ul>
<p>inside database.</p>
<h3>Example Database Entry</h3>
<p>Example:</p>
<pre><code class="language-json">{
  "profileImage": "/uploads/profile.jpg"
}
</code></pre>
<p>Frontend later uses this URL.</p>
<h3>Real Upload Architecture</h3>
<p>Typical flow:</p>
<pre><code class="language-text">User uploads file
   ↓
Backend receives file
   ↓
File stored
   ↓
File URL generated
   ↓
URL saved in database
   ↓
Frontend accesses file using URL
</code></pre>
<h3>Security Considerations for Uploads</h3>
<p>File uploads are extremely sensitive. Because attackers may upload:</p>
<ul>
<li><p>malware</p>
</li>
<li><p>dangerous scripts</p>
</li>
<li><p>huge files</p>
</li>
<li><p>fake file types</p>
</li>
</ul>
<p>Improper upload handling creates serious security risks.</p>
<h3>Problem 1 — Dangerous File Uploads</h3>
<p>Suppose attacker uploads:</p>
<pre><code class="language-text">virus.exe
</code></pre>
<p>or malicious scripts.</p>
<p>If server executes these files:</p>
<ul>
<li>system may become compromised</li>
</ul>
<h3>Solution</h3>
<p>Validate file types carefully.</p>
<p>Example:</p>
<ul>
<li><p>allow only images</p>
</li>
<li><p>reject executable files</p>
</li>
</ul>
<h3>Problem 2 — File Size Abuse</h3>
<p>Attackers may upload extremely large files.</p>
<p>This can:</p>
<ul>
<li><p>crash server</p>
</li>
<li><p>consume storage</p>
</li>
<li><p>increase bandwidth usage</p>
</li>
</ul>
<h3>Solution</h3>
<p>Set upload limits.</p>
<p>Example:</p>
<pre><code class="language-js">limits: {
  fileSize: 5 * 1024 * 1024
}
</code></pre>
<h3>Problem 3 — Fake File Extensions</h3>
<p>File may look like:</p>
<pre><code class="language-text">image.jpg
</code></pre>
<p>but actually contain malicious executable content.</p>
<h3>Solution</h3>
<p>Check:</p>
<ul>
<li><p>MIME type</p>
</li>
<li><p>actual file content</p>
</li>
<li><p>extension validation</p>
</li>
</ul>
<h3>Problem 4 — Public File Exposure</h3>
<p>If all uploaded files become public:</p>
<ul>
<li>sensitive documents may leak</li>
</ul>
<h3>Solution</h3>
<p>Use:</p>
<ul>
<li><p>authentication</p>
</li>
<li><p>private storage</p>
</li>
<li><p>signed URLs</p>
</li>
<li><p>permission checks</p>
</li>
</ul>
<h3>Problem 5 — Path Traversal Attacks</h3>
<p>Attackers may try manipulating filenames like:</p>
<pre><code class="language-text">../../../systemfile
</code></pre>
<h3>Solution</h3>
<p>Sanitize filenames properly. Never trust raw user input.</p>
<h3>Why Upload Security Is Extremely Important</h3>
<p>Large applications constantly face upload attacks.</p>
<p>Especially:</p>
<ul>
<li><p>social media</p>
</li>
<li><p>cloud systems</p>
</li>
<li><p>file sharing platforms</p>
</li>
</ul>
<p>Improper upload handling can lead to:</p>
<ul>
<li><p>server compromise</p>
</li>
<li><p>data leaks</p>
</li>
<li><p>malware distribution</p>
</li>
</ul>
<h3>Common Beginner Mistake</h3>
<p>Many beginners directly trust uploaded files.</p>
<p>Example:</p>
<ul>
<li><p>accepting all file types</p>
</li>
<li><p>exposing folders publicly</p>
</li>
<li><p>skipping validation</p>
</li>
</ul>
<p>This is dangerous.</p>
<h3>Another Common Mistake</h3>
<p>People often store uploaded files inside source code folders carelessly. During deployments:</p>
<ul>
<li>files may get deleted</li>
</ul>
<p>Production systems usually separate uploads from application code.</p>
<h3>Why Understanding File Storage Matters</h3>
<p>Modern applications heavily depend on uploads.Examples:</p>
<ul>
<li><p>social media images</p>
</li>
<li><p>resumes</p>
</li>
<li><p>invoices</p>
</li>
<li><p>product photos</p>
</li>
<li><p>videos</p>
</li>
</ul>
<p>Understanding:</p>
<ul>
<li><p>storage systems</p>
</li>
<li><p>static serving</p>
</li>
<li><p>file security</p>
</li>
</ul>
<p>is extremely important for backend development.</p>
<h3>Final Understanding</h3>
<p>Uploaded files are usually stored either:</p>
<ul>
<li><p>locally on server filesystem</p>
</li>
<li><p>externally using cloud storage services</p>
</li>
</ul>
<p>Express can serve uploaded files publicly using static middleware, allowing files to be accessed through URLs.</p>
<p>Modern applications often store:</p>
<ul>
<li><p>actual files in storage</p>
</li>
<li><p>file URLs in databases</p>
</li>
</ul>
<p>And because file uploads introduce major security risks, developers must carefully handle:</p>
<ul>
<li><p>file validation</p>
</li>
<li><p>file size limits</p>
</li>
<li><p>access control</p>
</li>
<li><p>dangerous file protection</p>
</li>
<li><p>storage security.</p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Getting Started with cURL]]></title><description><![CDATA[What Is cURL (In Very Simple Terms)
Whenever browsers open websites, they send requests to servers. Example:

browser requests webpage

server sends response


But developers often need a way to commu]]></description><link>https://backendwithshubham.hashnode.dev/getting-started-with-curl</link><guid isPermaLink="true">https://backendwithshubham.hashnode.dev/getting-started-with-curl</guid><dc:creator><![CDATA[Shubham Bhardwaj]]></dc:creator><pubDate>Sun, 10 May 2026 14:59:39 GMT</pubDate><content:encoded><![CDATA[<h3>What Is cURL (In Very Simple Terms)</h3>
<p>Whenever browsers open websites, they send requests to servers. Example:</p>
<ul>
<li><p>browser requests webpage</p>
</li>
<li><p>server sends response</p>
</li>
</ul>
<p>But developers often need a way to communicate with servers directly without using a browser UI. That is where:</p>
<pre><code class="language-text">cURL
</code></pre>
<p>comes in. cURL is basically:</p>
<blockquote>
<p>A command-line tool used to send requests to servers.</p>
</blockquote>
<p>You can think of cURL like:</p>
<blockquote>
<p>talking directly to websites and APIs from terminal.</p>
</blockquote>
<h3>What cURL Actually Does</h3>
<p>cURL allows developers to:</p>
<ul>
<li><p>send HTTP requests</p>
</li>
<li><p>test APIs</p>
</li>
<li><p>download data</p>
</li>
<li><p>upload files</p>
</li>
<li><p>debug backend systems</p>
</li>
</ul>
<p>All directly from terminal or command line.</p>
<h3>Real Thinking Behind cURL</h3>
<p>Instead of:</p>
<ul>
<li><p>opening browser</p>
</li>
<li><p>clicking buttons</p>
</li>
<li><p>filling forms manually</p>
</li>
</ul>
<p>developers can directly send requests using commands. This makes testing much faster.</p>
<h3>Why Programmers Need cURL</h3>
<p>Modern development heavily depends on:</p>
<ul>
<li><p>APIs</p>
</li>
<li><p>backend servers</p>
</li>
<li><p>authentication systems</p>
</li>
<li><p>external services</p>
</li>
</ul>
<p>Developers constantly need to:</p>
<ul>
<li><p>test endpoints</p>
</li>
<li><p>inspect responses</p>
</li>
<li><p>debug APIs</p>
</li>
<li><p>verify servers</p>
</li>
</ul>
<p>cURL helps do all this quickly.</p>
<h3>Real Use Cases of cURL</h3>
<p>Examples:</p>
<ul>
<li><p>testing backend APIs</p>
</li>
<li><p>checking server responses</p>
</li>
<li><p>sending POST requests</p>
</li>
<li><p>debugging authentication</p>
</li>
<li><p>downloading files</p>
</li>
<li><p>automating requests</p>
</li>
</ul>
<h3>Why cURL Became Popular</h3>
<p>Because:</p>
<ul>
<li><p>lightweight</p>
</li>
<li><p>fast</p>
</li>
<li><p>works from terminal</p>
</li>
<li><p>available almost everywhere</p>
</li>
</ul>
<p>Developers can test servers without needing full frontend applications.</p>
<h3>Making Your First Request Using cURL</h3>
<p>Basic syntax:</p>
<pre><code class="language-bash">curl https://example.com
</code></pre>
<p>Example:</p>
<pre><code class="language-bash">curl https://jsonplaceholder.typicode.com/posts
</code></pre>
<p>This sends a request to server. Server responds with data.</p>
<h3>Understanding What Happened</h3>
<p>When command runs:</p>
<ol>
<li><p>cURL sends HTTP request</p>
</li>
<li><p>server receives request</p>
</li>
<li><p>server sends response</p>
</li>
<li><p>cURL prints response in terminal</p>
</li>
</ol>
<h3>Very Simple Request Flow</h3>
<pre><code class="language-text">Client (cURL) → Request → Server
Server → Response → cURL
</code></pre>
<h3>Understanding Request and Response</h3>
<p>This is one of the most important concepts in web development.</p>
<h3>What Is a Request?</h3>
<p>Request means:</p>
<blockquote>
<p>Client asking server for something.</p>
</blockquote>
<p>Example:</p>
<ul>
<li><p>webpage</p>
</li>
<li><p>API data</p>
</li>
<li><p>user information</p>
</li>
</ul>
<h3>Example Request</h3>
<pre><code class="language-http">GET /posts
</code></pre>
<p>Meaning:</p>
<blockquote>
<p>“Give me posts data.”</p>
</blockquote>
<h3>What Is a Response?</h3>
<p>Response means:</p>
<blockquote>
<p>Server replying back with result.</p>
</blockquote>
<p>Example response:</p>
<pre><code class="language-json">[
  {
    "id": 1,
    "title": "Hello"
  }
]
</code></pre>
<h3>Real Communication Flow</h3>
<p>Example:</p>
<pre><code class="language-text">Browser/API Client/cURL
        ↓
     Request
        ↓
      Server
        ↓
     Response
</code></pre>
<p>This request-response cycle powers most internet communication.</p>
<h3>Using cURL to Talk to APIs</h3>
<p>One of the biggest uses of cURL is API testing.</p>
<h3>What Is an API?</h3>
<p>API allows applications to communicate with each other. Example:</p>
<ul>
<li><p>frontend asks backend for data</p>
</li>
<li><p>backend returns JSON response</p>
</li>
</ul>
<p>cURL helps developers simulate these requests manually.</p>
<h3>Example API Request</h3>
<p>Example:</p>
<pre><code class="language-bash">curl https://jsonplaceholder.typicode.com/users
</code></pre>
<p>Server returns JSON data.</p>
<h3>Understanding JSON Response</h3>
<p>Most APIs return:</p>
<pre><code class="language-text">JSON
</code></pre>
<p>format.</p>
<p>Example:</p>
<pre><code class="language-json">{
  "name": "John"
}
</code></pre>
<p>This data can later be used by applications.</p>
<h3>Sending POST Requests with cURL</h3>
<p>GET requests fetch data.<br />POST requests send data.<br />Example:</p>
<pre><code class="language-bash">curl -X POST https://example.com/api
</code></pre>
<p>Here:</p>
<pre><code class="language-text">-X POST
</code></pre>
<p>tells cURL:</p>
<blockquote>
<p>use POST method.</p>
</blockquote>
<h3>Sending Data with cURL</h3>
<p>Example:</p>
<pre><code class="language-bash">curl -X POST https://example.com/api \
-H "Content-Type: application/json" \
-d '{"name":"Shubham"}'
</code></pre>
<h3>Understanding This Command</h3>
<h4><code>-X POST</code></h4>
<p>Specifies HTTP method.</p>
<h4><code>-H</code></h4>
<p>Adds headers.</p>
<h4><code>-d</code></h4>
<p>Sends request body data.</p>
<h3>Why Headers Matter</h3>
<p>Headers provide extra information. Example:</p>
<pre><code class="language-text">Content-Type
Authorization
Accept
</code></pre>
<p>They help servers understand request details.</p>
<h3>Example — Authorization Header</h3>
<p>Example:</p>
<pre><code class="language-bash">curl -H "Authorization: Bearer TOKEN" https://example.com
</code></pre>
<p>Used heavily in authenticated APIs.</p>
<h3>Common HTTP Methods in cURL</h3>
<h3>GET</h3>
<p>Fetch data.</p>
<h3>POST</h3>
<p>Create/send data.</p>
<h3>PUT</h3>
<p>Update data.</p>
<h3>DELETE</h3>
<p>Remove data.</p>
<h3>Real API Development Workflow</h3>
<p>Backend developers constantly:</p>
<ul>
<li><p>create APIs</p>
</li>
<li><p>test endpoints</p>
</li>
<li><p>verify responses</p>
</li>
</ul>
<p>cURL becomes extremely useful during development.</p>
<h3>Common Mistakes Beginners Make with cURL</h3>
<h3>Mistake 1 — Forgetting Quotes</h3>
<p>Example:</p>
<p>Wrong:</p>
<pre><code class="language-bash">curl -H Content-Type: application/json
</code></pre>
<p>Correct:</p>
<pre><code class="language-bash">curl -H "Content-Type: application/json"
</code></pre>
<p>Quotes matter.</p>
<h3>Mistake 2 — Wrong HTTP Method</h3>
<p>Many beginners send:</p>
<ul>
<li><p>GET instead of POST</p>
</li>
<li><p>POST instead of PUT</p>
</li>
</ul>
<p>This causes API errors.</p>
<h3>Mistake 3 — Invalid JSON Data</h3>
<p>Example:</p>
<p>Wrong:</p>
<pre><code class="language-json">{name:Shubham}
</code></pre>
<p>Correct:</p>
<pre><code class="language-json">{"name":"Shubham"}
</code></pre>
<p>JSON format must be valid.</p>
<h3>Mistake 4 — Forgetting Authentication Headers</h3>
<p>Some APIs require:</p>
<ul>
<li><p>tokens</p>
</li>
<li><p>API keys</p>
</li>
<li><p>authorization headers</p>
</li>
</ul>
<p>Without them:</p>
<ul>
<li>requests fail</li>
</ul>
<h3>Mistake 5 — Confusing Request and Response</h3>
<p>Many beginners think:</p>
<ul>
<li>cURL creates APIs</li>
</ul>
<p>It does not. cURL is simply:</p>
<blockquote>
<p>a tool for communicating with APIs and servers.</p>
</blockquote>
<h3>Why cURL Is Important for Backend Developers</h3>
<p>cURL helps developers understand:</p>
<ul>
<li><p>HTTP communication</p>
</li>
<li><p>APIs</p>
</li>
<li><p>request-response cycles</p>
</li>
<li><p>headers</p>
</li>
<li><p>authentication</p>
</li>
</ul>
<p>And these are core backend development concepts.</p>
<h3>cURL vs Browser</h3>
<h4>Browser</h4>
<p>Designed for humans.</p>
<h4>cURL</h4>
<p>Designed for developers and debugging.</p>
<h3>Why Learning cURL Helps So Much</h3>
<p>Once developers understand cURL:</p>
<ul>
<li><p>APIs become easier to understand</p>
</li>
<li><p>backend debugging improves</p>
</li>
<li><p>HTTP concepts become clearer</p>
</li>
</ul>
<p>Many backend tools internally perform similar request logic.</p>
<h3>Final Understanding</h3>
<p>cURL is a command-line tool used for sending HTTP requests to servers and APIs.</p>
<p>It helps developers:</p>
<ul>
<li><p>test APIs</p>
</li>
<li><p>inspect responses</p>
</li>
<li><p>debug backend systems</p>
</li>
<li><p>simulate browser communication</p>
</li>
</ul>
<p>Using cURL, developers can:</p>
<ul>
<li><p>send GET and POST requests</p>
</li>
<li><p>attach headers</p>
</li>
<li><p>send JSON data</p>
</li>
<li><p>work with authenticated APIs</p>
</li>
</ul>
<p>And understanding cURL provides strong foundation for learning:</p>
<ul>
<li><p>APIs</p>
</li>
<li><p>backend development</p>
</li>
<li><p>HTTP communication</p>
</li>
<li><p>server debugging</p>
</li>
<li><p>modern web architecture.</p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Why Node.js is Perfect for Building Fast Web Applications]]></title><description><![CDATA[What Makes Node.js Fast
One of the biggest reasons developers love :contentReference[oaicite:0]{index=0} is speed. But many beginners hear things like:

“Node.js is super fast”

“Node.js handles milli]]></description><link>https://backendwithshubham.hashnode.dev/why-node-js-is-perfect-for-building-fast-web-applications</link><guid isPermaLink="true">https://backendwithshubham.hashnode.dev/why-node-js-is-perfect-for-building-fast-web-applications</guid><dc:creator><![CDATA[Shubham Bhardwaj]]></dc:creator><pubDate>Sun, 10 May 2026 14:56:46 GMT</pubDate><content:encoded><![CDATA[<h3>What Makes Node.js Fast</h3>
<p>One of the biggest reasons developers love :contentReference[oaicite:0]{index=0} is speed. But many beginners hear things like:</p>
<ul>
<li><p>“Node.js is super fast”</p>
</li>
<li><p>“Node.js handles millions of requests”</p>
</li>
<li><p>“Node.js is scalable”</p>
</li>
</ul>
<p>without understanding WHY. Node.js becomes fast mainly because of:</p>
<ul>
<li><p>non-blocking I/O</p>
</li>
<li><p>event-driven architecture</p>
</li>
<li><p>lightweight execution</p>
</li>
<li><p>asynchronous processing</p>
</li>
</ul>
<p>And understanding these concepts changes how you think about backend systems.</p>
<h3>Traditional Server Problem</h3>
<p>In traditional blocking systems, suppose a request needs:</p>
<ul>
<li><p>database query</p>
</li>
<li><p>file reading</p>
</li>
<li><p>API call</p>
</li>
</ul>
<p>The server may wait until task finishes. During waiting:</p>
<ul>
<li><p>thread stays blocked</p>
</li>
<li><p>resources stay occupied</p>
</li>
</ul>
<p>This reduces scalability.</p>
<h3>Real Problem with Blocking</h3>
<p>Imagine restaurant with one waiter. If waiter stands beside one customer for 10 minutes waiting for food:</p>
<ul>
<li>nobody else gets served</li>
</ul>
<p>That is blocking behavior. Node.js solves this differently.</p>
<h3>Non-Blocking I/O Concept</h3>
<p>This is one of the most important ideas in Node.js. Non-blocking means:</p>
<blockquote>
<p>Node.js does not wait for slow operations to finish before continuing other work.</p>
</blockquote>
<p>Example:</p>
<ul>
<li><p>database query starts</p>
</li>
<li><p>Node.js continues handling other requests</p>
</li>
<li><p>result comes later</p>
</li>
</ul>
<h3>Understanding I/O</h3>
<p>I/O means:</p>
<pre><code class="language-text">Input / Output
</code></pre>
<p>Examples:</p>
<ul>
<li><p>reading files</p>
</li>
<li><p>database operations</p>
</li>
<li><p>API requests</p>
</li>
<li><p>network communication</p>
</li>
</ul>
<p>These operations are usually slow compared to CPU operations.</p>
<h3>Blocking vs Non-Blocking Example</h3>
<h4>Blocking</h4>
<pre><code class="language-text">Start task
Wait...
Then continue
</code></pre>
<hr />
<h4>Non-Blocking</h4>
<pre><code class="language-text">Start task
Continue other work
Handle result later
</code></pre>
<p>This difference is huge for scalability.</p>
<h3>Simple Node.js Example</h3>
<p>Example:</p>
<pre><code class="language-js">const fs = require("fs");

fs.readFile("data.txt", "utf8", (err, data) =&gt; {
  console.log(data);
});

console.log("Other work continues");
</code></pre>
<p>Output:</p>
<pre><code class="language-text">Other work continues
[file content appears later]
</code></pre>
<p>Node.js does not stop execution while file is being read.</p>
<h3>Why Non-Blocking Makes Node.js Fast</h3>
<p>Because while one request waits for:</p>
<ul>
<li><p>database</p>
</li>
<li><p>file system</p>
</li>
<li><p>external API</p>
</li>
</ul>
<p>Node.js can continue handling thousands of other requests. This creates extremely efficient resource usage.</p>
<h3>Event-Driven Architecture</h3>
<p>Node.js is also:</p>
<pre><code class="language-text">Event-driven
</code></pre>
<p>Meaning:</p>
<blockquote>
<p>actions happen in response to events.</p>
</blockquote>
<h3>What Is an Event?</h3>
<p>An event is something that happens. Examples:</p>
<ul>
<li><p>request received</p>
</li>
<li><p>file loaded</p>
</li>
<li><p>button clicked</p>
</li>
<li><p>timer completed</p>
</li>
<li><p>database response returned</p>
</li>
</ul>
<h3>How Node.js Uses Events</h3>
<p>Node.js listens for events. When event occurs:</p>
<ul>
<li>corresponding callback/function runs</li>
</ul>
<p>Example:</p>
<pre><code class="language-js">server.on("request", () =&gt; {
  console.log("New request received");
});
</code></pre>
<p>Here:</p>
<ul>
<li><p><code>"request"</code> is event</p>
</li>
<li><p>callback executes when request arrives</p>
</li>
</ul>
<h3>Real Thinking Behind Event-Driven Systems</h3>
<p>Instead of constantly checking:</p>
<blockquote>
<p>“Is task completed yet?”</p>
</blockquote>
<p>Node.js says:</p>
<blockquote>
<p>“Notify me when task finishes.”</p>
</blockquote>
<p>This creates efficient asynchronous systems.</p>
<h3>Why Event-Driven Architecture Matters</h3>
<p>Event-driven architecture helps Node.js:</p>
<ul>
<li><p>handle many connections simultaneously</p>
</li>
<li><p>remain lightweight</p>
</li>
<li><p>respond quickly</p>
</li>
</ul>
<p>Especially for:</p>
<ul>
<li><p>APIs</p>
</li>
<li><p>chats</p>
</li>
<li><p>streaming</p>
</li>
<li><p>real-time systems</p>
</li>
</ul>
<h3>Single-Threaded Model Explanation</h3>
<p>This is where many beginners get confused.</p>
<p>Node.js is often called:</p>
<pre><code class="language-text">Single-threaded
</code></pre>
<p>Meaning:</p>
<blockquote>
<p>main JavaScript execution happens on one thread.</p>
</blockquote>
<h3>Common Beginner Confusion</h3>
<p>People hear:</p>
<pre><code class="language-text">single-threaded
</code></pre>
<p>and think:</p>
<blockquote>
<p>“Then Node.js should be slow.”</p>
</blockquote>
<p>But the important thing is:</p>
<ul>
<li>Node.js uses non-blocking architecture</li>
</ul>
<p>So even with one main thread:</p>
<ul>
<li>many requests can still be managed efficiently.</li>
</ul>
<h3>Understanding Single Thread Properly</h3>
<p>Node.js does NOT create one thread per request like many traditional servers. Instead:</p>
<ul>
<li><p>one main thread handles events</p>
</li>
<li><p>asynchronous tasks get delegated internally</p>
</li>
</ul>
<p>This avoids heavy thread creation overhead.</p>
<h3>Why Thread Creation Can Be Expensive</h3>
<p>Threads consume:</p>
<ul>
<li><p>memory</p>
</li>
<li><p>CPU resources</p>
</li>
<li><p>scheduling overhead</p>
</li>
</ul>
<p>Traditional systems with thousands of threads can become resource-heavy. Node.js avoids much of this complexity.</p>
<h3>Event Loop Connection</h3>
<p>Node.js uses something called:</p>
<pre><code class="language-text">Event Loop
</code></pre>
<p>The event loop continuously checks:</p>
<ul>
<li><p>completed async operations</p>
</li>
<li><p>pending callbacks</p>
</li>
<li><p>events waiting for execution</p>
</li>
</ul>
<p>And processes them efficiently.</p>
<h3>Real Understanding of Node.js Performance</h3>
<p>Node.js becomes powerful NOT because:</p>
<ul>
<li>JavaScript is magically faster</li>
</ul>
<p>but because:</p>
<ul>
<li>architecture is optimized for asynchronous I/O-heavy workloads</li>
</ul>
<h3>Where Node.js Performs Best</h3>
<p>Node.js performs best in:</p>
<ul>
<li><p>real-time applications</p>
</li>
<li><p>APIs</p>
</li>
<li><p>streaming systems</p>
</li>
<li><p>chat systems</p>
</li>
<li><p>microservices</p>
</li>
<li><p>I/O-heavy applications</p>
</li>
</ul>
<h3>Why Real-Time Systems Love Node.js</h3>
<p>Applications like:</p>
<ul>
<li><p>chat apps</p>
</li>
<li><p>notifications</p>
</li>
<li><p>multiplayer games</p>
</li>
<li><p>live dashboards</p>
</li>
</ul>
<p>require:</p>
<ul>
<li><p>many simultaneous connections</p>
</li>
<li><p>fast event handling</p>
</li>
</ul>
<p>Node.js handles this very efficiently.</p>
<h3>Excellent Use Cases for Node.js</h3>
<p>Examples:</p>
<ul>
<li><p>REST APIs</p>
</li>
<li><p>WebSocket servers</p>
</li>
<li><p>real-time chats</p>
</li>
<li><p>streaming platforms</p>
</li>
<li><p>collaborative tools</p>
</li>
</ul>
<h3>Where Node.js Is Less Ideal</h3>
<p>CPU-heavy operations can block main thread.</p>
<p>Examples:</p>
<ul>
<li><p>video rendering</p>
</li>
<li><p>heavy image processing</p>
</li>
<li><p>complex scientific computation</p>
</li>
</ul>
<p>Because JavaScript execution itself still uses one main thread.</p>
<h3>Real-World Companies Using Node.js</h3>
<p>Many major companies use :contentReference[oaicite:1]{index=1} in production. Examples:</p>
<ul>
<li><p>:contentReference[oaicite:2]{index=2}</p>
</li>
<li><p>:contentReference[oaicite:3]{index=3}</p>
</li>
<li><p>:contentReference[oaicite:4]{index=4}</p>
</li>
<li><p>:contentReference[oaicite:5]{index=5}</p>
</li>
<li><p>:contentReference[oaicite:6]{index=6}</p>
</li>
</ul>
<h3>Why Large Companies Chose Node.js</h3>
<p>Because Node.js provides:</p>
<ul>
<li><p>scalability</p>
</li>
<li><p>fast API handling</p>
</li>
<li><p>efficient concurrent connections</p>
</li>
<li><p>shared JavaScript ecosystem</p>
</li>
</ul>
<p>Especially for modern web applications.</p>
<h3>Example — Netflix</h3>
<p>:contentReference[oaicite:7]{index=7} used Node.js partly because:</p>
<ul>
<li><p>startup time improved</p>
</li>
<li><p>lightweight architecture helped scalability</p>
</li>
<li><p>JavaScript could be shared between frontend and backend</p>
</li>
</ul>
<h3>Example — PayPal</h3>
<p>:contentReference[oaicite:8]{index=8} reported performance improvements after adopting Node.js for certain systems.</p>
<h3>Common Beginner Mistake</h3>
<p>Many beginners think:</p>
<ul>
<li>Node.js handles everything simultaneously using many JavaScript threads</li>
</ul>
<p>Not exactly.</p>
<p>JavaScript execution itself mainly runs on:</p>
<blockquote>
<p>one main thread.</p>
</blockquote>
<p>Efficiency comes from:</p>
<ul>
<li><p>asynchronous architecture</p>
</li>
<li><p>event loop</p>
</li>
<li><p>non-blocking operations</p>
</li>
</ul>
<h3>Another Common Mistake</h3>
<p>People often think Node.js is always the fastest choice for every problem.</p>
<p>Not true.</p>
<p>Different architectures solve different problems better.</p>
<p>Node.js is especially powerful for:</p>
<ul>
<li><p>I/O-heavy systems</p>
</li>
<li><p>high concurrency</p>
</li>
<li><p>real-time communication</p>
</li>
</ul>
<h3>Why Understanding Node.js Internals Matters</h3>
<p>Once developers understand:</p>
<ul>
<li><p>event loop</p>
</li>
<li><p>non-blocking I/O</p>
</li>
<li><p>event-driven architecture</p>
</li>
</ul>
<p>backend systems become much easier to understand. And many “magic” behaviors of Node.js suddenly make sense.</p>
<h3>Final Understanding</h3>
<p>Node.js becomes fast mainly because of:</p>
<ul>
<li><p>non-blocking I/O</p>
</li>
<li><p>event-driven architecture</p>
</li>
<li><p>lightweight asynchronous handling</p>
</li>
</ul>
<p>Even though JavaScript runs on a single main thread, Node.js efficiently handles many concurrent requests using asynchronous operations and the event loop.</p>
<p>This makes Node.js extremely powerful for:</p>
<ul>
<li><p>APIs</p>
</li>
<li><p>real-time systems</p>
</li>
<li><p>streaming</p>
</li>
<li><p>scalable network applications</p>
</li>
</ul>
<p>And that is why many large companies use Node.js for modern backend infrastructure.</p>
]]></content:encoded></item><item><title><![CDATA[Sessions vs JWT vs Cookies: Understanding Authentication Approaches]]></title><description><![CDATA[What Sessions Are
Whenever users log into a website, the server needs some way to remember:

“Who is this user?”

Because HTTP itself is stateless.Meaning:

every request looks independent

server doe]]></description><link>https://backendwithshubham.hashnode.dev/sessions-vs-jwt-vs-cookies-understanding-authentication-approaches</link><guid isPermaLink="true">https://backendwithshubham.hashnode.dev/sessions-vs-jwt-vs-cookies-understanding-authentication-approaches</guid><dc:creator><![CDATA[Shubham Bhardwaj]]></dc:creator><pubDate>Sun, 10 May 2026 14:53:43 GMT</pubDate><content:encoded><![CDATA[<h3>What Sessions Are</h3>
<p>Whenever users log into a website, the server needs some way to remember:</p>
<blockquote>
<p>“Who is this user?”</p>
</blockquote>
<p>Because HTTP itself is stateless.<br />Meaning:</p>
<ul>
<li><p>every request looks independent</p>
</li>
<li><p>server does not automatically remember previous requests</p>
</li>
</ul>
<p>So authentication systems need memory mechanisms. One traditional solution is:</p>
<pre><code class="language-text">Sessions
</code></pre>
<h3>Understanding Sessions in Simple Terms</h3>
<p>A session is:</p>
<blockquote>
<p>Server-side stored user information.</p>
</blockquote>
<p>When user logs in:</p>
<ol>
<li><p>server creates session data</p>
</li>
<li><p>server stores it</p>
</li>
<li><p>server sends session ID to browser</p>
</li>
</ol>
<p>Now every future request includes that session ID. Server checks session storage and identifies user.</p>
<h3>Real Thinking Behind Sessions</h3>
<p>Think of hotel check-in.</p>
<p>After registration:</p>
<ul>
<li><p>hotel stores your details internally</p>
</li>
<li><p>you receive room card/token</p>
</li>
</ul>
<p>Whenever you show that card:</p>
<ul>
<li>hotel identifies you</li>
</ul>
<p>Sessions work similarly.</p>
<h3>Simple Session Flow</h3>
<pre><code class="language-text">User logs in
   ↓
Server creates session
   ↓
Session ID sent to browser
   ↓
Browser sends session ID on future requests
   ↓
Server identifies user
</code></pre>
<h3>What Cookies Are</h3>
<p>Cookies are:</p>
<blockquote>
<p>Small pieces of data stored inside the browser.</p>
</blockquote>
<p>Browsers automatically store and send cookies with requests.</p>
<h3>Important Understanding</h3>
<p>Cookies themselves are NOT authentication. They are just storage mechanisms. But sessions commonly use cookies.</p>
<h3>Example Cookie</h3>
<p>Example:</p>
<pre><code class="language-text">sessionId=abc123
</code></pre>
<p>Browser stores this cookie. Whenever user sends requests:</p>
<ul>
<li>browser automatically attaches cookie</li>
</ul>
<h3>Why Cookies Exist</h3>
<p>Cookies help store:</p>
<ul>
<li><p>session IDs</p>
</li>
<li><p>preferences</p>
</li>
<li><p>themes</p>
</li>
<li><p>login states</p>
</li>
<li><p>tracking information</p>
</li>
</ul>
<h3>Session + Cookie Relationship</h3>
<p>This is extremely important.</p>
<p>Usually:</p>
<ul>
<li><p>session data lives on server</p>
</li>
<li><p>session ID lives inside cookie</p>
</li>
</ul>
<p>Example:</p>
<pre><code class="language-text">Cookie → sessionId=abc123
</code></pre>
<p>Server uses:</p>
<pre><code class="language-text">abc123
</code></pre>
<p>to find corresponding session data.</p>
<h3>What JWT Tokens Are</h3>
<p>JWT stands for:</p>
<pre><code class="language-text">JSON Web Token
</code></pre>
<p>JWT is another authentication approach.<br />Unlike sessions:</p>
<ul>
<li>JWT stores user information directly inside token</li>
</ul>
<h3>Structure of JWT</h3>
<p>JWT usually contains:</p>
<ul>
<li><p>header</p>
</li>
<li><p>payload</p>
</li>
<li><p>signature</p>
</li>
</ul>
<p>Example format:</p>
<pre><code class="language-text">xxxxx.yyyyy.zzzzz
</code></pre>
<h3>Real Purpose of JWT</h3>
<p>JWT allows:</p>
<blockquote>
<p>authentication without storing session data on server.</p>
</blockquote>
<p>This creates:</p>
<pre><code class="language-text">Stateless Authentication
</code></pre>
<h3>Understanding JWT in Simple Terms</h3>
<p>When user logs in:</p>
<ol>
<li><p>server creates JWT</p>
</li>
<li><p>token contains encoded user information</p>
</li>
<li><p>token sent to client</p>
</li>
<li><p>client sends token with future requests</p>
</li>
</ol>
<p>Server verifies token signature.</p>
<p>If valid:</p>
<ul>
<li>user becomes authenticated</li>
</ul>
<h3>Example JWT Flow</h3>
<pre><code class="language-text">User logs in
   ↓
Server creates JWT
   ↓
JWT stored on client
   ↓
Client sends JWT in requests
   ↓
Server verifies token
</code></pre>
<h3>Stateful vs Stateless Authentication</h3>
<p>This is one of the most important concepts.</p>
<h3>Stateful Authentication</h3>
<p>Stateful means:</p>
<blockquote>
<p>Server stores authentication state.</p>
</blockquote>
<p>Sessions are stateful. Because:</p>
<ul>
<li>server remembers session data internally</li>
</ul>
<p>Example:</p>
<pre><code class="language-text">Session stored in database/memory
</code></pre>
<h3>Stateless Authentication</h3>
<p>Stateless means:</p>
<blockquote>
<p>Server does not store authentication state.</p>
</blockquote>
<p>JWT is stateless because:</p>
<ul>
<li>token itself contains required information</li>
</ul>
<p>Server only verifies token validity. No session storage needed.</p>
<h3>Real Difference in Thinking</h3>
<h4>Stateful</h4>
<p>Server remembers users.</p>
<h4>Stateless</h4>
<p>Client carries authentication data.</p>
<h3>Differences Between Session-Based Auth and JWT</h3>
<h3>1. Storage Location</h3>
<h4>Sessions</h4>
<p>Data stored on server.</p>
<h4>JWT</h4>
<p>Data stored inside token on client side.</p>
<h3>2. Scalability</h3>
<h4>Sessions</h4>
<p>Harder to scale because servers must share session storage.</p>
<h4>JWT</h4>
<p>Easier for distributed systems because token contains all required information.</p>
<h3>3. Server Memory Usage</h3>
<h4>Sessions</h4>
<p>Server memory/storage required.</p>
<h4>JWT</h4>
<p>No server-side session storage required.</p>
<h3>4. Token Size</h3>
<h4>Sessions</h4>
<p>Only session ID travels.</p>
<p>Small.</p>
<h4>JWT</h4>
<p>Entire token travels with requests.</p>
<p>Usually larger.</p>
<h3>5. Revoking Authentication</h3>
<h4>Sessions</h4>
<p>Easy.</p>
<p>Delete session from server.</p>
<h4>JWT</h4>
<p>Harder.</p>
<p>Because token may remain valid until expiration.</p>
<h3>6. Security Concerns</h3>
<h4>Sessions</h4>
<p>Safer against token leakage in some scenarios because sensitive data stays server-side.</p>
<h4>JWT</h4>
<p>Requires careful token handling because token contains encoded information.</p>
<h3>Session-Based Authentication Flow</h3>
<h3>Step 1</h3>
<p>User logs in.</p>
<h3>Step 2</h3>
<p>Server creates session.</p>
<h3>Step 3</h3>
<p>Session ID stored in cookie.</p>
<h3>Step 4</h3>
<p>Browser sends cookie automatically.</p>
<h3>Step 5</h3>
<p>Server checks session storage.</p>
<h3>JWT Authentication Flow</h3>
<h3>Step 1</h3>
<p>User logs in.</p>
<h3>Step 2</h3>
<p>Server creates JWT token.</p>
<h3>Step 3</h3>
<p>Client stores token.</p>
<h3>Step 4</h3>
<p>Client sends token in headers.</p>
<p>Usually:</p>
<pre><code class="language-http">Authorization: Bearer TOKEN
</code></pre>
<h3>Step 5</h3>
<p>Server verifies JWT signature.</p>
<h3>When to Use Session-Based Authentication</h3>
<p>Sessions work very well for:</p>
<ul>
<li><p>traditional web applications</p>
</li>
<li><p>server-rendered websites</p>
</li>
<li><p>smaller systems</p>
</li>
<li><p>applications needing strong session control</p>
</li>
</ul>
<h3>Why Sessions Are Still Popular</h3>
<p>Sessions provide:</p>
<ul>
<li><p>strong control</p>
</li>
<li><p>easy logout management</p>
</li>
<li><p>simpler revocation</p>
</li>
<li><p>mature browser support</p>
</li>
</ul>
<p>Many enterprise systems still heavily use sessions.</p>
<h3>Common Session Use Cases</h3>
<p>Examples:</p>
<ul>
<li><p>banking websites</p>
</li>
<li><p>admin dashboards</p>
</li>
<li><p>server-rendered applications</p>
</li>
</ul>
<h3>When to Use JWT</h3>
<p>JWT works very well for:</p>
<ul>
<li><p>APIs</p>
</li>
<li><p>mobile apps</p>
</li>
<li><p>microservices</p>
</li>
<li><p>distributed systems</p>
</li>
<li><p>frontend-backend separation</p>
</li>
</ul>
<h3>Why Modern APIs Prefer JWT</h3>
<p>Because APIs often need:</p>
<ul>
<li><p>stateless communication</p>
</li>
<li><p>scalability</p>
</li>
<li><p>cross-platform authentication</p>
</li>
</ul>
<p>JWT simplifies this.</p>
<h3>Common JWT Use Cases</h3>
<p>Examples:</p>
<ul>
<li><p>React frontend + Node.js backend</p>
</li>
<li><p>mobile applications</p>
</li>
<li><p>REST APIs</p>
</li>
<li><p>cloud-native architectures</p>
</li>
</ul>
<h3>Important Security Understanding</h3>
<p>JWT payload is encoded.</p>
<p>Not encrypted by default.</p>
<p>Meaning:</p>
<ul>
<li><p>anyone can decode payload</p>
</li>
<li><p>but signature prevents modification</p>
</li>
</ul>
<p>So sensitive information should not be stored carelessly inside JWT.</p>
<h3>Common Beginner Mistake</h3>
<p>Many beginners think:</p>
<ul>
<li>cookies and sessions are same thing</li>
</ul>
<p>They are not.</p>
<h4>Cookie</h4>
<p>Browser storage mechanism.</p>
<h4>Session</h4>
<p>Server-side authentication storage.</p>
<h3>Another Common Mistake</h3>
<p>People often think JWT automatically becomes more secure. Not necessarily. Security depends on:</p>
<ul>
<li><p>storage method</p>
</li>
<li><p>HTTPS usage</p>
</li>
<li><p>expiration handling</p>
</li>
<li><p>token protection</p>
</li>
</ul>
<h3>Why Authentication Design Matters</h3>
<p>Authentication affects:</p>
<ul>
<li><p>security</p>
</li>
<li><p>scalability</p>
</li>
<li><p>performance</p>
</li>
<li><p>user experience</p>
</li>
</ul>
<p>Choosing between:</p>
<ul>
<li><p>sessions</p>
</li>
<li><p>JWT</p>
</li>
</ul>
<p>depends on application architecture and requirements.</p>
<h3>Final Understanding</h3>
<p>Sessions, cookies, and JWT tokens are mechanisms used to manage user authentication.</p>
<ul>
<li><p>Sessions store authentication data on server</p>
</li>
<li><p>Cookies store small browser-side data</p>
</li>
<li><p>JWT stores encoded authentication information inside tokens</p>
</li>
</ul>
<p>Sessions represent:</p>
<pre><code class="language-text">Stateful Authentication
</code></pre>
<p>JWT represents:</p>
<pre><code class="language-text">Stateless Authentication
</code></pre>
<p>Session-based authentication is often preferred for traditional web applications, while JWT is commonly used for APIs and distributed systems.</p>
<p>Understanding the differences between these approaches is extremely important for designing secure and scalable modern applications.</p>
]]></content:encoded></item><item><title><![CDATA[What is Node.js? JavaScript on the Server Explained]]></title><description><![CDATA[What Node.js is
Node.js is a runtime environment for the JavaScript. In early days JavaScript can only work in the browsers, because browser have the JavaScrip engine that run the JavaScript. JavaScri]]></description><link>https://backendwithshubham.hashnode.dev/what-is-node-js-javascript-on-the-server-explained</link><guid isPermaLink="true">https://backendwithshubham.hashnode.dev/what-is-node-js-javascript-on-the-server-explained</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[Chaiaurcode]]></category><category><![CDATA[ChaiCohort]]></category><category><![CDATA[chaicode webdev cohort 2026]]></category><dc:creator><![CDATA[Shubham Bhardwaj]]></dc:creator><pubDate>Tue, 21 Apr 2026 18:18:25 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/67e41ba26be2bc2400a699ee/2a567f4f-0dbd-4fdb-8cd8-21755fe0a78a.svg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2>What Node.js is</h2>
<p>Node.js is a runtime environment for the JavaScript. In early days JavaScript can only work in the browsers, because browser have the JavaScrip engine that run the JavaScript. JavaScripts engine like V8, SpiderMonkey. In 2009 Ryan Dahl take the openSource V8 engine and bind with C++ combination of both become the Node.js. Binding V8 with C++, C++ gives the power to JavaScript to interact with Operating System things like reading files and accessing the network. Node.js provide the runtime environment for JavaScrip outside the browser.</p>
<h2>Why JavaScript was Originally browser-only</h2>
<p>In early days of the internet website are statics there is no interaction only the DOM(Document Object Model), In 1995 by Brendan Eich created the JavaScript at Netscape. The goal of the making the JavaScript is interactive websites. JavaScrip was made in 10 days (Not polished version its evolve). syntax like Java or C but under the hood contain the features like first class function(like Scheme), Dynamic typing(like Lisp), Prototypes(like Self). Because browser give the runtime environment(Engine) to the JavaScript for execution there was no other way to execute so JavaScript in the browser for long time and without engine JavaScript is a just plain text it can't execute anywhere.</p>
<h2>How Node.js made JavaScript run on servers</h2>
<p>In early days JavaScript is only ment to run on browser so in 2009 Ryan Dahl take the browser's engine V8 from chrome which is openSource. Now we can run the JavaScript in machines but it could execute JavaScript but not able to interact with the operating system like file reading and writing so he bind the V8 with C++. so that JavaScript can interact with the operating system. C++ acts as the bridge between JavaScript and the Operating System. and the last final part Node.js give us the some built in core modules like fs, http, os, etc with that now developers can write the server-side code. That's How the JavaScript run on servers.</p>
<img src="https://cdn.hashnode.com/uploads/covers/67e41ba26be2bc2400a699ee/254c9414-b70c-47c2-84ab-353880cc992f.svg" alt="" style="display:block;margin:0 auto" />

<h2>V8 engine overview (high level only)</h2>
<p>V8 is a JavaScript engine built by Google, first released in 2008 for the Chrome browser. Its only goal is to execute the JavaScript<br /><strong>Step 1 ( Parsing )</strong> : V8 reads your JavaScript code and converts it into a structure it can understand called an AST (Abstract Syntax Tree).<br /><strong>Step 2 ( Compilation (JIT) )</strong> : V8 uses JIT — Just In Time Compilation. Instead of interpreting line by line, it compiles JS into machine code right before executing it. <strong>Step 3 ( Execution )</strong> : The compiled machine code is executed directly by your CPU.</p>
<img src="https://cdn.hashnode.com/uploads/covers/67e41ba26be2bc2400a699ee/37cf9f6f-a5a0-422d-95b6-739dcc30e496.svg" alt="" style="display:block;margin:0 auto" />

<h2>Event-driven architecture idea</h2>
<p>Event-driven architecture is when a event happen reacting on the event is called the event-driven architecture let's take a example of register page in that when you fill the registation form and press enter a event happen and our server is ready to listen on the port and reacting on that event.</p>
<h2>Real-world use cases of Node.js</h2>
<p>There are the various real-world cases where Node.js shines like applications of Chat Applications, REST APIs &amp; Backend Services, Streaming Applications, E-Commerce Applications and many more. Making the sever is the main use case of the Node.jsny more. Making the sever is the main use case of the Node.js</p>
]]></content:encoded></item></channel></rss>