Skip to content

web servers

nbriz edited this page Oct 11, 2022 · 1 revision

The World Wide Web

The Web is an open and globally decentralized document and application distribution platform. Documents (ie. web pages) and software (ie. web apps), written in HTML, CSS, JavaScript and other languages, are stored on computers (connected to the Internet) all around the world running applications called servers, which wait for HTTP requests (small packets of data asking for specific files or resources) coming from other applications known as "clients" or "agents" (which are typically web browsers or web crawlers).

Anyone can create a website or web app and host their files on their own server running on their own computer connected to the Internet, and anyone else can use any number of web clients to request (ie. access) that website or web app over the Internet. As a platform, the Web has no central server, it's a collection of servers running on computers all over the world, which is what we mean when we say that the web is a "decentralized" platform. You don't need to ask anyone for permission or approval to run a server on the Web, all you need is a computer with an Internet connection, this is why we say the Web is an "open" platform.

While no one owns and operates the Web (the platform) itself, the protocols (like HTTP), core languages (HTML and CSS) and JavaScript APIs are designed and maintained by an internatoinal organization called the W3C, or the World Wide Web Consortium which consists of businesses, nonprofit organizations, universities, governmental entities, and individuals (anyone can join). The W3C don't own or control the Web (again, it's an open and decentralized platform), instead they're in charge with describing how it should work. These descriptions take the form of documents known as "specifications" (or "specs" for short) which are used as blue-prints for anyone interested in writing their own web server or web client software, as well as anyone interested in creating their own websites or web apps. If you don't follow the spec, then your client, server, site or app won't be compatible with the platform (ie. it won't work).

"hosting" a website or web app

A web "host" is a service running their own web servers on their own computers that you can use to host the websites or apps you create. While it's totally possible to host your site/app at home by running your own web server on your own computer, it isn't always practical (unless you've got lots of bandwidth, great upload Internet speeds and a computer you don't mind leaving on/connected running your server 24/7). For this reason there exist all sorts of different services for hosting your websites/app s on someone else's computer (aka on "the cloud"). Most of these servers will rent you space on their computers/data centers for a monthly fee, but there are also some "free" hosting providers out there. For example, we're be using GitHub not only to collaborate on a shared/versioned code base, but also as our app's web host (via their GitHub Pages feature).

running a local server

When your working a simple web page or web app you can test and see what it looks like by simply opening up your HTML file a web browser, without the need for a web server. When you view a "local" (ie. on your computer) HTML file in your browser you'll notice that the address bar has a file:/// protocol followed by the local path to your HTML file (ie. it's location on your hard drive). That said, in some instances,for security purposes (specifically when your project contains JavaScript code which loads data from other files), your browser will refuse to execute your code. In these circumstances it's necessary to run your own web server locally on your computer for testing. Then, rather than opening your file directly in your browser, you can send an HTTP request (locally on your comptuer, rather than over the Internet) to your server which in tern returns the requested files/resources to your browser. This time the browser will execute the code, because it came from a server (made clear by the http:// protocol in the address bar).

There are a number of different servers you can use to host your files locally (you can even write your own). Below I've got instructions for a couple of common severs as well as a simple one I made for these purposes. Feel free to use any of these (or any other) servers while locally working and testing your contributions to the class project.

1. a simple web server

I've written a simple web server (in JavaScript) which you can download for Mac, Windows and Linux. When you unzip the download you'll find 3 differnt foldrs for the three differnt versions, choose the one that reflects your system and double-click (or drag+drop into your terminal) the server file. It should open in your terminal (or command prompt) with a message that reads: Enter the path to the folder you want to serve:, type the path to the paintArtware1.0 project folder (or drag+drop your folder into your terminal, it should paste-in that folder's path automatically) and then press Enter.

Assuming you've entered a valid file path in the step above, the next message should read: What port would you like to run this server on: enter the port number 8000 (or any other unused port number you'd like). Assuming you entered a valid (and unused) port number, you should see a message displaying your computer's local IP address as well as the port the server is running on and the path to the folder being served. The terminal message will also list a few different URLs you can type into your browser to access the web app.

2. python server

Python is a popular programming language which comes with a simple web server module. Most Mac and Linux systems come with the python runtime (the language's command line interpreter) pre-installed (Windows users may need to manually install it). To check which version of the python runtime you have installed (if any) open a terminal (or command prompt) and run: python --version

Assuming you have python installed, the first step is to change your terminal's current directory to the directory you want to serve (ie. your local paintArtware1.0 folder). You can do this by typing cd (the "change directory" command) followed by space and then the path to the project folder (if you drag+drop a folder into your terminal it should paste-in that folder's path)

cd /path/to/folder

After you've navigated your terminal into the directory (ie. folder) you want to serve you can run the python web server, this command looks different depending on which version of python you have installed.

for python version 2, run:

python -m SimpleHTTPServer

for python version 3, run:

python -m http.server

You should then see a message that says something like "Serving HTTP on 0.0.0.0 port 8000 ...". When you want to quit running your server press Ctrl + C in your terminal.

3. php server

php was, for most of the Web's history, the most popular server side programming language. Like python, it also comes with a simple built-in server we can run from our terminals. Just like with our python server, the first thing you'll need to do is change your terminal's current directory to the directory you want to serve (ie. your local paintArtware1.0 folder). You can do this by typing cd (the "change directory" command) followed by space and then the path to the project folder (if you drag+drop a folder into your terminal it should paste-in that folder's path)

cd /path/to/folder

After that, assuming you've got php installed on your computer, you can run the following command:

php -S localhost:8000

You should then see a message that says something like "Listening on http://localhost:8000 ... Press Ctrl-C to quit."

sending HTTP requests to your local server

Once you've got a server running, you can send it requests from any web client (like a web browser) on your local network. That could mean the same computer you're running the server on, or any other computer on your same WiFi network. There are a few different ways to write that request, but the most common is http://localhost:8000, this assumes you're making the request form the same computer running the sever (ie localhost) to a sever running on port 8000 (ie. :8000). Alternatively you could replace "localhost" with "0.0.0.0". If you want to send a requst to the server from a different computer (on the same network) you'll need to replace "localhost" with the local IP address of the computer running the server.