Learn by doing: code a Web server in Node.js
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.
Protocols & communication.
HTTP in detail.
WebSocket & concurrency.
The project uses Node.js and TypeScript without any dependencies, but many concepts are language-agnostic, so it’s valuable for learners of any language.
Beyond coding exercises
At the end of each chapter, there are discussions about
What’s missing from the code? The gap between toys and the real thing, such as optimizations and applications.
Important concepts beyond coding, such as event loops and backpressure. These are what you are likely to overlook.
Design choices. Why stuff has to work that way? You can learn from both the good ones and the bad ones.
Alternative routes. Where you can deviate from this book.
Build your own X
Why take on a build-your-own-X challenge? A few scenarios to consider
Students: Solidify learning, build portfolio, stand out in future careers.
Developers: Master fundamentals beyond frameworks and tools.
Hobbyists: Explore interests with flexible, extensible projects.
This is part of the “Build Your Own X” book series, which includes books on building your own Redis, database, and compiler.
AI Reading Assistant
Whole-book reading guide from stratified index samples; jump to passages in the text
Tip the Site
Support this siteYour recognition and a small knowledge-service contribution help keep this technical work open source.Scan the WeChat Pay or Alipay code below. Logged-in and guest visitors can both tip.
WeChat Pay
Alipay
Open WeChat or Alipay and scan. No login required.
AI guide
【One-Line Pitch】
Learn by doing: build a complete, dependency-free web server in Node.js and TypeScript, mastering TCP, HTTP, and WebSocket fundamentals that apply to any language. Ideal for students, developers, and hobbyists who want to move beyond frameworks and truly understand the protocols powering the web.
【Book Arc】
- **Opening (~0%–9%)**: Introduces the "Build Your Own X" philosophy, explains why coding a web server from scratch deepens understanding, and outlines the book's structure—each chapter ends with discussions on real-world gaps, design choices, and alternative approaches. Sets expectations for a hands-on, language-agnostic journey.
- **Early (~9%–25%)**: Covers HTTP basics by manually crafting requests with tools like netcat, socat, and curl, then dives into TCP server coding. Key insight: TCP is a byte stream, not a packet stream—a critical distinction that shapes all protocol design. Introduces the socket API (listen, accept, read, write) and the event loop's single-threaded nature.
- **Early (~25%–34%)**: Explores asynchronous programming in depth: callbacks vs. Promises, async/await syntax, and converting callback-based APIs to promise-based ones. Discusses backpressure—why waiting for writes to complete prevents unbounded memory growth—and the importance of pausing data events for ordered execution.
- **Middle (~34%–47%)**: Builds a simple message echo server with dynamic buffers, implementing a custom protocol with delimiters. Explains pipelined requests as a correctness test for parsers, showing how treating TCP data as a continuous stream (not packets) enables handling multiple messages in a single read.
- **Middle (~47%–end)**: Moves to HTTP semantics and syntax, focusing on Content-Length and Transfer-Encoding as the core of message framing. Later chapters cover concurrent programming with blocking queues and a full WebSocket server implementation, tying together all prior concepts.
【Key Takeaways】
- **TCP is a byte stream, not a packet stream** (Early): The #1 beginner trap is "concatenating & splitting TCP packets"—they don't exist. Protocols must impose boundaries within the continuous byte flow, as DNS does with a 2-byte length prefix over TCP.
- **The event loop is single-threaded and shared with your code** (Early): While a callback runs, the runtime can't handle other connections. Keep handlers short, yield voluntarily, or offload CPU-intensive work to threads/processes to avoid blocking everything.
- **Backpressure prevents unbounded memory growth** (Middle): socket.write() always succeeds, even when the OS send buffer is full, pushing data into an unbounded internal queue. Waiting for write completion throttles production—look for unbounded queues as a sign of missing backpressure.
- **Promises make async logic linear** (Early): Converting callback-based APIs to promises via an executor (resolve/reject) lets you write `await socket.read()` style code without breaking logic into scattered callbacks. Multiple connections then handle concurrently without explicit threading.
- **Protocol parsers must be stateless regarding buffer size** (Middle): A correct parser consumes elements one by one, regardless of how much data arrives in a single read. Pipelined requests (multiple messages in one byte stream) are the ultimate test—if your parser treats reads as packets, it fails.
- **Content-Length and Transfer-Encoding define HTTP message boundaries** (Middle): These headers are the most critical because they determine where a message ends in the byte stream. Understanding them is essential for any HTTP implementation, from servers to clients.
- **Pause/resume data events for ordered execution** (Middle): Pausing the 'data' event isn't just for backpressure—it ensures events fire in a controlled sequence, preventing race conditions in callback-based code.
【Reading Tips】
- **Skim the discussion sections first** (end of each chapter): They explain "what's missing from the code" and "why it works that way"—the real value beyond the exercises. If short on time, read these before the code.
- **Deep-read Chapter 3–4 (TCP server + Promises)**: These are the conceptual foundation. Master the event loop, backpressure, and promise conversion—everything later builds on these ideas.
- **Code along with Chapter 5 (message protocol)**: The dynamic buffer and `cutMessage()` function are small but dense. Implement them yourself, then test with pipelined input (`echo -e 'asdf\n1234' | socat ...`) to verify your parser.
- **Use the suggested tools (socat, curl, openssl)**: Don't skip the manual HTTP request exercises—they build intuition for what your server will later parse. Note that HTTPS requires TLS, so use `openssl s_client` instead of netcat.
- **Treat the final WebSocket chapters as a capstone**: They combine concurrency (blocking queues) with protocol design. If you're short on time, skim the queue implementation and focus on the WebSocket handshake and framing.
【Coverage Limits】
This guide synthesizes excerpts covering roughly the first half of the book (through HTTP semantics) plus chapter titles for later sections. Detailed content on WebSocket implementation, concurrent programming specifics, and the final HTTP server assembly are not covered in depth here.
nds a single request message and the server responds with a single response message. A DNS message is encapsulated in a UDP packet. | IP header | IP payload...
types of JS functions: normal functions and async functions. Normal functions execute from start to return (either explicitly or implicitly). Since the JS ru...
an be more than 1 message in it. Support Pipelined Requests While you can make pipelined requests to many well-implemented network servers, such as Redis, NG...
ld-your-own.org 45 2024-02-02 06. HTTP Semantics and Syntax Text is More Work & Error-Prone Another downside is that dealing with text is a lot more work. To...
ly Do in Practice When developing networked applications: 1. Avoid small writes by combining small data before writing. 2. Disable Nagle’s algorithm. Nagle’s...
Report an Error | Ask a Question @ build-your-own.org 69 2024-02-02 08. Dynamic Content and Streaming 8.6 Discussion: WebSocket Polling for Updates Some web-...
eader { return { close: async (): Promise<void> => { // force it to `return` so that the `finally` block will execute await gen.return(); Report an Error | A...
Support this siteYour recognition and a small knowledge-service contribution help keep this technical work open source.
Scan the WeChat Pay or Alipay code below. Logged-in and guest visitors can both tip.
WeChat PayAlipay
Open WeChat or Alipay and scan. No login required.
Add Tag
Enter tag name (max 50 characters)
Share E-Book
Build Your Own Web Server From Scratch In Node.JS (James Smith) (Z-Library)
Scan QR code with your phone to access
Copy the link or scan the QR code to access this e-book on your phone
Share E-Book via Email
Please enter email address
Donation Statistics
¥.00
Total Donations
0
Donation Count
Build Your Own Web Server From Scratch In Node.JS (James Smith) (Z-Library)
Find Your Favorite Books
Only registered users can comment after logging in. Comments need to be reviewed by administrators before being displayed
Loading comments...
Reply to Comment
Edit Comment