Body-parser middleware in Node.js - GeeksforGeeks

25 Jun, 2021
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.  
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.  
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.  
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.  
node index.js
Filename: SampleForm.ejs  
html
<!DOCTYPE html><html> <head>    <title>Body-Parser Module Demo</title></head> <body>    <h1>Demo Form</h1>     <form action="saveData" method="POST">        <pre>            Enter your Email    : <input type="text"                                name="email"> <br>                                             <input type="submit" value="Submit Form">        </pre>    </form></body> </html>
Filename: index.js  
javascript
const bodyparser = require('body-parser')const express = require("express")const path = require('path')const app = express()  var PORT = process.env.port || 3000 // View Engine Setupapp.set("views", path.join(__dirname))app.set("view engine", "ejs") // Body-parser middlewareapp.use(bodyparser.urlencoded({extended:false}))app.use(bodyparser.json())  app.get("/", function(req, res){    res.render("SampleForm")});  app.post('/saveData', (req, res) => {    console.log("Using Body-parser: ", req.body.email)})  app.listen(PORT, function(error){    if (error) throw error    console.log("Server created Successfully on PORT", PORT)})
Steps to run the program:  
The project structure will look like this:  
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:  
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:  
Run index.js file using below command:  
node index.js
Now Open browser and type the below URL and you will see the Demo Form as shown below:  
Now Open browser and type the below URL and you will see the Demo Form as shown below:  
http://localhost:3000/
 
 
Now submit the form and then you will see the following output:  
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:  
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. 
jpd20
Node.js-Misc
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Node.js fs.readFile() Method
Node.js fs.writeFile() Method
How to install the previous version of node.js and npm ?
Difference between promise and async await in Node.js
Express.js res.render() Function
Roadmap to Become a Web Developer in 2022
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to fetch data from an API in ReactJS ?
Convert a string to an integer in JavaScript
How to insert spaces/tabs in text using HTML/CSS?