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

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

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

आज से पहले, मेरा server हर request के साथ एक जैसा व्यवहार करता था। अब, मैं user के intent के आधार पर backend से अलग-अलग कार्य करवाता हूँ। यह एक static endpoint को एक functional tool में बदल देता है।

मैंने req.method property पर ध्यान केंद्रित किया। यह property server को बताती है कि client क्या करना चाहता है।

यहाँ वे चार मुख्य methods हैं जो मैंने सीखे:

जब आप req.method का उपयोग करते हैं, तो आप अपने application के logic को नियंत्रित करते हैं। आप तय करते हैं कि उपयोगकर्ता एक ही 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