NODE JS

Node js to extract post requests in two steps

In this short tutorial I am going to perform how to use Node js to extract post requests using body parserIf your web application is completely dependent on node modules as server side language then you must know the basic funda behind get & post operations in node js.

Node js to extract post requests
Node js to extract post requests

Node js to extract post requests using body parser

Requirements

  1. Node modules
  2. An html & JS file
  3. Some coffee and a good mood.

STEP I

Our first step here is to download and install two node packages and create a node js server side js file to make it listen on a port say 3000.

Packages required

  • express
  • body-parser

To download these packages, head to your project root folder and run the following commands

Now to create a server side node js file head to your project root folder and create following file.

/var/www/NodejsProject/index.js

Now make sure the server is running by the command

Now hit localhost:3000 . A blank html page should appear. If no error then everything is fine else if you receive some error please comment below to discuss about it.

STEP II

Our second step here is to create a client side HTML file to send a post request to server. This is very easy and simple as you must have done it many times.

/var/www/NodejsProject/index.html

Please note that our goal is to type in above text box and submit the form using post requests and use Node js to extract post requests.

/var/www/NodejsProject/index.js

The final output is going to be rendered in http://localhost:3000/data  jsonParser is necessary to parse the request which is in json format and urlencodedParser is used to parse the url if any. req.body.name  will fetch the name attribute from request and get its value. Finally  res.send is used to render the data. Complete server side node js code is below

Conclusion

We performed how to use Node js to extract post requests using body parser. Using node js as a stand alone server side language is a good choice as it is pure javascript language. Other usage of node js are all similar like any other server side programming language. I would like to hear more from you about node js trick and tips. I am also researching more about node socket.io notification system. If you have any idea about nodejs notification system please let me know. Thank you !!!

Share

Leave a Comment

Your email address will not be published. Required fields are marked *