𝗗𝗮𝘆 𝟯𝟴 𝗼𝗳 𝗟𝗲𝗮𝗿𝗻𝗶𝗻𝗴 𝗠𝗘𝗥𝗡 𝗦𝘁𝗮𝗰𝗸

I am on day 38 of my MERN stack journey.

Yesterday, I learned how to extract query strings from a URL. Today, I studied HTTP request methods.

Before today, my server treated every request the same. Now, I make the backend perform different actions based on the user intent. This changes a static endpoint into a functional tool.

I focused on the req.method property. This property tells the server what the client wants to do.

Here are the four main methods I learned:

When you use req.method, you control the logic of your application. You decide if a user views a page or submits a form at the same URL.

Code example:

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("Reading database records..."); } else if (req.method === "POST") { res.writeHead(201, { "Content-Type": "text/plain" }); res.end("Creating new data!"); } } else { res.end("Standard Route"); } }); server.listen(8000);

Source: https://dev.to/ali_hamza_589ec7b3eb6688d/day-38-of-learning-mern-stack-opl