req.body contains the parsed body of a request. It's undefined by default and gets
populated only once a body-parsing middleware — express.json() or express.urlencoded()
— has processed the request.
Syntax
req.body
Example
const express = require("express");
const app = express();
const PORT = 4700;
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.post("/feedback", (req, res) => {
console.log(req.body);
res.send("Feedback received");
});
app.listen(PORT, () => console.log(`Listening on ${PORT}`));
Sending { "rating": 5, "comment": "Loved it" } populates req.body with that object.
Handling large bodies
app.use(express.json({ limit: "5mb" }));
app.use(express.urlencoded({ extended: true, limit: "5mb" }));
Common use cases
- Reading form submissions (login, registration).
- Handling JSON payloads from a frontend or another API.
- Reading fields from multi-part uploads (typically paired with
multer).