-
Notifications
You must be signed in to change notification settings - Fork 0
The Anatomy and Function of a Webserver
Before we go any further, we should take the opportunity to do a little bit of disambiguation. When we speak of webservers, we may be refererring to two different, but very closely things. They are the following:
- web server: this a computer (often a very high-powered, server-grade computer) which is equipped and optimized to store, process, and deliver requested web pages and applications to users over the internet. To equip a computer with this ability, we use the second form of a webserver, which is the
- webserver software: this is some software that enables a computer to listen to, understand, and formulate responses over HTTP communication (the language of the internet). When we want to work on a project that will allow us to interact with things over the internet (via a browser, mobile app, or internet of things), this is the software which will enable us to make the computers talk to one another.
Now that we covered the two possible meanings of a webserver, it should be noted that from here on out, when we use the term "webserver" we will be focusing on the second meaning of the word, which is the webserver software. Don't fret, we will be sure to cover the first topic (the computer and its components) in a future lesson!
Imagine it's Christmas time. You've just sat down, cozy in your ugly sweater with a glass of egg nog, ready to write a letter to Santa containing a list of all the things you deserve for your good behaviour all year. Well, you know exactly what you want, and in no time at all, you stuff your seven-page list of demands in an envelope, address it to the North Pole, and drop in the mailbox. From there, the ever-diligent postal system does what it does best and delivers the letter to Santa's workshop.
Side note: In this analogy, the postal system would be "the Internet" (TCP/IP; transport layer), but we will discuss that in more detail in a future lesson.
At Santa's workshop, we meet a bright little elf in the mailroom; let's call him... Tippy TwinkleToe. Tippy opens your letter, determines what the letter is about, who it's from, and what's in the list of presents you asked for, and figures out how to gather those presents. Some toys will be ready, waiting on the shelf, and some of them will need to be custom made for you. So, he writes a list of instructions for his fellow elves to go and get (or make) all the presents. Sometime later, when all the toys are ready, he goes and wraps each of the items in gift wrap and places them in Santa's sack to make its way to you on Christmas Eve.
Now, we can break down all that activity into individual components and relate them to the activity of searching for something on a browser:
SPOILER ALERT: Tippy is not really an elf. He is a webserver.
| Christmas Letter Analogy | Actual Internet Activity |
|---|---|
| You write a letter to Santa asking for some presents | You type a URL in the search bar (ie. 'www.santasworkshop.np/wishlist/AwesomeKid?item1=iPhone&item2=hotWheels') and press Enter, which sends an HTTP GET request |
| Tippy opens the letter | The 'www' webserver for 'santasworkshop.np' accepts the HTTP GET request; Tippy is the 'www' webserver |
| Tippy determines what the letter is about | The webserver has a router for 'wishlist', which executes a special function written to process Christmas wishes |
| Tippy figures out who it's from | The 'wishlist' function reads the URL to get the bit of text after 'wishlist' to figure out the user. In this case, it's 'AwesomeKid' |
| Tippy reads the list of wishes | The 'wishlist' function parses the query parameters. In this case, it's {item1: iPhone, item2: hotWheels} |
| Some toys are already made | The webserver will have static assets like pictures, videos, pre-written HTML which never change. They will be retrieved from the database |
| Some toys will have to be custom made | The webserver will perform some business logic and create custom content specifically for AwesomeKid |
| Tippy gift wraps all the presents | The webserver constructs an HTTP response using all the content the router function has gathered (e.g, an HTML page, or a JSON object) |
| The gifts are placed in Santa's sack, and delivered to your house | The HTTP response is sent via the internet's transport layer back to your browser |
To wrap up the analogy let's go over exactly how it relates to a webserver and how we interact with one. Tippy was the main point of contact for us in getting your list of Christmas wishes to Santa. Similarly, when we want to interact with a website like Facebook or Google, the servers (with webserver software running on them) in a data center somewhere are the things with which we are really interacting. In conversation, we often hear terms like "cloud-based" or "stored in the cloud". In reality, what that really means is that the information is stored in data centers somewhere, and webservers like Tippy are what serves you with all the cat videos you crave.
The function of a webserver is in three (3) parts:
- Accept HTTP requests. This request can be in one of many forms; in the analogy, we used a GET request, but other types also exist, which we will discuss further down.
- Run functions based on the request. The webserver has special routes for each variant of the URL (think 'brokenToyReturn' instead of 'wishlist' in the URL), which go on to execute unique functions, each doing a special task.
- Formulate an appropriate HTTP response. After the unique function returns some content, the webserver then needs to package it up in a format that travels well through the internet, back to your browser.
In the analogy above, we mentioned one particular type of HTTP request: the GET request. It is one of the most common request types (also called a request method), used to retrieve information from a webserver. However, many more types of requests exist, each with a specialized purpose. Some of the most used HTTP request methods are:
- GET: used to request a representation (JSON, HTML, etc) of a specified resource. This method should be used to retrieve information only, and not add/change anything on the server.
- POST: used to submit information to the webserver, or instruct the webserver to create new resources. In doing so, one can pass in additional information in the request body, often called the payload, or directly in the URL. When one needs to send private information not intended to be visible (cannot be put directly in the URL), they can do so by using POST request, with the secret information added in the request body.
- DELETE: used to instruct the webserver to delete a certain resource.
- PATCH: used to partially modify an existing resource in the webserver.
- PUT: used to update (overwrite) a particular resource with new information supplied in the request