Stop 11 · Connecting React to ExpressTopics · 1/10

How a MERN Request Flows End to End

Part of the MERN Stack Roadmap

In short

You have learned React, Express and MongoDB separately. The moment it all clicks is when you can trace one button click through every layer and back. That picture is also what interviewers want to hear.

A click in React sends an HTTP request to an Express route, which runs middleware, calls a controller, queries MongoDB through Mongoose and sends JSON back for React to render.

The jaadu dressed as a waiter carries an order slip from a dining table, through a kitchen door, past a chef and into a store room full of jars.

Think of it like this

Ordering at a restaurant

You (React) tell the waiter what you want. The waiter (HTTP request) walks to the kitchen door, where a manager checks your table number (middleware). The chef (controller) takes ingredients from the store room (MongoDB via Mongoose), cooks, and the waiter brings the plate back (JSON). Your table updates when the food arrives (re-render).

Watch it happen

Saving a new note

React
Express
Middleware
Controller
MongoDB

POST /api/notes

1/6Rahul clicks Save. React sends the title and body as JSON.

The big ideas

What you need to know

Route

A method plus a path, like POST /api/notes, decides which handler runs.

Middleware chain

Functions like express.json(), cors and auth run in order before the controller. Each calls next() or ends the response.

Model talks to MongoDB

The controller uses a Mongoose model. It never builds raw database connections per request.

React updates state

The response becomes new state. React re-renders, and the user sees the change.

How it works

Debugging by layer

  1. 1

    Browser

    Is the click handler firing? Add a console.log or check React DevTools.

  2. 2

    Network tab

    Did the request go out? Check the URL, method, payload and status code.

  3. 3

    Express log

    Did the route match? Log method and path with a tiny middleware or morgan.

  4. 4

    Controller

    Is the data valid? Log req.body and any thrown error.

  5. 5

    Database

    Check the document in Compass or Atlas to confirm it saved.

See it in code

Walk through the code

js
1// React: src/components/NewNote.jsx2async function saveNote(note) {3  const res = await fetch("/api/notes", {4    method: "POST",5    headers: { "Content-Type": "application/json" },6    body: JSON.stringify(note),7  });8  if (!res.ok) throw new Error("Could not save note");9  return res.json();10}11 12// Express: server/routes/notes.js13router.post("/", requireAuth, async (req, res, next) => {14  try {15    const note = await Note.create({16      title: req.body.title,17      body: req.body.body,18      owner: req.user.id,19    });20    res.status(201).json(note);21  } catch (err) {22    next(err);23  }24});

1/5React sends a POST with a JSON body and the right Content-Type.

Both ends of one request. The route is mounted with app.use("/api/notes", notesRouter).

Save yourself the pain

Common mistakes

Oops

Forgetting app.use(express.json()) and getting req.body as undefined.

Do this instead

Add express.json() before your routes so JSON bodies are parsed.

Oops

Sending JSON from React without the Content-Type header.

Do this instead

Set "Content-Type": "application/json", or Express will not parse the body as JSON.

Oops

Blaming React when the real issue is a 500 in Express.

Do this instead

Open the Network tab first. The status code tells you which layer to look at.

Your mission

Trace it yourself

~30 min

Quick check

React gets a 400 response when saving a note. Which layer most likely rejected it?

Go deeper

  1. 1Trace one request in your own app from the button click to the database and back.
  2. 2Add logs at each layer and read them in order.
  3. 3Draw the flow as a diagram and keep it in your project README.

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