Skip to content

Latest commit

 

History

History
96 lines (85 loc) · 2.34 KB

File metadata and controls

96 lines (85 loc) · 2.34 KB

Backend Development and REST APIs with Express

REST APIs

  • HTTP (HyperText Transfer Protocol)
    • GET request to get data
  • REpresentational State Transfer
    • a pattern for designing Web APIs
    • resources
      • identified by URL
      • passed as JSON payload of request or response
    • collections
      • sets of resources
  • Will be using on Levels 0-2 of Richardson Maturity Model
    • Level 0: Single URI
    • Level 1: Separate URIs for resources
    • Level 2: Use of HTTP methods

JSON

  • JavaScript Object Notation
  • Standard format for representing an object as text
  • Subtext of JS
  • Parsers and formatters available in many languages
    • JSON.parse()
    • JSON.stringify()
  • "Object" means POJSO (Plain Old JavaScript Object)
    • no classes or types

Implementing REST

  • A REST request is handled by code which defines an endpoint
  • Complete API often requires many endpoints
  • Requests are routed by matching URL patterns
    • /orders/:id

CRUD Operations

  • Read
    • GET
  • Create
    • POST
  • Update
    • PUT
  • Delete
    • DELETE
  • Update (partial)
    • PATCH (apply a patch (edit))

GET Request

  • To get a collection: GET /orders
  • To get a single resource: GET /orders/9011

POST Request

  • To create a resource: POST /orders
  • Body of request contains data for new resource
  • Response contains new resource with ID

DELETE Request

  • To delete a resource: DELETE /orders/9011
  • Response is empty but resulting state of backend is that resource is deleted

PUT Request

  • To update a resource: PUT /orders/9013
  • Body of request contains new data for resource
  • Response contains updated resource

Status Codes

  • 1xx: Informational
  • 2xx: Success
    • 200: OK
    • 201: Created
    • 204: No Content
  • 3xx: Redirection
    • 301: Moved Permanently
  • 4xx: Client Error
    • 400: Bad Request
    • 401: Unauthorized (really unauthenticated)
    • 403: Forbidden
    • 404: Not Found
    • 405" Method Not Allowed
  • 5xx: Server Error
    • 500: Internal Server Error
    • 501: Not Implemented
    • 503: Service Unavailable

Express

  • Runs in NodeJS
  • Web server that we can extend using JavaScript
  • Initialization steps
    • Create an app
    • Add your routes
    • Call app.listen()

Express Routes

  • Route includes
    • HTTP method
    • URL pattern
      • may include patterns
    • Handler function