The Wayback Machine - https://web.archive.org/web/20240907013133/https://www.geeksforgeeks.org/express-js-express-urlencoded-function/
Open In App

Express express.urlencoded() Function

Last Updated : 08 Jan, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The express.urlencoded() function is a built-in middleware function in Express. It parses incoming requests with URL-encoded payloads and is based on a body parser.

Syntax: 

express.urlencoded( [options] )

Parameter: The options parameter contains various properties like extended, inflate, limit, verify, etc.

Return Value: It returns an Object.

Steps to create the application:

Step 1: You can install this package by using this command.

npm install express

Step 2: After installing the express module, you can check your express version in the command prompt using the command.

npm version express

Project Structure:

NodeProj

The updated dependencies in package.json file will look like:

"dependencies": {
"express": "^4.18.2",
}

Example 1: Below is the code example for express.urlencoded() Function

javascript




const express = require('express');
const app = express();
const PORT = 3000;
 
app.use(express.urlencoded({ extended: false }));
 
app.post('/', function (req, res) {
    console.log(req.body);
    res.end();
});
 
app.listen(PORT, function (err) {
    if (err) console.log(err);
    console.log("Server listening on PORT", PORT);
});


Steps to run the program:  

Run the index.js file using the below command: 

node index.js

Console Output:  

Server listening on PORT 3000

Browser Output:  Now make a POST request to http://localhost:3000/ with header set to ‘content-type: application/x-www-form-urlencoded’ and body {“name”:”GeeksforGeeks”}, then you will see the following output on your console: 

Example 2: Below is the code example for express.urlencoded() Function

javascript




const express = require('express');
const app = express();
const PORT = 3000;
 
// Without this middleware
// app.use(express.urlencoded({extended:false}));
 
app.post('/', function (req, res) {
    console.log(req.body);
    res.end();
});
 
app.listen(PORT, function (err) {
    if (err) console.log(err);
    console.log("Server listening on PORT", PORT);
});


Steps to run the program:

node index.js

Now make a POST request to http://localhost:3000/ with header set to ‘content-type: application/x-www-form-urlencoded’ and body {“title”:”GeeksforGeeks”}, then you will see the following output on your console: 

Output:

Server listening on PORT 3000
undefined

We have a complete list of Express express module methods, to check those please go through this Express.js express() function Complete Reference article.



Previous Article
Next Article

Similar Reads

Express.js express.text() Function
The express.text() function is a built-in middleware function in Express. It parses the incoming request payloads into a string and is based on body-parser. Syntax: express.text( [options] ) Parameter: The options parameter contains various properties like defaultCharset, inflate, limit, verify, etc. Return Value: It returns an Object. Installation
2 min read
Express.js express.raw() Function
The express.raw() function is a built-in middleware function in Express. It parses incoming request payloads into a Buffer and is based on body-parser. Syntax: express.raw( [options] ) Parameter: The options parameter contains various properties like inflate, limit, type, etc. Return Value: It returns an Object. Installation of the express module:
2 min read
Express.js express() function Complete Reference
Express.js is a small framework that works on top of Node.js web server functionality to simplify its APIs and add helpful new features. It makes it easier to organize your application’s functionality with middleware and routing. Express.js express() Methods:Method Description Express.js express.json() Function The express.json() function is a buil
1 min read
Express express.Router() Function
The express.Router() function is used to create a new router object. This function is used when you want to create a new router object in your program to handle requests.  Syntax: express.Router( [options] )Optional Parameters: case-sensitive: This enables case sensitivity.mergeParams: It preserves the req.params values from the parent router.stric
2 min read
Express express.static() Function
The express.static() function is a built-in middleware function in Express. It serves static files and is based on serve-static.  Syntax: express.static(root, [options])Parameters: The root parameter describes the root directory from which to serve static assets.  Return Value: It returns an Object.  Steps to create the application:Step 1: Initiali
2 min read
Express JS express.json() Function
The express.json() function is a built-in middleware function in Express. It parses incoming requests with JSON payloads and is based on body-parser. It’s a critical component for handling JSON data in RESTful APIs and web applications, allowing the server to automatically parse JSON data from the request body. Syntax:express.json( [options] )Param
3 min read
Purpose of the express.Router middleware in Express
express.Router() in ExpressJS is like creating a mini-app within your main app. It helps organize routes and related functionalities, making it easier to manage different parts of your web application separately. express.Router() acts as a path organizer in ExpressJS, helping to structure and manage routes efficiently within a web application. Purp
1 min read
Explain the purpose of the express.static middleware in Express JS
The express.static middleware in ExpressJS simplifies serving static files like HTML, CSS, and images, improving performance and enhancing user experience in web applications. Purpose of the express.static middleware in ExpressJS:Serves Static Files: express.static middleware serves static files like HTML, CSS, JavaScript, and images from a specifi
2 min read
What is the purpose of the express-validator middleware in Express JS?
The express-validator middleware is a powerful tool for validating and sanitizing incoming request data in ExpressJS applications. It helps ensure that user input meets specified criteria, preventing security vulnerabilities and data corruption. With built-in error handling and customizable rules, express-validator seamlessly integrates with Expres
2 min read
How to Expire Session After 1 Min of Inactivity in express-session of Express.js ?
To automatically expire sessions after 1 minute of inactivity in an Express.js application using express-session, configure the session settings with the cookie.maxAge and implement session touch logic to track activity. ApproachTo expire the session after 1 min of inactivity in the express-session of Express.js we use expires: 60000 in the middlew
2 min read
What is express-session middleware in Express?
In the Express web application, the express-session middleware is mainly used for managing the sessions for the user-specific data. In this article, we will see the use of express-session middleware for session management in Express with practical implementation. PrerequisitesNode JSExpress JSTable of Content What is express-session Middleware?Step
2 min read
Express.js | app.disable() Function
The app.disable() function is used to set the boolean setting name to false. It is basically the shorthand for the app.set(name, false). So instead we can use app.disable(name) function to set the false boolean value to some system Express.js settings. Syntax: app.disable(name) Installation of the express module: You can visit the link to Install t
1 min read
Express.js | app.path() Function
The app.path() function returns the canonical path of the app as a string. When mounting advanced apps, the behavior of this approach can become extremely complicated. In most cases, it is better to use req.baseUrl to obtain the app's canonical path. Installation of the express module: You can visit the link to Install the express module. You can i
2 min read
Express.js | app.set() Function
The app.set() function is used to assign the setting name to value. You may store any value that you want, but certain names can be used to configure the behavior of the server. Syntax: app.set(name, value) Installation of the express module: You can visit the link to Install the express module. You can install this package by using this command. n
2 min read
Express.js | app.route() Function
The app.route() function returns an instance of a single route, which you can then use to handle HTTP verbs with optional middleware. Use app.route() to avoid duplicate route names (and thus typo errors). Syntax: app.route( path ) Installation of the express module: You can visit the link to Install the express module. You can install this package
2 min read
Express.js | app.put() Function
The app.put() function routes the HTTP PUT requests to the specified path with the specified callback functions. Syntax: app.put(path, callback [, callback ...]) Arguments: Path: The path for which the middleware function is invoked and can be any of the:A string represents a path.A path pattern.A regular expression pattern to match paths.An array
2 min read
Express.js | app.METHOD() Function
The app.METHOD() function is used to route an HTTP request, where METHOD is the HTTP method of the request, such as GET, PUT, POST, and so on, in lowercase. Thus, the actual methods are app.get(), app.post(), app.put(), and so on. Syntax: app.METHOD(path, callback [, callback ...]) Parameters: Path: The path for which the middleware function is inv
2 min read
Express.js | app.param() Function
The app.param() function is used to add the callback triggers to route parameters. It is commonly used to check for the existence of the data requested related to the route parameter. Syntax: app.param([name], callback) Parameters: name: It is the name of the parameter or an array of them.callback: It is a function that is passed as a parameter. In
2 min read
Express.js res.location() Function
The res.location() function is used to set the response Location HTTP header to the path parameter which is being specified. Basically, it is used to set the response header. It doesn't end the response, after using it you can write a response body if you want to write. Syntax: res.location( path ) Parameter: path: It refers to the URL which is spe
2 min read
Express.js | app.all() Function
The app.all() function is used to route all types of HTTP requests. Like if we have POST, GET, PUT, DELETE, etc, requests made to any specific route, let's say /user, so instead of defining different APIs like app.post('/user'), app.get('/user'), etc, we can define single API app.all('/user') which will accept all type of HTTP request. Syntax: app.
2 min read
Express.js | app.render() Function
The app.render() function is used to render the HTML of a view via the callback function. This function returns the HTML in the callback function. Syntax: app.render(view, [locals], callback) Parameters: view: It is the name of the HTML page which is to be rendered.locals: It is an optional parameter that is an object containing local variables for
2 min read
Express.js | app.get() Function
The app.get() function returns the value name app setting. The app.set() function is used to assign the setting name to value. This function is used to get the values that are assigned. Syntax: app.get(name) Installation of the express module: You can visit the link to Install the express module. You can install this package by using this command.
1 min read
Express.js | app.enabled() Function
The app.enabled() function is used to return the bool values of the setting name. It returns true if the setting name is enabled and returns false if the setting name is not enabled. Syntax: app.enabled(name) Installation of the express module: You can visit the link to Install the express module. You can install this package by using this command.
1 min read
Express.js | app.disabled() Function
The app.disabled() function is used to return the bool values of the setting name. It returns true if the setting name is disabled and returns false if the setting name is not disabled. Syntax: app.disabled(name) Installation of the express module: You can visit the link to Install the express module. You can install this package by using this comm
1 min read
Express.js | app.enable() Function
The app.enable() function is used to set the boolean value i.e. name to true. It is basically the shorthand for the app.set(name, true) or app.set(name, false). So instead we can use app.enable(name) function to set boolean values to some system Express.js settings. Syntax: app.enable(name) Installation of the express module: You can visit the link
1 min read
Express.js | res.format() Function
The res.format() function performs content negotiation on the Accept HTTP header on the request object if it is present. This function checks the Accept HTTP request header and then invokes the corresponding handler depending on the Accept value. Syntax: res.format(object) Installation of the express module: You can visit the link to Install the ex
2 min read
Express.js res.clearCookie() Function
The res.clearCookie() function is used to clear the cookie specified by name. This function is called for clearing the cookies which as already been set. For example, if a user cookie is set, then it can be cleared using this function. Syntax: res.clearCookie(name, [ options ]) Parameters: Name: It is the name of the cookie which is to be cleared.
2 min read
Express.js | router.use() Function
The router.use() function uses the specified middleware function or functions. It basically mounts middleware for the routes which are being served by the specific router. Syntax: router.use( path, function ) Parameters: Path: It is the path to this middleware, if we can have /user, now this middleware is called for all API's having /user of this r
2 min read
Express.js res.sendStatus() Function
The res.sendStatus() function is used to set the response HTTP status code to statusCode and send its string representation as the response body. Syntax: res.sendStatus( statusCode ) Parameter: The statusCode parameter describes the HTTP status code. Returns: It returns an Object. Installation of the express module: You can visit the link to Instal
2 min read
Express.js res.append() Function
The res.append() function appends the specified value to the HTTP response header field and if the header is not already set then it creates the header with the specified value. Syntax: res.append(field [, value]) Parameter: The field parameter describes the name of the field that need to be appended and the value parameter can be a string or an ar
2 min read