Skip to content

Instantly share code, notes, and snippets.

@Shurlow
Last active September 8, 2019 04:50
Show Gist options
  • Save Shurlow/9cc7b1ad1ffbdfdde165d2f7e2b98bdd to your computer and use it in GitHub Desktop.
Save Shurlow/9cc7b1ad1ffbdfdde165d2f7e2b98bdd to your computer and use it in GitHub Desktop.
REST & Express lesson notes

REST & Express

Objectives

  • Describe the concept of resources in relation to REST and APIs
  • Identify RESTful conventions for API routes
  • Explain what express is and why it's useful
  • Build RESTful routes using express

REST

  • What is a resource?

    Your answer...

  • What are the five common actions of an API? What methods and status codes are typically used for the five common actions?

    Your answer...

  • Build RESTful routes for one of the following resources: cards, dogs, engineers

    Action Method Path Body
    _
    _
    _
    _
    _

    Your answer...

  • Attept this REST Quiz!

    https://learn.co/lessons/rest-quiz

  • How would you build RESTful routes for the following data?

    const authors = [
      {
        name: 'Haruki Murakami',
        books: [
          { title: 'Hard-Boiled Wonderland and the End of the World' },
          { title: 'The Wind-Up Bird Chronicle' }
        ]
      },
      {
        name: 'Kurt Vonnegut'
        books: [
          { title: 'Slaughterhouse-Five' }
        ]
      }
    ]
    • Get all authors
    • Get one author
    • Add new author
    • Get all books by author
    • Get one book by author
    • Add new book by author
    • Edit one book by author
    • Delete one book by author
    • Challenge: Get a specific number of authors?

    Your answer...

Express

  • Compare the two example HTTP web servers bellow:
const fs = require('fs')
const http = require('http')

const server = http.createServer(function(request, response) {
  if(request.url === '/logo.png') {
    response.writeHead(200, {'Content-Type': 'image/gif'})

    fs.readFile(__dirname + '/public/logo.png', function(err, data) {
      if(err) console.log(err)

      response.end(data)
    })
  }
  else if (request.url === '/'){
    response.writeHead(200, {'Content-Type': 'text/html' })

    fs.readFile(__dirname + '/public/index.html', function(err, data) {
      if(err) console.log(err)

      response.end(data)
    })
  }
})
const express = require('express')
const app = express()

app.use(express.static('./public'))

app.get('/', function(req, res, next) {
  res.sendFile('./public/index.html')
})

app.listen(3000)
console.log('Express server is listening on port 3000')
  • What tasks does this server perform?
  • What parts of the process does Express help you with?

Practice

Lets practice building a simple server with express.

  • Create a new folder called express-lesson
  • Run npm init -y
  • Install express dependencies: npm i express body-parser nodemon morgan
  • Create a file called app.js
  • Add the following script to package.json: "start": "nodemon app.js"
  • Build the basic express server and open connections on port 3000.
  • Copy the author data from above into app.js
  • Implement the following author routes in express:
    • Get All
    • Post
    • Get One
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment