Joining the two halves
You already have a frontend (React) and a backend (Node/Express with an API). Full-stack is about connecting them: the frontend makes HTTP requests to the API and shows the response.
fetch
In the browser, fetch makes HTTP requests. It returns a promise:
async function loadUsers() {
const response = await fetch("/api/users");
const users = await response.json();
return users;
}
fetch(url)returns a promise with the response.response.json()(also a promise) extracts the body as an object.- To send data (POST), you pass options: method, headers, and
body.
The full cycle
- The user does something in the frontend (clicks a button).
- The frontend calls the API with
fetch. - The backend processes, queries the database, and responds with JSON.
- The frontend receives the response and updates the interface (React state).
In the exercise we give you an
apifunction that simulates the backend. You write the logic that uses it and processes the response.