- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Express.js – express.urlencoded() Method
express.urlencoded() is a built-in middleware in Express.js. The main objective of this method is to parse the incoming request with urlencoded payloads and is based upon the body-parser.
This method returns the middleware that parses all the urlencoded bodies.
Syntax
express.urlencoded( [options] )
Parameters
Following are the different options available with this method −
options −
inflate − This enables or disables the handling of the deflated or compressed bodies. Default: true
limit − This controls the maximum size of the request body.
extended − This option allows to choose between parsing the URL encoded data with the queryString Library or the qs Library.
type − This determines the media type for the middleware that will be parsed.
parameterLimit − This option controls the maximum number of parameters that are allowed in the URL-encoded data.
Example 1
Create a file with the name "urlencoded.js" and copy the following code snippet. After creating the file, use the command "node urlencoded.js" to run this code as shown in the example below −
// express.urlencoded() Demo Example // Importing the express module var express = require('express'); // Initializing the express and port number var app = express(); var PORT = 3000; // Using the express.text middleware // to read encoded string app.use(express.text()); // Reading content-type app.post('/', function (req, res) { console.log(req.body) res.end(); }) // Listening to the port app.listen(PORT, function(err){ if (err) console.log(err); console.log("Server listening on PORT", PORT); });
Before hitting the API endpoint, set the following 2 properties −
Set the content-type as application/x-www-form-urlencoded.
Pass the following body in the POST request: {"name": "TutorialsPoint"}
Output
C:\home
ode>> node urlencoded.js Server listening on PORT 3000 [Object: null prototype] { '{
"title": "tutorialspoint"
}': '' }
Example 2
Let's take a look at one more example.
// express.urlencoded() Demo Example // Importing the express module var express = require('express'); // Initializing the express and port number var app = express(); var PORT = 3000; // Commenting the express.urlencoded middleware // app.use(express.urlencoded({extended:false})); // Reading content-type app.post('/', function (req, res) { console.log(req.body) res.end(); }) // Listening to the port app.listen(PORT, function(err){ if (err) console.log(err); console.log("Server listening on PORT", PORT); });
Before hitting the API endpoint, set the following 2 properties −
Set the content-type as application/x-www-form-urlencoded in headers.
Pass the following body in the POST request: {"name": "TutorialsPoint"}
Output
C:\home
ode>> node urlencoded.js Server listening on PORT 3000 undefined