Frontend and APIs: follow one request
Hello, and welcome to the second chapter! In the last lesson, you read a program that adds up the committee's payments. A real app puts that kind of work behind a screen with buttons. Let's follow what happens when someone presses one.
In this lesson, you'll learn:
- what the frontend is;
- how one program asks another for information;
- what an API is;
- why a page that looks right can still be wrong.
Why it matters: when something breaks, you'll be able to ask where it broke, instead of saying "the whole app is broken".
The part you can see
Imagine the committee has a web page. Member A opens it and presses a button: View total received.
The frontend is the part of an app that people see and use: the button, the total on the screen, and any message when something fails. It is the app's interface, the way people use it.
On the web, people use the frontend through a browser, such as Chrome or Firefox. Three languages work together in it:
- HTML sets out the page's content, such as the text and the button.
- CSS sets how it looks: colours, sizes and layout.
- JavaScript adds behaviour, such as what happens when you press the button.
You don't need to know these languages yet. Just know their jobs.
The committee could also have a phone app with a different design. The screen can change while the way the total is worked out stays the same.
Asking another computer for the total
The payment records are not stored on Member A's phone. They are kept on another computer. So when Member A presses the button, the frontend has to ask that computer for the total.
- The program that asks is the client. Here, that's the frontend.
- The program that answers is the server.
These are roles, not special machines. The same computer can be a client for one job and a server for another.
The question the client sends is a request. The answer is a response. On the web, requests and responses follow shared rules called HTTP.
Here is the whole trip, step by step:
- Member A presses View total received.
- The client sends a request to the server: "What is the total received?"
- The server checks the request and works out the total from its records.
- The server sends back a response: the total, or a message saying what went wrong.
- The frontend shows that response to Member A.
APIs: the agreed way to ask
For this to work, the client and the server must agree on how to ask and how to answer. That agreement is an API, short for application programming interface.
For our committee, the API would say:
- what the client can ask for (the total received);
- what it must send with its request;
- what the answer looks like, and what an error looks like.
The client doesn't need to know how the server stores the records. It only needs the API. That's what makes APIs so useful: each side can change how it works inside without breaking the other, as long as the agreement stays the same.
APIs are everywhere. They connect programs on different computers, and also programs on the same computer.
A page that looks right can still be wrong
Say the page shows 2,500 after a successful response. Good. But if the connection fails, the page should say something like "Couldn't load the total". It should never quietly show an old number as if it were up to date.
And a good-looking page tells you nothing about whether the server used the right records. What you see on the screen is only the last step of the trip.
When something goes wrong, ask three questions: What was requested? What answered? What was shown?
Check what you can now explain
Close the lesson and try these:
- Describe the five steps of the button press in your own words.
- Why doesn't the client need to know how the records are stored?
- Would a nicely coloured button prove that the total is correct? Why not?
In the next lesson, we'll look at the server's side: where the records live, and who is allowed to change them.