<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Parmesh B</title>
    <description>The latest articles on Forem by Parmesh B (@parmesh119).</description>
    <link>https://forem.com/parmesh119</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1138869%2F23f54a0e-cb9f-4e97-a226-9028ca65ae9a.jpg</url>
      <title>Forem: Parmesh B</title>
      <link>https://forem.com/parmesh119</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/parmesh119"/>
    <language>en</language>
    <item>
      <title>If a backend endpoint is public (allowed from any origin), browsers don’t send cookies by default.
And if you try to send cookies, you’ll likely face a CORS error unless the backend is properly configured.
I wrote a full blog with details 👇</title>
      <dc:creator>Parmesh B</dc:creator>
      <pubDate>Sat, 04 Oct 2025 12:50:26 +0000</pubDate>
      <link>https://forem.com/parmesh119/if-a-backend-endpoint-is-public-allowed-from-any-origin-browsers-dont-send-cookies-by-default-25e4</link>
      <guid>https://forem.com/parmesh119/if-a-backend-endpoint-is-public-allowed-from-any-origin-browsers-dont-send-cookies-by-default-25e4</guid>
      <description>&lt;p&gt;

&lt;/p&gt;
&lt;div class="ltag__link--embedded"&gt;
  &lt;div class="crayons-story "&gt;
  &lt;a href="https://dev.to/parmesh119/understanding-cookie-transmission-in-cross-origin-api-requests-1gkc" class="crayons-story__hidden-navigation-link"&gt;Understanding Cookie Transmission in Cross-Origin API Requests&lt;/a&gt;


  &lt;div class="crayons-story__body crayons-story__body-full_post"&gt;
    &lt;div class="crayons-story__top"&gt;
      &lt;div class="crayons-story__meta"&gt;
        &lt;div class="crayons-story__author-pic"&gt;

          &lt;a href="/parmesh119" class="crayons-avatar  crayons-avatar--l  "&gt;
            &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1138869%2F23f54a0e-cb9f-4e97-a226-9028ca65ae9a.jpg" alt="parmesh119 profile" class="crayons-avatar__image"&gt;
          &lt;/a&gt;
        &lt;/div&gt;
        &lt;div&gt;
          &lt;div&gt;
            &lt;a href="/parmesh119" class="crayons-story__secondary fw-medium m:hidden"&gt;
              Parmesh B
            &lt;/a&gt;
            &lt;div class="profile-preview-card relative mb-4 s:mb-0 fw-medium hidden m:inline-block"&gt;
              
                Parmesh B
                
              
              &lt;div id="story-author-preview-content-2891758" class="profile-preview-card__content crayons-dropdown branded-7 p-4 pt-0"&gt;
                &lt;div class="gap-4 grid"&gt;
                  &lt;div class="-mt-4"&gt;
                    &lt;a href="/parmesh119" class="flex"&gt;
                      &lt;span class="crayons-avatar crayons-avatar--xl mr-2 shrink-0"&gt;
                        &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1138869%2F23f54a0e-cb9f-4e97-a226-9028ca65ae9a.jpg" class="crayons-avatar__image" alt=""&gt;
                      &lt;/span&gt;
                      &lt;span class="crayons-link crayons-subtitle-2 mt-5"&gt;Parmesh B&lt;/span&gt;
                    &lt;/a&gt;
                  &lt;/div&gt;
                  &lt;div class="print-hidden"&gt;
                    
                      Follow
                    
                  &lt;/div&gt;
                  &lt;div class="author-preview-metadata-container"&gt;&lt;/div&gt;
                &lt;/div&gt;
              &lt;/div&gt;
            &lt;/div&gt;

          &lt;/div&gt;
          &lt;a href="https://dev.to/parmesh119/understanding-cookie-transmission-in-cross-origin-api-requests-1gkc" class="crayons-story__tertiary fs-xs"&gt;&lt;time&gt;Oct 4 '25&lt;/time&gt;&lt;span class="time-ago-indicator-initial-placeholder"&gt;&lt;/span&gt;&lt;/a&gt;
        &lt;/div&gt;
      &lt;/div&gt;

    &lt;/div&gt;

    &lt;div class="crayons-story__indention"&gt;
      &lt;h2 class="crayons-story__title crayons-story__title-full_post"&gt;
        &lt;a href="https://dev.to/parmesh119/understanding-cookie-transmission-in-cross-origin-api-requests-1gkc" id="article-link-2891758"&gt;
          Understanding Cookie Transmission in Cross-Origin API Requests
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;div class="crayons-story__tags"&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/frontend"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;frontend&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/backend"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;backend&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/learning"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;learning&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/development"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;development&lt;/a&gt;
        &lt;/div&gt;
      &lt;div class="crayons-story__bottom"&gt;
        &lt;div class="crayons-story__details"&gt;
          &lt;a href="https://dev.to/parmesh119/understanding-cookie-transmission-in-cross-origin-api-requests-1gkc" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left"&gt;
            &lt;div class="multiple_reactions_aggregate"&gt;
              &lt;span class="multiple_reactions_icons_container"&gt;
                  &lt;span class="crayons_icon_container"&gt;
                    &lt;img src="https://assets.dev.to/assets/exploding-head-daceb38d627e6ae9b730f36a1e390fca556a4289d5a41abb2c35068ad3e2c4b5.svg" width="18" height="18"&gt;
                  &lt;/span&gt;
                  &lt;span class="crayons_icon_container"&gt;
                    &lt;img src="https://assets.dev.to/assets/multi-unicorn-b44d6f8c23cdd00964192bedc38af3e82463978aa611b4365bd33a0f1f4f3e97.svg" width="18" height="18"&gt;
                  &lt;/span&gt;
                  &lt;span class="crayons_icon_container"&gt;
                    &lt;img src="https://assets.dev.to/assets/sparkle-heart-5f9bee3767e18deb1bb725290cb151c25234768a0e9a2bd39370c382d02920cf.svg" width="18" height="18"&gt;
                  &lt;/span&gt;
              &lt;/span&gt;
              &lt;span class="aggregate_reactions_counter"&gt;7&lt;span class="hidden s:inline"&gt; reactions&lt;/span&gt;&lt;/span&gt;
            &lt;/div&gt;
          &lt;/a&gt;
            &lt;a href="https://dev.to/parmesh119/understanding-cookie-transmission-in-cross-origin-api-requests-1gkc#comments" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left flex items-center"&gt;
              Comments


              &lt;span class="hidden s:inline"&gt;Add Comment&lt;/span&gt;
            &lt;/a&gt;
        &lt;/div&gt;
        &lt;div class="crayons-story__save"&gt;
          &lt;small class="crayons-story__tertiary fs-xs mr-2"&gt;
            5 min read
          &lt;/small&gt;
            
              &lt;span class="bm-initial"&gt;
                

              &lt;/span&gt;
              &lt;span class="bm-success"&gt;
                

              &lt;/span&gt;
            
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;




</description>
      <category>frontend</category>
      <category>backend</category>
      <category>learning</category>
      <category>development</category>
    </item>
    <item>
      <title>Understanding Cookie Transmission in Cross-Origin API Requests</title>
      <dc:creator>Parmesh B</dc:creator>
      <pubDate>Sat, 04 Oct 2025 12:40:35 +0000</pubDate>
      <link>https://forem.com/parmesh119/understanding-cookie-transmission-in-cross-origin-api-requests-1gkc</link>
      <guid>https://forem.com/parmesh119/understanding-cookie-transmission-in-cross-origin-api-requests-1gkc</guid>
      <description>&lt;h3&gt;
  
  
  The Problem That Cost Me 2 Hours
&lt;/h3&gt;

&lt;p&gt;Picture this: Your frontend is calling a public API endpoint. No authentication barriers, no token requirements—just a simple, open endpoint. Everything should work smoothly, right?&lt;/p&gt;

&lt;p&gt;Wrong.&lt;/p&gt;

&lt;p&gt;I spent over 2 hours pulling my hair out because my cookies weren't being sent with the requests. The API was public, my code looked fine, but the browser simply refused to include the cookies I needed.&lt;/p&gt;

&lt;p&gt;After diving deep into browser behavior, CORS policies, and cookie attributes, I finally understood what was happening. Here's everything I learned so you don't have to waste your afternoon debugging the same issue.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Big Misconception: Public ≠ Cookie Freedom
&lt;/h3&gt;

&lt;p&gt;Here's the crucial insight that took me way too long to understand:&lt;/p&gt;

&lt;p&gt;Just because an endpoint is public doesn't mean the browser will freely send cookies to it.&lt;/p&gt;

&lt;p&gt;A "public API" simply means there's no authentication barrier blocking access. But whether cookies are included in requests depends on an entirely different set of rules around:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Cookie attributes&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Cross-origin request policies&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;CORS configuration&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The browser doesn't care if the endpoint is "public" or "private"—it cares about security, and it enforces strict rules to protect users from attacks like CSRF (Cross-Site Request Forgery).&lt;/p&gt;

&lt;h3&gt;
  
  
  The Three Factors That Control Cookie Transmission
&lt;/h3&gt;

&lt;p&gt;After hours of debugging and reading documentation, I identified three critical factors that determine whether cookies are sent with cross-origin requests:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. SameSite Attribute: The Gatekeeper
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;SameSite&lt;/code&gt;attribute is the first line of defense. It tells the browser when a cookie should be included:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;SameSite=Strict&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Cookie is ONLY sent on same-site requests&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Cross-site requests? Forget about it—no cookies for you&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Most secure, but breaks legitimate cross-site use cases&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;SameSite=Lax&lt;/code&gt; (default in modern browsers)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Cookie sent on same-site requests&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Also sent on top-level navigations (like clicking a link)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;NOT sent on cross-site API calls (like fetch or axios requests)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;SameSite=None; Secure&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Allows cookies on cross-site requests&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;MUST be paired with the Secure flag&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Required for cookies to work in cross-origin API scenarios&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Key takeaway: If your backend sets cookies without SameSite=None; Secure, they won't be sent in cross-origin API calls, period.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Client-Side Configuration: Ask for Credentials
&lt;/h3&gt;

&lt;p&gt;Even with the right cookie attributes, you need to explicitly tell the browser to include cookies in cross-origin requests.&lt;/p&gt;

&lt;h3&gt;
  
  
  With Fetch API:
&lt;/h3&gt;

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

&lt;p&gt;&lt;code&gt;fetch(&lt;/code&gt;{your_endpoint}&lt;code&gt;, {&lt;br&gt;
  credentials: 'include'  // This is the magic line&lt;br&gt;
})&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  With Axios:
&lt;/h3&gt;

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

&lt;p&gt;&lt;code&gt;axios.get(&lt;/code&gt;{your_endpoint}&lt;code&gt;, {&lt;br&gt;
  withCredentials: true  // Enable cookie transmission&lt;br&gt;
})&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Without these flags, the browser assumes you don't want cookies sent cross-origin, and it won't include them—even if everything else is configured correctly.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Server-Side CORS Headers: The Final Permission
&lt;/h3&gt;

&lt;p&gt;Here's where my 2-hour bug was hiding: the server must explicitly allow credentialed requests with proper CORS headers.&lt;/p&gt;

&lt;h3&gt;
  
  
  Required headers:
&lt;/h3&gt;

&lt;p&gt;`Access-Control-Allow-Credentials: true&lt;/p&gt;

&lt;h3&gt;
  
  
  Critical rules:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;You CANNOT use Access-Control-Allow-Origin: * with credentials&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The origin must be specific&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Both headers must be present for cookies to be sent&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In my case, the frontend had withCredentials: true, but the backend team hadn't configured these CORS headers. The browser saw the mismatch and silently blocked the cookies.&lt;/p&gt;

&lt;h3&gt;
  
  
  Other Cookie Flags Worth Understanding
&lt;/h3&gt;

&lt;p&gt;While debugging, I also learned about two other important cookie attributes:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;HttpOnly&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Prevents JavaScript from accessing the cookie via document.cookie&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Does NOT prevent the browser from sending it in HTTP requests&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Great for security (prevents XSS attacks from stealing session cookies)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;Secure&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Cookie will ONLY be sent over HTTPS connections&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Essential for production environments&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Required when using SameSite=None&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  My Debugging Story: What Went Wrong
&lt;/h3&gt;

&lt;p&gt;Here's exactly what happened in my case:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Frontend setup: I was using axios with withCredentials: true ✅&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Cookie attributes: Backend was setting cookies, but without proper SameSite attributes ❌&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;CORS headers: The public API team hadn't configured Access-Control-Allow-Credentials or a specific Access-Control-Allow-Origin ❌&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The result? The browser looked at this mismatched configuration and said, "Nope, not sending those cookies."&lt;/p&gt;

&lt;p&gt;Once the backend team added:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;SameSite=None; Secure to the cookies&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Access-Control-Allow-Credentials: true&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Everything worked perfectly.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Complete Checklist for Cross-Origin Cookies
&lt;/h3&gt;

&lt;p&gt;Before you waste hours debugging like I did, check these three things:&lt;/p&gt;

&lt;h3&gt;
  
  
  ✅ Backend Cookie Configuration
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Cookies set with SameSite=None; Secure&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Secure flag present (requires HTTPS)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;HttpOnly flag if you don't need JS access&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  ✅ Frontend Request Configuration
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;withCredentials: true (Axios) or credentials: 'include' (Fetch)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Requests are going to the correct domain&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  ✅ Server CORS Headers
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Access-Control-Allow-Credentials: true&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Access-Control-Allow-Origin set to a specific origin (NOT *)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Headers present on both preflight and actual responses&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All three must align perfectly, or cookies won't be sent.&lt;/p&gt;

&lt;h3&gt;
  
  
  Pro Debugging Tips
&lt;/h3&gt;

&lt;p&gt;Chrome DevTools is your friend:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Open DevTools → Network tab&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on the failed request&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Go to the "Cookies" tab&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Look for blocked cookies and the reason why&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The browser will tell you exactly why cookies were blocked—you just need to know where to look!&lt;/p&gt;

&lt;h3&gt;
  
  
  The Bigger Picture: Why Browsers Are So Strict
&lt;/h3&gt;

&lt;p&gt;You might wonder: "Why make this so complicated? Why not just send cookies everywhere?"&lt;/p&gt;

&lt;p&gt;The answer is security. These restrictions exist to protect users from:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;CSRF attacks: Malicious sites make requests on your behalf to legitimate sites where you're logged in&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Data leakage: Third-party sites accessing your cookies and session data&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Privacy violations: Tracking users across different websites&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The browser assumes cookies contain sensitive data (like session tokens), so it requires explicit permission from both the client AND server before allowing cross-origin transmission.&lt;/p&gt;

&lt;p&gt;Yes, it makes development harder. But it also keeps users safe.&lt;/p&gt;

&lt;h3&gt;
  
  
  Key Takeaways
&lt;/h3&gt;

&lt;p&gt;After spending 2 hours on this bug, here's what I want you to remember:&lt;/p&gt;

&lt;p&gt;"Public endpoint" doesn't mean "free cookie access"—browsers enforce strict security rules regardless of API accessibility&lt;/p&gt;

&lt;p&gt;Three things must align:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Cookie attributes (SameSite=None; Secure)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Client configuration (withCredentials: true)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Server CORS headers (Allow-Credentials + specific Allow-Origin)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Silent failures are the worst—browsers often block cookies without obvious error messages, so know how to use DevTools to debug&lt;/p&gt;

&lt;p&gt;This is a backend AND frontend problem—you can't fix it from just one side&lt;/p&gt;

&lt;p&gt;Security over convenience—these restrictions exist for good reasons, even if they complicate development&lt;/p&gt;

&lt;h3&gt;
  
  
  Final Thoughts
&lt;/h3&gt;

&lt;p&gt;Cross-origin cookie handling is one of those topics that seems simple until you actually need to implement it. The combination of cookie attributes, CORS policies, and browser security rules creates a complex system that's easy to misconfigure.&lt;/p&gt;

&lt;p&gt;But once you understand the rules, debugging becomes much easier. Instead of randomly trying different configurations, you can systematically check each requirement and identify exactly what's missing.&lt;/p&gt;

&lt;p&gt;I hope this post saves you from the 2 hours of frustration I went through. If you're working with public APIs and need to send cookies cross-origin, bookmark this checklist—you'll thank yourself later!&lt;/p&gt;

</description>
      <category>frontend</category>
      <category>backend</category>
      <category>learning</category>
      <category>development</category>
    </item>
    <item>
      <title>From V8 to Event Loop: The Inner Anatomy of Node.js</title>
      <dc:creator>Parmesh B</dc:creator>
      <pubDate>Fri, 27 Jun 2025 06:29:14 +0000</pubDate>
      <link>https://forem.com/parmesh119/from-v8-to-event-loop-the-inner-anatomy-of-nodejs-2kg7</link>
      <guid>https://forem.com/parmesh119/from-v8-to-event-loop-the-inner-anatomy-of-nodejs-2kg7</guid>
      <description>&lt;h4&gt;
  
  
  &lt;strong&gt;What is Node.js?&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Many people think that Node.js is a framework, while other new students, who are beginners, understand it as a library. But wait, my friends, Node is neither a framework nor a library.&lt;/p&gt;

&lt;p&gt;Node.js is a cross-platform, open-source server environment that can run on Windows, Linux, macOS, and other operating systems. Node.js is a back-end JavaScript runtime environment that utilizes the V8 JavaScript engine to execute JavaScript code outside of a web browser. Node is built using programming languages like C, C++, and JavaScript.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;You can explore more about Node.js from their &lt;a href="https://nodejs.org/en/docs" rel="noopener noreferrer"&gt;official documentation&lt;/a&gt; or &lt;a href="https://en.wikipedia.org/wiki/Node.js" rel="noopener noreferrer"&gt;Wikipedia&lt;/a&gt;.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;The V8 Engine — the backbone of Node applications&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;V8 is a free and open-source JavaScript and WebAssembly engine developed by the Chromium Project for Chromium and Google Chrome web browsers. The project’s creator is Lars Bak. V8 Engine is built using JavaScript, C++, ECMAScript, and Assembly languages.&lt;/p&gt;

&lt;p&gt;V8 is the name of the JavaScript engine that powers Google Chrome. It’s the thing that takes our JavaScript and executes it while browsing with Chrome. V8 provides the runtime environment in which JavaScript executes. V8 is written in C++, and it’s continuously improved. It is portable and runs on Mac, Windows, Linux, and several other systems.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;You can explore more about the V8 Engine from &lt;a href="https://v8.dev/docs" rel="noopener noreferrer"&gt;official documentation&lt;/a&gt; or &lt;a href="https://en.wikipedia.org/wiki/V8_(JavaScript_engine)" rel="noopener noreferrer"&gt;Wikipedia&lt;/a&gt;.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Performance Myth — Internal Working of Node.js&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Many people are still not able to understand the internal workings of Node.js because its non-blocking and asynchronous execution of programs makes it very complex to understand. Especially the internal working of Node.js, which is the Event Loop.&lt;/p&gt;

&lt;p&gt;Now, many people have questioned what asynchronous programming is. So, let’s understand what Synchronous and Asynchronous Programming are.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;What is Synchronous and Asynchronous Programming?&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Synchronous programming means that the code runs in the sequence it is defined. When a function is called and has returned some value in a synchronous program, only then will the next line be executed.&lt;/p&gt;

&lt;p&gt;In simple words, synchronous programming means the execution of a program happens line by line. If one line is taking so much time to execute, then the execution of the program will not go to the next line. It waits for that line to execute, and then it will go to the next line. And if one line takes so much time to execute, then the UI will be frozen.&lt;/p&gt;

&lt;p&gt;Asynchronous programming, on the other hand, refers to code that doesn’t execute in sequence. These functions are performed not according to the sequence they are defined inside a program, but only when certain conditions are met.&lt;/p&gt;

&lt;p&gt;In simple words, Asynchronous means the environment doesn’t wait for a specific line to execute. It will start executing that line and put it into the background, and when the output of that line comes, it will print it to the console. This way the UI will not be frozen, only that part will be frozen whose going to fetch the data from the actual application server.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;- JavaScript code&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log("Hello world!")

setTimeout(()=&amp;gt;{
    console.log('set timeout function')
}, 2000)

console.log('I am Parmesh Bhatt.')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;- Output&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello world!
I am Parmesh Bhatt.
set timeout function
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let’s understand about Event Loop in Node.js.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Event Loop&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;An event loop is an endless loop that waits for tasks, executes them, and then sleeps until it receives more tasks. Node handles requests using an Event loop inside the NodeJS environment.&lt;/p&gt;

&lt;p&gt;Node.js has mainly three parts in the backend: Call Stack, Call Back Queue, and Node APIs. The combination of all three is called an Event Loop.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Let’s have an example and understand it simply:&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Suppose we have a code like below,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let a = 2;
let b = 3;
const sum = a + b;
console.log(sum);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;- For this, the output will be,&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;When we write a simple code like the above, the first execution will start, and the main function goes into the Call stack.&lt;/p&gt;

&lt;p&gt;**Now you all have a question in JavaScript, we are not writing any main function as we write in C++ and Java. So, where does this main function come in the Call stack?&lt;/p&gt;

&lt;p&gt;My friends, JavaScript is internally built. When it starts executing, it will automatically put all of the code in the main function and then start its actual execution.**&lt;/p&gt;

&lt;p&gt;After the main function goes into the Call stack, the console.log function goes into the Call stack, and the Call stack executes it, and the output is printed on the screen.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Call Stack has an internal building that follows the LIFO property. LIFO means Last In, First Out. This means whichever function comes last will execute first, and that’s why the main function comes first and gets out of the stack last.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Another example:&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log('one');

setTimeout(()=&amp;gt;{
    console.log("Settimeout 1")
}, 2000)

setTimeout(()=&amp;gt;{
    console.log("Settimeout 2")
}, 0)

console.log('Two');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;- The output of the above code is:&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;one
Two
Settimeout 2
Settimeout 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;It’s really interesting. Let’s understand it…&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;We all know now that in NodeJS, there are three things: the Call stack, the second is Node APIs, and the third is the call-back queue.&lt;/p&gt;

&lt;p&gt;When we start executing the above code, as usual, the main function goes into the Call stack, and the console.log function will also go into the Call stack.&lt;/p&gt;

&lt;p&gt;But from the setTimeout function, the setTimeout function will not go into the Call stack directly, but it goes into the Node API.&lt;/p&gt;

&lt;p&gt;Now you all have a question: Why does it go into the node API directly? Why does it not go into the Call stack?&lt;/p&gt;

&lt;p&gt;Let’s understand it,&lt;/p&gt;

&lt;p&gt;The setTimeout function goes into the Node API directly because the set timeout function is inherited from C++, and every function that inherits from C++ will always go into the Node API directly, and it will not be executed until it goes into the call stack.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;There are many more functions like setTimeout in C++. You may explore it on the Internet.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So the setTimeout 1st function goes into the Node API first, and the thesetTimeoutt 2 function goes into the Node API 2nd.&lt;/p&gt;

&lt;p&gt;Then the last console.log will go into the call stack, and the call stack will execute it and print the output. Now the output is one and two. After the execution of the last console, every function present in the Call stack will come out of the Call stack, including the main, and the Call stack is now empty.&lt;/p&gt;

&lt;p&gt;When the call stack is empty, the functions that are present in the Node API will go into the Callback Queue one by one.&lt;/p&gt;

&lt;p&gt;The functions that have a lower priority will go first into the callback queue. When multiple functions are present in the node API, every function will go one by one into the Callback Queue, and then into the Call Stack.&lt;/p&gt;

&lt;p&gt;Every function that is present in the Callback Queue will only go into the Call stack when the Call stack is empty. If the Call stack is not empty, the functions will not go into the call stack from the Callback Queue.&lt;/p&gt;

&lt;p&gt;After going into the Call stack, the Call stack executes the functions one by one and prints the output on the console. And that’s why the output is like,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;one
Two
Settimeout 2
Settimeout 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is how the node works internally, and the combination of the call stack, Node API, and the Callback Queue is known as an Event Loop.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Features of Event Loop&lt;/strong&gt;
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;An event loop is an endless loop that waits for tasks, executes them, and then sleeps until it receives more tasks.&lt;/li&gt;
&lt;li&gt;The event loop executes tasks from the event queue only when the call stack is empty, i.e., there is no ongoing task.&lt;/li&gt;
&lt;li&gt;The event loop allows us to use callbacks and promises.&lt;/li&gt;
&lt;li&gt;The event loop executes the tasks starting from the oldest first.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;The event loop allows Node.js to perform non-blocking I/O operations even though JavaScript is single-threaded.&lt;/p&gt;

&lt;p&gt;Since most modern kernels are multi-threaded, they can handle multiple operations executing in the background. When one of these operations completes, the kernel tells Node.js so that the appropriate callback may be added to the poll queue to eventually be executed.&lt;/p&gt;

&lt;p&gt;Now you should understand why the two-second time delay function does not block the rest of the program from executing…&lt;/p&gt;

&lt;p&gt;So, I end my conversation here, but if anybody wants to connect with me, then the links are below…&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.linkedin.com/in/parmesh-bhatt/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://twitter.com/Parmesh_119" rel="noopener noreferrer"&gt;Twitter or X&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/Parmesh119" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>node</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>opensource</category>
    </item>
  </channel>
</rss>
