Day 02: Using Template View in Express

Continued working with express and backend dev material with Node.js. The curriculum has me walking through a practice project to get more familiar with using view templates with Express. Tbh, this seems really similar to using PHP. For all the hate that the language gets it does not seem like other languages are doing things that much different with server-side web frameworks. I could be wrong and I am not experienced enough to really judge. I am just saying so far things look pretty similar.

TLDR;

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

  • Node.js -> Read through the API docs for the fs core module in Node.js.
  • Practice -> Started working through the practice ‘Local Library’ project in the MDN ExpressJS guide. I also reproduced the ‘Basic Information site’ project with an Express server file instead of the pure Node.js server. I can see why people like Express LOL.

Basic Information site server file:

Only using Node.js and core modules:

// 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")
})

Using Express package to run server:

const path = require('path')
const app = require('express')()


app.get('/about', (req, res) => {
  res.sendFile(path.resolve(__dirname, './about.html'))
})
app.get('/contact-me', (req, res) => {
  res.sendFile(path.resolve(__dirname, './contact-me.html'))
})
app.get('/', (req, res) => {
  res.sendFile(path.resolve(__dirname, './index.html'))
})
app.get('*', (req, res) => {
  res.sendFile(path.resolve(__dirname, './404.html'))
})

app.listen(3000, () =>{
  console.log('listening on port: 3000')
})



Goal For Round 8 of the #100DaysofCode Challenge

This is my eighth round of the “#100daysofcode” challenge. I will be continuing my work from round five, six, and seven into round eight. I was working through the book “Cracking the Coding Interview” by Gayle Laakmann McDowell. My goal was 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 am currently putting a pause on the algorithm work to build some backend/full stack projects. I primarily want to improve my skills with the back-end from an implementation perspective. I have improved tremendously in terminal and CLI skills but I lost focus due to how abstract the algorithm concepts got. I wanted to work on things that were more tangible until I can get to a position where I could directly benefit from improving my algorithm skills and theoretical knowledge. So that’s the focus right now. Build my backend skills and prove my full stack capabilities by building some dope projects.

Again, I still 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. Best case scenario I actually become one of those unicorn developers that go on to start a billion dollar company… You never know LOL.