How to access form data without using bodyParser?

I always see bodyParser , which is used to access published form materials. By making it available at req.body. But how could you access this data if you did not want to use it bodyParser?

A related / similar question - how to bodyParserprovide you with data in req.body?

Edit: I ask how this stuff works low. This possible duplicate seems to solve it by recommending certain middleware and describing how to use them.

+4
source share
2 answers

bodyParser is a middleware that analyzes json data stream. I see no reason not to use bodyParser (unless you want to handle multi-part bodies), but you can analyze the threads yourself if you want. It will be something like middleware below:

app.use(function( req, res, next ) {
  var data = '';
  req.on( 'data', function( chunk ) {
    data += chunk;
  });
  req.on( 'end', function() {
    req.rawBody = data;
    console.log( 'on end: ', data )
    if ( data && data.indexOf( '{' ) > -1 ) {
      req.body = JSON.parse( data );
    }
    next();
  });
});

If you want to parse multi-part bodies, you can use one of the following modules:

  • busboy
  • multi-party
  • fine
  • multer
+6
source

A quick answer to this question is to use https://github.com/stream-utils/raw-body to parse the request body and then run JSON.parse as a result. Thus, the parser body receives the request body, from which it then parses json, urls, and raw text data.

0

All Articles