How to get data from post request in node.js | req.body in node js | req.body undefined in node js
Channel: Renu Rawat
Date: 12/05/2019

Hello and welcome back to our series on Node.JS tutorials. Lets create a simple Node.JS express application. So, far we've covered the basics of creating REST API n Node.JS. 

In this video, I'm going to create a new API endpoint for POST method and for testing the APIs I'll be using POSTMAN.  Lets say you are sending some data to Node.JS server that is you are making POST request to the server. 

Lets switch back to the code- This is a simple Node.JS project. At line. 1, it imports express module. I creates instance of express so that we could create routes for the project.
Then we could create routes for the project. Then we have a GET method with slash (/) route in its handler method we are returning Hello World to the request originator.

At last we are making this application listen on port 3000. Lets create a new route for POST method in this file. The POST method has 2 paramaters like GET method
first one is the route and second one is the handler. From the POST endpoint I'm returning this hardcoded message:- "I am post method!".

Now, lets open the ternminal and start application. The command to execute server file is: "node index.js". 

It gives server has started on port: 3000

To test the APIs you can use POSTMAN.  Lets open it, I have opened the POSTMAN. Lets try to run the GET method first.

For GET method it says- "Hello wold"
For POST method it says- "I am post method!" as expected.

Now, lets try to pass some data along with the POST request from the POSTMAN. GOTO body, then choose raw then select json (application/json) from this dropdown.
Open the terminal restart the currently running application by pressing CTRL+c twice from you keyboard and running the command:
"node index.js"

GOTO the POSTMAN and hit the POST API with this data and see the output in the console.

The API receives the data under req object. Now, If I log req object in the API. You should see the passed data somewhere in ther req object, which could be extracted when required.
The data should come inside req.body method.

Lets log req.body and see what I gets..
It prints nothing or undefined. you actually don't get the data. So, where is our data? How will I extract the data if req.body is empty.

The simple answer to this is http sends you data in bits and chunks/pieces which are  intended to get assembled as they reach their destination. So how would you extract your data.

But, why take this pain of everytime 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 to a format which you can easily extract for the relevant information.

For example- Let's say you have a sign-up form at your front end. You are filling it, and requesting the server to save the details.

Getting username and password from you request goes as simple as below if you use body parser-

var loginDetails = {    
    username : request.body.username,    
    password : request.body.password    
};

So, body-parser parses your incoming request, assembles the chunks containing your form data, then created this body object for you and filled it with your form data.
TABLE OF CONTENT

00:00 Introduction
00:30 A simple express application
03:38 How post method works?
04:00 How to get data from post using body-parser
5:40 Thanks for listening
-~-~~-~~~-~~-~-
ExpressJS/REST API Tutorials/API Playlist: https://youtu.be/NSOKTV9Udzs
Session 1: What is REST API?
https://youtu.be/Kw5-xbM1bWo
Session 2: Steps to build REST APIS with Express JS
https://youtu.be/6QperLQ2NBw
Session 3: The complete guid for building rest apis in express js
https://youtu.be/NSOKTV9Udzs
Session 4: How to get data from POST METHOD in Node.JS
https://youtu.be/0t93qDkGcB0
-~-~~-~~~-~~-~-
POSTMAN download link:
https://www.getpostman.com
~~~~~~~~~~~~~~
Please like my social media page:
https://www.facebook.com/saigraceschool
https://twitter.com/saigraceschool
https://www.instagram.com/saigraceschool
https://www.youtube.com/c/renurawat?sub_confirmation=1

To Get Regular Content Updates- 
https://www.youtube.com/c/renurawat?sub_confirmation=1

--
#RenuRawat #node #api #nodejs #express #httppost #post #bodyparser