express.urlencoded() parses form data sent with Content-Type: application/x-www-form-urlencoded and makes it available as an object on req.body.
Syntax
app.use(
express.urlencoded({
extended: true,
inflate: true,
limit: "1mb",
parameterLimit: 5000,
})
);
extended: true— allows nested objects/arrays via theqslibrary.inflate— auto-decompresses gzip/deflate bodies.limit— caps request body size.parameterLimit— caps the number of form fields.
Example: a simple sign-up form
const express = require("express");
const app = express();
const PORT = 4500;
app.use(express.urlencoded({ extended: true }));
app.get("/signup", (req, res) => {
res.send(
"<form method=POST action=/signup><input name=email><input name=age type=number><button>Join</button></form>"
);
});
app.post("/signup", (req, res) => {
console.log(req.body);
res.send("Signup data received");
});
app.listen(PORT, () => console.log(`Listening on ${PORT}`));
Submitting the form logs the parsed email and age fields on the server.
Where it's commonly used
- Login and sign-up forms.
- Contact or feedback forms.
- Checkout/order forms in e-commerce apps.
- Survey responses submitted via HTML forms.
- Profile-update forms in dashboards.