Web Server Concepts and Examples

WebConcepts
5 Oct 202019:39

Summary

TLDRThis script delves into the fundamentals of web servers, explaining their role in serving web content. It clarifies that a web server is software that listens on a network port for incoming requests via a transport protocol, such as HTTP or HTTPS. The server then processes these requests and returns a response with the requested resource. The distinction between static and dynamic routing is highlighted, with examples illustrating how web servers serve files and dynamically generate content based on user requests. The script demystifies the inner workings of web servers, emphasizing the simplicity of HTTP and the importance of routing in determining server responses.

Takeaways

  • πŸ–₯️ A web server is a piece of software that serves web content, not necessarily tied to specific hardware.
  • πŸ” Web servers listen on a network port for incoming requests, similar to a customer service representative waiting for calls.
  • 🌐 Any device with a network connection can act as a web server, including laptops, Raspberry Pis, and smartphones.
  • πŸ“‘ Web servers communicate using the Hypertext Transfer Protocol (HTTP), which is a text-based protocol.
  • πŸ“„ HTTP requests and responses are structured as text data, consisting of a start line, headers, and an optional body.
  • πŸ”„ Web servers use the Transmission Control Protocol (TCP) as the transport layer to ensure reliable data transmission.
  • πŸ“‚ Static routing involves serving files directly from a directory, while dynamic routing requires processing to generate content.
  • πŸ“ HTTP responses include a status code, headers, and a body that can contain various types of content like HTML, images, or JSON.
  • πŸ”§ Web servers can be configured to route requests to a single file or executable that acts as an entry point for a web application.
  • πŸ“Š Web applications can dynamically generate content based on user requests, often involving database interactions.
  • πŸš€ Hosting static websites can be cost-effective and scalable, with services like GitHub Pages or Amazon S3 handling high traffic.

Q & A

  • What is the primary function of a web server?

    -A web server's primary function is to serve web content, which involves listening for requests on a network port, processing those requests, and returning the requested resources as an HTTP response.

  • Can any device with a network connection act as a web server?

    -Yes, any device with a network connection, such as a laptop, Raspberry Pi, or smartphone, can act as a web server, provided it has the necessary software to handle and respond to web requests.

  • What is the difference between a web server and a gaming PC in terms of hardware?

    -The hardware of a web server is typically optimized for handling web applications at scale, which may include more RAM, faster CPUs, and better network capabilities compared to a gaming PC, which is optimized for graphics and high-performance gaming.

  • How does a web server listen for incoming requests?

    -A web server listens for incoming requests by remaining idle and waiting for connections on a specific network port provided by the operating system. It does not initiate outgoing connections.

  • What are the three main components of an HTTP request?

    -The three main components of an HTTP request are the start line (containing the request method, target, and HTTP version), the headers (key-value pairs providing additional information), and an optional body (containing form data or other content).

  • What is the role of the Transmission Control Protocol (TCP) in web communication?

    -TCP is the transport layer protocol used by web servers and clients to ensure reliable, ordered, and error-checked delivery of data packets over a network connection.

  • What is the purpose of HTTP status codes in web server responses?

    -HTTP status codes in web server responses indicate the outcome of the request. For example, a status code of 200 indicates a successful request, while a 404 indicates that the requested resource could not be found.

  • What are the two types of routing in web servers?

    -The two types of routing in web servers are static routing, which serves files directly from a directory, and dynamic routing, which involves processing requests through a web application to generate or retrieve content dynamically.

  • How can a web server serve both static and dynamic content?

    -A web server can serve static content by directly returning files from a specified directory, while dynamic content is served by routing requests to a web application that generates or retrieves the content based on the request.

  • What is the significance of the 'Content-Type' header in HTTP responses?

    -The 'Content-Type' header in HTTP responses informs the client about the nature of the content being returned, such as text/html for web pages, image/jpeg for images, or application/json for JSON data.

  • How does a web server determine which resource to return for a given request?

    -A web server determines which resource to return based on the routing rules configured for the server. These rules map incoming requests to the appropriate resources, whether they are static files or dynamic content generated by a web application.

Outlines

00:00

🌐 Understanding Web Servers

The paragraph discusses the concept of a web server, dispelling misconceptions about the hardware requirements. It explains that any network-connected device can act as a web server, and the primary function of a web server is to serve web content through software. The paragraph outlines the six main functions of a web server, starting with listening for requests on a network port and returning responses with the requested resources.

05:01

πŸ“„ HTTP and the Language of the Web

This section delves into the Hypertext Transfer Protocol (HTTP), which is the language used by web servers and clients for communication. It clarifies that HTTP is based on human-readable text files, and even though newer versions use binary, the foundation remains text-based. The paragraph breaks down the structure of an HTTP request, including the start line, headers, and optional body, and contrasts it with a File Transfer Protocol (FTP) server to illustrate the importance of protocol specificity.

10:02

πŸ”„ How Web Servers Send and Receive Data

The paragraph explains the process of sending HTTP requests to a web server using the Transmission Control Protocol (TCP), which is the transport layer protocol underlying HTTP. It describes how TCP ensures reliable data transmission by breaking data into packets and using handshakes and confirmations. The paragraph then discusses how web servers return HTTP-based responses, which include a status line, headers, and a body containing the requested resource.

15:02

πŸ“‚ Serving Static and Dynamic Content

This section distinguishes between static and dynamic routing in web servers. Static routing involves serving files directly from a folder, which is efficient and suitable for hosting simple websites. Dynamic routing, on the other hand, requires the web server to route requests to an entry point file or executable, which then dynamically prepares a response based on the request. The paragraph provides a practical example of a dynamic website using PHP and a SQLite database to serve order status information.

Mindmap

Keywords

πŸ’‘Web Server

A web server is a software that serves web content over the internet. It listens for requests from clients and returns the requested resources. In the video, the web server is compared to a customer service representative waiting for incoming calls, illustrating its passive role in waiting for client requests.

πŸ’‘Network Port

A network port is a logical channel on which a network service listens for incoming connections. The video uses the analogy of a call center with multiple extensions to explain how a web server can listen on a specific port for requests, like a customer service representative listening on a specific extension.

πŸ’‘Hypertext Transfer Protocol (HTTP)

HTTP is the language used for communication between web servers and clients. It is a text-based protocol that allows for the transfer of web content. The video describes HTTP as a simple, human-readable text file format, which is sent and received by web browsers and servers to request and deliver web pages or other resources.

πŸ’‘Request Method

A request method is a command used in an HTTP request to indicate the action to be performed on the resource. The video mentions 'GET' as an example, which is used to request data from a server. The 'POST' method is also mentioned, which is used for submitting form data to a server.

πŸ’‘Headers

Headers in an HTTP request or response are key-value pairs that provide additional information about the request or the resource being requested. The video explains that headers can include information like the 'Host' to specify the server's address and the 'User-Agent' to identify the browser type.

πŸ’‘Content-Type

The 'Content-Type' header indicates the nature of the data being sent or received in an HTTP request or response. The video mentions 'text/html' for HTML content, 'application/json' for JSON data, and 'image/jpeg' for image files, showing how the server informs the client about the type of content it is delivering.

πŸ’‘Routing

Routing is the process of determining how a web server should respond to a client request. The video distinguishes between static routing, where the server serves files directly from a directory, and dynamic routing, where the server processes the request and generates a response based on the content of a database or other dynamic sources.

πŸ’‘Static Content

Static content refers to web pages or other resources that do not change based on user interactions or data from a database. The video explains that serving static content is efficient and can be done with minimal server resources, making it suitable for hosting on platforms like GitHub Pages or Amazon S3.

πŸ’‘Dynamic Content

Dynamic content is content that changes based on user input, session data, or database information. The video demonstrates dynamic content with a simple PHP script that fetches order status from a database and serves it to the user, showing how a web server can generate and deliver personalized content for each request.

πŸ’‘Transmission Control Protocol (TCP)

TCP is a transport layer protocol that ensures reliable data transmission over a network. It breaks data into packets, manages their transmission, and confirms their receipt. The video briefly mentions TCP as the underlying protocol that carries HTTP requests and responses, comparing it to a telephone line that carries voices in the call center analogy.

Highlights

A web server is a piece of software that serves web content.

Any device with a network connection can be a web server, such as laptops, Raspberry Pi, or smartphones.

Web servers listen on a port for incoming requests and do not initiate outgoing connections.

The network protocol provides 65,535 ports for software to communicate over.

Web browsers automatically send web requests to port 80 (HTTP) or port 443 (HTTPS).

HTTP (Hypertext Transfer Protocol) is a simple, text-based protocol used for communication between web servers and clients.

An HTTP request consists of a start line, headers, and an optional body.

Web servers and web clients communicate using the application layer protocol HTTP, which runs on top of the transport layer protocol TCP.

TCP ensures that data packets are successfully received and in the correct order.

Web servers return HTTP-based responses, which include a status line, headers, and a body.

The content returned by a web server can be various types, such as web pages, images, PDFs, or JavaScript code.

Routing is the process of connecting a request with the requested resource.

Static routing involves serving actual files from a folder, while dynamic routing involves serving content from a database.

Static websites can be hosted cheaply and performantly, as they require minimal resources.

Dynamic websites require a web application to evaluate HTTP requests and prepare responses based on the requested resource.

Web servers can be configured to route all requests to a single file or executable, which acts as an entry point for a web application.

Dynamic web applications can serve content that changes based on user interactions or other factors.

The example dynamic website consists of a single PHP file and a SQLite database, demonstrating simplicity in web application setup.

The video provides a high-level overview of web server functionality to familiarize viewers with the concepts.

Transcripts

play00:00

so what is a web server i distinctly

play00:02

remember when i was first getting

play00:03

started programming

play00:04

wondering what kind of computers are

play00:05

used to serve web applications what like

play00:07

operating system do they run and

play00:09

how is the hardware different than the

play00:10

gaming pcs that i've built over the

play00:12

years

play00:13

and as it turns out as is often the case

play00:15

with all of this stuff where you don't

play00:16

know it's this black box you're trying

play00:18

to unwrap

play00:18

i was asking the wrong question from a

play00:20

hardware standpoint

play00:22

anything with a network connection could

play00:23

be a web server your laptop a raspberry

play00:25

pi

play00:25

a smartphone if you've ever done that

play00:27

dance where you're trying to configure

play00:28

an internet of things

play00:30

device and you have to connect to it

play00:31

over wi-fi that internet of things

play00:33

device

play00:34

is running a web server once you get

play00:35

connected to it that's what you're

play00:36

interacting with

play00:38

you see a web server it's not about

play00:39

hardware though certainly some hardware

play00:41

is better suited to run a large web

play00:43

application at scale

play00:44

instead a web server is really just a

play00:46

piece of software that serves web

play00:48

content

play00:49

but serving web content that's a tad

play00:51

generic so let's break it down further

play00:52

and define the six things that a web

play00:54

server does in roughly the order in

play00:56

which they happen

play00:57

so to serve web content a web server

play00:59

listens on a port

play01:01

for a request sent via a transport

play01:03

protocol

play01:04

and returns a response containing the

play01:06

requested resource

play01:07

again with this list we're still really

play01:09

abstract so let's go ahead and take each

play01:11

one of these items

play01:12

one by one for a closer look so to start

play01:14

let's focus for a minute on what it

play01:15

means for a web server to listen

play01:17

basically once a web server application

play01:19

starts up it just sits there

play01:21

idle waiting for incoming requests if no

play01:23

requests come in

play01:24

then the web server doesn't actually do

play01:26

anything i think everybody kind of

play01:28

intuitively knows this but it's

play01:29

important to keep the concept in the

play01:31

front of our minds to illustrate this

play01:32

imagine

play01:33

the web server is a guy named jim who

play01:34

works as a customer service

play01:36

representative a call center

play01:37

you can reach jim by dialing one two

play01:39

seven zero zero zero

play01:41

zero zero zero one and like jim the web

play01:44

server just sits there waiting for

play01:45

incoming requests jim never places an

play01:46

outgoing call

play01:47

he just waits and listens now i happen

play01:50

to have a web server running right now

play01:51

on my local machine of course it's

play01:53

running on the local host ip address of

play01:55

127.0.0.1 and if we

play01:57

dial that ip address into the browser

play01:59

you can see this message

play02:00

hey it's jim how can i help you so what

play02:03

exactly

play02:04

is the web server listening to the

play02:06

answer is a network port provided by the

play02:08

operating system that the web server is

play02:10

running on

play02:10

think like windows mac os more

play02:12

specifically the network protocol that

play02:14

we're using provides 65

play02:16

535 ports that software running on the

play02:18

computer can communicate over

play02:19

now when a web server starts up it

play02:21

begins listening on one or if so

play02:23

configured

play02:23

many of those ports going back to the

play02:25

call center example for a minute let's

play02:26

assume that jim works at a call center

play02:28

with 65

play02:29

535 different extensions it's like a

play02:31

really big facility

play02:33

and you can reach the call center's main

play02:34

line by dialing 127.0001

play02:38

but to reach the phone on jim's desk you

play02:40

have to dial his extension

play02:41

let's suppose that dialing extension

play02:43

8000 will make the phone on jim's desk

play02:46

ring

play02:46

jim's only listening for calls coming in

play02:48

on extension 8000 and if you dial

play02:50

any other extension then you won't get a

play02:51

boy jim so let's go back to our demo for

play02:54

a minute earlier when we dialed the ip

play02:55

address 127.0.0.1 we didn't provide a

play02:58

port

play02:58

and that's because a web browser

play03:00

automatically sends web requests to

play03:01

either port 80 or port 443 depending on

play03:04

whether we prefix the url with http

play03:06

or https in the demo i had the web

play03:09

server running on port 80

play03:10

using an unencrypted http connection so

play03:13

there was no need to specify a port as

play03:15

the web browser automatically routed our

play03:17

http

play03:18

request to port 80 by default but let's

play03:20

stop the web server and start it again

play03:22

on port 8000

play03:23

now if we reload the page you can see we

play03:25

get an error that this site can't be

play03:27

reached as the ip address refused to

play03:29

connect

play03:29

and that's because there's no longer a

play03:31

web server listening on port 80 on our

play03:33

local host

play03:34

we can tell the browser that we want our

play03:35

request sent to port 8000 instead by

play03:38

appending the ip address with a colon

play03:40

and then adding the port number

play03:41

now you can see since we've specified

play03:42

the right port to communicate with our

play03:44

server

play03:44

we've once again reached gym now just

play03:47

like you could have multiple people

play03:48

working at a call center

play03:49

each listening for incoming calls on a

play03:51

different extension so too can you have

play03:53

multiple web servers running on one

play03:54

machine let's suppose jim has a

play03:56

colleague named jane

play03:57

who can be reached by dialing extension

play03:59

8001. if you dial extension 8001 you'll

play04:01

hear jane speaking but if you dial

play04:02

extension 8000 you get hey it's jim how

play04:04

can i help you

play04:06

let's illustrate this by starting a

play04:08

second web server over on port 8001

play04:11

sure enough if you navigate to port 8001

play04:13

you see the message from jane

play04:14

but if you switch back to port 8000 then

play04:16

you get the message from jim

play04:18

the point is that one single computer

play04:19

can have many web servers or other

play04:21

networking services running at the same

play04:23

time

play04:23

and just like we have to dial the

play04:24

correct extension to reach jim at his

play04:26

desk

play04:27

so too must we send our request to the

play04:30

correct port

play04:30

in order to reach the desired web server

play04:33

speaking of sending a request let's take

play04:35

a closer look at what exactly sending a

play04:36

request to a web server means

play04:38

see web servers and web clients like

play04:40

your chrome firefox whatever

play04:42

communicate via the hypertext transfer

play04:44

protocol or http

play04:46

and you can think of this as the

play04:47

language that the web server and the web

play04:49

client use

play04:50

but what does http actually look like

play04:52

for me this was the most interesting

play04:53

part of researching this video

play04:55

because of the misconceptions i had

play04:56

about what http actually

play04:58

is now when they say hyper text transfer

play05:01

protocol

play05:01

they literally mean text like http is

play05:04

just a web browser and a web server

play05:05

sending raw human readable text files

play05:07

back and forth

play05:08

and i'm sure i'll get comments pointing

play05:10

out that http version 2 and beyond

play05:12

converts the text to a binary before

play05:13

sending but the point still stands http

play05:16

is based on text and i don't know why i

play05:19

thought it was more complicated than

play05:20

that well

play05:20

i kind of do and we'll cover that when

play05:22

we look at the transport layer later on

play05:24

but we are talking about the application

play05:26

layer for now and http

play05:27

is really really simple let's open up

play05:30

this text file here containing an

play05:31

example http request and have a look

play05:36

that's it that's a valid request in the

play05:37

http language

play05:39

so bringing the request down the data is

play05:40

separated into three blocks the first

play05:42

line is the start line which contains

play05:44

the request method which is git in this

play05:45

case

play05:46

the request target which is the specific

play05:48

web page or resource we're requesting

play05:50

here you can imagine we're looking up an

play05:51

order on an e-commerce website

play05:53

and finally the http version which in

play05:56

the case of this request is version 1.1

play05:58

now the next block is made up of every

play06:00

subsequent line up until the first

play06:02

line break we encounter these lines

play06:04

contain the headers of our request

play06:06

as defined as key value pairs here we

play06:08

just have two

play06:09

host which is the ip and port we're

play06:10

attempting to send the request to and

play06:12

the user agent which is the header that

play06:13

tells the web server what type of

play06:15

browser we're using

play06:16

this can be useful so the web server

play06:17

might know to serve you mobile content

play06:19

in case you're accessing on a mobile

play06:21

device

play06:22

finally the third block is made up of an

play06:24

optional body which begins

play06:26

after that first line break at the end

play06:28

of our header block

play06:29

and since the request we're looking at

play06:30

is a get request we actually don't have

play06:32

a body but suppose if the web server

play06:33

accepted a form submission to look up

play06:35

order status instead

play06:36

in that case the post request using the

play06:39

post request method

play06:40

might include a body that looks like

play06:41

this here we've added a body with the

play06:43

name of the form input where we entered

play06:46

our order id

play06:46

and the value of that input one two

play06:48

three in this case note that we also

play06:50

changed our request target to be just

play06:51

orders since we're providing the order

play06:53

id in the body instead of the url

play06:55

we also added a couple of new headers

play06:57

one content type which tells the web

play06:58

server how the data in our body is

play07:00

encoded

play07:01

and a content length header telling the

play07:03

web server the size of the body and

play07:04

that's it i've seen http requests

play07:06

represented this way before but like i

play07:08

always thought it was some kind of

play07:09

shorthand or human-friendly

play07:10

representation of what was going on

play07:11

under the hood

play07:12

i didn't realize i was looking at the

play07:13

http protocol itself

play07:15

basically sending the contents of that

play07:17

file to the ip address on our web server

play07:19

using a transport protocol is literally

play07:21

the same thing as typing the web

play07:22

server's ip address port and target

play07:24

resource into the browser

play07:26

and that's because http is what's

play07:27

considered to be an application layer

play07:29

protocol

play07:30

it's like the language that the web

play07:31

servers and the clients speak if any

play07:33

other application

play07:34

protocol other than http is sent to our

play07:37

web server it won't know how to handle

play07:38

it

play07:38

let's go back to jim and jane for a

play07:40

minute to kind of clarify this idea

play07:42

imagine that jim who if you recall

play07:43

available at extension 8000

play07:45

only speaks french meanwhile let's

play07:47

imagine jane who again is available at

play07:49

extension 8001

play07:50

only speaks german if you as a german

play07:52

speaker dial extension 8000

play07:54

jim won't have any idea what you're

play07:56

saying because he only speaks french

play07:57

he won't even be able to help you with

play07:59

your request he won't know that you're

play08:00

even making a request

play08:02

let's illustrate this point by starting

play08:03

jim's web server again on port 8000

play08:06

but instead of starting jane's web

play08:07

server on port 8001 we'll start an ftp

play08:10

server on port 8001 instead

play08:12

now ftp stands for file transfer

play08:13

protocol and as the name suggests

play08:15

ftp servers were typically used to

play08:17

transfer files in the dark ages before

play08:19

you had services like dropbox now if you

play08:21

start an ftp server it begins listening

play08:23

for requests sent in the file transfer

play08:24

protocol

play08:25

which is a different language than the

play08:27

hypertext transfer protocol that web

play08:28

servers and web browsers speak

play08:30

now that our ftp server is up and

play08:31

running on port 8001 let's go ahead and

play08:34

navigate our browser to that port on our

play08:35

local host

play08:37

and we can see that we get this error

play08:38

message this page isn't working

play08:41

127.0.0.1 sent an invalid response error

play08:44

invalid http response now basically what

play08:47

happened is the web browser sent an http

play08:49

request to the ftp server then the ftp

play08:51

server didn't

play08:52

understand the request and returned a

play08:54

response indicating as much in the ftp

play08:55

language which our browser that only

play08:57

speaks http didn't understand either

play08:59

and thus our browser is giving us this

play09:00

invalid http response error

play09:02

so now we know that http is the language

play09:05

web browsers and web servers speak

play09:07

but how is the http request actually

play09:09

being sent

play09:10

to the web server and i've kind of

play09:12

intentionally glossed over this until

play09:13

now while we've looked at

play09:14

what a valid http request looks like but

play09:17

we have to send it to the web server

play09:18

somehow

play09:19

you see http and ftp are what's known as

play09:21

application layer protocols

play09:23

meaning they both run on top of an

play09:25

underlying communications or

play09:27

transport layer protocol let's go back

play09:29

to jim and jane for a second think about

play09:30

how they speak

play09:31

different languages french and german in

play09:33

our latest hypothetical but both of

play09:35

their voices are

play09:36

carried over the telephone line german

play09:38

and french are the application protocol

play09:40

like http and ftp

play09:42

but the phone line that's carrying their

play09:44

voice that's the transport layer

play09:46

which in the web server world is the

play09:48

transmission control protocol or tcp

play09:50

now i'm not going to get too deep into

play09:52

the inner workings of tcp it's really

play09:54

beyond

play09:54

anything you need to know to get started

play09:56

with building web applications but just

play09:57

know that tcp at a very simple level

play09:59

breaks data up into packets then using

play10:02

handshake

play10:02

and confirmations it ensures that every

play10:04

packet is successfully received by the

play10:06

recipient

play10:07

and the recipient knows the order of

play10:10

each packet for reassembly

play10:11

now that we know how a request is sent

play10:12

to the web server the next thing that

play10:14

happens

play10:14

is that the web server returns a

play10:16

response again just like our browser

play10:18

sent our request

play10:19

in the http language so too does the web

play10:21

server return an http

play10:23

based response now just like our request

play10:26

an http response is just text data

play10:28

again separated into the same three

play10:30

blocks again we have the start line

play10:32

which

play10:33

for a response is sometimes referred to

play10:35

as the status line

play10:36

it contains our http version and an http

play10:39

status code here the response contains

play10:40

the status code 200 which is kind of the

play10:42

generic response for

play10:43

a successful http request pretty sure i

play10:46

could easily do a 20 minute video just

play10:47

on http status code so make sure

play10:49

that you subscribe if you're interested

play10:51

in seeing that video

play10:52

but back to the request below that first

play10:54

line just like in our http request we

play10:56

have multiple lines of headers defined

play10:58

again as key value pairs

play10:59

and just like the request again headers

play11:01

are enumerated beginning on line two and

play11:03

are bounded by the first line break that

play11:04

we encounter one of the important

play11:06

headers in this example response is the

play11:07

content type

play11:08

which here is text slash html which lets

play11:11

our browser know to expect

play11:13

a text file text content and

play11:16

that the text content will be html

play11:19

hypertext markup language

play11:21

aka a web page in the body of the

play11:23

response and moving down to the body

play11:24

sure enough as

play11:25

promised by our content type header our

play11:27

body contains html content

play11:29

here the html just a simple header tag

play11:31

containing the text hello world

play11:33

now if this were an api response the

play11:35

content type might instead say

play11:37

application json in which case the

play11:39

response body would contain some json

play11:40

data for

play11:41

our application to consume and taking it

play11:43

one step further if the resource we were

play11:44

requesting was an

play11:45

image the content type might be image

play11:47

slash jpeg

play11:48

and the body would contain the binary

play11:50

contents of an image file

play11:51

the main takeaway here is that the

play11:53

content returned by a web server is not

play11:56

limited to just web pages

play11:57

the body of an http response can contain

play11:59

all kinds of things like images and pdf

play12:01

documents and javascript code or web

play12:03

fonts

play12:03

in fact the video you're watching right

play12:05

now was returned piecemeal in bodies of

play12:07

a bunch of http responses

play12:09

sent by youtube's web servers to the web

play12:11

browser or your youtube app

play12:13

all right now this kind of transitions

play12:14

us to the last piece of what a web

play12:16

server does

play12:17

now we've looked at the http response in

play12:19

the abstract but let's talk a bit about

play12:21

how a web server decides what to respond

play12:23

with

play12:24

and i mean that in the practical sense

play12:25

if i request an image resource and the

play12:27

web server returns it

play12:29

where's that web server getting the

play12:30

image from how do we actually set this

play12:33

up in the real world

play12:34

determining which resource to return is

play12:36

often referred to as routing which is

play12:38

really just the practice of connecting a

play12:39

request with the

play12:41

requested resource the content that's

play12:42

being requested

play12:44

and for our purposes you know it's

play12:45

helpful to think about routing as being

play12:47

separated into two distinct categories

play12:49

there's static routing and then there's

play12:51

dynamic routing now

play12:52

static routing is just the web server

play12:54

serving actual files out of a folder

play12:56

just like you have a folder on your

play12:58

desktop that you can navigate to and

play13:00

open various files you can open an image

play13:01

of a cat you could open a text file

play13:03

similarly you can point a web server to

play13:05

a directory on the file system

play13:07

and tell the web server to return

play13:08

anything that's requested in fact

play13:10

let's do that now we'll open this folder

play13:12

and as you can see i've got an image

play13:13

file

play13:14

called cat.jpg which as you might have

play13:16

guessed just a picture of a cat

play13:17

and two html files index.html and

play13:20

hello.html

play13:21

and if we take a look at those files

play13:22

they're very simple html files that just

play13:24

contain their respective file names in

play13:26

an h1 tag

play13:27

so let's go ahead and start up a web

play13:29

server listening on port 8000 inside of

play13:31

this folder

play13:32

now if we navigate to 127.0.0.1.cat.jpg

play13:37

our web server returns the image of the

play13:39

cat and if we navigate to the root at

play13:42

127.0.0.1 we get the default index.html

play13:45

file's contents

play13:46

and finally if we add a slash hello.html

play13:49

to the root

play13:49

then we get the hello.html contents and

play13:53

if we navigate to somewhere that doesn't

play13:54

exist in the target folder then we get a

play13:56

404 status for file not found

play13:58

and that's because the file does not

play13:59

exist on the web server now static html

play14:01

websites like this

play14:02

they can be really cheap and performant

play14:04

to host since they require very little

play14:06

resources

play14:07

a static website the web server is just

play14:09

taking a file that already exists

play14:10

returning the contents and the body of

play14:12

the response something that web servers

play14:14

are highly optimized for

play14:15

and to that end you can host static

play14:17

sites completely free using something

play14:18

like github pages

play14:19

or mostly free with an amazon s3 bucket

play14:22

and handle huge spikes in traffic

play14:24

hundreds of thousands of visitors

play14:25

in case one of your blog posts makes it

play14:27

to the front page of hacker news or

play14:28

reddit

play14:29

without any slowness or downtime for

play14:31

only a few cents a month

play14:33

but what about situations where the

play14:34

content that exists at a particular

play14:36

resource

play14:37

changes dynamically think about a my

play14:39

profile type page that changes depending

play14:41

on who the logged eden user is or

play14:43

imagine

play14:44

an e-commerce web page where you can

play14:46

check your order status

play14:47

surely you wouldn't generate a static

play14:49

html page for every single order in your

play14:51

system and then have to switch out those

play14:52

pages whenever an order status changes

play14:54

from processing to shipped

play14:56

to delivered of course not instead you'd

play14:58

save that info to a database

play15:00

and serve the contents of that database

play15:02

dynamically

play15:03

in practice this usually means

play15:05

configuring your web server to route all

play15:07

requests

play15:08

to a single file or executable which

play15:10

serves as an entry point for your web

play15:12

application

play15:13

and that web application could be

play15:14

created using programming languages like

play15:16

javascript php python ruby

play15:18

and it would evaluate the http request

play15:21

and then dynamically prepare a response

play15:23

depending on the requested resource

play15:25

opening up the http request we looked at

play15:27

before we see it's requesting the

play15:29

resource

play15:30

for order id 123 and if you sent that

play15:34

to a web application it would search the

play15:35

database for order 123

play15:37

and return the status now i happen to

play15:39

have a very simple example of a dynamic

play15:41

website that does

play15:42

exactly that okay so now we're in this

play15:44

dynamic folder and

play15:46

i'm going to start a web server on port

play15:47

8000 and if we navigate over and we go

play15:50

to

play15:51

localhost 8000 orders and we'll check

play15:54

order id of one

play15:55

you can see that it comes back says

play15:57

order one's been delivered

play15:58

and gives us a tracking number let's go

play16:00

and check the status of order two

play16:02

we can see that order two has been

play16:03

shipped and again it provides us with

play16:04

the tracking number one more time let's

play16:06

go ahead and check us

play16:07

order status of order three we can see

play16:09

that it's processing

play16:10

and we don't have a tracking number yet

play16:12

let's see if there's an order status for

play16:14

four

play16:14

we check order status for four and it's

play16:16

the same processing and there is no

play16:18

tracking number

play16:18

let's go and check and see if there's an

play16:20

order status for five here we get

play16:22

this page cannot be found an error of

play16:24

404 was given so that means there is no

play16:26

order id number five in our system here

play16:28

let's go ahead and jump into the code

play16:29

and see how i set this up

play16:31

now i wanted to keep this as simple as

play16:32

possible so you can see this whole

play16:33

dynamic website consists of just a php

play16:36

file

play16:36

it's not importing any libraries at all

play16:38

no composer files

play16:40

it's just raw php standard library and

play16:42

you can see that the entire dynamic web

play16:44

application

play16:45

is only 45 lines long and we also have a

play16:50

sql lite database and if we jump into

play16:52

the contents of that real quick

play16:53

you can see that it has one table and in

play16:56

that table there's columns of id

play16:57

status and tracking number so let's jump

play16:59

into this index.php and see how it's

play17:01

dynamically routing our request

play17:03

in order to return our order status we

play17:05

first are getting our order id from the

play17:07

route

play17:07

here we're using this parts url helper

play17:10

in order to get the path out of that

play17:11

request uri

play17:13

and then we're passing it to this regex

play17:14

method which basically says

play17:16

look for or this regex pattern says look

play17:19

for orders

play17:20

followed by a slash and then any digit

play17:22

and then it takes that regex pattern

play17:24

and it does this purex match and if no

play17:27

matches are found then it returns this

play17:28

not found

play17:29

function or calls this not found

play17:30

function there it sets the header to 404

play17:33

and then just exits right there

play17:34

but if this path variable does match

play17:36

this regex so it's like order slash one

play17:38

or order slash two

play17:39

then it returns that match as the order

play17:42

id so you basically get this integer

play17:44

back you can see it's

play17:44

saying return an integer from this

play17:46

function and that gets stored into this

play17:48

order id variable

play17:49

that order id then gets passed to our

play17:51

get order status from database function

play17:54

and we can see how that works down here

play17:56

basically it takes that order id as a

play17:57

property

play17:58

it creates a new connection to our

play18:01

sqlite database

play18:02

then it creates this sql statement that

play18:04

says select all from orders where id

play18:06

equals question mark

play18:08

then it binds the order id and then here

play18:11

it fetches the first

play18:12

result as a associative array stores it

play18:15

in this result variable

play18:16

so if the order id is not found this

play18:18

result variable is null and then again

play18:20

it returns this not found function

play18:22

or calls this not found function that

play18:24

sets the response status code to 404 and

play18:26

then exits

play18:27

but if a request is found it's being

play18:29

returned as an associative array so

play18:31

that's going to contain

play18:32

the id the status and the tracking

play18:34

number if it's not null

play18:36

and it's going to return all that back

play18:38

up here and store it in

play18:40

the order details variable finally we

play18:42

call the print html function and we pass

play18:44

it that order details array that we got

play18:46

back from the database and if we scroll

play18:48

down to the print html function

play18:51

you can see it takes that order those

play18:52

order details that array of order

play18:54

details and it just says print and it

play18:56

defines basic hypertech markup language

play18:58

you've got

play18:59

the opening html tag the body tag and

play19:02

then you've got an h1 tag where you're

play19:04

reaching in and getting the order id

play19:06

and the order status and then you're

play19:08

getting the tracking number and

play19:09

returning it right here

play19:10

so again going back if we go to orders

play19:13

like one

play19:14

it's reaching into the database getting

play19:16

the content for order number one

play19:18

and returning it to the user so that's a

play19:20

really high level overview of what a web

play19:22

server does as always with this channel

play19:24

i'm just trying to introduce you to

play19:25

these concepts so when you encounter

play19:26

them later you have enough familiarity

play19:28

to google your way to a better

play19:29

understanding

play19:30

something that all software developers

play19:31

do if you enjoy this kind of content

play19:34

please consider subscribing

play19:35

thank you so much for watching and i'll

play19:37

see you guys next time

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
WebServersHTTPProtocolWebDevelopmentNetworkingSoftwareEngineeringInternetCommunicationWebContentDynamicRoutingStaticContentWebApplications