Lesson 2 of 5
How a web application works
Listen to an AI summary of How a web application works
What is a web application
A web application is an application that people use through a browser. An online shop is a web application. The lesson gives a simplified view of how a web application works. A real one also depends on parts the lesson leaves out, such as DNS, which finds the server, and TCP/IP, which carries the messages.
A customer of the sports shop called Sam has bought a hoodie and wants to see the order.
Where the work happens
A browser, a server and a database share the work of the sports shop. The browser runs on Sam's phone. The browser is a client, the program a person uses to reach a web application. A phone app can be a client too.
A server is a computer that provides a service to other computers, such as sending out a website's pages. The sports shop's server runs the code of the sports shop.
A database is an organised store of data that software can search and change. The sports shop's database holds every item, every account and every order. Only the server reads the database.
Requests and responses
A web application works like a paper order form handed in at the counter of a shop. A customer fills in the form and hands the form over the counter. The person behind the counter fetches the item from the stock room and hands the item back.
A browser and a server send messages in place of paper. A request is a message that a browser sends to a server to ask for something. A response is the message that a server sends back to answer a request.
Each part of the shop stands for one part of a web application.
- The customer
- The browser.
- The paper order form
- The request.
- The person behind the counter
- The server.
- The stock room
- The database.
- The item handed back
- The response.
The five steps of loading a page
Sam clicks a link named Order 1042. The link holds a URL. A URL is the address of a page on the web, such as https://shop.example/orders/1042.
HTTP is the set of rules that a browser and a server follow to send web pages. Early websites sent every page with HTTP as plain text, which anyone on the way could read. HTTPS is HTTP wrapped in encryption, scrambling that only the browser and the server can undo. The https at the start of the URL says the page is sent with HTTPS, and the S stands for secure.
Five steps follow the click, in this order:
- The browser sends a request to the server for the page at the URL.
- The server reads the request and runs the sports shop's code for an order page.
- The code asks the database for order 1042 with a line of SQL, such as
SELECT * FROM orders WHERE id = 1042. The database sends the order back. - The code builds a page from the order, and the server sends the page back in a response.
- The browser reads the response and draws the page on the screen.
1.Browser
sends a request
- request
2.Server
runs the code
- SQL
3.Database
finds order 1042
- the order
4.Server
builds the page
- response
5.Browser
draws the page
What is HTML
The response to Sam's request holds a page written in HTML. HTML is the language a web page is written in. The browser reads the HTML to draw the page.
HTML describes what is on a page, such as a heading, a paragraph or a link. HTML is not a programming language, because HTML cannot make a decision or repeat a step.
Each piece of the page starts with a name in angle brackets, such as <h1>. The same name with a slash, such as </h1>, ends the piece. <h1> marks a heading, <p> marks a paragraph and <a> marks a link. The href in the last line gives the URL that the link opens.
The order number, the item and the total came from the database. The server's code put all three into the HTML in step 4.
What is JavaScript
A page can also hold code for the browser to run. JavaScript is a programming language that every browser can run. The lessons use JavaScript and Python, two languages a reviewer often meets.
JavaScript in a page can change the page on the screen without a new request.
The line below is JavaScript from Sam's order page. Look at the word alert and the text in the brackets.
alert('Your order is on the way')alert is a function that every browser has. The function shows a small box on the screen with the text in the brackets.
Client-side and server-side code
The code of a web application runs in two places. Client-side code runs in the client, which here is the browser, and server-side code runs on the server.
Browser
runs client-side code
- request and response
Server
runs server-side code
- SQL
Database
stores the data
The browser reads the HTML and runs the JavaScript in a response, so both are client-side. The server-side code in the first lessons is JavaScript too, run on the server by a program named Node. The language does not say where code runs.
findOrder is a function written for the sports shop that asks the database for an order.
Client-side code is sent to the browser, so the person using the browser can read and change the code. Server-side code stays on the server. A check the shop depends on, such as a check on the price of an order, belongs in server-side code.
Check your understanding
Which of HTML and JavaScript is a programming language?
Show the answer
JavaScript is a programming language. HTML describes what is on a page.
Sam clicks the link to order 1042. What does the server do after the database sends the order back?
Show the answer
The server's code builds a page from the order. The server sends the page to the browser in a response.
Give one reason why a price check belongs in server-side code.
Show the answer
The person using the browser can read and change client-side code. Server-side code stays on the server.