Fetch API aur Undici ka Mast Combo: Node.js Mein Bina Dikkat HTTP Calls!

Fetch API aur Undici ka Mast Combo: Node.js Mein Bina Dikkat HTTP Calls!

Aaj kal har koi web development mein Fetch API se pyaar karta hai. Browser mein toh Fetch API ka istemal bahut common hai, lekin Node.js mein iska native support nahi tha… until now! Node.js v18 ne finally Fetch API ko include kiya, but agar tum older version use kar rahe ho ya extra speed aur control chahiye, tab kya karein? Undici lao! Ye article mein bataunga ki kaise Undici ke saath Fetch API ko Node.js mein use karte hain.

πŸ” Undici Kya Hai?

Undici ek HTTP/1.1 client hai jo specifically Node.js ke liye banaya gaya hai. Yeh Node.js core team ne develop kiya hai, matlab official-level ki performance aur reliability milegi! Traditional http module se 30% tak faster hai πŸš€. Undici ka naam Italian word “undici” (meaning “eleven”) se aaya hai β€” HTTP/1.1 connection ki tribute!

πŸ’‘ Kyon Use Karein?

  • Speed Ka Jhakaas: Connection pooling, pipelining jaise features ke saath lightning-fast HTTP calls.
  • Fetch API Support: Browser jaisa modern interface (fetch, Request, Response) milta hai.
  • Lightweight: No heavy dependencies!

πŸ”§ Setup Karo (Step-by-Step)

1. Undici Install Karo:

npm install undici

2. Fetch API Use Karne Ka Tarika:
Undici apne saath fetch function deta hai. Seedha require karo aur chalao!

import { fetch } from 'undici';

// Ya CommonJS mein
const { fetch } = require('undici');

async function dataFetcher() {
  try {
    const response = await fetch('https://jsonplaceholder.typicode.com/users/1');

    // Response handle karo
    if (response.ok) {
      const userData = await response.json();
      console.log(userData);
    } else {
      console.log('Error:', response.status);
    }
  } catch (error) {
    console.log('Network error!', error);
  }
}

dataFetcher();

🌟 Features jo Undici ke Saath Aate Hain:

  1. Streaming Support: Bina buffer ke data ko stream karo!
   const response = await fetch('https://example.com/big-file.zip');
   const fileStream = response.body;
   // Pipe karo file save karne ke liye!
  1. Advanced Control: Timeouts, redirect policies, headers customize karo.
   await fetch('https://api.com/data', {
     method: 'POST',
     headers: { 'Content-Type': 'application/json' },
     body: JSON.stringify({ name: 'Raju' }),
     redirect: 'follow',
     dispatcher: undiciAgent // Custom agent bhi use kar sakte ho!
   });
  1. Connection Pooling: Har request ke liye naya connection nahi, existing pool reuse hota hai β€” efficiency badhao!

πŸš€ Performance Tip

Undici ka Dispatcher use karke tum multiple requests ko optimize kar sakte ho. Connection pool size set karo:

const { fetch, Agent } = require('undici');
const undiciAgent = new Agent({ connections: 10 }); // 10 connections ka pool

fetch('https://api.com', { dispatcher: undiciAgent });

🧩 Node.js v18+ Mein Kya Karein?

Agar tum latest Node.js (v18+) use kar rahe ho, toh native Fetch API already hai! Par agar tumhe Undici ke advanced features chahiye (jaise connection tuning), toh Undici ko manually install karke use karo.

Final Vichar πŸ’¬

Undici aur Fetch API ka combo Node.js mein HTTP communication ko easy aur tez bana deta hai. Browser-style coding ka maza ab server-side bhi! Toh agla REST API call karte waqt axios ya node-fetch ko bye bolo aur Undici ko try karo. 10/10 recommend karunga main! πŸ˜„

Pro Tip: Error handling zaroor karo β€” network issues kabhi bhi aasakte hain!

Code mauj karo, performance badhao! πŸŽ‰
Agar query ho toh comment mein poocho!

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply

    Your email address will not be published. Required fields are marked *