01. Introduction

1.1 Why Code a Web Server?

Most people use HTTP daily, but few understand its inner workings. This “Build Your Own X” book dives deep, teaching basics from scratch for a clearer understanding of the tools and tech we rely on.

Network Programming

The first step is to make programs talk over a network. This is also called socket programming. But socket programming is more than just gluing APIs together! It’s easy to end up with half-working solutions if you skip the basics.

Protocols & Communication

In order to communicate over a network, the data sent over the network must conform to a specific format called a “protocol”. Learn how to create or implement any network protocols by using HTTP as the target.

HTTP in Detail

You probably already know something about HTTP, such as URLs, different methods like GET and POST, response codes, various headers, and etc. But have you ever thought that you can create all the details from scratch, by your own code? It’s not very complicated and it’s rewarding.

1.2 Build Your Own X From Scratch

Why take on a build-your-own-X challenge? A few scenarios to consider:

1.3 The Book

Project-Centric

Designed to guide you through your own web server implementation, the book follows a step-by-step approach, and each chapter builds on the previous one.

An outline of each step:

  1. TCP echo server.
    • Socket primitives in Node.JS.
    • Event-based programming.
    • async/await and promises.
  2. A simple messaging protocol.
    • Implementing a network protocol in TCP.
    • Working with byte streams and managing buffers.
  3. Basic HTTP server.
    • HTTP semantics and syntax.
    • Generating dynamic content.
      • Async generators.
  4. Core applications.
    • Serving static files.
      • File IO in Node.JS.
      • Programming tip: avoiding resource leaks.
      • Range requests.
    • Caching control.
    • Compression.
      • The stream and pipe abstraction.
  5. WebSocket.
    • Message-based designs.
    • Concurrent programming.
      • Blocking queues.

Producing code is the easiest part and you’ll also need to debug the code to make it work. So I have included some tips on this.

Discussions at the End of Chapter

Getting your own web server running is rewarding, but it should not be your only goal. There are many blog posts that show you a toy HTTP server. But how do you get beyond toys?

At the end of each chapter, there are discussions about:

Node.js and TypeScript

The project uses Node.js without any dependencies, but many concepts are language-agnostic, so it’s valuable for learners of any language.

Code samples use TypeScript with type annotations for readability, but the differences from JS are minor.

The code is mostly data structures + functions, avoiding fancy abstractions to maximize clarity.

Book Series

This is part of the “Build Your Own X” book series, which includes books on building your own Redis, database, and compiler.

https://build-your-own.org