𝗗𝗮𝘆 𝟯𝟵 𝗼𝗳 𝗟𝗲𝗮𝗿𝗻𝗶𝗻𝗴 𝗠𝗘𝗥𝗡 𝗦𝘁𝗮𝗰𝗸
I am on day 39 of my MERN stack journey.
Yesterday I studied HTTP verbs like GET and POST. Today I focused on backend operations. I studied how servers handle data streams and body parsing.
When a user sends a form or a file, the server does not get it all at once. Data arrives in small pieces. These pieces are called chunks.
Node.js uses streams to save memory. It does not load a large file into your server memory immediately. Instead, it handles data in small parts.
Here is how the process works:
- Incoming POST data acts as a Readable Stream.
- The server receives small chunks of binary data.
- You must collect these chunks before you use the data.
In Node.js, you do not get an instant request body. You listen to specific network events:
- req.on("data"): This event triggers every time a new chunk arrives. You save these chunks in an array.
- req.on("end"): This event triggers when the stream finishes. This tells you all data has arrived.
You use Buffer.concat to join the pieces. Then you convert them into a string.
This method keeps your server fast and efficient. It prevents memory crashes when handling large files.
Source: https://dev.to/ali_hamza_589ec7b3eb6688d/day-39-of-learning-mern-stack-hkp