HTTP Methods क्या हैं
GET, POST, PUT, PATCH, और DELETE server को बताते हैं कि आपको किस तरह का action चाहिए।
In this page:
HTTP Methods
GET data पढ़ता है, POST बनाता है, PUT replace करता है, PATCH partially update करता है, और DELETE remove करता है। Servers req.method पर branch करते हैं। GET और DELETE typically बिना body के होते हैं, और safe या idempotent semantics retries के लिए matter करते हैं।
Note:
GET को कभी भी data नहीं बदलना चाहिए।
उदाहरण: HTTP methods
const http = require("http");
const items = ["a"];
const server = http.createServer((req, res) => {
if (req.method === "GET") return res.end(JSON.stringify(items));
if (req.method === "POST") { items.push("new"); res.statusCode = 201; return res.end("created"); }
res.statusCode = 405; res.end("method not allowed");
});
server.listen(0, async () => {
const base = "http://localhost:" + server.address().port;
console.log((await fetch(base)).status, await (await fetch(base)).text());
console.log((await fetch(base, { method: "POST" })).status);
console.log((await fetch(base, { method: "DELETE" })).status);
server.close();
});
// Output:
// 200 ["a"]
// 201
// 405
⚠️ Run this in your own terminal or Node.js environment.
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
- data बदलने के लिए GET इस्तेमाल करना
- unsupported methods के लिए 405 return न करना
- PUT और PATCH confuse करना
चैप्टर सारांश
- GET पढ़ता है
- POST बनाता है
- PUT replace करता है, PATCH update करता है
- DELETE remove करता है
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: