Course contents · Lesson 2 of 17

Frontend and Backend: How a Web App Works

How Apps Are Built · Lesson 2 of 17 · 8 min Foundations

You click Order on a shopping site, and a moment later the page says "Order confirmed" without reloading. Meanwhile, a stock count went down and your order was saved. This lesson explains the frontend and backend of a web app: what each part does, where it runs, and how one click travels through both.

The frontend: what runs on your screen

The frontend is the part of a web app that runs on your own device: everything you see, tap and type into. On a website it runs inside your browser. On a phone, it is the installed app, often built with the phone's own tools rather than web languages.

A website's frontend uses three languages:

  • HTML (HyperText Markup Language) gives the structure: headings, paragraphs, pictures, links and buttons.
  • CSS (Cascading Style Sheets) sets the looks: colours, fonts, spacing and layout.
  • JavaScript, the language you typed into the Console in What Is Software?, adds behaviour: it reacts to clicks and changes part of the page without reloading the rest.

One correction to the analogy: a frontend is delivered to you. When you open a site, your browser downloads its HTML, CSS and JavaScript files from a server and runs them on your computer, where you can read every line.

The backend and the database: kitchen and store room

The backend is code that runs on a server, the computer that answers requests over the internet, which you met in lesson 1 (Client and Server goes deeper). It is written in languages such as Python, Java, Go or JavaScript, and you never see its code, just as guests never see the kitchen.

The backend applies the business rules, the rules of how the service works. It logs you in by checking your password against a stored hash, a one-way scrambled fingerprint of it (Encryption vs Hashing). It works out prices, decides whether an item can still be sold and refuses requests that break the rules, such as viewing someone else's orders.

The backend needs data that lasts. A program's memory, the short-term workspace from lesson 1, is emptied when the program stops or the server restarts. So anything that must survive goes into a database: organised storage that keeps data for the long term and finds it quickly. What Is a Database? shows later how it is organised.

In the usual design, the browser never talks to the database; only the backend does, as only kitchen staff enter the store room. In the diagram, every arrow to the database starts at the backend.

Three cards in a row. The frontend runs in your browser or app, with HTML for structure, CSS for looks and JavaScript for behaviour; it sends a request to the backend and gets a response. The backend is code on a server that logs users in, works out prices and checks the stock; it reads and saves data in the database, which keeps users, products and stock, and orders for the long term. Under the cards are their restaurant partners: the dining room that guests see, the kitchen that prepares the food and the store room that keeps the ingredients. A banner says only the backend touches the database
Each part has one job, and only the backend touches the database.

One click, end to end

Follow one click through all three parts: the frontend asks, the backend decides and the database remembers. In the flowchart, the left lane is your device; the others are the shop's servers.

  1. You click Order, and JavaScript in the page notices.
  2. The page sends a request, a message asking the server to do something: "order one kettle". It travels using HTTP (HyperText Transfer Protocol), the language of browsers and servers, in its encrypted form, HTTPS (HTTP Secure).
  3. The backend checks who you are, reads the stock count from the database and works out the price itself.
  4. If a kettle is in stock, the backend saves the order in the database and lowers the stock by one. If not, this step is skipped.
  5. The backend sends a response, the answer to the request: "confirmed" or "sold out".
  6. JavaScript reads the response and updates just that part of the page.
Flowchart in three lanes: browser (frontend), server (backend) and database. Step 1 in the browser: you click Order on the kettle's page. Step 2: the page sends a request, order 1 kettle, to the server. Step 3 on the server: the backend checks the stock and works out the price, reading the stock from the database, which holds 4 kettles in this illustrative example. A decision asks in stock? If yes, step 4 in the database saves the order and the stock goes from 4 to 3, then step 5 follows; if no, it goes straight to step 5. Step 5: the response goes back, confirmed or sold out. Step 6 in the browser: the page shows the result with no full reload. A note says steps 3 and 4 happen out of your sight
The frontend asks and shows the answer; the backend decides.

The "aha": the real decisions, steps 3 and 4, happen out of your sight. Requests and responses get their own lessons, What Is an API? and REST APIs and JSON.

Why split an app this way?

Secrets stay on the server. Anything sent to the browser can be read, and changed, by its user. So the database password, the secret keys for taking payments and the pricing rules stay on the server. The frontend may check a form for quick feedback, but the backend checks again, and only its check counts.

Everyone shares one copy of the data. One backend and database serve every shopper on every device. That is why your laptop's basket appears on your phone, and why all shoppers see the same stock count, kept in one place.

Teams work in parallel. The two halves meet only through requests and responses. Once teams agree what those look like, one builds the screens while another builds the rules, and one backend can serve the website and the phone app.

In the table, notice the last column: the user can read everything in the browser, so nothing secret belongs there.

PieceRuns onIts jobUser can read it?
HTMLBrowserPage structureYes
CSSBrowserLooks and layoutYes
Frontend JavaScriptBrowserBehaviour, sending requestsYes
Business rulesServerPrices, stock checksNo
Password checksServerLogging users inNo
Database accessServerReading and saving dataNo

Check yourself

A page shows ₹2000. Someone edits it to ₹1 in their browser and clicks Buy. In a well-built shop, what do they pay?

₹2000. The on-screen number is only the frontend's copy, which anyone can change; the backend works out the price from its own rules and data.

A book you put in your basket on your laptop later shows up in the shop's phone app. Which part made that possible?

The backend and its database. The basket was saved on the shop's server, so every frontend you sign in with gets it from the same backend.

Why must the database password never appear in a site's JavaScript, even in a long, hard-to-read file?

Your browser downloads every frontend file, and anyone can read it in DevTools. Whoever found the password could reach the data directly, skipping the backend's rules.

Go deeper