Full lesson
Explore the full explanation, examples, and visuals at your own pace.
Part 1
A single Add to cart click for product 42 has to cross from your browser to a server. On screen, the AddToCart component inside the browser sends a fetch POST to the POST /api/cart route on the server, carrying a small JSON body with the product id. That's the first hop.
AddToCart.jsx
The click runs handleClick, which sets status to adding. JSON.stringify turns productId and a quantity of one into a text body for the POST. For same-origin requests, the browser attaches the session cookie by default. Then await pauses until a response arrives, and res.ok decides added or error.
function AddToCart({ productId }) {
const [status, setStatus] = useState("idle");
async function handleClick() {
setStatus("adding");
const res = await fetch("/api/cart", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ productId, qty: 1 }),
});
setStatus(res.ok ? "added" : "error");
}
return <button onClick={handleClick}>Add to cart</button>;
}server.js (Express + node-postgres)
On the server, express.json has already parsed the body, and requireSession, which we're assuming here, sets req.user to 7. Validation returns 400 for a bad qty. Then the INSERT uses dollar-one through dollar-three placeholders, so the values travel separately from the SQL text.
app.post("/api/cart", requireSession, async (req, res) => {
const { productId, qty } = req.body;
if (!Number.isInteger(qty) || qty < 1) {
return res.status(400).json({ error: "bad qty" });
}
const { rows } = await db.query(
"INSERT INTO cart_items (user_id, product_id, qty) VALUES ($1, $2, $3) RETURNING id",
[req.user.id, productId, qty]
);
res.status(201).json({ id: rows[0].id });
});Hop 3: the query reaches Postgres
Now the query reaches Postgres. Notice the browser never talks to the database. Only the server does, over its own connection, sending the query text and the parameters separately. And user 7 isn't from the browser's JSON, which only carried product 42. The server adds it from the session.
The row that lands in cart_items
Postgres checks the constraints, then assigns the next id from its sequence. Here that's 901, sitting under existing rows 899 and 900. The row holds user 7, product 42, quantity 1. It's committed, and RETURNING hands that id back in the same round trip. These ids are illustrative.
Each hop translates the click
The click never travels as itself. It becomes a JSON body, then a parameterized query, then a row in cart_items. Same intent, four forms: event, JSON, SQL, row. That's why a bug usually lives in one translation, and once you know which, you know where to look.
The trip back, and where it breaks
Now the trip back. Postgres returns id 901 to the server, the server replies 201 Created, and in the browser res.ok flips the button to added. Failures show up at specific hops. No session gives a 401 at the first hop. A missing product trips a foreign key error at the INSERT, which becomes a 500 unless the handler catches it.
Each hop translates the click: event, JSON, query, row.
Each hop translates the click: event, JSON, query, row. Then the result travels back the same way. So when something breaks, don't guess. Check the Network tab first, then the server log, then the database, in hop order, and you'll find the boundary that failed.
