GET DATA FROM POST METHOD IN NODE.JS EXPRESS

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 say you are sending a some data to node js server, you are making a request to the server. The server file receives the data under request object. Now, if you log req. object in your server file you should see your input data somewhere in request or req. object, which could be extracted when required.

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.


To Get Regular Content Updates, subscribe my youtube channel-

https://www.youtube.com/c/renurawat?sub_confirmation=1

Comments