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

Cross Origin Resource Sharing

2m:3s

When you use XHR or Fetch to request a resource from a browser, you may run into a CORS error. (This is very common).

Error when trying to fetch google.com

At this point, you’ll probably have a few questions:

  • What is CORS?
  • What is an opaque response?
  • What is the no-cors mode?
  • What is the Access-Control-Allow-Origin header?

We’ll answer these questions in this lesson.

Let’s begin with the most obvious one.

What is CORS?

CORS is short for Cross Origin Resource Sharing.

It enables two things:

  • It lets you fetch resources from a browser.
  • It lets the server decide whether to accept your request.

Servers decide whether they want to accept your request with an Access-Control-Allow-Origin header. (This is a header set by the server).

If the server does not set an Access-Control-Allow-Origin header, or if the header doesn’t contain your origin, you won’t be able to get resources from the server via a browser.

Your only choice, if you want to fetch resources from that server, is to use another server. That’s because servers are not constrained by CORS.

What’s an origin?

An origin contains three things:

  1. A Protocol (the file://, http:// or https:// part)
  2. A Host (the domain name)
  3. A Port (the number that comes after the domain name)
Terminal window
# Example of origins
https://zellwk.com
https://students.magicaldevschool.com
https://api.github.com
http://locahost:3000

In case you’re wondering, the root of an API is also the origin of the API.

You’ll learn how to allow specific origins to access your server later.

Opaque responses are usually useless

Opaque responses are responses that don’t return CORS headers. If you send an opaque response, you won’t be able to read the data returned or even view the status of the request — so you won’t know if the request is successful.

Opaque responses are useless because we need to know the status of the response — otherwise, we won’t know what to do next.

So instead of using no-cors, a better alternative is to send requests through servers.

Wrapping Up

The best way to mitigate CORS errors is to send requests from servers — this is almost everything you need to know about CORS for now.

This is also why servers are important.

But before we dive into servers, you still have a few more things to learn about Rest APIs.

Next, you’ll learn about the anatomy of a response.