← Back to Node.js Course | Chapter 6: HTTP Module | Lesson 6 of 7

HTTP Methods क्या हैं

GET, POST, PUT, PATCH, और DELETE server को बताते हैं कि आपको किस तरह का action चाहिए।

In this page:

  1. HTTP Methods

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

javascript
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. #}
आम गलतियां
  1. data बदलने के लिए GET इस्तेमाल करना
  2. unsupported methods के लिए 405 return न करना
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.