<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Krati Joshi</title>
    <description>The latest articles on DEV Community by Krati Joshi (@joshikrati03).</description>
    <link>https://dev.to/joshikrati03</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4040583%2F7d30ee5e-5124-4c74-8ef9-8fb626646302.png</url>
      <title>DEV Community: Krati Joshi</title>
      <link>https://dev.to/joshikrati03</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/joshikrati03"/>
    <language>en</language>
    <item>
      <title>Postgresql Simplified</title>
      <dc:creator>Krati Joshi</dc:creator>
      <pubDate>Mon, 10 Aug 2026 18:15:49 +0000</pubDate>
      <link>https://dev.to/joshikrati03/postgresql-simplified-m8o</link>
      <guid>https://dev.to/joshikrati03/postgresql-simplified-m8o</guid>
      <description>&lt;h1&gt;
  
  
  PostgreSQL Backend Developer Essentials: The Concepts I Learned Beyond CRUD
&lt;/h1&gt;

&lt;p&gt;When working with Node.js backend applications, knowing SQL CRUD operations is only the beginning.&lt;/p&gt;

&lt;p&gt;As backend developers, we eventually need to understand transactions, indexes, concurrency, query optimization, JSONB, connection pooling, and how PostgreSQL actually executes our queries.&lt;/p&gt;

&lt;p&gt;Today I explored some of the PostgreSQL concepts that matter most for backend development.&lt;/p&gt;

&lt;h2&gt;
  
  
  🐘 What is PostgreSQL?
&lt;/h2&gt;

&lt;p&gt;PostgreSQL is an open-source object-relational database management system (ORDBMS).&lt;/p&gt;

&lt;p&gt;It provides traditional relational database features such as tables, rows, columns, primary keys, foreign keys, and SQL, while also supporting advanced capabilities such as JSONB, arrays, custom types, functions, extensions, and specialized indexes.&lt;/p&gt;

&lt;p&gt;A typical Node.js backend flow looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Client
   ↓
Node.js / Express
   ↓
Prisma / PostgreSQL driver
   ↓
PostgreSQL
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  🔑 Primary Key vs Foreign Key
&lt;/h2&gt;

&lt;p&gt;A primary key uniquely identifies a row.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="n"&gt;UUID&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A foreign key creates a relationship between tables.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="n"&gt;user_id&lt;/span&gt; &lt;span class="n"&gt;UUID&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The foreign key also helps maintain referential integrity.&lt;/p&gt;

&lt;p&gt;A simple way to remember:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Primary Key → identifies
Foreign Key → relates
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  🔒 Transactions and ACID
&lt;/h2&gt;

&lt;p&gt;A transaction groups multiple database operations into one logical unit.&lt;/p&gt;

&lt;p&gt;For example, a money transfer requires both:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Account A → -₹1000
Account B → +₹1000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If one operation fails, we don't want only half of the transaction to be committed.&lt;/p&gt;

&lt;p&gt;That's where ACID comes in:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A → Atomicity
C → Consistency
I → Isolation
D → Durability
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Transactions help make database operations reliable.&lt;/p&gt;

&lt;h2&gt;
  
  
  🔄 MVCC
&lt;/h2&gt;

&lt;p&gt;One PostgreSQL concept I found particularly important is MVCC — Multi-Version Concurrency Control.&lt;/p&gt;

&lt;p&gt;PostgreSQL maintains different row versions/snapshots so concurrent transactions can work with consistent views of data.&lt;/p&gt;

&lt;p&gt;The goal is to allow reads and writes to happen concurrently with less blocking than a simple locking model would provide.&lt;/p&gt;

&lt;p&gt;For a backend developer, the key takeaway is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;MVCC is one of the mechanisms PostgreSQL uses to provide concurrency and transaction isolation.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  🧩 JSONB
&lt;/h2&gt;

&lt;p&gt;PostgreSQL supports JSONB for storing semi-structured data.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="n"&gt;UUID&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;preferences&lt;/span&gt; &lt;span class="n"&gt;JSONB&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We could store:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"theme"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"dark"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"language"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"en"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"notifications"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then query a property:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;preferences&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="s1"&gt;'theme'&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;JSONB becomes particularly powerful when combined with appropriate indexing, such as a GIN index.&lt;/p&gt;

&lt;p&gt;However, JSONB shouldn't automatically replace relational columns. Frequently queried, constrained, and relational data is often better represented using normal columns.&lt;/p&gt;

&lt;h2&gt;
  
  
  📊 Indexes
&lt;/h2&gt;

&lt;p&gt;Indexes can significantly improve query performance by providing a faster access path to relevant rows.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_users_email&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But indexes aren't free.&lt;/p&gt;

&lt;p&gt;They consume storage and add overhead to INSERT, UPDATE, and DELETE operations because the index also needs to be maintained.&lt;/p&gt;

&lt;p&gt;So:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Don't create indexes blindly. Create them based on actual query patterns and execution plans.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  🔍 EXPLAIN and EXPLAIN ANALYZE
&lt;/h2&gt;

&lt;p&gt;When a query becomes slow, we need to understand how PostgreSQL is executing it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'test@example.com'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;EXPLAIN&lt;/code&gt; shows the planner's estimated execution plan.&lt;/p&gt;

&lt;p&gt;For actual execution information:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="k"&gt;ANALYZE&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'test@example.com'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; actually executes the query and reports actual runtime statistics.&lt;/p&gt;

&lt;p&gt;For deeper investigation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can help identify CPU and I/O-related bottlenecks.&lt;/p&gt;

&lt;p&gt;Some important things to look for include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sequential Scan&lt;/li&gt;
&lt;li&gt;Index Scan&lt;/li&gt;
&lt;li&gt;Actual execution time&lt;/li&gt;
&lt;li&gt;Estimated vs actual rows&lt;/li&gt;
&lt;li&gt;Loops&lt;/li&gt;
&lt;li&gt;Buffer hits&lt;/li&gt;
&lt;li&gt;Buffer reads&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A Sequential Scan isn't automatically bad. If a query needs a large percentage of the table, PostgreSQL may correctly decide that scanning the table is cheaper than using an index.&lt;/p&gt;

&lt;h2&gt;
  
  
  🧠 CTEs
&lt;/h2&gt;

&lt;p&gt;CTE stands for Common Table Expression.&lt;/p&gt;

&lt;p&gt;It allows us to define a named intermediate query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="n"&gt;active_users&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
    &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;
    &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'active'&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;active_users&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;CTEs can make complex SQL easier to read and can also be useful for recursive queries and multi-step data processing.&lt;/p&gt;

&lt;h2&gt;
  
  
  📈 Window Functions
&lt;/h2&gt;

&lt;p&gt;Window functions allow calculations across related rows without collapsing the result into one row per group.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;RANK&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="n"&gt;OVER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;rank&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Common window functions include:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ROW_NUMBER()
RANK()
DENSE_RANK()
LAG()
LEAD()
SUM() OVER()
AVG() OVER()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is an important distinction:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GROUP BY
→ reduces/groups rows

Window Function
→ keeps rows + calculates across them
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  🧱 Other PostgreSQL Features
&lt;/h2&gt;

&lt;p&gt;Some other concepts worth knowing as a backend developer are:&lt;/p&gt;

&lt;h3&gt;
  
  
  Arrays
&lt;/h3&gt;

&lt;p&gt;PostgreSQL can store arrays directly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="n"&gt;skills&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  UUID
&lt;/h3&gt;

&lt;p&gt;UUIDs provide globally unique identifiers and can be useful in distributed systems.&lt;/p&gt;

&lt;h3&gt;
  
  
  Views
&lt;/h3&gt;

&lt;p&gt;A view is a saved query that behaves like a virtual table.&lt;/p&gt;

&lt;h3&gt;
  
  
  Functions and Procedures
&lt;/h3&gt;

&lt;p&gt;PostgreSQL allows reusable logic to execute inside the database.&lt;/p&gt;

&lt;h3&gt;
  
  
  Extensions
&lt;/h3&gt;

&lt;p&gt;Extensions add additional functionality to PostgreSQL.&lt;/p&gt;

&lt;p&gt;Examples include:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pgcrypto
pg_trgm
PostGIS
citext
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  🚀 PostgreSQL in a Node.js Backend
&lt;/h2&gt;

&lt;p&gt;A typical architecture can look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Client
   ↓
Express API
   ↓
Controller
   ↓
Service
   ↓
Prisma
   ↓
Connection Pool
   ↓
PostgreSQL
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Connection pooling is important because creating a new database connection for every request is expensive.&lt;/p&gt;

&lt;p&gt;A connection pool allows the application to reuse a controlled number of database connections.&lt;/p&gt;

&lt;h2&gt;
  
  
  🧪 A Real Query Optimization Example
&lt;/h2&gt;

&lt;p&gt;One practical query I work with filters a large consumer table using conditions such as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;DIV_CODE&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(...)&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;BILL_CYC_CD&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'SBM'&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;CON_STATUS&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(...)&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;SUPPLY_TYPE&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="mi"&gt;59&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of immediately creating an index, the better approach is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;EXPLAIN
   ↓
EXPLAIN ANALYZE
   ↓
EXPLAIN (ANALYZE, BUFFERS)
   ↓
Inspect execution plan
   ↓
Check existing indexes
   ↓
Optimize
   ↓
Measure again
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This taught me an important backend lesson:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Performance optimization should be measurement-driven, not guess-driven.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  🎯 Key Takeaways
&lt;/h2&gt;

&lt;p&gt;The PostgreSQL concepts I consider most important for backend interviews are:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ACID
MVCC
Indexes
EXPLAIN ANALYZE
Connection Pooling
JSONB
JOINs
CTEs
Window Functions
Constraints
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Knowing CRUD tells us how to interact with a database.&lt;/p&gt;

&lt;p&gt;Understanding &lt;strong&gt;transactions, concurrency, indexing, query plans, and connection management&lt;/strong&gt; helps us understand how to build reliable and performant backend systems.&lt;/p&gt;

&lt;p&gt;That's the difference I'm aiming for: not just knowing how to write a query, but understanding &lt;strong&gt;what PostgreSQL is doing underneath it.&lt;/strong&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  PostgreSQL #NodeJS #BackendDevelopment #Database #SQL #WebDevelopment #Programming
&lt;/h1&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>javascript</category>
      <category>database</category>
    </item>
    <item>
      <title>🚀 Day 14 of My Node.js Learning Journey: Understanding the HTTP Module in Node.js</title>
      <dc:creator>Krati Joshi</dc:creator>
      <pubDate>Tue, 04 Aug 2026 11:41:50 +0000</pubDate>
      <link>https://dev.to/joshikrati03/day-14-of-my-nodejs-learning-journey-understanding-the-http-module-in-nodejs-46k6</link>
      <guid>https://dev.to/joshikrati03/day-14-of-my-nodejs-learning-journey-understanding-the-http-module-in-nodejs-46k6</guid>
      <description>&lt;p&gt;As I continue my Node.js learning journey, today's focus was on one of the most fundamental concepts of backend development—the &lt;strong&gt;HTTP Module&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Before learning frameworks like Express.js, it's important to understand how Node.js handles HTTP requests and responses under the hood. Since Express is built on top of the HTTP module, mastering this concept makes debugging and backend development much easier.&lt;/p&gt;




&lt;h1&gt;
  
  
  🌐 What is HTTP?
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;HTTP (HyperText Transfer Protocol)&lt;/strong&gt; is the protocol that enables communication between a client (browser, mobile app, or Postman) and a server.&lt;/p&gt;

&lt;p&gt;Every time you visit a website, the following happens:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Browser
   │
HTTP Request
   │
   ▼
Node.js Server
   │
HTTP Response
   ▼
Browser
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The client sends a request, the server processes it, and then returns a response.&lt;/p&gt;




&lt;h1&gt;
  
  
  📦 What is the HTTP Module?
&lt;/h1&gt;

&lt;p&gt;The &lt;strong&gt;HTTP module&lt;/strong&gt; is a built-in Node.js module that allows us to create web servers and handle HTTP requests without installing any external packages.&lt;/p&gt;

&lt;p&gt;Importing the module is simple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;http&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since it's a core module, no installation is required.&lt;/p&gt;




&lt;h1&gt;
  
  
  🛠️ Creating Your First HTTP Server
&lt;/h1&gt;

&lt;p&gt;One of the first things I learned was how easy it is to create a basic HTTP server.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;http&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createServer&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;end&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello, Node.js!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Server is running on port 3000&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After running this program and opening &lt;code&gt;http://localhost:3000&lt;/code&gt;, the browser displays the response sent by the server.&lt;/p&gt;




&lt;h1&gt;
  
  
  🔍 Understanding &lt;code&gt;req&lt;/code&gt; and &lt;code&gt;res&lt;/code&gt;
&lt;/h1&gt;

&lt;p&gt;The callback inside &lt;code&gt;createServer()&lt;/code&gt; receives two important objects:&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;req&lt;/code&gt; (Request Object)
&lt;/h3&gt;

&lt;p&gt;Contains information sent by the client, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Requested URL&lt;/li&gt;
&lt;li&gt;HTTP Method&lt;/li&gt;
&lt;li&gt;Headers&lt;/li&gt;
&lt;li&gt;Client IP&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some commonly used properties are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;req.url&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;req.method&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;req.headers&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;code&gt;res&lt;/code&gt; (Response Object)
&lt;/h3&gt;

&lt;p&gt;Used to send data back to the client.&lt;/p&gt;

&lt;p&gt;Some frequently used methods include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;res.write()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;res.end()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;res.setHeader()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;res.writeHead()&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One important thing I learned today:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Always call &lt;code&gt;res.end()&lt;/code&gt; to complete the response.&lt;/strong&gt; Without it, the client keeps waiting because the server doesn't know the response has finished.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  🧭 Simple Routing Without Express
&lt;/h1&gt;

&lt;p&gt;Even without Express.js, we can create basic routes using conditions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;end&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Home Page&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/about&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;end&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;About Page&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;statusCode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;404&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;end&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Page Not Found&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This helped me understand how frameworks like Express simplify routing.&lt;/p&gt;




&lt;h1&gt;
  
  
  📌 Common HTTP Methods
&lt;/h1&gt;

&lt;p&gt;Every API uses HTTP methods to perform different operations.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Method&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;GET&lt;/td&gt;
&lt;td&gt;Retrieve data&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;POST&lt;/td&gt;
&lt;td&gt;Create new data&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PUT&lt;/td&gt;
&lt;td&gt;Replace existing data&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PATCH&lt;/td&gt;
&lt;td&gt;Update part of existing data&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;DELETE&lt;/td&gt;
&lt;td&gt;Remove data&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;These methods form the foundation of REST APIs.&lt;/p&gt;




&lt;h1&gt;
  
  
  📄 HTTP Status Codes
&lt;/h1&gt;

&lt;p&gt;Servers use status codes to indicate the result of a request.&lt;/p&gt;

&lt;p&gt;Some commonly used ones are:&lt;/p&gt;

&lt;p&gt;✅ Success&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;200 — OK&lt;/li&gt;
&lt;li&gt;201 — Created&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;❌ Client Errors&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;400 — Bad Request&lt;/li&gt;
&lt;li&gt;401 — Unauthorized&lt;/li&gt;
&lt;li&gt;403 — Forbidden&lt;/li&gt;
&lt;li&gt;404 — Not Found&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔥 Server Errors&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;500 — Internal Server Error&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Learning these status codes is essential for backend interviews and API development.&lt;/p&gt;




&lt;h1&gt;
  
  
  📦 Headers and MIME Types
&lt;/h1&gt;

&lt;p&gt;HTTP headers carry metadata about requests and responses.&lt;/p&gt;

&lt;p&gt;One commonly used response header is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Content-Type&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;application/json&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This tells the browser that the response contains JSON data.&lt;/p&gt;

&lt;p&gt;Some common MIME types include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;application/json&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;text/html&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;text/plain&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;image/png&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;application/pdf&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  ⚡ HTTP Module vs Express.js
&lt;/h1&gt;

&lt;p&gt;One question that often comes up is:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;"If Express exists, why learn the HTTP module?"&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The answer is simple.&lt;/p&gt;

&lt;p&gt;The HTTP module provides the core functionality, while Express builds on top of it by adding features like routing, middleware, and request parsing.&lt;/p&gt;

&lt;p&gt;Understanding the HTTP module gives you a much clearer picture of what happens behind the scenes whenever an Express application receives a request.&lt;/p&gt;




&lt;h1&gt;
  
  
  🎯 Key Takeaways
&lt;/h1&gt;

&lt;p&gt;Today I learned:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;HTTP enables communication between clients and servers.&lt;/li&gt;
&lt;li&gt;Node.js provides a built-in HTTP module for creating servers.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;createServer()&lt;/code&gt; creates an HTTP server.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;req&lt;/code&gt; contains request information.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;res&lt;/code&gt; is used to send responses.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;res.end()&lt;/code&gt; is mandatory to complete the response.&lt;/li&gt;
&lt;li&gt;Routing can be implemented without Express.&lt;/li&gt;
&lt;li&gt;HTTP methods define CRUD operations.&lt;/li&gt;
&lt;li&gt;Status codes communicate the outcome of a request.&lt;/li&gt;
&lt;li&gt;Express.js is built on top of the HTTP module.&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  💭 Final Thoughts
&lt;/h1&gt;

&lt;p&gt;Today's session helped me understand what happens behind every API request.&lt;/p&gt;

&lt;p&gt;Whenever we use Express.js, call an API, or open a webpage, the HTTP module is working behind the scenes. Learning it first makes backend concepts much easier to understand and gives a stronger foundation for building scalable server-side applications.&lt;/p&gt;

&lt;p&gt;Every day of learning brings me one step closer to becoming a better backend developer. Looking forward to exploring more Node.js concepts in the coming days!&lt;/p&gt;




&lt;p&gt;If you're also learning Node.js, I'd love to hear your thoughts or discuss backend concepts in the comments. Let's keep learning together! 🚀&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;#NodeJS #JavaScript #BackendDevelopment #ExpressJS #WebDevelopment #RESTAPI #100DaysOfCode #LearningInPublic #Programming #DevCommunity&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>node</category>
      <category>programming</category>
      <category>ai</category>
    </item>
    <item>
      <title>🚀 Day 13 of My Node.js Learning Journey: Understanding Buffers in Node.js</title>
      <dc:creator>Krati Joshi</dc:creator>
      <pubDate>Mon, 03 Aug 2026 17:07:52 +0000</pubDate>
      <link>https://dev.to/joshikrati03/day-11-of-my-nodejs-learning-journey-understanding-buffers-in-nodejs-5h9d</link>
      <guid>https://dev.to/joshikrati03/day-11-of-my-nodejs-learning-journey-understanding-buffers-in-nodejs-5h9d</guid>
      <description>&lt;p&gt;Today, I learned one of the core concepts behind how Node.js handles binary data—&lt;strong&gt;Buffers&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When working with files, images, videos, PDFs, or network communication, Node.js doesn't process everything as normal JavaScript strings. Instead, it uses &lt;strong&gt;Buffers&lt;/strong&gt; to efficiently store and manipulate raw binary data.&lt;/p&gt;

&lt;h2&gt;
  
  
  🤔 What is a Buffer?
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;Buffer&lt;/strong&gt; is a built-in Node.js class used to temporarily store &lt;strong&gt;raw binary data&lt;/strong&gt; in memory.&lt;/p&gt;

&lt;p&gt;Think of it as a temporary container that holds small chunks of data before they're processed or transferred.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Simple definition:&lt;/strong&gt; A Buffer is a fixed-size memory allocation used to store binary data efficiently.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  💡 Why Do We Need Buffers?
&lt;/h2&gt;

&lt;p&gt;JavaScript mainly works with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Strings&lt;/li&gt;
&lt;li&gt;Numbers&lt;/li&gt;
&lt;li&gt;Objects&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But backend applications often deal with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;📄 PDF files&lt;/li&gt;
&lt;li&gt;🖼️ Images&lt;/li&gt;
&lt;li&gt;🎥 Videos&lt;/li&gt;
&lt;li&gt;🎵 Audio files&lt;/li&gt;
&lt;li&gt;🌐 Network packets&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Since these are binary data, Node.js uses &lt;strong&gt;Buffers&lt;/strong&gt; to handle them efficiently without converting everything into strings.&lt;/p&gt;




&lt;h2&gt;
  
  
  ⚡ Where Are Buffers Used?
&lt;/h2&gt;

&lt;p&gt;Buffers are used in many backend operations, including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;File System (&lt;code&gt;fs&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Streams&lt;/li&gt;
&lt;li&gt;HTTP requests and responses&lt;/li&gt;
&lt;li&gt;TCP sockets&lt;/li&gt;
&lt;li&gt;WebSockets&lt;/li&gt;
&lt;li&gt;Image processing&lt;/li&gt;
&lt;li&gt;Video and audio streaming&lt;/li&gt;
&lt;li&gt;Encryption and compression&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you're building backend applications, you're already using Buffers—even if you don't realize it.&lt;/p&gt;




&lt;h2&gt;
  
  
  🧠 Interesting Interview Fact
&lt;/h2&gt;

&lt;p&gt;One thing that surprised me today is that &lt;strong&gt;Buffers are not stored inside the V8 JavaScript heap.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Instead, they are allocated in &lt;strong&gt;Native (C++) Memory&lt;/strong&gt;, which helps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reduce Garbage Collection overhead&lt;/li&gt;
&lt;li&gt;Improve performance&lt;/li&gt;
&lt;li&gt;Efficiently manage large binary data&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🛠️ Creating Buffers
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Create from a String
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Allocate Safe Memory
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;alloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Fixed size&lt;/li&gt;
&lt;li&gt;Filled with zeros&lt;/li&gt;
&lt;li&gt;Safe to use&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Allocate Fast Memory
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;allocUnsafe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Faster allocation&lt;/li&gt;
&lt;li&gt;Memory isn't initialized&lt;/li&gt;
&lt;li&gt;Should always overwrite before using&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔥 Common Buffer Methods
&lt;/h2&gt;

&lt;p&gt;Some methods I learned today:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Buffer.from()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Buffer.alloc()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Buffer.allocUnsafe()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;toString()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;write()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;copy()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;slice()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Buffer.concat()&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These methods are frequently used while working with files and streams.&lt;/p&gt;




&lt;h2&gt;
  
  
  📦 Buffer vs Stream
&lt;/h2&gt;

&lt;p&gt;This was the most important takeaway for me.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Buffer&lt;/th&gt;
&lt;th&gt;Stream&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Stores binary data temporarily&lt;/td&gt;
&lt;td&gt;Transfers data continuously&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Fixed-size memory&lt;/td&gt;
&lt;td&gt;Chunk-by-chunk data flow&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Holds data&lt;/td&gt;
&lt;td&gt;Moves data&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Used internally by Streams&lt;/td&gt;
&lt;td&gt;Uses Buffers internally&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Easy way to remember:
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Buffer stores. Stream transports.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  🎯 Key Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Buffers store &lt;strong&gt;raw binary data&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;They are &lt;strong&gt;fixed-size&lt;/strong&gt; and &lt;strong&gt;mutable&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Buffers are stored in &lt;strong&gt;Native (C++) Memory&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Streams use Buffers internally&lt;/li&gt;
&lt;li&gt;They improve performance while working with files and network communication&lt;/li&gt;
&lt;li&gt;Essential for backend development and frequently asked in Node.js interviews&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  📚 What I Learned Today
&lt;/h2&gt;

&lt;p&gt;Today's topic helped me understand what actually happens behind the scenes when Node.js reads a file or receives data over the network.&lt;/p&gt;

&lt;p&gt;Instead of loading everything into memory at once, Node.js processes data efficiently using Buffers, making applications faster and more memory-efficient.&lt;/p&gt;

&lt;p&gt;Every new Node.js concept makes the backend ecosystem feel more connected. Understanding Buffers also makes it much easier to understand Streams, file handling, and network programming.&lt;/p&gt;




&lt;p&gt;Thanks for reading! 🙌&lt;/p&gt;

&lt;p&gt;I'm currently documenting my &lt;strong&gt;Node.js interview preparation journey&lt;/strong&gt; by sharing what I learn every day. If you're preparing for backend interviews or starting with Node.js, feel free to follow along.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;#NodeJS #JavaScript #BackendDevelopment #WebDevelopment #Programming #100DaysOfCode #Coding #OpenSource #LearningInPublic #DevCommunity&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>javascript</category>
      <category>node</category>
    </item>
    <item>
      <title>🚀 Day 12 of My Node.js Learning Journey: Mastering Streams &amp; Backpressure 📂⚡</title>
      <dc:creator>Krati Joshi</dc:creator>
      <pubDate>Sun, 02 Aug 2026 11:07:22 +0000</pubDate>
      <link>https://dev.to/joshikrati03/day-12-of-my-nodejs-learning-journey-mastering-streams-backpressure-3889</link>
      <guid>https://dev.to/joshikrati03/day-12-of-my-nodejs-learning-journey-mastering-streams-backpressure-3889</guid>
      <description>&lt;p&gt;One of the biggest strengths of Node.js is its ability to process large amounts of data efficiently. Today, I explored &lt;strong&gt;Streams&lt;/strong&gt;, one of the most important concepts for backend development and a frequently asked topic in Node.js interviews.&lt;/p&gt;

&lt;h2&gt;
  
  
  💡 What are Streams?
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;Stream&lt;/strong&gt; is a way to process data &lt;strong&gt;chunk by chunk&lt;/strong&gt; instead of loading the entire file into memory.&lt;/p&gt;

&lt;p&gt;Instead of reading a 2 GB file at once, Node.js reads small chunks (typically &lt;strong&gt;64 KB&lt;/strong&gt; for &lt;code&gt;fs.createReadStream()&lt;/code&gt;), making applications much more memory-efficient and scalable.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why use Streams?
&lt;/h3&gt;

&lt;p&gt;✅ Lower memory usage&lt;/p&gt;

&lt;p&gt;✅ Faster processing&lt;/p&gt;

&lt;p&gt;✅ Better performance&lt;/p&gt;

&lt;p&gt;✅ Ideal for large files&lt;/p&gt;

&lt;p&gt;✅ Non-blocking data transfer&lt;/p&gt;




&lt;h2&gt;
  
  
  📦 What is a Chunk?
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;chunk&lt;/strong&gt; is a small piece of data transferred by a stream.&lt;/p&gt;

&lt;p&gt;Instead of:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2 GB File
      ↓
Load everything into RAM
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Streams work like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2 GB File
      ↓
64 KB
      ↓
64 KB
      ↓
64 KB
      ↓
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is why Node.js can efficiently handle file uploads, downloads, video streaming, and large datasets.&lt;/p&gt;




&lt;h2&gt;
  
  
  🌊 Types of Streams
&lt;/h2&gt;

&lt;p&gt;Node.js provides four types of streams:&lt;/p&gt;

&lt;h3&gt;
  
  
  📖 Readable Stream
&lt;/h3&gt;

&lt;p&gt;Used to read data from a source.&lt;/p&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;fs.createReadStream()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;HTTP Request&lt;/li&gt;
&lt;li&gt;&lt;code&gt;process.stdin&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  ✍️ Writable Stream
&lt;/h3&gt;

&lt;p&gt;Used to write data to a destination.&lt;/p&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;fs.createWriteStream()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;HTTP Response&lt;/li&gt;
&lt;li&gt;&lt;code&gt;process.stdout&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  🔄 Duplex Stream
&lt;/h3&gt;

&lt;p&gt;Can both &lt;strong&gt;read&lt;/strong&gt; and &lt;strong&gt;write&lt;/strong&gt; data.&lt;/p&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;TCP Sockets&lt;/li&gt;
&lt;li&gt;Network connections&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  ⚙️ Transform Stream
&lt;/h3&gt;

&lt;p&gt;A special type of Duplex Stream that modifies data while passing it through.&lt;/p&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Compression (&lt;code&gt;zlib.createGzip()&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Encryption&lt;/li&gt;
&lt;li&gt;Decryption&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  🚀 &lt;code&gt;pipe()&lt;/code&gt; — The Easiest Way to Transfer Data
&lt;/h1&gt;

&lt;p&gt;Instead of manually reading and writing chunks:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;fs&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;readStream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createReadStream&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;input.txt&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;writeStream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createWriteStream&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;output.txt&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;readStream&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;writeStream&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Benefits of &lt;code&gt;pipe()&lt;/code&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Less code&lt;/li&gt;
&lt;li&gt;Automatic chunk transfer&lt;/li&gt;
&lt;li&gt;Handles flow efficiently&lt;/li&gt;
&lt;li&gt;Automatically ends the destination stream&lt;/li&gt;
&lt;li&gt;Manages backpressure internally&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  🌊 Understanding Backpressure
&lt;/h1&gt;

&lt;p&gt;One of the most interesting concepts I learned today was &lt;strong&gt;Backpressure&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Imagine:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;📥 Producer can generate &lt;strong&gt;100 MB/sec&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;📤 Consumer can process only &lt;strong&gt;20 MB/sec&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Without flow control, data would keep accumulating in memory.&lt;/p&gt;

&lt;p&gt;Backpressure solves this by &lt;strong&gt;pausing the producer until the consumer catches up&lt;/strong&gt;, preventing unnecessary memory growth and improving stability.&lt;/p&gt;

&lt;p&gt;Node.js handles this automatically when using &lt;code&gt;pipe()&lt;/code&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  💧 &lt;code&gt;highWaterMark&lt;/code&gt;
&lt;/h1&gt;

&lt;p&gt;&lt;code&gt;highWaterMark&lt;/code&gt; defines the &lt;strong&gt;buffer threshold&lt;/strong&gt; that helps determine when backpressure should start.&lt;/p&gt;

&lt;p&gt;Some useful defaults:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;fs.createReadStream()&lt;/code&gt; → &lt;strong&gt;64 KB&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Most Readable Streams → &lt;strong&gt;16 KB&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Most Writable Streams → &lt;strong&gt;16 KB&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It's important to remember that &lt;strong&gt;&lt;code&gt;highWaterMark&lt;/code&gt; is a threshold, not a hard memory limit&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  🔄 Flowing Mode vs Paused Mode
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Flowing Mode
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Uses the &lt;code&gt;data&lt;/code&gt; event&lt;/li&gt;
&lt;li&gt;Data flows automatically&lt;/li&gt;
&lt;li&gt;Most commonly used&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Paused Mode
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Uses the &lt;code&gt;readable&lt;/code&gt; event&lt;/li&gt;
&lt;li&gt;Data is read manually using &lt;code&gt;stream.read()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Gives more control over reading&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  📦 Buffer vs Stream
&lt;/h1&gt;

&lt;p&gt;One interview question that helped me understand the difference:&lt;/p&gt;

&lt;h3&gt;
  
  
  Buffer
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Temporary memory&lt;/li&gt;
&lt;li&gt;Stores binary data&lt;/li&gt;
&lt;li&gt;Holds one chunk at a time&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Stream
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Transfers data continuously&lt;/li&gt;
&lt;li&gt;Processes multiple chunks&lt;/li&gt;
&lt;li&gt;Better suited for large files&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A simple way to remember it:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Buffer stores data. Stream moves data.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  🚀 &lt;code&gt;pipeline()&lt;/code&gt; vs &lt;code&gt;pipe()&lt;/code&gt;
&lt;/h1&gt;

&lt;p&gt;Although &lt;code&gt;pipe()&lt;/code&gt; is simple and powerful, Node.js provides &lt;code&gt;pipeline()&lt;/code&gt; for production-ready applications.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why use &lt;code&gt;pipeline()&lt;/code&gt;?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Better error handling&lt;/li&gt;
&lt;li&gt;Automatically cleans up streams&lt;/li&gt;
&lt;li&gt;Prevents resource leaks&lt;/li&gt;
&lt;li&gt;Recommended for production code&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  🎯 Interview Takeaways
&lt;/h1&gt;

&lt;p&gt;Today's most important interview concepts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What is a Stream?&lt;/li&gt;
&lt;li&gt;Why Streams are better than &lt;code&gt;fs.readFile()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Readable vs Writable Stream&lt;/li&gt;
&lt;li&gt;Duplex vs Transform Stream&lt;/li&gt;
&lt;li&gt;&lt;code&gt;pipe()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Backpressure&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;drain&lt;/code&gt; event&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;pause()&lt;/code&gt; and &lt;code&gt;resume()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;&lt;code&gt;highWaterMark&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Flowing vs Paused Mode&lt;/li&gt;
&lt;li&gt;Buffer vs Stream&lt;/li&gt;
&lt;li&gt;&lt;code&gt;pipeline()&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  📚 Key Learning
&lt;/h1&gt;

&lt;p&gt;Streams are one of the core reasons why Node.js is highly efficient for backend development. They allow applications to process large files with minimal memory usage while maintaining excellent performance and scalability.&lt;/p&gt;

&lt;p&gt;Understanding &lt;strong&gt;Streams, Backpressure, and &lt;code&gt;pipe()&lt;/code&gt;&lt;/strong&gt; has given me a much deeper appreciation of how Node.js handles data under the hood.&lt;/p&gt;

&lt;p&gt;Looking forward to learning the &lt;strong&gt;HTTP module&lt;/strong&gt; next! 🚀&lt;/p&gt;




&lt;p&gt;If you have any interview tips or real-world use cases for Streams, I'd love to hear them in the comments.&lt;/p&gt;

&lt;h1&gt;
  
  
  NodeJS #JavaScript #Backend #WebDevelopment #Programming #Coding #100DaysOfCode #OpenSource #Developer #Streams #LearningInPublic
&lt;/h1&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>javascript</category>
      <category>node</category>
    </item>
    <item>
      <title>🚀 Day 11 of My Node.js Learning Journey: Understanding Events &amp; EventEmitter</title>
      <dc:creator>Krati Joshi</dc:creator>
      <pubDate>Sat, 01 Aug 2026 15:30:42 +0000</pubDate>
      <link>https://dev.to/joshikrati03/day-11-of-my-nodejs-learning-journey-understanding-events-eventemitter-4l97</link>
      <guid>https://dev.to/joshikrati03/day-11-of-my-nodejs-learning-journey-understanding-events-eventemitter-4l97</guid>
      <description>&lt;p&gt;One of the biggest reasons Node.js is so powerful is its &lt;strong&gt;event-driven architecture&lt;/strong&gt;. Today, I learned how Node.js uses &lt;strong&gt;events&lt;/strong&gt; to build scalable and loosely coupled applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  💡 What are Events?
&lt;/h2&gt;

&lt;p&gt;An &lt;strong&gt;event&lt;/strong&gt; is an action or occurrence that happens during the execution of a program. Instead of constantly checking whether something has happened, Node.js waits for an event and reacts when it occurs.&lt;/p&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;User logs in&lt;/li&gt;
&lt;li&gt;HTTP request reaches the server&lt;/li&gt;
&lt;li&gt;File reading completes&lt;/li&gt;
&lt;li&gt;Database connection is established&lt;/li&gt;
&lt;li&gt;Timer finishes&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  📌 EventEmitter
&lt;/h2&gt;

&lt;p&gt;Node.js provides the built-in &lt;strong&gt;EventEmitter&lt;/strong&gt; class from the &lt;code&gt;events&lt;/code&gt; module to create and handle custom events.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;EventEmitter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;events&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;emitter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;EventEmitter&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;emitter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;greet&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello, Developer!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;emitter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;greet&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&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, Developer!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🔥 Common EventEmitter Methods
&lt;/h2&gt;

&lt;h3&gt;
  
  
  ✅ &lt;code&gt;on()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Registers a listener for an event.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;emitter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;login&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;User Logged In&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  ✅ &lt;code&gt;emit()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Triggers an event and executes all registered listeners.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;emitter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;login&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  ✅ &lt;code&gt;once()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Runs the listener only the first time the event is emitted.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;emitter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;once&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;welcome&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Welcome!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  ✅ Passing Data with Events
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;emitter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;login&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Welcome &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;emitter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;login&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Krati&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  ✅ Multiple Listeners
&lt;/h3&gt;

&lt;p&gt;A single event can notify multiple independent listeners.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;emitter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;orderPlaced&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Inventory Updated&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="nx"&gt;emitter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;orderPlaced&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Email Sent&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="nx"&gt;emitter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;orderPlaced&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Invoice Generated&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="nx"&gt;emitter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;orderPlaced&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  ⚠️ The Special &lt;code&gt;error&lt;/code&gt; Event
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;"error"&lt;/code&gt; event is treated specially in Node.js. If it is emitted without an error listener, the application may terminate.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;emitter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;error&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;emitter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;error&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Something went wrong&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Always register an &lt;code&gt;"error"&lt;/code&gt; listener when emitting error events.&lt;/p&gt;




&lt;h2&gt;
  
  
  🌍 Real-World Example
&lt;/h2&gt;

&lt;p&gt;Imagine an e-commerce application:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Order Placed
      │
      ├── Update Inventory
      ├── Send Confirmation Email
      ├── Generate Invoice
      ├── Notify Admin
      └── Reward Loyalty Points
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of calling every function manually, the application emits a single &lt;code&gt;orderPlaced&lt;/code&gt; event, and different modules respond independently. This makes the system easier to maintain and extend.&lt;/p&gt;




&lt;h2&gt;
  
  
  🎯 Interview Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;EventEmitter is provided by the built-in &lt;code&gt;events&lt;/code&gt; module.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;on()&lt;/code&gt; registers a listener.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;emit()&lt;/code&gt; triggers an event.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;once()&lt;/code&gt; executes only once.&lt;/li&gt;
&lt;li&gt;One event can have multiple listeners.&lt;/li&gt;
&lt;li&gt;EventEmitter listeners execute &lt;strong&gt;synchronously&lt;/strong&gt; by default.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;"error"&lt;/code&gt; event should always have an error listener.&lt;/li&gt;
&lt;li&gt;EventEmitter is &lt;strong&gt;different from the Event Loop&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Streams, HTTP servers, and the &lt;code&gt;process&lt;/code&gt; object all use EventEmitter internally.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  📚 What I Learned Today
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;✔️ What events are in Node.js&lt;/li&gt;
&lt;li&gt;✔️ How EventEmitter works&lt;/li&gt;
&lt;li&gt;✔️ Difference between &lt;code&gt;on()&lt;/code&gt;, &lt;code&gt;emit()&lt;/code&gt;, and &lt;code&gt;once()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;✔️ Passing data through events&lt;/li&gt;
&lt;li&gt;✔️ Handling multiple listeners&lt;/li&gt;
&lt;li&gt;✔️ Why the &lt;code&gt;error&lt;/code&gt; event is important&lt;/li&gt;
&lt;li&gt;✔️ Real-world use cases of EventEmitter&lt;/li&gt;
&lt;li&gt;✔️ Why Node.js follows an event-driven architecture&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  💭 Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Events are one of the core concepts that make Node.js efficient and scalable. By using &lt;strong&gt;EventEmitter&lt;/strong&gt;, different parts of an application can communicate without being tightly connected, making code cleaner, more modular, and easier to maintain.&lt;/p&gt;

&lt;p&gt;I'm excited to continue exploring more Node.js concepts in the coming days! 🚀&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;#NodeJS #JavaScript #BackendDevelopment #WebDevelopment #100DaysOfCode #OpenSource #Coding #EventEmitter #LearningInPublic #DevCommunity&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>node</category>
      <category>javascriptlibraries</category>
      <category>programming</category>
    </item>
    <item>
      <title>🚀 Day 10 of My Node.js Learning Journey – Mastering the Path &amp; OS Modules</title>
      <dc:creator>Krati Joshi</dc:creator>
      <pubDate>Fri, 31 Jul 2026 14:42:50 +0000</pubDate>
      <link>https://dev.to/joshikrati03/day-10-of-my-nodejs-learning-journey-mastering-the-path-os-modules-49hg</link>
      <guid>https://dev.to/joshikrati03/day-10-of-my-nodejs-learning-journey-mastering-the-path-os-modules-49hg</guid>
      <description>&lt;p&gt;One thing I really like about Node.js is that it provides several built-in modules that solve common backend problems without requiring any external packages.&lt;/p&gt;

&lt;p&gt;Today I learned two of those modules: &lt;strong&gt;Path&lt;/strong&gt; and &lt;strong&gt;OS&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;At first, these modules might look “too basic” or even optional—but in reality, they are &lt;strong&gt;fundamental building blocks of backend development&lt;/strong&gt;. We don’t just learn them for interviews; we learn them because they solve real problems that every backend application eventually faces.&lt;/p&gt;




&lt;h1&gt;
  
  
  🤔 Why do we even need to learn Path &amp;amp; OS modules?
&lt;/h1&gt;

&lt;p&gt;Before jumping into methods, it’s important to understand &lt;em&gt;why these modules matter in the first place&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  ✅ 1. Because backend apps deal with files and servers constantly
&lt;/h3&gt;

&lt;p&gt;In real-world Node.js applications, we often:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Upload files (images, PDFs, videos)&lt;/li&gt;
&lt;li&gt;Store logs&lt;/li&gt;
&lt;li&gt;Read configuration files&lt;/li&gt;
&lt;li&gt;Serve static assets&lt;/li&gt;
&lt;li&gt;Deploy apps on different servers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All of these involve &lt;strong&gt;file paths and system-level information&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Without the &lt;code&gt;path&lt;/code&gt; module, we would be manually handling strings like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;uploads/images/profile.png
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This becomes risky because different operating systems handle paths differently.&lt;/p&gt;




&lt;h3&gt;
  
  
  ✅ 2. Because Node.js runs on different operating systems
&lt;/h3&gt;

&lt;p&gt;Your code might run on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Windows (development)&lt;/li&gt;
&lt;li&gt;Linux (production servers)&lt;/li&gt;
&lt;li&gt;macOS (team machines)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each OS behaves differently in terms of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;File paths&lt;/li&gt;
&lt;li&gt;CPU architecture&lt;/li&gt;
&lt;li&gt;Memory availability&lt;/li&gt;
&lt;li&gt;System uptime&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;os&lt;/code&gt; module helps us &lt;strong&gt;write code that adapts to the environment instead of breaking in production&lt;/strong&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  ✅ 3. Because production systems need reliability
&lt;/h3&gt;

&lt;p&gt;Small mistakes like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;wrong file path&lt;/li&gt;
&lt;li&gt;incorrect directory reference&lt;/li&gt;
&lt;li&gt;assuming server specs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;can cause real production bugs.&lt;/p&gt;

&lt;p&gt;These modules help prevent that.&lt;/p&gt;




&lt;h3&gt;
  
  
  ✅ 4. Because interviews expect this understanding
&lt;/h3&gt;

&lt;p&gt;Companies don’t just ask “what is path.join()?”&lt;/p&gt;

&lt;p&gt;They want to know:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Do you understand file system behavior?&lt;/li&gt;
&lt;li&gt;Do you know cross-platform issues?&lt;/li&gt;
&lt;li&gt;Can you write production-safe backend code?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So yes—this is also &lt;strong&gt;high-frequency interview knowledge&lt;/strong&gt;, but more importantly, it’s &lt;strong&gt;real engineering knowledge&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  📂 The Path Module
&lt;/h1&gt;

&lt;p&gt;The &lt;code&gt;path&lt;/code&gt; module helps us work with file and directory paths in a &lt;strong&gt;cross-platform way&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Why is this important?&lt;/p&gt;

&lt;p&gt;Windows and Linux use different path formats:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Windows&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;C:\Users\Krati\Documents\resume.pdf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Linux/macOS&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;/home/krati/Documents/resume.pdf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we manually concatenate strings, our application may break on different systems. The &lt;code&gt;path&lt;/code&gt; module solves this by generating correct paths automatically.&lt;/p&gt;




&lt;h3&gt;
  
  
  Key Methods I Learned
&lt;/h3&gt;

&lt;p&gt;✅ &lt;strong&gt;path.join()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Safely joins multiple path segments.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;uploads&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;images&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;profile.png&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Best for building folder structures dynamically.&lt;/p&gt;




&lt;p&gt;✅ &lt;strong&gt;path.resolve()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Converts a relative path into an absolute path.&lt;/p&gt;

&lt;p&gt;Useful when a library or API requires a full file location.&lt;/p&gt;




&lt;p&gt;✅ &lt;strong&gt;path.basename()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Extracts the filename from a full path.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/users/docs/resume.pdf → resume.pdf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;✅ &lt;strong&gt;path.dirname()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Returns the parent directory of a file.&lt;/p&gt;




&lt;p&gt;✅ &lt;strong&gt;path.extname()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Returns file extension like &lt;code&gt;.pdf&lt;/code&gt;, &lt;code&gt;.jpg&lt;/code&gt;, &lt;code&gt;.js&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Very useful for file validation during uploads.&lt;/p&gt;




&lt;h2&gt;
  
  
  ⭐ My Biggest Learning Today
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;path.join()&lt;/code&gt; vs &lt;code&gt;path.resolve()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Before today, I thought they were interchangeable.&lt;/p&gt;

&lt;p&gt;Now I understand:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;path.join()&lt;/code&gt;&lt;/strong&gt; → just combines path segments&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;path.resolve()&lt;/code&gt;&lt;/strong&gt; → builds an absolute path from the current working directory&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This difference matters a lot in real backend projects.&lt;/p&gt;




&lt;h1&gt;
  
  
  📁 Another Important Concept
&lt;/h1&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;__dirname&lt;/code&gt; vs &lt;code&gt;process.cwd()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;This is a classic Node.js interview question—but also a real-world debugging tool.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;__dirname&lt;/code&gt;&lt;/strong&gt; → directory of the current file&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;process.cwd()&lt;/code&gt;&lt;/strong&gt; → directory where the Node app was started&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This difference becomes important when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;loading config files&lt;/li&gt;
&lt;li&gt;serving static files&lt;/li&gt;
&lt;li&gt;handling uploads&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  💻 The OS Module
&lt;/h1&gt;

&lt;p&gt;The &lt;code&gt;os&lt;/code&gt; module gives us information about the operating system.&lt;/p&gt;

&lt;p&gt;Instead of guessing server details, we can directly access them using Node.js.&lt;/p&gt;




&lt;h3&gt;
  
  
  Useful methods I explored:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;os.platform()&lt;/code&gt; → OS type (win32, linux, etc.)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;os.arch()&lt;/code&gt; → CPU architecture&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;os.hostname()&lt;/code&gt; → machine name&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;os.cpus()&lt;/code&gt; → CPU core details&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;os.totalmem()&lt;/code&gt; → total RAM&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;os.freemem()&lt;/code&gt; → available RAM&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;os.uptime()&lt;/code&gt; → system running time&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  🌍 Real-World Backend Usage
&lt;/h1&gt;

&lt;p&gt;These modules are not theoretical—they are used in production systems daily.&lt;/p&gt;

&lt;h3&gt;
  
  
  Path module is used for:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;File uploads&lt;/li&gt;
&lt;li&gt;Static file serving&lt;/li&gt;
&lt;li&gt;Configuration management&lt;/li&gt;
&lt;li&gt;Cross-platform file handling&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  OS module is used for:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Health check APIs&lt;/li&gt;
&lt;li&gt;Monitoring dashboards&lt;/li&gt;
&lt;li&gt;Performance tracking&lt;/li&gt;
&lt;li&gt;Scaling decisions (based on CPU cores)&lt;/li&gt;
&lt;li&gt;Logging system environment details&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  🎯 Interview Takeaways
&lt;/h1&gt;

&lt;p&gt;Today’s key concepts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Why we should never manually build file paths&lt;/li&gt;
&lt;li&gt;Difference between &lt;code&gt;path.join()&lt;/code&gt; and &lt;code&gt;path.resolve()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Difference between &lt;code&gt;__dirname&lt;/code&gt; and &lt;code&gt;process.cwd()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Why OS-level information matters in backend systems&lt;/li&gt;
&lt;li&gt;Real-world use cases of Path and OS modules&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  💡 What I Learned Today
&lt;/h1&gt;

&lt;p&gt;The biggest realization today is this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Backend development is not just about writing APIs—it’s about understanding the environment your code runs in.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The &lt;code&gt;path&lt;/code&gt; module ensures our file handling is safe and cross-platform, while the &lt;code&gt;os&lt;/code&gt; module helps our application understand the system it is running on.&lt;/p&gt;

&lt;p&gt;These modules may look small, but they are &lt;strong&gt;core to building reliable, production-ready backend systems&lt;/strong&gt;.&lt;/p&gt;




&lt;p&gt;Looking forward to continuing my Node.js journey and exploring more modules that power real-world applications! 🚀&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;#NodeJS #JavaScript #BackendDevelopment #WebDevelopment #100DaysOfCode #LearningInPublic #ExpressJS #SoftwareEngineering #Coding #SystemDesign&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>node</category>
      <category>programming</category>
      <category>javascript</category>
    </item>
    <item>
      <title>🚀 fs Module=&gt; Loaded</title>
      <dc:creator>Krati Joshi</dc:creator>
      <pubDate>Thu, 30 Jul 2026 17:16:14 +0000</pubDate>
      <link>https://dev.to/joshikrati03/fs-module-loaded-14od</link>
      <guid>https://dev.to/joshikrati03/fs-module-loaded-14od</guid>
      <description>&lt;p&gt;Today I explored one of the most fundamental Node.js modules—&lt;strong&gt;the File System (&lt;code&gt;fs&lt;/code&gt;) module&lt;/strong&gt;. It's the module that enables Node.js applications to interact with files and directories, making it essential for backend development.&lt;/p&gt;

&lt;h2&gt;
  
  
  💡 Key Takeaways
&lt;/h2&gt;

&lt;p&gt;✅ &lt;strong&gt;&lt;code&gt;fs&lt;/code&gt; is a built-in module&lt;/strong&gt; – No installation required.&lt;/p&gt;

&lt;p&gt;✅ &lt;strong&gt;Three ways to work with files&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Callback API&lt;/li&gt;
&lt;li&gt;Promise API (&lt;code&gt;fs/promises&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Synchronous API (&lt;code&gt;readFileSync&lt;/code&gt;, &lt;code&gt;writeFileSync&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;✅ &lt;strong&gt;Asynchronous operations are preferred&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Don't block the Event Loop.&lt;/li&gt;
&lt;li&gt;File operations are handled by &lt;strong&gt;libuv's thread pool&lt;/strong&gt;, allowing the server to continue processing other requests.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;✅ &lt;strong&gt;Common File Operations&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;fs.readFile()&lt;/code&gt; → Read a file&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;fs.writeFile()&lt;/code&gt; → Create or overwrite a file&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;fs.appendFile()&lt;/code&gt; → Add content to an existing file&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;fs.rename()&lt;/code&gt; → Rename a file&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;fs.unlink()&lt;/code&gt; → Delete a file&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;fs.mkdir()&lt;/code&gt; → Create a directory&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;fs.readdir()&lt;/code&gt; → Read directory contents&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;fs.stat()&lt;/code&gt; → Get file metadata&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;✅ &lt;strong&gt;Always handle errors&lt;/strong&gt;&lt;br&gt;
Every file operation can fail (missing file, permissions, etc.), so proper error handling is essential.&lt;/p&gt;

&lt;p&gt;✅ &lt;strong&gt;Use Streams for large files&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;fs.readFile()&lt;/code&gt; loads the entire file into memory, whereas streams process data in chunks, making them much more memory-efficient.&lt;/p&gt;

&lt;h2&gt;
  
  
  🎯 Interview Nuggets
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;There is &lt;strong&gt;no &lt;code&gt;fs.createFile()&lt;/code&gt;&lt;/strong&gt; method. Use &lt;code&gt;fs.writeFile()&lt;/code&gt; to create a new file.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;fs.writeFile()&lt;/code&gt; overwrites existing content, while &lt;code&gt;fs.appendFile()&lt;/code&gt; preserves it and adds new data.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;fs.readFile()&lt;/code&gt; is asynchronous and doesn't block the Event Loop.&lt;/li&gt;
&lt;li&gt;Avoid synchronous (&lt;code&gt;Sync&lt;/code&gt;) methods in production request handlers because they block the Event Loop.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  📚 Example
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;fs&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;users.txt&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;utf8&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every backend developer works with files at some point—whether it's reading configuration files, generating reports, processing uploads, or maintaining logs. Understanding the &lt;code&gt;fs&lt;/code&gt; module is a foundational step toward building robust Node.js applications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;✅ Complete. On to Path &amp;amp; OS Module next! 🚀&lt;/strong&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  NodeJS #JavaScript #Backend #WebDevelopment #100DaysOfCode #OpenToWork #LearningInPublic #FullStack #SoftwareEngineer #Developers
&lt;/h1&gt;

</description>
      <category>webdev</category>
      <category>node</category>
      <category>programming</category>
      <category>javascript</category>
    </item>
    <item>
      <title>🚀 Node.js Streams &amp; Buffers Explained: Handle Large Files Without Crashing Your Server</title>
      <dc:creator>Krati Joshi</dc:creator>
      <pubDate>Wed, 29 Jul 2026 17:39:42 +0000</pubDate>
      <link>https://dev.to/joshikrati03/nodejs-streams-buffers-explained-handle-large-files-without-crashing-your-server-3lb8</link>
      <guid>https://dev.to/joshikrati03/nodejs-streams-buffers-explained-handle-large-files-without-crashing-your-server-3lb8</guid>
      <description>&lt;p&gt;If you've ever used &lt;code&gt;fs.readFile()&lt;/code&gt; to load a large file, you might have noticed high memory usage or even application crashes.&lt;/p&gt;

&lt;p&gt;The reason? &lt;code&gt;fs.readFile()&lt;/code&gt; loads the &lt;strong&gt;entire file into memory&lt;/strong&gt; before your application can process it.&lt;/p&gt;

&lt;p&gt;Node.js solves this problem with &lt;strong&gt;Streams&lt;/strong&gt; and &lt;strong&gt;Buffers&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let's understand why they are one of the most important concepts for every backend developer.&lt;/p&gt;




&lt;h2&gt;
  
  
  📌 What is a Stream?
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;Stream&lt;/strong&gt; is a way to process data &lt;strong&gt;chunk by chunk&lt;/strong&gt; instead of loading everything into memory at once.&lt;/p&gt;

&lt;p&gt;Think of watching a movie on Netflix. The entire movie isn't downloaded before playback starts—you receive small chunks continuously.&lt;/p&gt;

&lt;p&gt;That's exactly how Streams work.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why use Streams?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;✅ Memory Efficient&lt;/li&gt;
&lt;li&gt;✅ Better Performance&lt;/li&gt;
&lt;li&gt;✅ Handles Large Files&lt;/li&gt;
&lt;li&gt;✅ Ideal for Network Communication&lt;/li&gt;
&lt;li&gt;✅ Used in File Uploads &amp;amp; Downloads&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  📦 What is a Buffer?
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;Buffer&lt;/strong&gt; is a temporary memory area that stores &lt;strong&gt;raw binary data&lt;/strong&gt; before it's processed or transmitted.&lt;/p&gt;

&lt;p&gt;Whenever Node.js reads data from a file or receives data from a network, it first stores it in a Buffer.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Buffer = Temporary Binary Storage&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  🌊 Types of Streams
&lt;/h2&gt;

&lt;p&gt;Node.js provides four types of Streams:&lt;/p&gt;

&lt;h3&gt;
  
  
  1️⃣ Readable Stream
&lt;/h3&gt;

&lt;p&gt;Reads data from a source.&lt;/p&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;fs.createReadStream()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;HTTP Request&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  2️⃣ Writable Stream
&lt;/h3&gt;

&lt;p&gt;Writes data to a destination.&lt;/p&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;fs.createWriteStream()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;HTTP Response&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  3️⃣ Duplex Stream
&lt;/h3&gt;

&lt;p&gt;Supports both reading and writing.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;TCP Socket&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  4️⃣ Transform Stream
&lt;/h3&gt;

&lt;p&gt;Reads, modifies, and writes data.&lt;/p&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Compression&lt;/li&gt;
&lt;li&gt;Encryption&lt;/li&gt;
&lt;li&gt;Decompression&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔄 &lt;code&gt;pipe()&lt;/code&gt; — Connecting Streams
&lt;/h2&gt;

&lt;p&gt;One of the most useful Stream methods is &lt;code&gt;pipe()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Instead of manually reading chunks and writing them yourself, you can directly connect a Readable Stream to a Writable Stream.&lt;/p&gt;

&lt;p&gt;This keeps the code simple and automatically handles data flow efficiently.&lt;/p&gt;




&lt;h2&gt;
  
  
  🚦 Backpressure
&lt;/h2&gt;

&lt;p&gt;Imagine a fast producer sending data to a slow consumer.&lt;/p&gt;

&lt;p&gt;Without flow control, memory usage keeps increasing.&lt;/p&gt;

&lt;p&gt;Node.js solves this using &lt;strong&gt;Backpressure&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When the Writable Stream becomes slower, Node pauses the Readable Stream until the destination catches up.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Backpressure = Flow Control&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  💧 What is &lt;code&gt;highWaterMark&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;highWaterMark&lt;/code&gt; defines the maximum amount of data that can be buffered before backpressure is applied.&lt;/p&gt;

&lt;p&gt;Think of it as a &lt;strong&gt;water level limit&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Once the internal buffer reaches this limit:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reading pauses&lt;/li&gt;
&lt;li&gt;Writable Stream processes buffered data&lt;/li&gt;
&lt;li&gt;Reading resumes automatically&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  📊 Stream Flow
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Large File
     │
Readable Stream
     │
   Chunks
     │
Internal Buffer
     │
HighWaterMark Reached
     │
Backpressure
     │
Writable Stream
     │
Output
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  💡 Key Interview Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Streams process data &lt;strong&gt;chunk by chunk&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Buffers temporarily store &lt;strong&gt;binary data&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;pipe()&lt;/code&gt; connects Readable and Writable Streams.&lt;/li&gt;
&lt;li&gt;Backpressure prevents memory overflow.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;highWaterMark&lt;/code&gt; controls when backpressure starts.&lt;/li&gt;
&lt;li&gt;Streams are ideal for large files, uploads, downloads, and video streaming.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🎯 Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Streams and Buffers are at the heart of Node.js performance.&lt;/p&gt;

&lt;p&gt;If you're building APIs that handle file uploads, large downloads, logs, media, or network communication, understanding these concepts will help you write scalable and memory-efficient applications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What real-world use case have you used Streams for? Share it in the comments! 👇&lt;/strong&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  #NodeJS #JavaScript #Backend #WebDevelopment #Programming #Streams #Buffer #Performance #100DaysOfCode
&lt;/h3&gt;

</description>
      <category>node</category>
      <category>webdev</category>
      <category>programming</category>
      <category>productivity</category>
    </item>
    <item>
      <title>🚀 Understanding Async Patterns in Node.js: From Callbacks to Async/Await</title>
      <dc:creator>Krati Joshi</dc:creator>
      <pubDate>Tue, 28 Jul 2026 14:13:02 +0000</pubDate>
      <link>https://dev.to/joshikrati03/understanding-async-patterns-in-nodejs-from-callbacks-to-asyncawait-2gih</link>
      <guid>https://dev.to/joshikrati03/understanding-async-patterns-in-nodejs-from-callbacks-to-asyncawait-2gih</guid>
      <description>&lt;p&gt;JavaScript is &lt;strong&gt;single-threaded&lt;/strong&gt;, but Node.js can efficiently handle thousands of concurrent operations. The secret lies in its &lt;strong&gt;asynchronous programming model&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Whether you're reading files, querying databases, calling APIs, or communicating with Redis or Kafka, understanding async patterns is essential for writing scalable Node.js applications.&lt;/p&gt;

&lt;p&gt;Let's explore how asynchronous programming in Node.js has evolved over time.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Asynchronous Programming?
&lt;/h2&gt;

&lt;p&gt;Imagine your application needs to read a large file.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;synchronous&lt;/strong&gt; approach blocks the entire program until the file is read.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFileSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;data.txt&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;utf8&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Finished&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nothing else executes until the file operation completes.&lt;/p&gt;

&lt;p&gt;Now compare that with an asynchronous approach:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;data.txt&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;utf8&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Finished&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Finished
&amp;lt;File Content&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of waiting, Node.js continues executing other code while the file is being read in the background.&lt;/p&gt;

&lt;p&gt;That's the power of asynchronous programming.&lt;/p&gt;




&lt;h1&gt;
  
  
  The Evolution of Async Patterns
&lt;/h1&gt;

&lt;p&gt;Over time, Node.js introduced better ways to manage asynchronous code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Callbacks
      ↓
Callback Hell
      ↓
Promises
      ↓
Async/Await
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each step improved readability, maintainability, and error handling.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. Callbacks
&lt;/h2&gt;

&lt;p&gt;A callback is simply a function passed to another function that executes after an asynchronous task completes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Node.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt;
&lt;span class="nx"&gt;Node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;js&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Callbacks were the foundation of asynchronous programming in Node.js.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Problem: Callback Hell
&lt;/h2&gt;

&lt;p&gt;As applications grow, callbacks become deeply nested.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;login&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;fetchProfile&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;fetchOrders&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;sendEmail&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nf"&gt;logout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

                &lt;span class="p"&gt;});&lt;/span&gt;
            &lt;span class="p"&gt;});&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates the infamous &lt;strong&gt;"Pyramid of Doom."&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Problems include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;❌ Hard to read&lt;/li&gt;
&lt;li&gt;❌ Difficult to debug&lt;/li&gt;
&lt;li&gt;❌ Complex error handling&lt;/li&gt;
&lt;li&gt;❌ Poor maintainability&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  2. Promises
&lt;/h2&gt;

&lt;p&gt;Promises solve callback hell by representing the eventual result of an asynchronous operation.&lt;/p&gt;

&lt;p&gt;A Promise has three states:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Pending&lt;/li&gt;
&lt;li&gt;Fulfilled&lt;/li&gt;
&lt;li&gt;Rejected&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Creating a Promise:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;promise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;success&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;success&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Success&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Failed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using a Promise:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;promise&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Promises make asynchronous code much easier to understand.&lt;/p&gt;




&lt;h2&gt;
  
  
  Promise Chaining
&lt;/h2&gt;

&lt;p&gt;Instead of nesting callbacks, Promises allow chaining.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;login&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fetchProfile&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fetchOrders&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sendEmail&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;logout&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The execution flow becomes much cleaner.&lt;/p&gt;




&lt;h2&gt;
  
  
  Useful Promise Methods
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;Promise.all()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Runs multiple asynchronous operations in parallel.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="nf"&gt;getOrders&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="nf"&gt;getNotifications&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If &lt;strong&gt;one promise rejects&lt;/strong&gt;, the entire operation fails.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;code&gt;Promise.allSettled()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Waits for every promise to finish, regardless of success or failure.&lt;/p&gt;

&lt;p&gt;Useful when every result matters.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;code&gt;Promise.race()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Returns whichever promise settles first.&lt;/p&gt;

&lt;p&gt;Perfect for implementing request timeouts.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;code&gt;Promise.any()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Returns the first successfully resolved promise while ignoring rejected ones.&lt;/p&gt;

&lt;p&gt;Useful when multiple sources can provide the same data.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Async/Await
&lt;/h2&gt;

&lt;p&gt;Async/Await is built on top of Promises and makes asynchronous code look almost synchronous.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetchUser&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Much cleaner than multiple &lt;code&gt;.then()&lt;/code&gt; calls.&lt;/p&gt;




&lt;h2&gt;
  
  
  Error Handling
&lt;/h2&gt;

&lt;p&gt;One of the biggest advantages of Async/Await is cleaner error handling.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;registerUser&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;createUser&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

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

&lt;/div&gt;



&lt;p&gt;Instead of attaching &lt;code&gt;.catch()&lt;/code&gt; everywhere, a single &lt;code&gt;try...catch&lt;/code&gt; block handles failures gracefully.&lt;/p&gt;




&lt;h2&gt;
  
  
  Sequential vs Parallel Execution
&lt;/h2&gt;

&lt;p&gt;Sequential execution:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetchUser&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetchOrders&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetchProducts&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each operation waits for the previous one.&lt;/p&gt;

&lt;p&gt;Parallel execution:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="nf"&gt;fetchUser&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="nf"&gt;fetchOrders&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="nf"&gt;fetchProducts&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Independent tasks execute simultaneously, significantly reducing response time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Rule of thumb:&lt;/strong&gt; Use &lt;code&gt;Promise.all()&lt;/code&gt; whenever tasks don't depend on each other.&lt;/p&gt;




&lt;h2&gt;
  
  
  Common Mistakes
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Forgetting &lt;code&gt;await&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;fetchUser&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;Promise&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;pending&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Correct:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetchUser&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Using &lt;code&gt;await&lt;/code&gt; Outside an Async Function
&lt;/h3&gt;

&lt;p&gt;Incorrect:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetchUser&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Correct:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetchUser&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Ignoring Errors
&lt;/h3&gt;

&lt;p&gt;Always handle rejected promises.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetchUser&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Real-World Example
&lt;/h2&gt;

&lt;p&gt;A user registration API typically performs multiple asynchronous operations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;registerUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;hashedPassword&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;hashPassword&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;password&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;saveUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;hashedPassword&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;sendWelcomeEmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;201&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

        &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

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

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

&lt;/div&gt;



&lt;p&gt;This pattern is common in modern Express.js applications.&lt;/p&gt;




&lt;h1&gt;
  
  
  Key Takeaways
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Node.js uses asynchronous programming to avoid blocking the Event Loop.&lt;/li&gt;
&lt;li&gt;Callbacks introduced async programming but often led to callback hell.&lt;/li&gt;
&lt;li&gt;Promises improved readability and simplified error handling.&lt;/li&gt;
&lt;li&gt;Async/Await provides the cleanest syntax for asynchronous code.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;Promise.all()&lt;/code&gt; for independent operations to improve performance.&lt;/li&gt;
&lt;li&gt;Always wrap asynchronous code in &lt;code&gt;try...catch&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Choose Async/Await for modern Node.js applications whenever possible.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Asynchronous programming is one of the biggest reasons Node.js excels at building scalable backend services.&lt;/p&gt;

&lt;p&gt;Mastering callbacks, Promises, and Async/Await isn't just about passing interviews—it's about writing cleaner, faster, and more maintainable production code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What async pattern do you use most in your projects—Callbacks, Promises, or Async/Await? Let me know in the comments! 🚀&lt;/strong&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  NodeJS #JavaScript #Backend #AsyncProgramming #Promises #AsyncAwait #WebDevelopment #100DaysOfCode #DevCommunity
&lt;/h1&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>node</category>
      <category>javascript</category>
    </item>
    <item>
      <title>🚀 Backend Internals #7: libuv — The Real Reason Node.js Is Asynchronous</title>
      <dc:creator>Krati Joshi</dc:creator>
      <pubDate>Mon, 27 Jul 2026 18:15:04 +0000</pubDate>
      <link>https://dev.to/joshikrati03/backend-internals-7-libuv-the-real-reason-nodejs-is-asynchronous-16bj</link>
      <guid>https://dev.to/joshikrati03/backend-internals-7-libuv-the-real-reason-nodejs-is-asynchronous-16bj</guid>
      <description>&lt;p&gt;&lt;em&gt;By Krati Joshi&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Most developers say:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Node.js is single-threaded."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's true—but it's also incomplete.&lt;/p&gt;

&lt;p&gt;If JavaScript runs on a single thread, then who reads files?&lt;br&gt;
Who handles thousands of HTTP connections?&lt;br&gt;
Who performs DNS lookups and cryptographic operations without freezing your application?&lt;/p&gt;

&lt;p&gt;The answer is &lt;strong&gt;libuv&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Understanding libuv is the moment Node.js starts making sense.&lt;/p&gt;

&lt;p&gt;Let's dive in.&lt;/p&gt;


&lt;h1&gt;
  
  
  The Problem
&lt;/h1&gt;

&lt;p&gt;Imagine this code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;fs&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFileSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;movie.mp4&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Done&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If &lt;code&gt;movie.mp4&lt;/code&gt; is 5 GB, your application waits until the file is completely read.&lt;/p&gt;

&lt;p&gt;Nothing else executes.&lt;/p&gt;

&lt;p&gt;This is called &lt;strong&gt;blocking I/O&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Now imagine an Express server handling 500 users.&lt;/p&gt;

&lt;p&gt;If every request performs blocking file operations, your server quickly becomes unresponsive.&lt;/p&gt;

&lt;p&gt;Clearly, Node.js needed a better approach.&lt;/p&gt;




&lt;h1&gt;
  
  
  Enter libuv
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;libuv&lt;/strong&gt; is a &lt;strong&gt;cross-platform C library&lt;/strong&gt; used internally by Node.js.&lt;/p&gt;

&lt;p&gt;Its job is to provide:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Asynchronous I/O&lt;/li&gt;
&lt;li&gt;Event Loop implementation&lt;/li&gt;
&lt;li&gt;Worker Thread Pool&lt;/li&gt;
&lt;li&gt;Timers&lt;/li&gt;
&lt;li&gt;File System APIs&lt;/li&gt;
&lt;li&gt;Networking abstraction&lt;/li&gt;
&lt;li&gt;DNS operations&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Without libuv, Node.js would not be able to perform asynchronous operations efficiently.&lt;/p&gt;




&lt;h1&gt;
  
  
  Where Does libuv Fit?
&lt;/h1&gt;

&lt;p&gt;Every Node.js program roughly follows this architecture:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;JavaScript Code
        │
        ▼
     V8 Engine
        │
        ▼
Node.js Native C++ Bindings
        │
        ▼
      libuv
   ┌────┴────┐
   │         │
   ▼         ▼
Thread Pool  Operating System
             Async I/O APIs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each component has a different responsibility.&lt;/p&gt;

&lt;h3&gt;
  
  
  V8
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Executes JavaScript&lt;/li&gt;
&lt;li&gt;Performs JIT compilation&lt;/li&gt;
&lt;li&gt;Manages memory and garbage collection&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Node.js Native Bindings
&lt;/h3&gt;

&lt;p&gt;Expose APIs like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;fs&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;http&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;crypto&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;dns&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;to JavaScript.&lt;/p&gt;

&lt;h3&gt;
  
  
  libuv
&lt;/h3&gt;

&lt;p&gt;Coordinates asynchronous work with the operating system.&lt;/p&gt;




&lt;h1&gt;
  
  
  Why JavaScript Can't Read Files
&lt;/h1&gt;

&lt;p&gt;JavaScript was originally designed for browsers.&lt;/p&gt;

&lt;p&gt;It has no built-in ability to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Read files&lt;/li&gt;
&lt;li&gt;Create sockets&lt;/li&gt;
&lt;li&gt;Open network connections&lt;/li&gt;
&lt;li&gt;Access operating system APIs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When you write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;notes.txt&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;JavaScript doesn't read the file itself.&lt;/p&gt;

&lt;p&gt;Instead, the request travels through Node.js into libuv, which performs the work using the operating system or its worker thread pool.&lt;/p&gt;




&lt;h1&gt;
  
  
  The Flow of &lt;code&gt;fs.readFile()&lt;/code&gt;
&lt;/h1&gt;

&lt;p&gt;Internally, the request looks like this:&lt;br&gt;
&lt;/p&gt;

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

↓

V8 Engine

↓

Node.js Native Bindings

↓

libuv

↓

Worker Thread

↓

Operating System

↓

File Read Complete

↓

Completion Queue

↓

Event Loop

↓

JavaScript Callback
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice something important:&lt;/p&gt;

&lt;p&gt;The JavaScript thread never waits.&lt;/p&gt;

&lt;p&gt;It simply continues executing other work until the callback is ready.&lt;/p&gt;

&lt;p&gt;That's the foundation of Node.js's non-blocking model.&lt;/p&gt;




&lt;h1&gt;
  
  
  The Thread Pool
&lt;/h1&gt;

&lt;p&gt;Some operations cannot rely entirely on the operating system's asynchronous APIs.&lt;/p&gt;

&lt;p&gt;For these, libuv maintains a &lt;strong&gt;Worker Thread Pool&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;By default:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;4 Worker Threads
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Typical operations that use the thread pool include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;File System (&lt;code&gt;fs&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Cryptography (&lt;code&gt;crypto&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Compression (&lt;code&gt;zlib&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Some DNS lookups (&lt;code&gt;dns.lookup()&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The pool size can be increased when appropriate:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;UV_THREADPOOL_SIZE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;8 node app.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can improve throughput for workloads dominated by thread-pool operations like hashing or compression.&lt;/p&gt;




&lt;h1&gt;
  
  
  Not Every Async Operation Uses the Thread Pool
&lt;/h1&gt;

&lt;p&gt;One of the biggest interview misconceptions is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"All asynchronous work uses the thread pool."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It doesn't.&lt;/p&gt;

&lt;h3&gt;
  
  
  Uses Thread Pool
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;fs.readFile()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;crypto.pbkdf2()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;crypto.scrypt()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;zlib&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;dns.lookup()&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Doesn't Use Thread Pool
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;HTTP requests&lt;/li&gt;
&lt;li&gt;TCP sockets&lt;/li&gt;
&lt;li&gt;Timers&lt;/li&gt;
&lt;li&gt;Most network I/O&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Networking is usually handled using efficient operating-system mechanisms, while libuv waits for readiness notifications and schedules callbacks.&lt;/p&gt;




&lt;h1&gt;
  
  
  Why Can Node.js Handle Thousands of Connections?
&lt;/h1&gt;

&lt;p&gt;Unlike many traditional server models, Node.js does &lt;strong&gt;not&lt;/strong&gt; create one thread per connection.&lt;/p&gt;

&lt;p&gt;Instead:&lt;br&gt;
&lt;/p&gt;

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

↓

Operating System

↓

libuv Event Loop

↓

JavaScript Callback
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Most connections spend their time waiting for network events.&lt;/p&gt;

&lt;p&gt;Because JavaScript isn't blocked waiting for I/O, one thread can coordinate a huge number of concurrent connections.&lt;/p&gt;




&lt;h1&gt;
  
  
  Event Loop vs Thread Pool
&lt;/h1&gt;

&lt;p&gt;These two concepts are often confused.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Event Loop&lt;/th&gt;
&lt;th&gt;Thread Pool&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Executes callbacks&lt;/td&gt;
&lt;td&gt;Performs supported blocking work&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Runs on the JavaScript thread&lt;/td&gt;
&lt;td&gt;Runs on worker threads&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Single-threaded&lt;/td&gt;
&lt;td&gt;Multi-threaded&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Schedules completed tasks&lt;/td&gt;
&lt;td&gt;Executes background operations&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The Event Loop doesn't read files.&lt;/p&gt;

&lt;p&gt;The Thread Pool doesn't execute JavaScript.&lt;/p&gt;

&lt;p&gt;They work together.&lt;/p&gt;




&lt;h1&gt;
  
  
  Common Interview Questions
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Why is Node.js called single-threaded?
&lt;/h3&gt;

&lt;p&gt;Because JavaScript execution happens on a single main thread.&lt;/p&gt;

&lt;p&gt;Node.js itself also uses libuv, worker threads, and operating-system asynchronous I/O.&lt;/p&gt;




&lt;h3&gt;
  
  
  Does every async API use the thread pool?
&lt;/h3&gt;

&lt;p&gt;No.&lt;/p&gt;

&lt;p&gt;Only certain APIs such as &lt;code&gt;fs&lt;/code&gt;, &lt;code&gt;crypto&lt;/code&gt;, &lt;code&gt;zlib&lt;/code&gt;, and some DNS operations use it.&lt;/p&gt;

&lt;p&gt;Most networking operations rely on the operating system's asynchronous I/O facilities.&lt;/p&gt;




&lt;h3&gt;
  
  
  Why is libuv written in C?
&lt;/h3&gt;

&lt;p&gt;Because it needs direct access to low-level operating system APIs while providing a consistent interface across Linux, Windows, and macOS.&lt;/p&gt;




&lt;h1&gt;
  
  
  Key Takeaways
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;JavaScript execution is single-threaded.&lt;/li&gt;
&lt;li&gt;libuv powers Node.js asynchronous behavior.&lt;/li&gt;
&lt;li&gt;libuv implements the Event Loop and manages the worker thread pool.&lt;/li&gt;
&lt;li&gt;File system, crypto, compression, and some DNS operations use the thread pool.&lt;/li&gt;
&lt;li&gt;HTTP requests and most networking do not use the thread pool.&lt;/li&gt;
&lt;li&gt;Understanding libuv helps explain why Node.js scales well for I/O-heavy applications.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;If you've ever wondered &lt;strong&gt;why Node.js can serve thousands of concurrent requests without creating thousands of threads&lt;/strong&gt;, the answer isn't magic—it's the combination of &lt;strong&gt;libuv, the operating system, and the Event Loop&lt;/strong&gt; working together.&lt;/p&gt;

&lt;p&gt;In the next article, we'll explore &lt;strong&gt;how the Node.js HTTP module works internally&lt;/strong&gt; and see what actually happens when a browser sends a request to your server.&lt;/p&gt;

</description>
      <category>node</category>
      <category>backenddevelopment</category>
      <category>saas</category>
      <category>opensource</category>
    </item>
    <item>
      <title>🚀 Backend Internals #6: The Node.js Event Loop Explained — Why Async Code Doesn't Block</title>
      <dc:creator>Krati Joshi</dc:creator>
      <pubDate>Sun, 26 Jul 2026 08:58:43 +0000</pubDate>
      <link>https://dev.to/joshikrati03/-backend-internals-6-the-nodejs-event-loop-explained-why-async-code-doesnt-block-1552</link>
      <guid>https://dev.to/joshikrati03/-backend-internals-6-the-nodejs-event-loop-explained-why-async-code-doesnt-block-1552</guid>
      <description>&lt;p&gt;&lt;em&gt;By Krati Joshi&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you've ever wondered how Node.js can handle thousands of requests while running on a &lt;strong&gt;single JavaScript thread&lt;/strong&gt;, the answer is the &lt;strong&gt;Event Loop&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Many developers know &lt;em&gt;what&lt;/em&gt; the Event Loop is, but understanding &lt;em&gt;how&lt;/em&gt; it works can completely change the way you write and debug asynchronous code.&lt;/p&gt;

&lt;p&gt;Let's break it down.&lt;/p&gt;




&lt;h2&gt;
  
  
  🤔 The Problem
&lt;/h2&gt;

&lt;p&gt;JavaScript is &lt;strong&gt;single-threaded&lt;/strong&gt;, which means it can execute only &lt;strong&gt;one task at a time&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Imagine reading a large file or waiting for a database query. If JavaScript had to wait for these operations to finish, the entire application would freeze.&lt;/p&gt;

&lt;p&gt;That's where Node.js shines.&lt;/p&gt;




&lt;h2&gt;
  
  
  ⚡ How Node.js Solves This
&lt;/h2&gt;

&lt;p&gt;Node.js delegates long-running operations to &lt;strong&gt;libuv&lt;/strong&gt;, which works with the operating system to handle asynchronous tasks.&lt;/p&gt;

&lt;p&gt;While those tasks are running, JavaScript continues executing other code.&lt;/p&gt;

&lt;p&gt;When an operation completes, its callback is placed in a queue, waiting for the Event Loop to execute it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;JavaScript Code
      │
      ▼
  Call Stack
      │
      ▼
Async Operation (libuv)
      │
      ▼
Callback Queue
      │
      ▼
  Event Loop
      │
      ▼
  Call Stack
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Event Loop constantly checks:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"Is the Call Stack empty?"&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If the answer is &lt;strong&gt;Yes&lt;/strong&gt;, it moves the next callback to the Call Stack.&lt;/p&gt;




&lt;h2&gt;
  
  
  🧠 A Simple Example
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Start&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Timeout&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;End&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Start
End
Timeout
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even with a delay of &lt;code&gt;0&lt;/code&gt;, the callback cannot execute until the current synchronous code finishes and the Call Stack becomes empty.&lt;/p&gt;




&lt;h2&gt;
  
  
  🔄 The Event Loop Phases
&lt;/h2&gt;

&lt;p&gt;The Node.js Event Loop consists of several phases:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Timers&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Executes &lt;code&gt;setTimeout()&lt;/code&gt; and &lt;code&gt;setInterval()&lt;/code&gt; callbacks.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Pending Callbacks&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Executes certain deferred system callbacks.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Poll&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Processes completed I/O operations such as file reads, network requests, and database responses.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Check&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Executes callbacks scheduled with &lt;code&gt;setImmediate()&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Close Callbacks&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Handles cleanup events like socket close callbacks.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;strong&gt;Poll phase&lt;/strong&gt; is where most asynchronous I/O callbacks are processed, making it one of the most important phases to understand.&lt;/p&gt;




&lt;h2&gt;
  
  
  ⚔️ &lt;code&gt;process.nextTick()&lt;/code&gt; vs Promise vs Timers
&lt;/h2&gt;

&lt;p&gt;Node.js executes asynchronous callbacks in a specific order.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Start&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;nextTick&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;nextTick&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Promise&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Timeout&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;setImmediate&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Immediate&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;End&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Typical output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Start
End
nextTick
Promise
Timeout
Immediate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Priority Order
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Current Synchronous Code
        ↓
process.nextTick()
        ↓
Promise Microtasks
        ↓
Timers
        ↓
Poll
        ↓
Check (setImmediate)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Understanding this execution order helps explain many "unexpected" behaviors in asynchronous JavaScript.&lt;/p&gt;




&lt;h2&gt;
  
  
  💡 Key Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;JavaScript is &lt;strong&gt;single-threaded&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Node.js remains &lt;strong&gt;non-blocking&lt;/strong&gt; by delegating asynchronous work to &lt;strong&gt;libuv&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;Event Loop&lt;/strong&gt; moves completed callbacks to the Call Stack when it's empty.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;process.nextTick()&lt;/code&gt; executes before Promise callbacks.&lt;/li&gt;
&lt;li&gt;Promise callbacks execute before timer callbacks.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;setImmediate()&lt;/code&gt; executes during the &lt;strong&gt;Check&lt;/strong&gt; phase.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🎯 Final Thoughts
&lt;/h2&gt;

&lt;p&gt;The Event Loop is the engine behind Node.js's scalability. Once you understand how callbacks, microtasks, and Event Loop phases interact, asynchronous code becomes much easier to reason about.&lt;/p&gt;

&lt;p&gt;Mastering this concept is essential for writing efficient Node.js applications and performing well in backend interviews.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;What's the most confusing Event Loop behavior you've encountered? Drop it in the comments, and let's discuss! 👇&lt;/strong&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  NodeJS #JavaScript #Backend #EventLoop #AsyncProgramming #WebDevelopment #Coding #Programming #100DaysOfCode
&lt;/h1&gt;

</description>
      <category>node</category>
      <category>webdev</category>
      <category>programming</category>
      <category>productivity</category>
    </item>
    <item>
      <title>🚀Backend Internals #5: Stop Installing Everything Globally—Understand Local vs Global npm Packages</title>
      <dc:creator>Krati Joshi</dc:creator>
      <pubDate>Sat, 25 Jul 2026 18:06:14 +0000</pubDate>
      <link>https://dev.to/joshikrati03/backend-internals-5-stop-installing-everything-globally-understand-local-vs-global-npm-packages-3b7</link>
      <guid>https://dev.to/joshikrati03/backend-internals-5-stop-installing-everything-globally-understand-local-vs-global-npm-packages-3b7</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;One of the most confusing topics for beginners in Node.js isn't Express, APIs, or asynchronous programming—it's understanding where npm packages should be installed.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When I started learning Node.js, I thought there were only two commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;package-name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-g&lt;/span&gt; package-name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I knew they both installed packages, but I had no idea &lt;strong&gt;when to use which one&lt;/strong&gt;. Eventually, I realized they solve two completely different problems.&lt;/p&gt;

&lt;p&gt;If you're learning Node.js, this article will save you from one of the most common beginner mistakes.&lt;/p&gt;




&lt;h1&gt;
  
  
  First, What Does npm Actually Do?
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;npm (Node Package Manager)&lt;/strong&gt; is the package manager that comes with Node.js.&lt;/p&gt;

&lt;p&gt;It helps you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Install libraries&lt;/li&gt;
&lt;li&gt;Manage project dependencies&lt;/li&gt;
&lt;li&gt;Update packages&lt;/li&gt;
&lt;li&gt;Share your own packages&lt;/li&gt;
&lt;li&gt;Run project scripts&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Whenever you install a package, npm has to decide &lt;strong&gt;where&lt;/strong&gt; to install it.&lt;/p&gt;

&lt;p&gt;That's where &lt;strong&gt;local&lt;/strong&gt; and &lt;strong&gt;global&lt;/strong&gt; installations come in.&lt;/p&gt;




&lt;h1&gt;
  
  
  Local Installation (The Default)
&lt;/h1&gt;

&lt;p&gt;When you run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;express
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;npm installs Express &lt;strong&gt;inside your current project&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Your folder now looks something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;my-project/
│
├── node_modules/
├── package.json
├── package-lock.json
└── app.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It also adds Express to your &lt;code&gt;package.json&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"dependencies"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"express"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^5.0.0"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Express belongs to this project.&lt;/li&gt;
&lt;li&gt;Anyone who clones your repository can simply run:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and npm installs everything automatically.&lt;/p&gt;

&lt;p&gt;That's exactly what you want for project dependencies.&lt;/p&gt;




&lt;h1&gt;
  
  
  Why Local Installation Matters
&lt;/h1&gt;

&lt;p&gt;Imagine you're building an API.&lt;/p&gt;

&lt;p&gt;Your code contains:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now imagine another developer clones your project.&lt;/p&gt;

&lt;p&gt;If Express was installed locally, they only need to run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Everything works.&lt;/p&gt;

&lt;p&gt;If it wasn't, they'll see something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Cannot find module 'express'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;because the dependency isn't part of the project.&lt;/p&gt;

&lt;p&gt;That's why libraries your application &lt;strong&gt;depends on&lt;/strong&gt; should almost always be installed locally.&lt;/p&gt;




&lt;h1&gt;
  
  
  Global Installation
&lt;/h1&gt;

&lt;p&gt;Now consider this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-g&lt;/span&gt; nodemon
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This installs &lt;strong&gt;nodemon globally&lt;/strong&gt; on your computer.&lt;/p&gt;

&lt;p&gt;Instead of living inside a project, it's available from any terminal.&lt;/p&gt;

&lt;p&gt;You can now run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;nodemon app.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;from any project on your machine.&lt;/p&gt;

&lt;p&gt;Global packages are mainly meant for &lt;strong&gt;command-line tools (CLI tools)&lt;/strong&gt; rather than libraries used by your application.&lt;/p&gt;




&lt;h1&gt;
  
  
  Common Global Packages
&lt;/h1&gt;

&lt;p&gt;Some examples include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;nodemon&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;typescript&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;npm&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;pm2&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;eslint&lt;/code&gt; (if you prefer using it globally)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These provide commands that you use while developing rather than becoming part of your application's runtime.&lt;/p&gt;




&lt;h1&gt;
  
  
  A Common Beginner Mistake
&lt;/h1&gt;

&lt;p&gt;Many beginners do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-g&lt;/span&gt; express
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It works on &lt;strong&gt;their&lt;/strong&gt; machine because Express is installed globally.&lt;/p&gt;

&lt;p&gt;Then they push their code to GitHub.&lt;/p&gt;

&lt;p&gt;Another developer clones the repository and gets:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Error: Cannot find module 'express'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why?&lt;/p&gt;

&lt;p&gt;Because &lt;strong&gt;global packages are not shared with your project&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;They only exist on your own computer.&lt;/p&gt;

&lt;p&gt;The correct way is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;express
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now Express becomes part of the project and is recorded in &lt;code&gt;package.json&lt;/code&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  So When Should You Install Globally?
&lt;/h1&gt;

&lt;p&gt;Ask yourself one simple question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Is this package used by my project, or by me?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If the project needs it:&lt;/p&gt;

&lt;p&gt;✅ Install locally&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;package-name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If &lt;strong&gt;you&lt;/strong&gt; need the command across many projects:&lt;/p&gt;

&lt;p&gt;✅ Install globally&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-g&lt;/span&gt; package-name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This simple rule avoids most confusion.&lt;/p&gt;




&lt;h1&gt;
  
  
  What About &lt;code&gt;npx&lt;/code&gt;?
&lt;/h1&gt;

&lt;p&gt;Modern Node.js development has made global installations less necessary.&lt;/p&gt;

&lt;p&gt;Instead of installing a CLI globally, you can often use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx nodemon app.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx create-react-app my-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;npx&lt;/code&gt; will use the local package if it's already installed, or temporarily fetch and execute it when appropriate.&lt;/p&gt;

&lt;p&gt;That means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No global installation&lt;/li&gt;
&lt;li&gt;No version conflicts between projects&lt;/li&gt;
&lt;li&gt;Cleaner development environment&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is why you'll see &lt;code&gt;npx&lt;/code&gt; recommended in many official tutorials.&lt;/p&gt;




&lt;h1&gt;
  
  
  Quick Comparison
&lt;/h1&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Local Installation&lt;/th&gt;
&lt;th&gt;Global Installation&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Inside current project&lt;/td&gt;
&lt;td&gt;Installed on your computer&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Saved in &lt;code&gt;package.json&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Not saved in project&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Used by project code&lt;/td&gt;
&lt;td&gt;Used by developers&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Shared with team&lt;/td&gt;
&lt;td&gt;Personal to your machine&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Installed with &lt;code&gt;npm install&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Installed with &lt;code&gt;npm install -g&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h1&gt;
  
  
  My Rule of Thumb
&lt;/h1&gt;

&lt;p&gt;Whenever I'm unsure, I remember this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;📦 &lt;strong&gt;If my application needs it → Install locally&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;🖥️ &lt;strong&gt;If I need the command everywhere → Install globally&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;⚡ &lt;strong&gt;If I only want to run it once → Use &lt;code&gt;npx&lt;/code&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This simple mindset has helped me avoid a lot of confusion while learning Node.js.&lt;/p&gt;




&lt;h1&gt;
  
  
  Final Thoughts
&lt;/h1&gt;

&lt;p&gt;Understanding the difference between local and global packages isn't just about memorizing commands—it's about understanding &lt;strong&gt;who the installation is for&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Your &lt;strong&gt;application&lt;/strong&gt; depends on local packages.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;You&lt;/strong&gt;, the developer, benefit from global CLI tools.&lt;/li&gt;
&lt;li&gt;And &lt;code&gt;npx&lt;/code&gt; gives you a convenient way to run many tools without installing them globally.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It's a small concept, but getting it right makes working with Node.js projects much smoother.&lt;/p&gt;




&lt;p&gt;Thanks for reading! 👋&lt;/p&gt;

&lt;p&gt;I'm currently documenting my Node.js learning journey through the &lt;strong&gt;Backend Internals&lt;/strong&gt; series. If you're learning Node.js too, feel free to connect—I hope these notes make your journey a little easier.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>node</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
