Stop 03 · Express.js BasicsTopics · 7/8

Parsing JSON Request Bodies

Part of the MERN Stack Roadmap

In short

When your React form sends a new product to the API, the data arrives as raw text. One line of middleware turns it into a JavaScript object in req.body. Forget it and req.body is undefined.

When React sends JSON in a POST or PATCH request, express.json() middleware parses it into req.body. Without it, req.body is undefined.

The jaadu sits at a post office counter, cutting open a parcel and laying its items out neatly on the desk for a waiting officer.

Think of it like this

Opening the parcel at the post office counter

A sealed parcel arrives at the counter with a label saying "contains: JSON". The clerk checks the label, opens it and lays the items neatly on the desk for the officer. express.json() is that clerk: it reads the Content-Type, unpacks the body and hands you a ready object.

Watch it happen

A POST request with a JSON body

React form
express.json()
Route handler

POST /api/products

1/4fetch sends the body as a string with Content-Type: application/json.

The big ideas

What you need to know

express.json()

Built into Express. Register it once with app.use(express.json()) before your routes.

Content-Type matters

The parser only runs when the request says Content-Type: application/json. React must set that header.

Size limit

The default limit is 100kb. Raise it with express.json({ limit: "1mb" }) only if you truly need bigger bodies.

Bad JSON is an error

Malformed JSON makes the parser call next(err) with status 400. Your error middleware should reply clearly.

See it in code

Walk through the code

js
1import express from "express";2 3const app = express();4app.use(express.json({ limit: "100kb" }));5app.use(express.urlencoded({ extended: true }));6 7app.post("/api/products", (req, res) => {8  const { name, price } = req.body;9  if (!name || typeof price !== "number") {10    return res.status(400).json({ message: "name and numeric price are required" });11  }12  res.status(201).json({ id: Date.now(), name, price });13});14 15// React side:16// fetch("/api/products", {17//   method: "POST",18//   headers: { "Content-Type": "application/json" },19//   body: JSON.stringify({ name: "Notebook", price: 60 }),20// });

1/4Register the JSON parser before any route that reads req.body.

Parse JSON once at the top, then every route can read req.body safely.

Save yourself the pain

Common mistakes

Oops

Adding app.use(express.json()) after the routes.

Do this instead

Middleware runs in order. Put the parser above your routes, or req.body will be undefined in them.

Oops

Sending body: { name: "Pen" } from fetch without JSON.stringify.

Do this instead

fetch needs a string. Use JSON.stringify and set Content-Type: application/json.

Oops

Installing the old body-parser package for JSON.

Do this instead

Express 4.16+ and Express 5 include express.json(), so no extra package is needed.

Your mission

Echo a JSON body

~15 min

Quick check

A POST route logs req.body as undefined. What is the most likely cause?

Go deeper

  1. 1Add app.use(express.json()) before your routes and log req.body in a POST handler.
  2. 2Set a body size limit so huge payloads are rejected early.
  3. 3Send a request without the Content-Type: application/json header and see what happens.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge