36 36. What Happens on the Server: Back Ends, Databases, and APIs
The browser shows the page, but much of the important work happens on servers you never see: remembering who you are, storing your orders, enforcing the rules. This page explains static vs. dynamic sites, what back-end code does, where data lives (databases), how logging in works, and how programs talk to each other (APIs).
36.1 Static vs. dynamic websites
- A static site sends files that were prepared ahead of time - the same HTML, CSS and JavaScript for every visitor. A portfolio, a restaurant menu or the hello-site from the previous page are static. (This book is too: its pages were turned into HTML files before you visited.) Static sites are simple, fast and cheap to host, and JavaScript can still make them interactive.
- A dynamic site runs code on the server for each request to build a response for that user at that moment: your order history, your bank balance, search results.
Two people can open the very same URL, such as an online store’s “Your orders” page, and see completely different content. That takes three things a static site doesn’t have: server-side code, a database, and a way to know who is asking (logging in).
36.2 Server-side code
Back-end code (or server-side code) runs on the server, not in the browser. It can be written in many languages: JavaScript or TypeScript (running on Node.js), Python, Java, C#, PHP, Ruby, Go and others.
When a request arrives - say, “sign me up for event 10” - the back end typically:
- reads the request (method, path, data, cookies);
- checks who the user is and what they are allowed to do;
- applies the business rules (“an event can’t have more than 30 sign-ups”, “members can’t sign up twice”);
- reads and writes the database;
- maybe calls other services (send a confirmation email, charge a card);
- sends back a response: a whole HTML page, or just data (usually JSON) for the front end’s JavaScript to display.
Because users never see back-end code, it is the safe place for secrets such as database passwords and API keys. Front-end JavaScript is the opposite: anyone can read it in DevTools.
36.3 Databases
A database stores data permanently and lets many users read and change it at the same time, safely and quickly. A database server program (for example PostgreSQL) manages it. Spreadsheets struggle with what apps need: thousands of users at once, millions of rows, rules that are always enforced, and control over who may see what.
36.3.1 Relational (SQL) databases
A relational database keeps data in tables - like Excel sheets, with rows and columns - and links tables through ID numbers. You ask it questions in SQL (Structured Query Language). Popular examples: PostgreSQL, MySQL, Microsoft SQL Server, and SQLite (a whole database in a single file, common in small apps and phones).
An events table:
| id | title | date | capacity |
|---|---|---|---|
| 10 | Resume workshop | 2026-10-20 | 30 |
| 11 | Chess night | 2026-10-22 | 16 |
| 12 | Museum trip | 2026-11-05 | 40 |
A SQL query that lists the bigger events, in date order:
SELECT title, date
FROM events
WHERE capacity >= 30
ORDER BY date;The result is a small table:
| title | date |
|---|---|
| Resume workshop | 2026-10-20 |
| Museum trip | 2026-11-05 |
Sign-ups would live in a separate signups table with two columns, member_id and event_id: the row (1, 10) means “member 1 signed up for event 10”. Linking tables by IDs avoids repeating each member’s name and email on every sign-up.
36.3.2 Document (NoSQL) databases
A document database stores each record as a flexible, JSON-like document that can contain lists and nested data, and records don’t all need the same fields. Examples: MongoDB and Google’s Firebase Firestore. Relational databases are the usual default for business data; the database service used in this book’s tutorial, Supabase, is built on PostgreSQL.
36.3.3 Why the browser doesn’t talk to the database directly (usually)
To connect to a database you need its password, and anything in front-end code is visible to everyone. If the browser held the database password, any visitor could read or delete all the data. So the browser asks the back end, the back end checks who is asking and what they may do, and only then runs the query.
The exception: services such as Supabase and Firebase let front-end code read and write the database through their API using a key that is meant to be public. That is safe only because rules stored on the server - Supabase’s Row Level Security policies or Firebase’s Security Rules - decide exactly what each user may see and change. Without those rules, the data is exposed.
36.5 APIs: programs talking to programs
An API (Application Programming Interface) is a defined way for one program to ask another program for data or actions. Think of a restaurant menu: it lists what you can order and how to ask for it, without telling you how the kitchen works.
Web APIs use HTTP. A program sends a request to a URL, called an endpoint, and gets back data instead of a web page - almost always in JSON (JavaScript Object Notation), a simple text format for data:
{
"id": 10,
"title": "Resume workshop",
"date": "2026-10-20",
"capacity": 30,
"spotsLeft": 12,
"tags": ["career", "workshop"],
"online": false
}Curly braces { } hold an object made of "name": value pairs; square brackets [ ] hold a list; text goes in double quotes; numbers, true, false and null don’t.
Who calls APIs? Your own front end (the page’s JavaScript asks your back end for the list of events), mobile apps, and your back end itself when it uses other companies’ services such as payments or AI.
36.5.1 REST in one paragraph
Most web APIs follow a style called REST: each kind of thing (a resource, such as events) gets a URL, and the HTTP method says what to do with it. The four basic actions - Create, Read, Update, Delete - are often called CRUD:
| Request | Meaning |
|---|---|
GET /api/events |
Read: list all events |
GET /api/events/10 |
Read: get event 10 |
POST /api/events |
Create: add a new event (its data goes in the request body) |
PATCH /api/events/10 |
Update: change some fields of event 10 (PUT replaces it entirely) |
DELETE /api/events/10 |
Delete: remove event 10 |
36.6 The typical “stack”
Put together, most web apps look like this:
+------------------+ +------------------------------+
| Browser | HTTPS | Back end (your server code) |
| front end: | <-------> | - who is this? (auth) |
| HTML, CSS, JS | JSON | - business rules |
+------------------+ +------------------------------+
| |
v v
+----------------+ +-----------------------+
| Database | | Third-party APIs |
| (PostgreSQL) | | payments, email, AI |
+----------------+ +-----------------------+
The particular technologies chosen for each layer are called the stack - for example, “Next.js and Supabase, hosted on Vercel”. The next pages cover each layer: frameworks (Web Frameworks and Meta-Frameworks), third-party services (APIs, API Keys, and Third-Party Services) and hosting (Hosting and Deployment).
Practice Questions
Easy
Question 1 · Multiple choice
Which of these requires a dynamic site?
- A restaurant’s menu page
- A student’s portfolio
- A page showing your current bank balance
- A company’s “About us” page
Question 2 · Classify
Label each situation as authentication or authorization.
- Entering your password on the login page
- Only officers see the “Delete event” button
- Clicking “Log in with Google”
- You can view your own orders but not anyone else’s
Question 3 · Short answer
What is a database? Name one relational database and one document database.
Question 4 · True or false
It is safe to put the database password in the front-end JavaScript as long as the file has an unusual name.
Question 5 · Multiple choice
What is an API?
- A type of database
- A defined way for one program to request data or actions from another
- A programming language for servers
- The part of a URL after the
?
Question 6 · Short answer
What is JSON, and what do { } and [ ] mean in it?
Medium
Question 7 · Read the JSON
Using the event JSON shown on this page: How many people can still sign up? How many tags does the event have? Is it online?
Question 8 · Predict the output
Using the events table on this page, what does this query return?
SELECT title
FROM events
WHERE date < '2026-11-01'
ORDER BY capacity;Question 9 · Matching
Match each REST request with what it does.
| Request | Action | |
|---|---|---|
1. GET /api/events/7 |
A. Delete event 7 | |
2. POST /api/events |
B. Change some fields of event 7 | |
3. PATCH /api/events/7 |
C. Get event 7 | |
4. DELETE /api/events/7 |
D. Create a new event |
Question 10 · Scenario
Which status code fits each case, 401 or 403? (a) A visitor who is not logged in requests /my-events. (b) A logged-in member tries to delete an event, which only officers may do.
Question 11 · Short answer
HTTP “has no memory”. How does a site still know you are logged in when you click from page to page?
Question 12 · Ordering
Put the back end’s steps for “sign me up for event 10” in a sensible order: (a) save the sign-up in the database, (b) check who the user is, (c) send back a response, (d) check that the event isn’t full, (e) send a confirmation email.
Hard
Question 13 · Scenario
A classmate’s app shows “Spots left: 3” and hides the sign-up button when an event is full - but only in the front-end JavaScript. The back end saves any sign-up it receives. What can go wrong, and where should the rule be enforced?
Question 14 · Scenario
A Supabase app’s front-end code contains a key that anyone can see in DevTools. A teammate panics. When is this acceptable, and what must be true for the data to be safe?
Question 15 · Long answer
A club tracks sign-ups in one big spreadsheet: each row repeats the member’s name, email and phone, plus the event’s title, date and room. Explain why a relational design with three tables (members, events, signups) is better.
Question 16 · Compare
Your team must choose between a relational database (PostgreSQL) and a document database (MongoDB) for a club app with members, events and sign-ups. Give one argument for each, and say which you would pick.
Question 17 · Scenario
A website shows your name at the top of every page. Using the terms session, cookie and database, explain how the server knows which name to put there on each request.
Question 18 · Long answer
Pick an app you use (for example a food-delivery or ride-sharing app). Sketch its stack: what runs in the front end, what the back end does, what the database stores, and which third-party APIs it probably uses.