Express express.urlencoded() Function (original) (raw)

Last Updated : 12 Apr, 2025

The express.urlencoded() middleware in Express.js is used to parse URL-encoded form data, making it accessible as a JavaScript object in req.body. It's essential for handling form submissions in application/x-www-form-urlencoded format.

**Syntax

app.use( express.urlencoded({ extended: true, inflate: true, limit: "1mb", parameterLimit: 5000, type: "application/x-www-form-urlencoded", }) );

How express.urlencoded Works?

The express.urlencoded() middleware processes URL-encoded form data, converts it into a JavaScript object, and makes it available via req.body.

  1. First it processes form data sent via POST requests with application/x-www-form-urlencoded encoding.
  2. Then it converts the incoming data into a JavaScript object and makes it accessible through req.body.
  3. When { extended: true } is set, it supports nested objects and arrays using the qs library.
  4. It includes security options like limit to restrict body size and parameterLimit to prevent excessive form fields.
  5. It only parses requests with Content-Type: application/x-www-form-urlencoded, ignoring other data formats.

**Now lets understand express.urlencoded with an example

JavaScript `

const express = require('express') const app = express() const PORT = 3000 app.use(express.urlencoded({ extended: true })) app.get('/login', (req, res) => { res.send('

') }) app.post('/login', (req, res) => { console.log(req.body) res.send('data has been recieved by the server') }) app.listen(PORT, () => { console.log('Server is running on localhost://3000') })

`

**Output

Use cases of express.urlencoded

Conclusion

The express.urlencoded() middleware is crucial for handling application/x-www-form-urlencoded form submissions. It simplifies processing form data by converting it into a JavaScript object, which is easily accessible via req.body. This functionality is especially useful for login forms, registration pages, and other web forms that send user input to the server.