Skip to content

Instantly share code, notes, and snippets.

@pwrmvspark
Forked from Shurlow/rest-express.md
Last active June 17, 2019 19:04
Show Gist options
  • Select an option

  • Save pwrmvspark/9a30a62870d314f3f9334372d78a50c5 to your computer and use it in GitHub Desktop.

Select an option

Save pwrmvspark/9a30a62870d314f3f9334372d78a50c5 to your computer and use it in GitHub Desktop.
REST & Express lesson notes

REST (Representational State Transfer) & 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

  • Given the following examples of a web server written with Node http and Express that perform the same tasks

    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)
        })
      }
    }) // and much more besides
    const express = require('express')
    const http = require('http')
    
    const app = express()
    
    app.use(express.static('./public'))
    
    app.get('/', function(req, res, next) {
      res.sendFile('./public/index.html')
    })
    
    http.createServer(app).listen(3000)
    console.log('Express server is listening on port 3000')
  • What are the tasks performed?

  • What parts are of the process does Express help you with

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment