Learn how a client and server interact using HTTP over the internet or other network.
Our goal in this project is to solidify our understanding of what a client and server are in relation to the internet, and also understand how clients and servers interact over the internet.
In the case of this project the client or user-agent, as it is sometimes referred to in the web development world, is going to be our web-browser. This is typically what we mean when we refer to the client in a web development context.
We all probably have a web-browser installed on our computers, so we don't need to worry about that at the moment. But we need to install something that can be considered a server.
A server in the context of web development is basically any program that can respond to client requests while adhering to the HTTP network protocol. So we need to install a program that can do that, many people would refer to that program as an http server (a server that accepts and responds to http requests).
Summary - Install the http server.
Pre-Requisites:
npmmust be installed on your computer (you can check by runningnpm -vin your terminal) it should output the version ofnpmyou have installed.
Instructions:
- Open a terminal
- Install the
http-serverpackage usingnpm- command:
npm install --global http-server - explanation: The command above instructs
npmto install a package globally, the idea being that we want to be able to use that package from anywhere on our computer.
- command:
- Check your installation
-
command:
http-server -
explanation: This will run the http server you should see output like below
Starting up http-server, serving ./ Available on: http://127.0.0.1:8080 http://10.1.20.69:8080 http://10.1.20.104:8080 Hit CTRL-C to stop the server
-
Summary - Create content for server to serve.
Instructions:
- Create an
index.htmlfile at the root of this repository. - Edit the
index.htmlfile to contain a valid HTML webpage. - Add an
h1tag with the contents ofHello World. - Add three links below the
h1tag in theindex.htmlfile.- first link to
pages/one.html - second link to
pages/two.html - third link to
pages/three.html
- first link to
Summary - Serve index.html with web server
Instructions:
-
From the root of this repository run...
-
command:
http-server -
explanation: This will start the http server serving content from the root of this directory you should see the following output
Starting up http-server, serving ./ # notice here it is serving this directory i.e. ./ Available on: http://127.0.0.1:8080 # by default the webserver is serving on address 127.0.0.1 and port 8080 http://10.1.20.69:8080 http://10.1.20.104:8080 Hit CTRL-C to stop the serverNote:
Address:
127.0.0.1is a special address, it's the address of our local machine it is often also referred to aslocalhost. You could also visit the addresslocalhost:8080and it would be the same location.Port: the
8080part of the address is called a port number. You can think of port numbers much like a telephone number extension. We can have one address127.0.0.1but many different processes listening to specific ports. In this case our http-server is listening on port8080
-
-
Now open your web-browser and navigate to address
127.0.0.1:8080.- You should see your Hello World webpage with its three links displayed in you browser
- explanation: You just told your web-browser (client) to make a
GETrequest to127.0.0.1:8080. the http server we started, that is listening on127.0.0.1:8080, sent a response back and that response was theindex.htmldocument. Now if you're observant you'll notice that we did not specify that we wanted theindex.htmldocument i.e.127.0.0.1:8080/index.html, but the http server gave us that document by default. This is the typical behavior most http servers adhere to, by default they will serve theindex.htmlfile it resides at the root of the of location being served.
-
Now go back to your terminal where the http servier is running. There you should see the logs of the request that the server has handled.
-
Open the browser development tools and reload the page. You can also see the request log from the client side.
-
Go back to the web-browser and click on link one. You'll notice that the request fails and the page can't be found. This is because the http server doesn't have a
pages/one.htmldocument to serve. Go look at the http server log again.
You'll notice the first log line is the GETrequest for location/pages/one.html. That file does not exist so the http server sends a response back to the client with a404error message which indicates the requested resource could not be found. -
Look at the browser development tools again. You can see the failed request for the
pages/one.html. -
Now go back and add a
pagesdirectory and addHTMLdocuments forpages/one.htmlpages/two.htmlpages/three.html -
Now try navigating to one of those pages again, and you should see your web-browser successfully navigate to those pages.
-
You should now have a basic understanding of how clients and servers request and serve html documents using HTTP.


