<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Bilal Ul Haque</title>
    <description>The latest articles on Forem by Bilal Ul Haque (@bilalulhaque).</description>
    <link>https://forem.com/bilalulhaque</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1138172%2F9515a120-51a8-412e-841d-c80f552f754c.JPG</url>
      <title>Forem: Bilal Ul Haque</title>
      <link>https://forem.com/bilalulhaque</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/bilalulhaque"/>
    <language>en</language>
    <item>
      <title>Ngrok: Exposing local server on the internet</title>
      <dc:creator>Bilal Ul Haque</dc:creator>
      <pubDate>Tue, 19 Mar 2024 05:58:08 +0000</pubDate>
      <link>https://forem.com/bilalulhaque/ngrok-exposing-local-server-on-the-internet-3dej</link>
      <guid>https://forem.com/bilalulhaque/ngrok-exposing-local-server-on-the-internet-3dej</guid>
      <description>&lt;p&gt;Have you ever wanted to expose your local server to the internet for development purposes in a simple way? The answer is probably yes. I faced the exact same issue when I needed to expose my local server. Today, I’m going to discuss one of the solutions that caught my attention and is easy to use. First of all, let me briefly explain my problem.&lt;/p&gt;

&lt;p&gt;As a Backend Engineer. I was working on a product that involved integration of payment gateways like Stripe, PayPal, etc. As some of you may know, we need to listen to webhooks to trigger various events in our application based on the events sent by the webhook. The main issue occurred was that the project was still in development phase and the payment gateway webhook couldn’t access the local server as it requires a public HTTPS URL. That’s when I had to use ngrok to expose my local webserver to the internet allowing the local server to listen to the events sent via the webhook.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding WebHooks:
&lt;/h2&gt;

&lt;p&gt;Webhook is a mechanism that allows one application to send real-time data to another when a specific event occurs. This asynchronous communication is useful for scenarios where immediate updates or responses are needed, such as in chat apps, payment gateways, etc.&lt;/p&gt;

&lt;p&gt;Webhooks operate on a simple principle that when an event happens in the source application, it sends an HTTP POST request to a predefined URL in the destination application. This URL is often called the &lt;strong&gt;“callback URL”&lt;/strong&gt; or &lt;strong&gt;“endpoint.”&lt;/strong&gt; The destination application then processes the incoming data and executes the necessary actions.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Ngrok?
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Ngrok&lt;/strong&gt; is a multi-platform tunnelling tool that creates secure tunnels to localhost, making local servers accessible from the internet. It simplifies the process of exposing local services, such as web servers, to external networks, enabling developers to test and interact with webhooks in a real-world environment.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frjvdq74u6yc4mkndkm1o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frjvdq74u6yc4mkndkm1o.png" alt="Architecture diagram" width="714" height="424"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Key Features of Ngrok:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Secure Tunnelling:&lt;/strong&gt; Establishes secure tunnels using HTTPS, ensuring that data transferred between the local server and external services is encrypted.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Port Forwarding:&lt;/strong&gt; Allows developers to expose specific local ports, redirecting incoming requests to the corresponding services.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Public URLs:&lt;/strong&gt; It generates public URLs for the exposed local servers, eliminating the need for a public IP address or domain name.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Inspection and Replay:&lt;/strong&gt; Offers a web-based interface for developers to inspect HTTP traffic and replay requests. This feature is particularly useful for debugging and analysing webhook payloads.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Authentication and Access Control:&lt;/strong&gt; Enables developers to add authentication to their tunnels, ensuring that only authorized users can access the exposed services.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Custom Subdomains:&lt;/strong&gt; By providing the option to use custom subdomains for the public URLs, it adds flexibility to the testing environment.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Ngrok Use Cases:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Web Development:&lt;/strong&gt; Allows to make your local server accessible via a public URL, so that any device from any location can access it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Webhooks Testing:&lt;/strong&gt; Most of our application relies on webhooks to receive data or events from external services. Ngrok enables the exposure of your local server to the internet, allowing the application to listen to incoming webhook endpoints.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Demonstration Purposes:&lt;/strong&gt; It gives a temporary public URL, which enables us to showcase local development environment for demonstrations.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Ngrok&lt;/strong&gt; serves as a powerful tool for simplifying webhook development and testing. Its ability to create secure tunnels, generate public URLs, and provide advanced features makes it an amazing resource for developers working on applications that require real-time communication.&lt;br&gt;
Integrating Ngrok into your development process streamlines the process of exposing local servers, testing webhooks, and debugging applications.&lt;/p&gt;

</description>
      <category>webhook</category>
      <category>webdev</category>
      <category>softwareengineering</category>
      <category>security</category>
    </item>
    <item>
      <title>Exploring DNS</title>
      <dc:creator>Bilal Ul Haque</dc:creator>
      <pubDate>Tue, 05 Dec 2023 06:32:47 +0000</pubDate>
      <link>https://forem.com/bilalulhaque/exploring-dns-37bf</link>
      <guid>https://forem.com/bilalulhaque/exploring-dns-37bf</guid>
      <description>&lt;p&gt;Exploring DNS: Understanding the Communication Between Domain Names and IP Addresses!&lt;/p&gt;

&lt;p&gt;Ever wondered how, when you type "example.com" in your browser, what happens behind the scenes? 🌐 That's where DNS (Domain Name System) steps in.&lt;/p&gt;

&lt;p&gt;DNS, which stands for Domain Name System, plays a crucial role in facilitating user access to websites by translating domain names into IP addresses.&lt;br&gt;
When a user enters a website like example.com in the search bar, the DNS server performs the essential task of converting the website name into its corresponding IP address and promptly delivers it to the client. This IP address enables the web browser to establish communication with the web server, ultimately retrieving the requested data.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EkTAH9df--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mh8yjh8r2vyvti3pzlg2.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EkTAH9df--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mh8yjh8r2vyvti3pzlg2.gif" alt="dns flow diagram" width="694" height="552"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>domain</category>
      <category>webdev</category>
      <category>opensource</category>
      <category>cloud</category>
    </item>
    <item>
      <title>Unlocking Efficiency: A Deep Dive into Redis as an In-Memory Datastore</title>
      <dc:creator>Bilal Ul Haque</dc:creator>
      <pubDate>Tue, 21 Nov 2023 06:18:56 +0000</pubDate>
      <link>https://forem.com/bilalulhaque/unlocking-efficiency-a-deep-dive-into-redis-as-an-in-memory-datastore-4n39</link>
      <guid>https://forem.com/bilalulhaque/unlocking-efficiency-a-deep-dive-into-redis-as-an-in-memory-datastore-4n39</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gxVoVOAA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3xfv7ep9acpfjgkk6rf9.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gxVoVOAA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3xfv7ep9acpfjgkk6rf9.jpg" alt="Cover-image" width="720" height="405"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction:
&lt;/h2&gt;

&lt;p&gt;In today’ s world, web applications require a lot of things up and running on the server to serve the purpose of users. Unfortunately, many developers might not be aware of factors that could ruin the user experience. The quality of code optimization, server response, and database integration all play crucial roles, and this can vary depending on the chosen framework or programming language. However, developers have the power to boost their application’s performance significantly through optimization. One key optimization is ensuring an efficient database connection for speedy responses. This is where an &lt;strong&gt;In-memory datastore&lt;/strong&gt; comes into play.&lt;/p&gt;

&lt;p&gt;An in-memory data store is a type of database or data storage system that primarily relies on keeping data in the system’s main memory for data storage and retrieval, in contrast to traditional databases that store data on disk drives. This approach allows for faster data access and retrieval since reading and writing to system’s memory is much quicker than accessing data from disk storage. In-memory data stores are commonly used in various applications, ranging from caching mechanisms to real-time analytics and high-performance computing.&lt;/p&gt;

&lt;p&gt;In this article we’ll be looking at one of the most popular in-memory data store &lt;strong&gt;Redis&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zU_UQhZ0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/voay60zut9a33jc9tuv6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zU_UQhZ0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/voay60zut9a33jc9tuv6.png" alt="red-logo" width="800" height="267"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Redis&lt;/strong&gt; is an open-source, in-memory data structure store, used as a distributed, in-memory key-value database, cache, and message broker, with optional durability. Redis supports different kinds of abstract data structures, such as strings, lists, maps, sets, sorted sets, etc.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use Cases:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Caching:&lt;/strong&gt; Redis is an ideal choice for establishing a high-performance, in-memory cache to reduce data access latency and boost throughput. Its ability to deliver responses in milliseconds contributes to its impressive speed, and its scalability allows for handling increased loads without substantial backend costs. Utilizing Redis for caching purposes is common in scenarios such as caching database query results, persisting session data, storing web pages, and caching frequently accessed objects like images, files, and metadata.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4XlA4iJf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jztnlgve8r2q460s0vbh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4XlA4iJf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jztnlgve8r2q460s0vbh.png" alt="redis-caching" width="800" height="475"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Messaging and Queue:&lt;/strong&gt; Redis has a great support for Pub/Sub (Publish and Subscribe) along with pattern matching and a diverse set of data structures. This functionality empowers Redis to facilitate high-performance messaging, chat rooms, and server intercommunication. Utilizing Redis List data structure, we’ve implemented a lightweight queue that proves advantageous for applications requiring a dependable message broker. Lists, with their great operations and blocking capabilities, are well-suited for various scenarios where reliability is important.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LHI6W51r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j08q04uvqxf510i778ae.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LHI6W51r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j08q04uvqxf510i778ae.png" alt="redis-queue" width="800" height="339"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Session Store:&lt;/strong&gt; For scalable web applications, Redis stands out as the optimal selection due to its in-memory support. It delivers sub-millisecond latency, scalability, and resilience essential for handling session data such as session state, credentials, and other critical information.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zDsiNpU9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/78z16nmnz14l4xfxiwvo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zDsiNpU9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/78z16nmnz14l4xfxiwvo.png" alt="redis-session" width="800" height="352"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Different types of Redis Architecture:
&lt;/h2&gt;

&lt;p&gt;Redis provides multiple architectural options to deal with different scalability and fault-tolerance requirements.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Standalone:&lt;/strong&gt;&lt;br&gt;
In its standalone mode, Redis operates as a singular instance, offering a straightforward and uncomplicated deployment choice that is well-suited for smaller-scale applications or development environments. However, it presents a single point of failure, implying that if the instance experiences downtime, the entire Redis system would immediately stop functioning.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--C4OnP4Hg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sxxjx2tsw74e58neruzu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--C4OnP4Hg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sxxjx2tsw74e58neruzu.png" alt="standalone-architecture" width="595" height="491"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Master-Slave Replication:&lt;/strong&gt;&lt;br&gt;
Redis incorporates master-slave replication, allowing for the existence of multiple instances. In this configuration, one Redis instance serves as the master, managing both read and write operations, while several slave instances replicate data from the master and are exclusively utilized for read operations. This arrangement improves data availability and enhances read scalability.&lt;br&gt;
But only one node can be the master and the number of slaves nodes can be incremented according to the requirement.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BVhO3pcv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lvmcfa17bfcxiglrrsn9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BVhO3pcv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lvmcfa17bfcxiglrrsn9.png" alt="redis-masterSlave" width="721" height="491"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Redis Sentinel:&lt;/strong&gt;&lt;br&gt;
This architecture serves as a high-availability solution that introduces automatic failover and monitoring capabilities to the master-slave replication architecture. In simple terms, in a traditional Master-slave setup, if the master goes down, human intervention is needed to promote one of the slaves to become the new master since write operations are halted. Sentinel streamlines this process by automating the promotion of a slave node to the master, eliminating the need for manual intervention. Redis Sentinel utilizes a special TCP port, typically set to 26379 by default. This port facilitates communication among sentinels, allowing them to monitor the health of the Redis node. If the master node becomes unresponsive via the TCP port, sentinels collaborate, and the one receiving the most votes assumes the leadership role. The leader Sentinel then initiates the promotion of one of the slaves to take over as the new master.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Redis Cluster:&lt;/strong&gt;&lt;br&gt;
Redis Cluster represents a more advanced architecture compared to Master-Slave and Redis Sentinel. It surpasses them in power, robustness, and efficiency, particularly during failovers. Redis Cluster features multiple master nodes, enhancing its overall performance.&lt;/p&gt;

&lt;p&gt;In this architecture, data is divided into 16384 hash slots, each assigned to a single master node. The number of hash slots increases with the addition of more master nodes. When a client sends a request to Redis, the request is directed to a specific hash slot based on the shard key provided.&lt;/p&gt;

&lt;p&gt;The sharding process involves generating a hash key (shard key) from the key sent to Redis. This shard key determines the slots to which the request should be directed. It’s important to note that each slot functions as a Redis sentinel, enabling automatic recovery of the master node in case of a failure. This design enhances the resilience and reliability of Redis Cluster.&lt;/p&gt;

&lt;p&gt;Redis cluster is highly scalable when it comes for large enterprise application since the load is now distributed to the different slots based on sharding and can process more number of request at once.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7q4vmGhP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wap0j169txhbaf8kcgzj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7q4vmGhP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wap0j169txhbaf8kcgzj.png" alt="redis-cluster" width="800" height="565"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion:
&lt;/h2&gt;

&lt;p&gt;In conclusion, Redis is a robust and versatile in-memory data store, renowned for its speed, efficiency, and adaptability. Whether serving as a caching solution or a message broker, Redis excels in providing low-latency access, scalability, and resilience. With features like Pub/Sub, master-slave replication, and Redis Sentinel for high availability, it caters to a wide range of applications. From small-scale to internet-scale deployments, Redis remains a top choice for developers and businesses seeking a reliable and high-performance data management solution. Its simplicity, speed, and flexibility position Redis as a fundamental component in the realm of modern computing.&lt;/p&gt;

</description>
      <category>api</category>
      <category>website</category>
      <category>performance</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Cross-Origin Resource Sharing (CORS)</title>
      <dc:creator>Bilal Ul Haque</dc:creator>
      <pubDate>Mon, 13 Nov 2023 15:55:21 +0000</pubDate>
      <link>https://forem.com/bilalulhaque/cross-origin-resource-sharing-cors-3go8</link>
      <guid>https://forem.com/bilalulhaque/cross-origin-resource-sharing-cors-3go8</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F58xecdt8vwf3pghv6e0e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F58xecdt8vwf3pghv6e0e.png" alt="CORS main image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Consider the following scenario: You attempt to use your website’s GET() endpoint to retrieve some data from an API, but an error results.&lt;/p&gt;

&lt;p&gt;The Access-Control-Allow-Origin header has a value of “some URL” that is not equal to the supplied origin,” or “The Access-Control-Allow-Origin header has a value of “No Access-Control-Allow-Origin header is present on the requested resource,” written in red text, indicating that your request was denied due to CORS policy.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkdgltmr85d7v69cxizb7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkdgltmr85d7v69cxizb7.png" alt="Client server communication"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is CORS?
&lt;/h2&gt;

&lt;p&gt;CORS (Cross-Origin Resource Sharing) is a method that permits resources to be loaded from one origin to another while preserving the security and integrity of the website. Popular web browsers like Chrome and Mozilla Firefox use this security technique to determine which cross-site requests are secure.&lt;/p&gt;

&lt;p&gt;Modern browsers use the same-origin policy to limit access from scripts to resources outside their domain for security concerns. This policy was created in reaction to malicious activities including cross-site request forgery (CSRF) assaults and theft of personal information from other web servers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why does the same origin exist?
&lt;/h2&gt;

&lt;p&gt;You might use cookies, like many websites, to remember login information or session details. When they are created, those cookies are restricted to a specific domain. The browser will include the cookies made specifically for that domain with each HTTP request to that domain. This occurs on every HTTP request, whether those for AJAX, HTML sites, or static images.&lt;/p&gt;

&lt;p&gt;This indicates that a cookie is stored for &lt;a href="https://exampleapi.com" rel="noopener noreferrer"&gt;https://exampleapi.com&lt;/a&gt; when you log into https:// exampleapi.com. The bank may have developed a REST API at https:// exampleapi.com/api if it is a single-page React application so that the SPA can interface with it using AJAX.&lt;/p&gt;

&lt;p&gt;Unfortunately, developers that want to fetch different resources from various origins found this same-origin constraint to be somewhat restrictive. The CORS HTTP protocol was created to inform browsers to allow restricted resources on a web page to be requested from other domains in order to loosen restrictions a little. Here is an example of a case where information could be requested from an external source, such as an API (this is a very standard practice for client-side JavaScript code):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Using CORS headers, the resource origin sends a preflight request to the external web server.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The preflight request is then validated by the external web server to ensure that scripts are authorized to make the request.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The external web server replies with its own set of HTTP headers that specify the allowed request methods, origins, and custom headers after the request has been confirmed. Information on whether it is appropriate to pass along credentials, such as authentication headers, may also be included in this final server response.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why do we need CORS?
&lt;/h2&gt;

&lt;p&gt;A script that runs in a user’s browser would typically only ever need to access resources from the same origin (think about API calls to the same backend that served the JavaScript code in the first place). Therefore, it is good for security that JavaScript typically cannot access resources on other origins.&lt;/p&gt;

&lt;p&gt;“Other origins” here refers to the fact that the URL being viewed is different from the location where the JavaScript is being executed by having:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A different scheme (HTTP or HTTPS)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;An alternative domain or port&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cross-origin access, however, may be required or even desired in certain situations. As in the case of a React SPA that communicates with an API backend that is hosted on a different domain.&lt;/p&gt;

&lt;p&gt;As you can see, CORS is an important protocol for making cross-domain requests possible, in cases where there’s legitimate need.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does CORS work?
&lt;/h2&gt;

&lt;p&gt;CORS requests come in two types: simple requests and preflighted requests. The criteria for determining if a request is preflighted are discussed later:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Simple requests:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A CORS request that doesn’t need to go through any preflight requests (initial checks) is referred to as a simple request.&lt;/li&gt;
&lt;li&gt;AJAX request is started by a browser tab that is open to &lt;a href="https://www.mydomain.com" rel="noopener noreferrer"&gt;https://www.mydomain.com&lt;/a&gt;. GET widgets at &lt;a href="https://api.mydomain.com" rel="noopener noreferrer"&gt;https://api.mydomain.com&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;The browser automatically inserts the Origin Request Header along with headers like Host for cross-origin requests&lt;/li&gt;
&lt;li&gt;The Origin request header is examined by the server. The Access-Control-Allow-Origin is set to the value in the request header Origin if the Origin value is allowed.&lt;/li&gt;
&lt;li&gt;The Access-Control-Allow-Origin header is checked by the browser to verify if it matches the origin of the tab when it receives the answer. If not, the reply is suppressed. If the Access-Control-Allow-Origin either contains the wildcard * operator or exactly matches the single origin, the check succeeds, as in this example.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Preflight requests:&lt;/strong&gt;&lt;br&gt;
The other kind of CORS request is a preflight. A preflight request is a CORS request in which the browser must first make a preflight request (also known as a preliminary probe) to the server to determine whether the original CORS request can be processed. It is an additional HTTP request using the OPTIONS method. The browser performs this for every unsafe request that is intended to alter data — for example, POST, PUT, or DELETE requests.&lt;/p&gt;

&lt;p&gt;We refer to the original CORS request as preflighted because it was preceded by a preflight request.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Common circumstances necessitating preflighted requests:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The Content-Type header for a website’s AJAX call to POST JSON data to a REST API is application/json.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A website makes an AJAX call to an API and includes a token in the request header to authenticate the API. Authorization&lt;/li&gt;
&lt;li&gt;This means that a REST API frequently has the bulk of AJAX requests preflighted when it powers a single-page application.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpfflsn0ov3fij0dxfnzx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpfflsn0ov3fij0dxfnzx.png" alt="Process communication"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Simple requests vs. pre-flighted requests:&lt;/strong&gt;&lt;br&gt;
Simple requests are defined as those that do not result in a CORS preflight. However, after the request has been determined to be a basic request, some requirements must be met. These circumstances are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The request’s HTTP method needs to be one of the following: head, Post, or Get&lt;/li&gt;
&lt;li&gt;Except for the headers that are automatically set by the user agent, the request headers should only contain CORS safe-listed headers like Accept, Accept-Language, Content-Language, and Content-Type.&lt;/li&gt;
&lt;li&gt;Only one of these three values, total, should be present in the Content-Type header: Multipart/form-data, application/x-www-form-URLencoded, or text/plain&lt;/li&gt;
&lt;li&gt;The XMLHttpRequest returned object has no registered event listeners.&lt;/li&gt;
&lt;li&gt;XMLHttpRequest must use the upload attribute.&lt;/li&gt;
&lt;li&gt;There shouldn’t be any Readable Stream objects in the request.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The request is regarded as a pre-flighted request if either of these requirements is not met. For these requests, the browser must first submit an OPTIONS method request to the alternative origin.&lt;/p&gt;

&lt;p&gt;This is used to determine whether the actual request can be sent to the server without risk. The response headers to the pre-flighted request determine whether the real request is approved or denied. The request is not sent if the main request’s headers and these response headers conflict.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Enabling CORS:&lt;/strong&gt;&lt;br&gt;
Suppose we faced the CORS error. There are many ways we could solve this issues depending on our access to the server on which the resources are hosted. There are two ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Access to the backend server&lt;/li&gt;
&lt;li&gt;Access to only the frontend&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Access to the backend:&lt;/strong&gt;&lt;br&gt;
You may set up the server to respond with the proper headers to enable resource sharing across various origins because CORS is essentially an HTTP header-based method. Look at the CORS headers we covered previously, and adjust the headers as necessary.&lt;/p&gt;

&lt;p&gt;For example in node.js+express.js application:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8qelc89pht2u7v1c66p4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8qelc89pht2u7v1c66p4.png" alt="backend 1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The default configuration will be applied if a CORS configuration object is not given, which is identical to:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F62sv4q9nu6yd7iytdjme.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F62sv4q9nu6yd7iytdjme.png" alt="backend 2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The following describes how to set up CORS on your server such that it will only accept GET requests from &lt;a href="https://example.com" rel="noopener noreferrer"&gt;https://example.com&lt;/a&gt; with the headers Content-Type and Authorization and a preflight cache time of 10 minutes:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdzv8pjt6qf75bkzb1sj8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdzv8pjt6qf75bkzb1sj8.png" alt="backend 3"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Access to the front end:&lt;/strong&gt;&lt;br&gt;
If you don’t have access to the backend server like a public API. Due to this, you cannot add headers to the response received. So in that case you could use a proxy server that’ll add the CORS headers to the proxied request. The proxy server is available at &lt;a href="https://cors-anywhere.herokuapp.com/" rel="noopener noreferrer"&gt;https://cors-anywhere.herokuapp.com/&lt;/a&gt;. You can also build your own proxy server.&lt;/p&gt;

&lt;p&gt;Instead of directly making the request to the server, simply append the proxy server’s URL to the start of the API’s URL.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk92lu82a8fws8s09whvc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk92lu82a8fws8s09whvc.png" alt="frontend 1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfalls:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Using the Access-Control-Allow-Origin (all) operator:&lt;/strong&gt;&lt;br&gt;
The same-origin criterion is overridden by CORS in order to ensure security. While utilising *, the majority of CORS’ security rules are disabled. Wildcard is permissible in some circumstances, such as with an open API that is integrated into various websites hosted by third parties. The security may be enhanced by giving the API its own domain. For instance, your open API &lt;a href="https://api.example.com" rel="noopener noreferrer"&gt;https://api.example.com&lt;/a&gt; may respond with Access-Control-Allow-Origin while your main website’s API &lt;a href="https://www.example.com/api" rel="noopener noreferrer"&gt;https://www.example.com/api&lt;/a&gt; may still respond with Access-Control-Allow-Origin: &lt;a href="https://www.example.com" rel="noopener noreferrer"&gt;https://www.example.com&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Multiple domains are returned for the Access-Control-Allow-Origin header:&lt;/strong&gt;&lt;br&gt;
Unfortunately, the standard does not allow the Access-Control-Allow-Origin: https:// example.com, &lt;a href="https://www.example.com" rel="noopener noreferrer"&gt;https://www.example.com&lt;/a&gt;. Although the server can only return one domain or *, the Origin request header can be used.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Choosing a wildcard, such as *.example.com:&lt;/strong&gt;&lt;br&gt;
This is not covered by the CORS specification because wildcard can only be used to imply that all domains are permitted.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Not including non-standard or protocol ports:&lt;/strong&gt;&lt;br&gt;
Access-Control-Allow-Origin: example.com is invalid since the protocol is absent.&lt;br&gt;
Similar to this, you will encounter problems with Access-Control-Allow-Origin: localhost unless the server is actually running on a normal HTTP port like: 80.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Ignoring the Origin header in the Vary response:&lt;/strong&gt;&lt;br&gt;
Most CORS frameworks do this automatically, but you still need to let clients know that server responses differ based on where the request came from.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Failure to define Access-Control-Expose-Headers:&lt;/strong&gt;&lt;br&gt;
If a required header is absent, the CORS request will still be successful, but any response headers that are not whitelisted will be obscured in the browser tab. The following common response headers are always made available for CORS requests:&lt;br&gt;
Cache-Control, Content-Type, Language, Expires, and Last-Modified&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;When Access-Control-Allow-Credentials is set to true, using a wildcard:&lt;/strong&gt;&lt;br&gt;
Many people become entangled in this complex subject. The wildcard operator cannot be used on any of the response headers, including Access-Control-Allow-Origin, if the response includes Access-Control-Allow-Credentials: true.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>cors</category>
      <category>security</category>
      <category>api</category>
      <category>website</category>
    </item>
  </channel>
</rss>
