Essential CSS
JavaScript Roadmap
Learn JavaScript
Mastering Web Typography
Practical Astro
Understanding Async JS
Unorthodox Tailwind
API Masterclass
Build and Deploy
The Beginner Freelancer
Real World Node
Profile Billing Logout
Understanding Async JS
Lesson Plan
  1. Introduction
    1. What is Asynchronous JS?
    2. Callbacks are not automatically asynchronous
    3. What is an API?
    4. Evolution of HTTP APIs
    5. Ajax vs Asynchronous JavaScript
  2. Fundamental Concepts
    1. How to fetch resources
    2. Using XHR
    3. Using the Fetch API
    4. Using libraries
    5. Core JavaScript concepts
    6. Understanding Promises
    7. Understanding Async Functions
    8. Understanding the Event Loop
    9. Understanding JSON
    10. Async JS Best Practices
    11. Using Optimistic UI
  3. Understanding Rest API
    1. Diving into Rest APIs
    2. The Rest Architecture
    3. The Rest Structure
    4. Anatomy of a Request
    5. What's an endpoint
    6. Request methods
    7. Understanding request headers
    8. Understanding the request body
    9. Authentication
    10. Basic Authentication
    11. Bearer Authentication
    12. Authenticating with API keys and API Secrets
    13. Cross Origin Resource Sharing
    14. Anatomy of a response
    15. HTTP Statuses
    16. Response headers
    17. The response body
    18. Handling Errors
    19. Reading API documentations
    20. Understanding Curl
  4. Node Basics
    1. Why we are using Node
    2. Command Line Basics
    3. Installing Node
    4. Managing dependencies
    5. Using ES Modules in Node
    6. Using Node
    7. Restarting the server automatically
    8. Sending requests via Node
    9. Protecting API keys and secrets
  5. Building a server
    1. What's a server
    2. Building a simple server with Express
    3. Two kinds of servers
    4. API Server architecture
    5. Handling routes
    6. Understanding Middlewares
    7. Enabling CORS
    8. Receiving data
    9. Sending responses
    10. Handling errors in Express
    11. Using the server as a proxy
    12. Minimum viable server
  6. Open Authorization
    1. Open Authorisation (OAuth)
    2. Authenticating with Access Tokens
    3. Obtaining Access Tokens
    4. Public and Confidential Clients
    5. Authorisation Code Grant
    6. Understanding scope
    7. Proof Key for Code Exchange (PKCE)
    8. PKCE with Twitter
    9. Dynamic state and `code_verifier`
    10. Saving the access token in cookies
    11. Passing access tokens to the browser
    12. Saving the access token in local storage
    13. Refresh Token Grant
    14. Device Code Grant
    15. Client libraries vs REST
  7. Building Social Media Logins
    1. Introduction
    2. Starter Files
    3. Creating SSL certificates
    4. Login in with Github
    5. Github: Saving the token to local storage
    6. Github: Persisting the login status
    7. Login with Twitter
    8. Twitter: Saving the token to local storage
    9. Twitter: Persisting the login status
    10. Twitter: Refreshing the access token
    11. Login with Facebook
    12. Facebook: Saving the token to localStorage
    13. Facebook: Persisting the login status
    14. Facebook: Refreshing the access token
    15. Login with google
    16. Google: Saving the token to localStorage
    17. Google: Refreshing the access token

The Rest Architecture

5m:32s

The Rest architecture is one of the most confusing things for beginners because it contains 6 abstract-sounding principles:

  1. Client-Server
  2. Stateless
  3. Cacheable
  4. Uniform Interface
  5. Layered System
  6. Code on Demand

These principles sound abstract, but they make sense when you look at the way we build websites and applications today.

I’m going to give you an understanding of each of these principles so you don’t get overwhelmed when hearing other people talk about them.

Note

You don’t have to remember any of these principles to use Rest APIs — just read through and know what they are.

Let’s begin!

Principle 1: Client-Server

Client-Server states that the clients and servers should be separate.

  • Clients refer to user interfaces. In web development, it’s the frontend we show to users. (I use browser and clients interchangeably in this course).
  • Servers refer to the backend. Clients connect to these to get data. One example is Github’s API server.

When clients and servers are separated, they can be worked on separately and thus be evolved separately.

The key is to have a uniform interface between clients and servers so they can talk to each other. This interface is the API.

Principle 2: Uniform Interface

Having a uniform interface means we have a set of consistent rules that can be used to communicate between clients and servers.

In REST, four rules were created for this purpose:

  1. Resource identification in requests
  2. Self-descriptive messages
  3. Manipulation of resources through these representations
  4. Hypermedia as the engine of application state (HATEOAS)

We’ll go into more detail on each of them.

Resource identification in requests

Anything transferred from a server to a client is considered to be a resource. For example:

  • The HTML file is a resource
  • The CSS file is a resource
  • The JavaScript file is a resource
  • Any data you fetched through XHR or Fetch is also a resource

This rule means: the request must identify what resource they’re trying to fetch. In practice, this is done with the URL we send in the request.

For example, the following URL fetches a list of my repositories on GitHub.

Terminal window
https://api.github.com/users/zellwk/repos

Self-descriptive messages

This rule means: Servers must tell the client how to process the data they’re sending. This allows the client to process the data and convert it into a usable form.

This message is sent via a MIME Type.

In simple words:

  • If the server sends JSON, it must say it’s sending JSON.
  • If the server sends XML, it must say it’s sending XML.
  • And so on.

You don’t have to worry about the MIME type in practice (if you’re using libraries). Most libraries would have already handled it for you.

Resource manipulation through representations

A representation is the data that the server sends back. It can be in any format: JSON, XML, PDF, etc.

This rule means: The response from the server must tell the client how to manipulate the data.

Manipulation in this case means:

  1. It must tell a client how to update the data
  2. It must tell a client how to delete the data

In practice, servers often send over the id of a resource. The id can then be used in a request to manipulate the data.

Of course, the user must have the necessary permissions to manipulate the data before he or she can do so.

Hypermedia as the engine of application state (HATEOAS)

HATEOAS sounds complicated, but they’re not.

It means: Servers should send URLs to help clients discover other resource endpoints. Clients can then use these endpoints to get additional data.

For example, if you send a request to Github’s root endpoint, you get a list of URLs. This list of URLs tells you other resources you can fetch.

const response = await zlFetch('https://api.github.com')
console.log(response.body)
List of URLs from Github's response.

Principle 3: Stateless

Stateless means the server will not save information from a previous request. It will treat each request as a brand-new one.

In practice, this means you have to send all information required by the server in each request — including information to authenticate the user, even if you have authenticated before in a previous request.

This principle makes servers easier to code since it passes the responsibility of managing user state to the client.

Principle 4: Cacheable

Caching is a process where the client stores information upon receiving them. The client can use this stored information later. Information must be permitted by the server before it can be stored

Examples of caching include:

  • Storing the HTML so the browser doesn’t have to ask for it again
  • Storing the CSS so pages load quicker
  • Storing the JavaScript so pages become interactive quicker

Caching speeds things up by reducing the amount of connections to the server. If a connection is required, it can still reduce the amount of work done by a server by reducing the amount of checks required.

The downside is cached resources can become outdated, so cacheable data often comes with an expiry date. Servers must tell clients how long to cache the data for and when the data expires.

Principle 5: Layered System

Layered system expands on the Client-Server concept.

The Client is a layer, the Server is another layer, and they know nothing about each other except for the APIs connecting the two.

The Layered System principle says you can add additional layers between clients and servers like:

  • A cache layer (which allows caching)
  • A load balancing layer (which divides how traffic hits the server)

There are other possible layers, but we won’t dive into them since they’re not the focus of this course.

Principle 6: Code on Demand

The Code on Demand principle says the server can send you executable code (often JavaScript). The client can then execute this code upon receiving it.

You’ve seen if you’ve been given a <script> to put on your website. Google Analytics is one example.

<!-- Google analytics script uses the Code on Demand principle -->
<script
async
src="https://www.googletagmanager.com/gtag/js?id=GOOGLE_ANALYTICS_ID"
></script>
<script>
window.dataLayer = window.dataLayer || []
function gtag() {
dataLayer.push(arguments)
}
gtag('js', new Date())
gtag('config', 'GOOGLE_ANALYTICS_ID')
</script>

Wrapping up

Don’t worry about remembering or knowing these principles by heart. You don’t have to — because most of these principles are already baked into the way we work.

I’m giving you an understanding of these principles so you don’t get overwhelmed when hearing other people talk about them.