Bodyparser: The BodyParser is a library in Node.js. Body Parsing is used to parse the body of an incoming HTTP request in a middleware. It captures the HTTP request generated by clients on the server when they submit a form, makes an API request, etc. The data information is included in the HTTP request.Â
The body-parser middleware extracts and parses the information into a suitable format to make processing with Node.js easy and effective. Let us learn how to install the body-parser using npm.
BodyParser Definition
A body-parser is a Node.js library used to extract information from an incoming HTTP request. It produces the information in a processed form by passing it into a middleware.Â
Also Read: Best Language For Web Development: Top 10 Languages in 2024
BodyParser: Why use body-parser middleware?
BodyParser middleware requests the clients into an HTML form or Js object based on the HTTP request type. The requested data is kept in req.body which can be used by the middleware for further processing.Â
BodyParser: How to install BodyParser using npm
BodyParser can easily be installed using the npm module. Check the command in the table below.
BodyParser Middleware |
npm install body-parser |
To ensure the body-parser has been installed successfully, check the version in the command prompt using the following command.
BodyParser Middleware |
npm version body-parser |
Now, using require and use functions, you can use body-parser in your Node.js application. Create a JS folder and run this file using the .node.
To include the body-pareser in your Node.js application, use the following command given below.
BodyParser Middleware |
var parser = require(‘body-parser’); |
BodyParser: Require and Use BodyParser
Check the following example to using body-parser to fetch the incoming JSON and URL encoded data from HTTP.Â
BodyParser Middleware |
const express = require(‘express’);
const bodyParser = require(‘body-parser’); const app = express(); // Use body-parser middleware to parse JSON and URL-encoded data app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); // Your routes and other middleware go here… // Start the server const port = 3000; app.listen(port, () => { Â Â console.log(`Server is running on port ${port}`); }); |
After passing the HTTP request, it is examined by the parser and then implemented accordingly. The data after parsing is made available in req.body for further processing. The req. body encloses the data passed in the middleware.
Also Read: Background Color HTML: How to Add & Change Background Color in HTML
BodyParser: HTML Form
Using HTTP, request data from the post method. Check the implementation example below.
HTML Form:
The HTML post method can easily be parsed using the body-parser. Check the form below.
BodyParser Middleware |
<!DOCTYPE html>
<html lang=”en”> <head> Â Â Â Â <meta charset=”UTF-8″> Â Â Â Â <meta name=”viewport” content=”width=device-width, initial-scale=1.0″> Â Â Â Â <title>Form Example</title> </head> <body> Â Â Â Â <form method=”post” action=”http://127.0.0.1:3000/formdata”> Â Â Â Â Â Â Â Â <input type=”text” name=”username” required> Â Â Â Â Â Â Â Â <input type=”password” name=”userpass” required> Â Â Â Â Â Â Â Â <button type=”submit”>Send</button> Â Â Â Â </form> </body> </html> |
Node.js server
BodyParser Middleware |
const express = require(‘express’);
const app = express(); const bodyParser = require(‘body-parser’); // Parse application/x-www-form-urlencoded app.use(bodyParser.urlencoded({ extended: false })); app.post(‘/formdata’, (req, res) => { Â Â Â Â console.log(req.body); Â Â Â Â res.json(req.body); }); const port = 3000; app.listen(port, () => { Â Â Â Â console.log(`Server is running on http://127.0.0.1:${port}`); }); |
BodyParser: Steps to Run the Program in Node.js
Follow the steps given here to run the BodyParser. First, install the express and body-parser using the following commands in the command prompt.
BodyParser Middleware |
npm install express
npm install body-parser |
After successful installation, now run the dot js file, consider index.js file using node index.js. To check the working open localhost on your browser, type the URL below.
BodyParser Middleware |
http://localhost:3000/ |
Learn Node.JS with PW Skills
Hurry! Enroll in our Full Stack Web Development Course to learn crucial technologies such as Node.js, Javascript, CSS, React, SQL and many others to start your career as a web developer. The course consists of more than 7+ industry-relevant projects.Â
Learn with the best industry experts. Get industry-recognized course completion certification, placement assistance and much more. Go to our official website at @pwskills.com
For Latest Tech Related Information, Join Our Official Free Telegram Group : PW Skills Telegram Group
BodyParser FAQs
What is a BodyParser in Node.js?
A body-parser is a Node.js library used to extract information from an incoming HTTP request. It produces the information in a processed form by passing it into a middleware.
How to install a BodyParser using npm?
To install body-parser using npm, type the following command in the command prompt: npm install body-parser.
Can we use express JSON() instead of a BodyParser?
JSON() is a built-in package that can easily parse JSON data in your Node.js application. BodyParser, on the other hand, had to be installed explicitly using npm.