DEV Community

Cover image for Day 39 of Learning MERN Stack
Ali Hamza
Ali Hamza

Posted on

Day 39 of Learning MERN Stack

Hello Dev Community! 👋

It is officially Day 39 of my non-stop run toward full-stack MERN engineering! Yesterday, I mapped out basic HTTP verbs like GET and POST. Today, I advanced into Prashant Sir's (Complete Coding) backend masterclass to tackle one of the most critical core operations: Handling Data Streams and Body Parsing.

When a user submits a form or uploads data, the server doesn't receive the file all at once. It arrives as an asynchronous stream of network data chunks. Today, I learned how to collect and decode those packets natively!


🧠 Key Learnings From Node.js Lecture 7 (Streams & Buffers)

Node.js is designed to be non-blocking and memory-efficient. Here is the technical breakdown of how it intercepts client payloads:

1. Inbound Streams & Data Chunks

I learned that incoming POST data is treated as a Readable Stream. Instead of loading a massive data file into the server memory instantly, Node transmits the payload in tiny pieces called Chunks (hexadecimal binary data).

2. Event Listeners for Requests (req.on)

Natively, we don't have an instant req.body object. We have to listen to the network events on the incoming request stream:

  • req.on("data", (chunk) => { ... }): Fires every single time a fresh chunk of binary data arrives at the network interface. We push these raw chunks into a temporary array.
  • req.on("end", () => { ... }): Fires automatically once the stream concludes and all chunks have arrived safely.

javascript
if (req.url === "/submit" && req.method === "POST") {
    let body = [];

    req.on("data", (chunk) => {
        body.push(chunk); // Collecting raw binary chunks
    });

    req.on("end", () => {
        // Concatenating and converting hexadecimal binary buffers into a readable string layout
        let parsedBody = Buffer.concat(body).toString();
        console.log("Received Form Payload:", parsedBody);

        res.writeHead(200, { "Content-Type": "text/plain" });
        res.end("Data received and parsed successfully!");
    });
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)