Chapter 7: HTTP
Hypertext Transfer protocol, or HTTP, is the protocol that powers the web, and, as discussed in Chapter 6, it sits on top of the TCP stack,
Throughout this chapter you’ll learn how to leverage the Node.JS Server and Client APIs. Both are really easy to use to get started, but you’ll also learn some of the shortcomings that come up when building actual websites and web applications with them. For that, in the coming chapters I will introduce you to abstractions that sit on the HTTP server to introduce reusable components.
Keep in mind that since you’re programming both the server and the website as part of the same code, every time you make changes with your text editor to code that’s running, you need to restart the Node process that powers it to have the changes reflect. At the end of this chapter I’ll teach you how to leverage a tool to make this process straightforward.
To get started, let’s review the anatomy of the HTTP protocol.
The structure of HTTP
The protocol is structured around the concept of requests and responses, materialized in Node.JS as objects of the
http.ServerResponse constructors, respectively.
When a user first browses to a website, the user agent (the browser) creates a request that gets sent to the web server over TCP, and a response is emitted.
What do requests and responses look like? To find out, first create a Hello World Node HTTP server that listens on