Day 97: Hostnames and HTTP Headers

Started working on the first Node.js project in the curriculum. It is a simple project but I want to use it as a study tool. Rather than looking at sample code and copying it, I looked into what a hostname is and reviewed HTTP headers to better understand what I am doing with this project. I outlined what I need to do to successfully complete the project and I started to read up on the related topics.

The project requires that I build a basic server that will send 3 different HTML files to the client based on the url sub-directories provided. I wanted to use this project as an opportunity to better familiarize myself with the parts of a URL. HTTP Requests and Responses as well as how to use the Node.js Built-in classes and modules to get this job done.

The idea behind this approach is to both improve my high-level knowledge of the process and to improve my skills with the specific tool I am using (i.e. Node.js). To accomplish this, I am trying my best to read through articles online about back-end processes and read the Node.js API docs to find my answers. Hopefully I can resist the temptation to just use Stack Overflow and I force myself to get more comfortable with these concepts and this tool.

TLDR;

Okay, so here are the highlights of what I did:

  • Back-end Dev -> Read an interesting article on what a server hostname is and how to find it on your local computer. It definitely got me thinking about so many other things I never considered. This journey is a not going to be short LOL.
  • Node.js -> Read through some of the API docs on the http.Server class in Node.js. I still need to read more about the fs module and the http.ServerResponse and http.ClientRequest classes. There is a lot of reading to do but I am slowly getting more comfortable with the tools and how I can use them.
  • Practice -> Started working on the Basic Informational Site project. I wrote the basic server but now I need to clean the request URL, use the fs module to retrieve the related HTML file and send it in the response. I don’t want to skip steps and act without knowledge so I am going to read the related docs and try to solve this on my own without googling or using methods and classes I have not looked into.

Server Code so far:

// Basic Information Site (Server)

const http = require('http')
const fs = require('fs')
const port = process.env.PORT || 8080

const server = http.createServer((req, res) => {
  /*
   * What is needed:
   * - clean request url to determine which file to send
   * - Set the Correct headers to send an html file as a response to the HTTP request
   * - Use the fs module to get the file and send it back to the client
   *
   */
  res.statusCode = 200
  res.setHeader('Content-Type', 'text/html')
  res.end('<h1>Hello World</h1>')
})

server.listen(port, 'localhost', ()=>{
  console.log("Connection made")
})


Goal For Round 7 of the #100DaysofCode Challenge

This is my seventh round of the “#100daysofcode” challenge. I will be continuing my work from round five and round six into round seven. I am currently working through the book “Cracking the Coding Interview” by Gayle Laakmann McDowell. My goal is to become more familiar with algorithms and data structures. This goal was derived from my goal to better understand operating systems and key programs that I use in the terminal regularly e.g. Git. This goal was in turn derived from my desire to better understand the fundamental tools used for coding outside of popular GUIs. This in turn was derived from my desire to be a better back-end developer.

I have no idea if my path is correct but I am walking down this road anyways. Worst case scenario I learn a whole bunch of stuff that will help me out on my own personal projects.