top of page
Writer's pictureDibyojyoti Sanyal

How would you access form data without using bodyParser ?

Updated: Sep 18, 2021

Generally we use the body-parser node module to parse request bodies. When for some reason we don't want to use express or want to know how the body-parser probably works, this post explains how developers in vanilla node.js can extract request bodies from an HTTP(S) POST, PUT and PATCH request. The request object in express works with few stream events. One of the events is 'data' and another is 'end'. Using these two stream events we can do our job.

 

Content

 

What are streams in node.js ?

Streams are one of the fundamental concepts in node.js used for data- handling. Using streams we can read data from an input and write data to an output in a sequence. Streams can be used in any kind of information exchange. For example, to read data from files and write data to files. Streams can be used for network communications also. In streams data is not read from input at once and then written to output. Instead data is read and written at the same time as a sequence of chunks. This makes streams powerful when dealing with large amounts of data. We can write memory and time efficient code using streams. Streams uses an asynchronous programming model and provides few events to listen to. That is a brief on streams which we need to understand for this post.


What are data and end events?

A data event is triggered when the stream receives a chunk of data. When the data ends the stream end event is called.


How to use stream events to read and parse data from a request object in node.js ?

We need to implement logic on the data and the end stream event. We have to listen to the data event and capture the chunk from there. Then we collect it in a variable. We also need to listen to the end event. When the end event is generated we are sure that we have received all data chunks sent as a request body. We need to consolidate the data we collected and process the data as a whole. Here in this example, we are verifying whether the data starts with a curly brace, if it is then we assume the data is in JSON format. So we convert it to a JSON data structure. After that by calling the next() method we finish the data processing on the hook that we have written on the end event and pass the control to other event listeners that may exist.

app.use(function(request, response, next) {
  let whole_data = '';
  request.on('data', function(data_chunk) {
 console.log('on data: ', data_chunk);
 whole_data += data_chunk;
  });
  request.on('end', function() {
 req.rawBody = whole_data;
 console.log('on end: ', whole_data);
 if (whole_data && whole_data.indexOf('{') > -1 ) {
  req.body = JSON.parse(whole_data);
 }
 next();
  });
});

The above code is just an example of what we might like to do when we get the whole data at the end. We can write any logic we want here. For example, if we just want to print the whole data, the code will be as follows

app.use(function(request, response, next) {
  let whole_data = '';
  request.on('data', function(data_chunk) {
 whole_data += data_chunk;
  });
  request.on('end', function() {
 console.log(whole_data);
 response.end();
  });
});

Today, we learned how data can be read using streams events in node.js when we don't want to use the body-parser or similar packages.




Recent Posts

See All

Comments


bottom of page