Course contentsShow
← Node.js
Lesson 528 of 1,9806. Networking and HTTP FundamentalsFree lesson

Body Parsing Strategies

Create custom logic to extract and validate multi-part form data or raw JSON from request streams.

In this lesson, you will learn how to manually extract and validate incoming data from a raw HTTP request stream by managing buffers and event listeners.

When a client sends data to your server, it arrives as a stream of bytes. Because TCP packets can be fragmented, we cannot assume the entire request body arrives at once. Instead, we must listen for the data event to collect these "chunks" and the end event to know when the transmission is complete.

Collecting Raw Streams

To parse a body, we initialize an empty array to store incoming Buffer objects. As each piece of the request arrives, we push it into the array. Once the stream ends, we concatenate these buffers into a single unit.

server.on('request', (req, res) => {
  let body = [];

  req.on('data', (chunk) => {
    body.push(chunk);
  });

  req.on('end', () => {
    const rawData = Buffer.concat(body).toString();
    
    // Custom Validation Strategy
    try {
      const parsed = JSON.parse(rawData);
      console.log('Received JSON:', parsed);
    } catch (e) {
      res.statusCode = 400;
      res.end('Invalid JSON format');
    }
  });
});

Strategy for Multi-Part Data

When dealing with multipart/form-data (like file uploads), the stream contains boundaries defined in the Content-Type header. A manual strategy involves searching the concatenated Buffer for these boundary strings. Unlike simple JSON, multi-part data requires us to scan the raw bytes for specific byte sequences that act as delimiters between form fields or files.

Why Manual Validation?

By handling the stream yourself, you control memory usage. If an incoming request is massive, you can implement a "maximum size" check inside the data event listener. If the accumulated body length exceeds your defined limit, you can call req.destroy() to abort the connection, preventing memory exhaustion attacks before the full body is even processed.

Key Takeaway: Manual body parsing requires careful assembly of stream chunks into a complete buffer, followed by validation logic that confirms the integrity and format of the raw data before further processing.