Why do we use Body-parser in Node js

0 votes
When we can get the request without body-parser in Node.js using express .get() method. why do we need it?
May 20, 2019 in Others by ArchanaNagur
• 2,360 points

retagged May 22, 2019 by Kalgi 7,787 views

1 answer to this question.

0 votes

For understanding this first you need to know the structure of a Request

Where URL - Is the resources you want to access to like example- http://example.com

Method- what you want the server to do, like do you want the server to POST, PUT, GET or DELETE

Header- Contains the meta-data of your request

Body- Contains the information which is required to process the request.

Suppose if you want to insert a data into the database, you have to first get the data from the user (Which is present in the body of your HTTP request) and then insert it into your database through your application.

So what exactly your express .get() method does is that it is used to get the raw data. But if you want only the data which is present in the body of your request then you have to parse using body-parser.

const bodyParser = require('body-parser');

// support parsing of application/json type post data

//support parsing of application/x-www-form-urlencoded post data
app.use(bodyParser.urlencoded({ extended: true }));

answered May 20, 2019 by sunshine
• 1,300 points

Related Questions In Others

0 votes
1 answer

What is use of Babel in Node.js ?

Node.js is a runtime environment  which let us ...READ MORE

answered Jul 2, 2019 in Others by ArchanaNagur
• 2,360 points
0 votes
1 answer

Why do we need virtual functions in C++?

A virtual function is a base class member function that we expect to redefine in derived classes. In the base class, a virtual function is used to ensure that the function is overridden.  This is especially true when a pointer from a base class points to an object from a derived class. For example, consider the code below: class Base ...READ MORE

answered May 27, 2022 in Others by Damon
• 4,960 points
+1 vote
1 answer

Why is the result distorted when we use threads?

A thread is basically a part of ...READ MORE

answered Jun 6, 2019 in Others by Nisa
• 1,090 points
0 votes
1 answer

How do I start studying Node.JS to create a restful API?

Node.js is an open source server environment ...READ MORE

answered Jun 11, 2019 in Others by ArchanaNagur
• 2,360 points
0 votes
1 answer

how to safely deploy npm install without it causing inconsistencies?

The recent versions on npm generates a ...READ MORE

answered Apr 11, 2018 in DevOps on Cloud by DareDev
• 6,890 points
0 votes
1 answer

Unable to request channel creation using Rest Api

I'd recommend taking a look at the ordering ...READ MORE

answered Jul 16, 2018 in Blockchain by Perry
• 17,100 points
0 votes
1 answer

Why and when to use Node.js?

The major reason why they are going ...READ MORE

answered Jun 14, 2019 in Others by sunshine
• 1,300 points

edited Jun 14, 2019 by sunshine 836 views
+1 vote
1 answer

what is the difference between error and stderr in Node.js?

Error is an object created by Node.js to handle ...READ MORE

answered Jul 4, 2019 in Others by sunshine
• 1,300 points
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP