Chapter 6. The Request and Response Objects
In this chapter, we’ll learn the important details of the request and response objects—which are very much the beginning and end of everything that happens in an Express application. When you’re building a web server with Express, most of what you’ll be doing starts with a request object and ends with a response object.
These two objects originate in Node and are extended by Express. Before we delve into what these objects offer us, let’s establish a little background on how a client (a browser, usually) requests a page from a server and how that page is returned.
The Parts of a URL
We see URLs all the time, but we don’t often stop to think about their component parts. Let’s consider three URLs and examine their component parts:
- Protocol
-
The protocol determines how the request will be transmitted. We will be dealing exclusively with http and https. Other common protocols include file and ftp.
- Host
-
The host identifies the server. Servers on your computer (localhost) or a local network may be identified simply be one word or by a numeric IP address. On the internet, the host will end in a top-level domain (TLD) like .com or .net. Additionally, there may be subdomains, which prefix the hostname. www is a common subdomain, though it can be anything. Subdomains are optional.
- Port
-
Each server has a collection of numbered ports. ...
Get Web Development with Node and Express, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.