DEV Community

Rajat Sharma
Rajat Sharma

Posted on

Understanding CORS and Proxies in Web Development: A Simple Analogy

Introduction

In web development, one common challenge developers face is the dreaded CORS error. If you’ve ever worked with APIs, you might have encountered this frustrating issue when trying to fetch data from a different domain. But why does this happen, and how can we fix it? In this article, we’ll dive into the concept of CORS (Cross-Origin Resource Sharing) and Proxies with a simple analogy to make it easy to understand, even for beginners!


What Is CORS?

Before we dive into proxies, let's first understand what CORS is and why it exists.

Imagine you're sending a letter (a request) to your friend’s house (the backend server) located in another city. You live in a different city (the frontend server), and the postal service (the browser) checks where the letter is coming from and whether it’s allowed to be delivered.

If the postal service (the browser) sees that the letter is coming from a different city (a different origin), it might stop the letter from reaching your friend because it’s not trusted.

CORS is like this “security measure” that ensures requests from different domains are safe. Without it, malicious websites could make requests to other servers and steal sensitive data.

Key Points:

  • CORS is a security mechanism that restricts how resources on a web page can be requested from another domain.
  • Cross-Origin refers to when the request is being made to a different origin (different domain, protocol, or port).

Why Does CORS Happen?

Without a Proxy: The Letter Analogy

Let’s say your web app (the frontend) is running on http://localhost:3000, and the backend API is running on http://localhost:2000. The frontend sends a request directly to the backend API, but the postal service (the browser) sees this as a request coming from different cities. This is considered cross-origin.

Since CORS is enforced by the browser, the postal service (browser) checks if the backend API allows the request from localhost:3000. If the backend doesn’t send the correct permissions (CORS headers), the browser blocks the request and throws a CORS error.

Here’s the flow:

  1. Frontend: http://localhost:3000 sends a request.
  2. Backend: http://localhost:2000 receives the request but doesn't send the CORS headers.
  3. Browser: Blocks the request due to lack of CORS headers.

The Role of Proxies: How They Solve CORS Issues

Now, let’s introduce the concept of a proxy using the same analogy.

With a Proxy: The Trusted Mail Service

Instead of sending your letter (request) directly from your house (frontend) to your friend’s house (backend), you send it to a trusted mail service that operates from the same city (same domain). This mail service (the proxy server) forwards the letter to your friend (the backend API) on your behalf.

Since the mail service is trusted (it’s from the same city as you), the postal service (the browser) doesn’t block it. The response comes back through the same mail service, and everything seems to have been sent within your own city, even though it actually passed through the proxy.

In technical terms:

  1. Frontend: Sends a request to http://localhost:3000/api/youtube (same origin).
  2. Development Server (Proxy): The proxy forwards the request to http://localhost:2000/api/youtube.
  3. Backend: The backend receives the request from the proxy and responds with the data.
  4. Browser: Sees the request as coming from localhost:3000 (no cross-origin request), so no CORS error occurs.

Why Proxies Are Used in Development

The proxy server is usually used during development to bypass CORS restrictions because it helps:

  • Avoid CORS issues by making the request appear to be from the same origin.
  • Improve security by hiding the real API endpoint.
  • Simplify local development without needing to modify the backend.

Note: This setup is only for development. In production, you have to configure the backend server to handle CORS properly, as proxies are typically not used in production environments.


Comparing Without Proxy vs. With Proxy

Without Proxy With Proxy
Browser sends a request directly to http://localhost:2000 Browser sends a request to http://localhost:3000/api/youtube
CORS issue arises due to the cross-origin request The proxy forwards the request internally to http://localhost:2000
The backend API must handle CORS headers No need for CORS headers in the frontend, as the browser treats it as same-origin
Browser blocks the request due to missing CORS headers Browser doesn’t block the request since it’s technically same-origin

Conclusion

In summary:

  • CORS is a security mechanism that prevents unauthorized cross-origin requests.
  • During development, a proxy helps you bypass CORS issues by forwarding requests to the backend without triggering cross-origin checks in the browser.
  • In production, you’ll need to configure your backend to handle CORS properly, as proxies are generally only used in development environments.

By using the proxy setup in development, you can focus on building your application without worrying about CORS, allowing smooth communication between your frontend and backend during testing and development.


Call to Action

Have you faced CORS issues while working on your frontend? How did you solve them? Let me know your thoughts or any questions you have about proxies in the comments below!


Top comments (0)