Day 99: Incoming and Outgoing Messages

There is still so much left for me to learn but I have to keep it moving. I read through the http.IncomingMessage and http.OutgoingMessage classes. I am slowly getting more comfortable working with Node.js. Time to move on since I learned enough to comfortably finish the ‘Basic Information Site’ project. My routing probably is not the best but it’s a start.

TLDR;

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

  • Back-end Dev -> Learned about Hexadecimal usage in programming and how it easily represents bytes in human readable format.
  • Node.js -> Read through the API docs on the http.IncomingMessage class as well as the http.OutgoingMessage class. I still need more practice but at least I am increasing my exposure with pure Node.js code.
  • Practice -> Finished working on the Basic Informational Site project. I added the routing and got the file data sent in the response. I am still somewhat uncomfortable with pipes and streaming data. I need to read more on it and see some examples to pull apart some of the abstractions.

Server Code so far:

// Basic Information Site (Server)

const http = require('http')
const fs = require('fs')
const path = require('path')
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')

  let page
  switch(req.url){
    case '/':
      page = 'index.html'
      break

    case '/contact-me':
      page = 'contact-me.html'
      break

    case '/about':
      page = 'about.html'
      break

    default:
      page = '404.html'
  }

  const filePath = path.resolve(__dirname, page)
  const readStream = fs.createReadStream(filePath)
  readStream.on('open', () => {
    readStream.pipe(res)
  })

  readStream.on('error', (err) => {
    console.error(err)
    res.end(err)
  })

})

server.listen(port, ()=>{
  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.