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 in Others by ArchanaNagur
• 2,270 points

retagged May 22 by Kalgi 144 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
app.use(bodyParser.json());

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

answered May 20 by sunshine
• 1,240 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 in Others by ArchanaNagur
• 2,270 points
96 views
+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 in Others by Nisa
• 1,090 points
32 views
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 10 in Others by ArchanaNagur
• 2,270 points
44 views
0 votes
1 answer

Why is MongoDB associated with Node.js?

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

answered Jul 18 in Others by ArchanaNagur
• 2,270 points
36 views
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,810 points
66 views
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,020 points
135 views
0 votes
1 answer

Why and when to use Node.js?

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

answered Jun 14 in Others by sunshine
• 1,240 points

edited Jun 14 by sunshine 61 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 in Others by sunshine
• 1,240 points
63 views