Privacy note: We capture your IP address to provide customised and focused services. See our Privacy Policy.

Full-Stack Web Development — Alpha School

Lesson 13: Servers and HTTP: Requests, Responses and Status Codes

Until now, your pages have lived on your own computer. To share them with the world, and to build features that need data, you need a server, a program that waits for requests and answers them. This lesson explains what a server does, how HTTP structures the conversation between a browser and a server, and what the numbers in a response mean, because reading those numbers is the fastest way to diagnose almost any web problem. You will also run a tiny server yourself, which is the first step from pages that sit still to applications that respond, and it prepares you for building an API in the next lesson.

What a server does

A server is a program that listens on a port, a numbered doorway on a computer, and answers requests that arrive there. A web server's simplest job is to send files: when a browser asks for /index.html, the server reads that file from its disk and sends it back. A more interesting server also runs code, such as looking up a course in a database and building a page from the result, which is how sites show different content to different visitors. Servers run all the time, handle many requests at once, and log what they do, so an administrator can see what was asked for and what went wrong. The server you will use in this course is Node.js, which lets you write server code in JavaScript, the language you already know, and its built-in http module can serve a page in fewer than twenty lines.

Anatomy of a request and a response

Every HTTP exchange has the same shape. A request has a method, a path, headers and sometimes a body: the method says what kind of action is wanted, GET to read something, POST to create something, PUT to replace it, PATCH to change part of it, and DELETE to remove it; the path says what it applies to, such as /api/courses; headers carry extra information, such as what formats the client accepts; and the body carries data, such as the contents of a form. A response has a status code, headers and a body: the status code says what happened, the headers describe the body, such as its type and length, and the body carries the file or the data. Seeing requests and responses this way makes the web feel orderly: every feature you build is a request with a method and a path, and a response with a status and a body.

const http = require('http');
const server = http.createServer(function (req, res) {
  res.writeHead(200, { 'Content-Type': 'text/html' });
  res.end('<h1>Hello from the server</h1>');
});
server.listen(3000, function () {
  console.log('Listening on port 3000');
});

Status codes: the numbers that tell the story

Status codes are grouped by their first digit, and learning the groups is enough to read most of them. Codes starting with 2 mean success: 200 OK is the usual answer, and 201 Created means something new was made. Codes starting with 3 mean redirection: 301 means a page has moved permanently, and the browser should use the new address. Codes starting with 4 mean the client made a mistake: 400 Bad Request means the request was malformed, 401 means the visitor must log in, 403 means they are logged in but not allowed, and 404 means the thing asked for does not exist. Codes starting with 5 mean the server failed: 500 is a general server error, and 503 means the service is temporarily unavailable. When a page fails to load, the status code is the first clue, and checking it in the browser's network tab is the professional habit that saves hours of guessing.

Running your first server

To run the example above, save it as server.js, install Node.js from its official site, and run node server.js in a terminal, which is a text-based way of talking to your computer. The server prints “Listening on port 3000” and waits, and when you visit http://localhost:3000 in your browser, localhost means “this computer”, the server answers with the heading. Press Ctrl+C in the terminal to stop it. That loop, write code, start the server, visit the page, read the result, is the rhythm of backend development, and it feels strange at first only because the page now comes from a program instead of a file. Keep the server simple for now: a few routes, a few responses, and the confidence that you can make a computer answer requests, which is the foundation of the next three lessons.

Key Terms

Server
A program that listens for requests and answers them, such as by sending files or data.
Port
A numbered doorway on a computer that a server listens on, such as 3000.
HTTP method
The action in a request, such as GET to read, POST to create, PUT to replace and DELETE to remove.
Header
Extra information in a request or response, such as the body's type or what the client accepts.
Status code
A number in a response saying what happened, grouped as 2xx success, 3xx redirect, 4xx client error, 5xx server error.
localhost
A name meaning “this computer”, used to test a server running on your own machine.
Node.js
A runtime that lets you run JavaScript on a server, outside the browser.

Discussion Questions

  1. Why does HTTP use different methods (GET, POST, DELETE) instead of one method for everything?
  2. A visitor sees a 403 error and a 404 error on two different pages. What is the difference between them, and what should each page say?
  3. Why is the status code the first thing to check when a page fails to load?

Practice Questions

  1. What is the difference between GET and POST?
  2. What does the status code 404 mean, and which group does it belong to?
  3. In the example server, what does server.listen(3000) do?
  4. What three parts does an HTTP response have?
  5. What does 500 mean, and who is at fault?
Show answers
  1. GET reads something; POST creates something. GET asks for data without changing anything, while POST sends data to be stored or processed.
  2. It means the thing asked for was not found, and it is a 4xx client error. The server answered, but it has nothing at the requested path.
  3. It starts the server listening for requests on port 3000. Visiting http://localhost:3000 then reaches that server on your own computer.
  4. A status code, headers and a body. The code says what happened, the headers describe the body, and the body carries the file or data.
  5. It is a general server error, so the server is at fault. The request may have been fine, but something went wrong on the server while handling it.
Keep reading to complete this lesson