DEV Community

Ali Hamza
Ali Hamza

Posted on

Day 38 of Learning MERN Stack

Hello Dev Community! 👋

It is officially Day 38 of my unbroken streak toward mastering the MERN stack! Yesterday, I learned how to extract query strings from the URL bar. Today, I took a massive step forward into full-stack backend architecture by diving into Prashant Sir's (Complete Coding) roadmap to master HTTP Request Methods.

Up until now, our server treated every incoming request the same way. Today, I learned how to make the backend execute completely different actions based on the "intent" (HTTP Verb) of the user!


🧠 Key Learnings From Node.js Lecture 6 (HTTP Verbs)

An endpoint is no longer static when you map request methods against it. Here is the technical breakdown of what I locked down today:

1. Cracking req.method

I discovered that the incoming Request object holds a crucial property called req.method. This tells the server exactly what action the client wants to perform.

2. The Big Four Core Methods

  • GET: Used when the user simply wants to read or fetch data from the server (e.g., viewing a product page).
  • POST: Used when the user wants to securely send or create new data on the server (e.g., submitting a signup form).
  • PUT/PATCH: Used to update existing data records.
  • DELETE: Used to wipe a specific data entry from the server storage.

javascript
const http = require("http");

const server = http.createServer((req, res) => {
    if (req.url === "/api/data") {
        if (req.method === "GET") {
            res.writeHead(200, { "Content-Type": "text/plain" });
            res.end("Fetching and reading secure database records...");
        } else if (req.method === "POST") {
            res.writeHead(201, { "Content-Type": "text/plain" });
            res.end("Securely creating and injecting new data into the server!");
        }
    } else {
        res.end("Standard Route");
    }
});
server.listen(8000);
Enter fullscreen mode Exit fullscreen mode

Top comments (0)