Parsing JSON Bodies
6 min read
Parsing JSON Bodies
By default, Express does not parse the request body. To read JSON sent by a client, you must apply the built-in express.json() middleware. Once applied, the parsed object is available on req.body.
js
import express from "express";
const app = express();
// Apply BEFORE your route handlers
app.use(express.json());
app.post("/echo", (req, res) => {
// req.body is now a JS object
res.json(req.body);
});Express also ships with express.urlencoded({ extended: true }) for parsing HTML form submissions, and you can add multer for multipart/form-data (file uploads).
If req.body is undefined, the most common cause is forgetting to place app.use(express.json()) before your routes. Order matters.
Ready to test yourself?
Practice Express— quiz & coding exercises