0

Okay, So i was going through my old node code which I wrote when I was following some tutorial.

I am having tough time figuring out the exact function of Body Parser does. I am also using ejs package

So, What my basic instinct tells me is that body parser is used to transfer data between node and my html template and ejs package is used to embed javascript onto the HTML template.

Can someone confirm if i am thinking in the right direction? for some reason their documentation seems too advance for me to understand. Also, If someone can also confirm that the function of **npm request** is to make API calls?

  • 1
    ejs is used for rendering responses, bodyparser is used for decoding requests. They have nothing to do with each other. – Bergi Mar 28 '18 at 23:44
  • bodyparser is a middleware; it's code that will execute at each request before ever touching your handlers, and it will parse the body of the request into something directly usable in javascript so you don't have to worry about parsing JSON yourself for example. – mgul Mar 28 '18 at 23:44
  • Essentially, it sets `req.body` based on how you call it (e.g., json). – Behrooz Mar 29 '18 at 00:08
  • Possible duplicate of [What does body-parser do with express?](https://stackoverflow.com/questions/38306569/what-does-body-parser-do-with-express) – zero298 Jun 25 '19 at 16:21

2 Answers2

1

let’s try to keep this least technical.

Let’s say you are sending a html form data to node-js server i.e. you made a request to the server. The server file would receive your request under a request object. Now by logic, if you console log this request object in your server file you should see your form data some where in it, which could be extracted then, but whoa ! you actually don’t !

So, where is our data ? How will we extract it if its not only present in my request.

Simple explanation to this is http sends your form data in bits and pieces which are intended to get assembled as they reach their destination. So how would you extract your data.

An actual way is described here :How bodyParser() works – Adam Zerner – Medium

But, why take this pain of every-time manually parsing your data for chunks and assembling it. Use something called “body-parser” which would do this for you.

body-parser parses your request and converts it into a format from which you can easily extract relevant information that you may need.

For example, let’s say you have a sign-up form at your frontend. You are filling it, and requesting server to save the details somewhere.

Extracting username and password from your request goes as simple as below if you use body-parser.

…………………………………………………….

var loginDetails = {

username : request.body.username,

password : request.body.password

};

…………………………………………………….

So basically, body-parser parsed your incoming request, assembled the chunks containing your form data, then created this body object for you and filled it with your form data.

Alwaysblue
  • 9,948
  • 38
  • 121
  • 210
0

In addition to iRohitBhatia's answer:

If you are using Express >= 4.16.0 you can use the body parser direct from the Express ,since the body parser has been re-added under the methods: express.json() and express.urlencoded().

Source:

The express.json() and express.urlencoded() middleware have been added to provide request body parsing support out-of-the-box. This uses the expressjs/body-parser module module underneath, so apps that are currently requiring the module separately can switch to the built-in parsers.

greybeard
  • 2,249
  • 8
  • 30
  • 66
Naftalix
  • 26
  • 4