Node.js Handling POST request in Node.js

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Remarks

Node.js uses streams to handle incoming data.

Quoting from the docs,

A stream is an abstract interface for working with streaming data in Node.js. The stream module provides a base API that makes it easy to build objects that implement the stream interface.

To handle in request body of a POST request, use the request object, which is a readable stream. Data streams are emitted as data events on the request object.

  request.on('data', chunk => {
    buffer += chunk;
  });
  request.on('end', () => {
    // POST request body is now available as `buffer`
  });

Simply create an empty buffer string and append the buffer data as it received via data events.

NOTE

  1. Buffer data received on data events is of type Buffer
  2. Create new buffer string to collect buffered data from data events for every request i.e. create buffer string inside the request handler.


Got any Node.js Question?