Body-parser is the Node.js body parsing middleware. It is responsible for parsing the incoming request bodies in a middleware before you handle it.
Installation of body-parser module:
- You can visit the link to Install body-parser module. You can install this package by using this command.
npm install body-parser
- After installing body-parser you can check your body-parser version in command prompt using the command.
npm version body-parser
- After that, you can just create a folder and add a file, for example, index.js. To run this file you need to run the following command.
Steps to run the program:
- The project structure will look like this:
- Make sure you have installed ‘view engine’ like I have used “ejs” and also installed express and body-parser module using following commands:
npm install express npm install ejs npm install body-parser
- Run index.js file using below command:
- Now Open browser and type the below URL and you will see the Demo Form as shown below:
- Now submit the form and then you will see the following output:
- But if we do not use this body-parser middle, then while parsing, error will occur as show below:
So this is how you can use body-parser module for parsing incoming request bodies in a middleware before you handle it.
- Implementing Csurf Middleware in Node.js
- Nodejs | Automatic restart NodeJs server with nodemon
- Nodejs | DNS
- Nodejs | Jimp
- Nodejs | DNS | setServers()
- Web-Socket in NodeJS
- Nodejs | GM channel() Function
- Nodejs | GM implode() Function
- Nodejs | GM flop() Function
- Nodejs | GM bordercolor() Function
- Nodejs | GM edge() Function
- Nodejs | GM flip() Function
- Nodejs | GM chop() Function
- Nodejs | GM magnify() Function
- Nodejs | GM minify() Function
- Nodejs | GM write() Function
- Nodejs | GM whiteThreshold() Function
- Nodejs | GM whitePoint() Function
- Nodejs | GM border() Function
- Nodejs | GM enhance() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.