📤
Request & Response

Response Methods

send, json, status, sendFile
💡 Response methods EK COURIER ke alag-alag SERVICE OPTIONS jaise hain — chitthi (send), form-bharkar data (json), parcel (sendFile), aur bina saamaan ka receipt (sendStatus).

res.json() data ko JSON mein serialize karke Content-Type: application/json set karta hai — API ke liye yahi default choice hai. res.send() type GUESS karta hai: string ho to HTML, object ho to JSON, Buffer ho to binary.

res.status() sirf status code SET karta hai, response bhejta nahi — isliye use chain karna padta hai: res.status(404).json({...}). res.sendStatus(404) status set karke standard message bhi bhej deta hai.

res.json({ id: 1, name: "Aarav" });          // 200 + JSON
res.status(201).json({ created: true });      // chained
res.status(204).end();                        // no content
res.sendFile(path.join(__dirname, "a.pdf"));  // file stream
res.sendStatus(403);                          // "Forbidden" text
📤
Response methods EK COURIER ke alag-alag SERVICE OPTIONS jaise hain — chitthi (send), form-bharkar data (json), parcel (sendFile), aur bina saamaan ka receipt (sendStatus).
1 / 2
⚡ झट से Recap
  • res.json() = API ke liye default, Content-Type khud set karta hai
  • res.status() sirf code set karta hai — chain karke bhejna zaroori hai
  • res.sendFile() files stream karta hai, res.sendStatus() code + default message
इस page में (2 subtopics)

res.send() apne argument ka type dekhkar Content-Type decide karta hai — string par text/html, object/array par application/json, Buffer par application/octet-stream. res.json() hamesha JSON hi bhejta hai (aur JSON.stringify replacer/spaces settings respect karta hai). res.end() bina body ke response band karta hai.

res.send("<h1>Hi</h1>");          // text/html
res.send({ ok: true });            // application/json
res.json({ ok: true });            // explicit, API ke liye behtar
res.status(204).end();             // koi body nahi

Response methods chainable hain: res.status(201).location("/users/1").json(user). Badi files ke liye res.sendFile() ya stream.pipe(res) use karo — poori file memory mein padhne ke bajaye chunks mein jaati hai, jisse memory bachti hai.

const stream = fs.createReadStream("big-video.mp4");
res.set("Content-Type", "video/mp4");
stream.pipe(res);                  // chunked, memory-safe