<?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>DEV Community: Hartaj-Singh-Dev</title>
    <description>The latest articles on DEV Community by Hartaj-Singh-Dev (@hartajsinghdev).</description>
    <link>https://dev.to/hartajsinghdev</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%2F611734%2Ff3c2d0ce-91e2-4301-9b92-4ab109752fb8.jpg</url>
      <title>DEV Community: Hartaj-Singh-Dev</title>
      <link>https://dev.to/hartajsinghdev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hartajsinghdev"/>
    <language>en</language>
    <item>
      <title>🔥 CORS explained: Error fixing on both client and server side</title>
      <dc:creator>Hartaj-Singh-Dev</dc:creator>
      <pubDate>Thu, 29 Dec 2022 13:11:31 +0000</pubDate>
      <link>https://dev.to/hartajsinghdev/cors-explained-error-fixing-on-both-client-and-server-side-5bb3</link>
      <guid>https://dev.to/hartajsinghdev/cors-explained-error-fixing-on-both-client-and-server-side-5bb3</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%2Fres.cloudinary.com%2Fpracticaldev%2Fimage%2Ffetch%2Fs--tRB8TzRI--%2Fc_imagga_scale%2Cf_auto%2Cfl_progressive%2Ch_420%2Cq_auto%2Cw_1000%2Fhttps%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fpokn6wy4nwkx7s5bkd9w.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%2Fres.cloudinary.com%2Fpracticaldev%2Fimage%2Ffetch%2Fs--tRB8TzRI--%2Fc_imagga_scale%2Cf_auto%2Cfl_progressive%2Ch_420%2Cq_auto%2Cw_1000%2Fhttps%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fpokn6wy4nwkx7s5bkd9w.png" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Prerequists:-
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Having knowledge about how HTTPS works &lt;/li&gt;
&lt;li&gt;Have basic knowledge how web works&lt;/li&gt;
&lt;li&gt;Have basic knowledge of server and client architecture 
___
So, either right now you are stuck at this error or once in your full stack web dev journey have faced it, well either case in this article we will fix it.
Before fixing any problem, the most crucial thing is to know what is the problem exactly and why is it occurring.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;CORS (cross-origin-resource-sharing) is a policy by web browsers for security purposes and also an option for HTTP request headers to a server to indicate an origin ( domain, URL ), which allows the browser to permit loading resources ( scripts or code ) from other servers.&lt;/p&gt;

&lt;p&gt;Now let’s use an example, let’s say you are building a website (full-fledged) and on the dashboard page of the website you want to access data from the database which is on this URL (&lt;a href="https://api.backend.com/%7Busername%7D" rel="noopener noreferrer"&gt;https://api.backend.com/{username}&lt;/a&gt;) from the origin (&lt;a href="https://frontend.com/dashboard" rel="noopener noreferrer"&gt;https://frontend.com/dashboard&lt;/a&gt;), Now overtop view is that from origin you will make a request to the backend for data using HTTP and server will give you data, now let’s break it down&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GET https://api.backend.com/{username} HTTP/1.1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here browser The web enforces something called the same-origin policy. By default, we can only access resources that are located at the same origin as the origin of our request! 💪🏼 It's totally okay to load an image that's located at &lt;a href="https://frontend.com/image1.png" rel="noopener noreferrer"&gt;https://frontend.com/image1.png&lt;/a&gt;. So now you will questioning how would you determine same origin and cross orgin .&lt;/p&gt;

&lt;h3&gt;
  
  
  Same origin:
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://frontend.com/%7Banything%7D" rel="noopener noreferrer"&gt;https://frontend.com/{anything}&lt;/a&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  Corss origin:
&lt;/h3&gt;

&lt;p&gt;different domain -&lt;br&gt;
 &lt;a href="https://api.backend.com/%7Banything%7D" rel="noopener noreferrer"&gt;https://api.backend.com/{anything}&lt;/a&gt;   &lt;/p&gt;

&lt;p&gt;different protocol -&lt;br&gt;
 &lt;a href="http://api.backend.com/%7Banything%7D" rel="noopener noreferrer"&gt;http://api.backend.com/{anything}&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;different subdomain -&lt;br&gt;
 &lt;a href="https://server.api.backend/%7Banything%7D" rel="noopener noreferrer"&gt;https://server.api.backend/{anything}&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;different port -&lt;br&gt;
   &lt;a href="https://api.backend.com:8080" rel="noopener noreferrer"&gt;https://api.backend.com:8080&lt;/a&gt;&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%2Fres.cloudinary.com%2Fpracticaldev%2Fimage%2Ffetch%2Fs--ZLRnzzX7--%2Fc_limit%252Cf_auto%252Cfl_progressive%252Cq_66%252Cw_880%2Fhttps%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0qe4yzasvrm7r0a76kui.gif" 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%2Fres.cloudinary.com%2Fpracticaldev%2Fimage%2Ffetch%2Fs--ZLRnzzX7--%2Fc_limit%252Cf_auto%252Cfl_progressive%252Cq_66%252Cw_880%2Fhttps%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0qe4yzasvrm7r0a76kui.gif" alt="same origin and corss origin animation"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So as you have idea to these policy, now as you already made a request to server it will by default have a response of&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; HTTP 404 code 
Access to fetched has been blocked by CORS policy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But now as you already are familiar with the problem now let’s fix this problem &lt;br&gt;
From server-side CORS , now edit server response with a header&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;response.setHeader(
“Allow-Control-Allow-Origin” , ”https://frontend.com”
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or if you are using node.js or anything framework for backend, then you can search for docs&lt;/p&gt;

&lt;p&gt;Awesome , now as you added this header into your server response , now same-origin policy won’t restrict us from getting Data&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%2Fres.cloudinary.com%2Fpracticaldev%2Fimage%2Ffetch%2Fs--vM2oftF6--%2Fc_limit%252Cf_auto%252Cfl_progressive%252Cq_66%252Cw_880%2Fhttps%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fakf0epavr00o2vo857lc.gif" 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%2Fres.cloudinary.com%2Fpracticaldev%2Fimage%2Ffetch%2Fs--vM2oftF6--%2Fc_limit%252Cf_auto%252Cfl_progressive%252Cq_66%252Cw_880%2Fhttps%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fakf0epavr00o2vo857lc.gif" alt="alt ext"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The CORS mechanism within the browser checks whether the value of the Access-Control-Allow-Origin header equals the value of the Origin that was sent by the request &lt;br&gt;
In this case, the origin of our request is &lt;a href="https://www.frontend.com" rel="noopener noreferrer"&gt;https://www.frontend.com&lt;/a&gt;, which is listed in the Access-Control-Allow-Origin response header!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;And&lt;/em&gt; if you are still having Issues with CORS , search for official docs of whatever tech you are using in backend.&lt;/p&gt;

&lt;h2&gt;
  
  
  Credentials
&lt;/h2&gt;

&lt;p&gt;Even if you are using token or cookies in your website , by default cookies are also on same-origin policy , even if you allow access to resources from cross origin , still by default cookies are not accessable to it , &lt;em&gt;To get access&lt;/em&gt; to cookies in cross-origin , you have to set another http header " Access-Control-Allow-Credentials  " &lt;br&gt;
 &lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fpracticaldev%2Fimage%2Ffetch%2Fs--JOa04OkM--%2Fc_limit%252Cf_auto%252Cfl_progressive%252Cq_66%252Cw_880%2Fhttps%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fvg3yo6qfqw12oh0f68yg.gif" 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%2Fres.cloudinary.com%2Fpracticaldev%2Fimage%2Ffetch%2Fs--JOa04OkM--%2Fc_limit%252Cf_auto%252Cfl_progressive%252Cq_66%252Cw_880%2Fhttps%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fvg3yo6qfqw12oh0f68yg.gif" alt="alt text"&gt;&lt;/a&gt;&lt;br&gt;
 All set! We can now include credentials in our cross-origin request 🥳&lt;/p&gt;

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

&lt;p&gt;Though , we all devs can agree , first time encountring CORS error is hell lot of frustrating ,but after a 15 min Read , it's all solved now  you make cross-origin requests safely , As this Blog was a short introduction to CORS , you can have much more on these resources &lt;a href="https://www.w3.org/wiki/CORS_Enabled" rel="noopener noreferrer"&gt;W3C Cors enabled&lt;/a&gt; and &lt;a href="https://livebook.manning.com/book/cors-in-action/part-1/" rel="noopener noreferrer"&gt;another one&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;** And that's it , Feel Free to Connect with Me 😀 **&lt;br&gt;
And also you can checkout my new blog &lt;br&gt;
&lt;a href="https://hxrtajwrites.vercel.app/" rel="noopener noreferrer"&gt;Hxrtaj Writes&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>opensource</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Hurry Up !! Developers A new supportive community is here</title>
      <dc:creator>Hartaj-Singh-Dev</dc:creator>
      <pubDate>Sat, 10 Apr 2021 06:31:41 +0000</pubDate>
      <link>https://dev.to/hartajsinghdev/hurry-up-developers-a-new-supportive-community-is-here-43n5</link>
      <guid>https://dev.to/hartajsinghdev/hurry-up-developers-a-new-supportive-community-is-here-43n5</guid>
      <description>&lt;p&gt;Only developers' is an online community for both newbie coders and professional Programmers , It is the place where a newbie can ask for help, and professionals can get their project team members and also help new coders with their projects. you can share your screen and seek help from professionals Our Clear vision is to help programmers to master their skill set and tackle errors. If you join the community then it would be very beneficial to you also and other developers also, In the server there would be other developers with whom you can team up and do exciting projects and also we have voice channels where you can share the screen and take help with your error, Comm'n all developers we can create a community where we help each other and grow, hurry up join now or otherwise you will lose the opportunity, discord Link:- &lt;a href="https://discord.gg/d2X8jxyj6y"&gt;https://discord.gg/d2X8jxyj6y&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Developers just try to join the server, I bet you will not regret it, There are specific Voice Channels where you can share your screen and ask for help ,Hope You will definitely Look after this&lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>fullstack</category>
      <category>community</category>
    </item>
  </channel>
</rss>
