MERN Stack सीखने का 38वाँ दिन

मैं अपनी MERN stack यात्रा के 38वें दिन पर हूँ।

कल मैंने URLs से query strings निकालना सीखा। आज मैंने HTTP request methods का अध्ययन किया।

एक सर्वर को उपयोगकर्ता के इरादे (intent) को जानने की आवश्यकता होती है। इसे जानने के लिए आप req.method प्रॉपर्टी का उपयोग करते हैं। यह प्रॉपर्टी आपके backend को बताती है कि क्या कार्रवाई करनी है।

एक endpoint इस्तेमाल किए गए method के आधार पर अपना व्यवहार बदलता है।

यहाँ चार मुख्य methods दिए गए हैं:

जब आप इन methods को एक endpoint के साथ मैप करते हैं, तो आपका backend कार्यात्मक (functional) हो जाता है।

Example code:

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 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