Emails / Email Course (3) Handling the Request & Preparing a Response

Email Course (3) Handling the Request & Preparing a Response
Hello, {{ subscriber.first_name | default: "friend" }}! šŸ‘‹
The request is in our server's hands.
What does the server do with it? Well, that depends on what's in the request.
The URL contains other details that help the server find what it needs, but so do request headers.
Now, you've likely had some experience working with headers.
A standard header is Content-Type, which can specify the type of response we're looking for. A server might be able to send different sets of data for different content types.
What if we're trying to access a dashboard that requires authentication?
In that case, an Authorization or Cookie header might be used to tell the server to send you the protected content. Without the right headers, the server will need to redirect you to login instead.
You might send many other useful headers with the request but, of course, it's up to the server to know what to do with them.
So our server needs a request handler. In Node.js that might look something like this:
const http = require("node:http");
const server = http.createServer();

server.on("request", function handleRequest(request, response) {});
What if the user requested the root URL? In that case, we can use our request handler to send them the homepage content.
function handleRequest(request, response) {
  // the response depends on the request URL
  switch (request.url) {
    case "/":
      // send back the homepage!
      response.writeHead(200);
      response.end(homePageContent);
      break;
  }
}
But what if the user is in France and prefers to see the content in French? Their request might include an Accept-Language header, and we can tailor the response accordingly.
function handleRequest(request, response) {
  // the response depends on the request URL
  switch (request.url) {
    case "/":
      response.writeHead(200);
      let lang = request.headers["Accept-Language"];
      if (lang.startsWith("fr-")) {
        response.end(homePageContentFrench);
      } else {
        response.end(homePageContentEnglish);
      }
      break;
  }
}
Remember, HTTP headers let the client and the server pass additional information with an HTTP request or response.
And a good way to think about Headers is that they can be grouped according to their contexts:
ā€¢ Request headers contain more information about the resource to be fetched
ā€¢ Response headers hold additional information about the response
ā€¢ Representation headers contain information about the body of the resource
ā€¢ Payload headers contain representation-independent information about payload data
Once we've handled the request and prepared a response, it's time to send it back to the browser and let it finish the work. šŸ›«
Challenge
As you can see, headers can be pretty valuable to help our server figure out what the user wants.
But when the server sends a response, it will come back with its own headers.
Your task is to think of what kind of information the server might have that could be useful to the browser. Think of a few things, and respond here with your ideas.
Answer the Challenge and Cement Your Knowledge