Decoding HTTP Status Codes: 200, 400, 404, and 500 Explained

Ecommerce website banate waqt inhe samajhna bohot zaroori hai taaki aap frontend par sahi message dikha sakein.


1. 200 OK (Success) ✅

Ye sabse achha signal hai. Iska matlab hai ki sab kuch sahi gaya.

  • Kab aata hai? Jab aapne products mangwaye aur server ne list bhej di, ya user ne login kiya aur details mil gayi.
  • Ecommerce Example: User “Cart” page par gaya aur use apne items dikh gaye.

2. 400 Bad Request (Client Error) ❌

Ye error tab aata hai jab frontend ne galat data bheja ho jo server samajh nahi pa raha.

  • Kab aata hai? Jab aapne req.body mein productId toh bheja par userId bhool gaye, aur backend ne wahan validation lagayi hui hai.
  • Ecommerce Example: “Add to Cart” dabaya par quantity “abc” (string) bhej di jabki Number chahiye tha.

3. 404 Not Found (Missing Resource) 🔍

Iska matlab hai ki jo cheez aap dhoond rahe hain, wo server par exist hi nahi karti.

  • Kab aata hai? Jab URL galat ho ya database mein wo ID na mile.
  • Ecommerce Example: User ne kisi aise product ka URL khola jo delete ho chuka hai (/product/wrong-id).

4. 500 Internal Server Error (Server Blast) 🔥

Ye sabse khatarnak error hai. Iska matlab hai frontend sahi hai, lekin backend ke code mein koi “Bug” hai ya database crash ho gaya hai.

  • Kab aata hai? Jab aapne try-catch toh lagaya par code ke andar koi aisi galti kar di jisse server crash ho gaya (e.g., null ka property access karna).
  • Ecommerce Example: User ne “Checkout” kiya par payment gateway connect karte waqt backend crash ho gaya.

Blog ke liye Comparison Table

CodeTypeMeaningSimple Logic
200SuccessOK“Kaam ho gaya, ye lo data!”
400Client ErrorBad Request“Tumne data galat bheja hai, sahi karo.”
404Client ErrorNot Found“Jo dhoond rahe ho, wo yahan nahi hai.”
500Server ErrorInternal Error“Meri (Server ki) galti hai, kuch toot gaya.”

Inhe Code mein kaise use karein? (Express Example)

Aap apne backend mein aise response bhejte hain:

JavaScript

app.get('/product/:id', async (req, res) => {
    try {
        const product = await Product.findById(req.params.id);
        
        if (!product) {
            return res.status(404).json({ message: "Product nahi mila!" }); // 404
        }
        
        res.status(200).json(product); // 200
    } catch (err) {
        res.status(500).json({ message: "Server mein kuch gadbad hai" }); // 500
    }
});

Leave a Comment