{"id":41,"date":"2022-10-28T00:16:07","date_gmt":"2022-10-28T00:16:07","guid":{"rendered":"https:\/\/blogs.oregonstate.edu\/obataf\/?p=41"},"modified":"2022-10-28T02:30:32","modified_gmt":"2022-10-28T02:30:32","slug":"post-3-battle-of-the-cors","status":"publish","type":"post","link":"https:\/\/blogs.oregonstate.edu\/obataf\/2022\/10\/28\/post-3-battle-of-the-cors\/","title":{"rendered":"Post #3: Battle of the CORS"},"content":{"rendered":"\n<p>Ever heard of CORS? For those who are not familiar with web development, CORS may be a new concept. <\/p>\n\n\n\n<p>It stands for &#8220;<strong>C<\/strong>ross-<strong>O<\/strong>rigin <strong>R<\/strong>esource <strong>S<\/strong>haring&#8221; and there are great online resources that go over the concept such on the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/CORS\">MDN web docs<\/a> or a quick overview provided on the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Cross-origin_resource_sharing\">wikipedia<\/a> page. Here&#8217;s a brief description taken from the wikipedia article on CORS:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Cross-origin resource sharing (CORS) is a mechanism that allows restricted resources on a web page to be requested from another domain outside the domain from which the first resource was served.<\/p><cite><a href=\"https:\/\/en.wikipedia.org\/w\/index.php?title=Cross-origin_resource_sharing&amp;oldid=1113351727\">&#8211; Cross-origin resource sharing on Wikipedia <\/a><\/cite><\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">So What Exactly Is It?<\/h2>\n\n\n\n<p>Say you created a full-stack web application and you want to test it out locally. You&#8217;ve selected two different ports to run the frontend and backend:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>http:\/\/localhost:3000\/ for frontend<\/li><li>http:\/\/localhost:8080\/ for backend<\/li><\/ol>\n\n\n\n<p>Frontend wants to fetch some data that&#8217;s stored in a backend server that&#8217;s hosting a database, which is currently running on the address http:\/\/localhost:8080\/ so frontend decides to send a <code><mark style=\"background-color:#7bdcb5\" class=\"has-inline-color\">GET<\/mark><\/code> request to the backend with http:\/\/localhost:3000\/ as the origin point.<\/p>\n\n\n\n<p>Depending on how the backend is setup, one of the three responses will be returned:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Send back the requested data along with <code>Access-Control-Allow-Origin<\/code> (ACAO) header in its response, letting the requester (frontend) know that request from that particular origin (http:\/\/localhost:3000\/) is allowed. <ul><li>It may look like:<br><mark style=\"background-color:#7bdcb5\" class=\"has-inline-color has-black-color\"><code>Access-Control-Allow-Origin: http:\/\/localhost:3000\/<\/code><\/mark><\/li><\/ul><\/li><li>Send back the requested data along with ACAO header in its response, letting the requester know that requests from all domains are allowed with a wildcard (<code><mark style=\"background-color:#7bdcb5\" class=\"has-inline-color\">*<\/mark><\/code>). <ul><li>It may look like:<br><mark style=\"background-color:#7bdcb5\" class=\"has-inline-color has-black-color\"><code>Access-Control-Allow-Origin: *<\/code><\/mark><\/li><\/ul><\/li><li>An error if the server does not allow a cross-origin request<ul><li>If you have the Web Developer Tool on your browser open, you may see it in the console<\/li><\/ul><\/li><\/ol>\n\n\n\n<p>It&#8217;s a way to prevent unauthorized (and unintended) HTTP requests from being made; <a href=\"https:\/\/en.wikipedia.org\/wiki\/Same-origin_policy\">same-origin security policy<\/a> is also in place for majority of the major browsers used today in order to protect the users of these browsers from ending up in a wrong place. Browsers don&#8217;t want users to end up like Dorothy telling Toto &#8220;<em>I&#8217;ve a feeling we&#8217;re not in Kansas anymore<\/em>&#8221; like in <em>The Wizard of Oz<\/em>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">My First Hand Experience<\/h2>\n\n\n\n<p>As a naive, newbie developer that only had few experiences of coding my own personal website prior to attending OSU, when I took CS290 (Web Development) and CS361 (Software Development I) and was tasked to develop a full-stack web app during these classes, I was destined to encounter and figure out how to handle CORS. <\/p>\n\n\n\n<p><span style=\"text-decoration: underline\">Spoiler alert<\/span>: I did.<\/p>\n\n\n\n<p>Even after reaching a point where I&#8217;m currently developing a capstone project with my teammate, CORS has still made a comeback and managed to block our path. Since my teammate and myself have looked into this for a good few hours one afternoon\/evening, I&#8217;ll be sharing how we resolved it (and later found more material on it).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Error<\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"115\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6060\/files\/2022\/10\/cors-1024x115.png\" alt=\"\" class=\"wp-image-43\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6060\/files\/2022\/10\/cors-1024x115.png 1024w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6060\/files\/2022\/10\/cors-300x34.png 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6060\/files\/2022\/10\/cors-768x86.png 768w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6060\/files\/2022\/10\/cors-1536x173.png 1536w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6060\/files\/2022\/10\/cors-2048x230.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Hello CORS error, my old friend, I&#8217;ve come to talk with you again<\/figcaption><\/figure>\n\n\n\n<p>So what exactly is going on here? For quick context, we&#8217;ve decided to create a React-based frontend with a backend server that&#8217;s run on node.js and express for our capstone project (called Kaizen Manager). <\/p>\n\n\n\n<p>When one of our team members pushed the change to the Github for our project and set up the Pull Request, I decided to test the frontend to backend connection locally, but encountered the above error when we thought we appropriately configured each end of the web app.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"552\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6060\/files\/2022\/10\/image-2-1024x552.png\" alt=\"\" class=\"wp-image-45\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6060\/files\/2022\/10\/image-2-1024x552.png 1024w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6060\/files\/2022\/10\/image-2-300x162.png 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6060\/files\/2022\/10\/image-2-768x414.png 768w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6060\/files\/2022\/10\/image-2-1536x828.png 1536w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6060\/files\/2022\/10\/image-2-2048x1105.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>This is the frontend if the backend isn&#8217;t properly getting fetched<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6060\/files\/2022\/10\/image-3-1024x718.png\" alt=\"\" class=\"wp-image-46\" width=\"674\" height=\"472\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6060\/files\/2022\/10\/image-3-1024x718.png 1024w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6060\/files\/2022\/10\/image-3-300x210.png 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6060\/files\/2022\/10\/image-3-768x538.png 768w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6060\/files\/2022\/10\/image-3-1536x1076.png 1536w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6060\/files\/2022\/10\/image-3-2048x1435.png 2048w\" sizes=\"auto, (max-width: 674px) 100vw, 674px\" \/><figcaption>This is the frontend if we successfully get the backend data<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"337\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6060\/files\/2022\/10\/image-1-1024x337.png\" alt=\"\" class=\"wp-image-44\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6060\/files\/2022\/10\/image-1-1024x337.png 1024w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6060\/files\/2022\/10\/image-1-300x99.png 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6060\/files\/2022\/10\/image-1-768x253.png 768w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6060\/files\/2022\/10\/image-1-1536x505.png 1536w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6060\/files\/2022\/10\/image-1.png 1568w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Backend server that&#8217;s on localhost:8080\/ is running fine when we directly access it, and GET request to the \/api endpoint returns the message (data) we&#8217;re looking for<\/figcaption><\/figure>\n\n\n\n<p>As we can see in the first image, we&#8217;re encountering CORS issue. The backend server on <code>localhost:8080\/<\/code> isn&#8217;t accepting the request getting sent by frontend over on <code>localhost:3000\/<\/code>. What should we do?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Solutions<\/h2>\n\n\n\n<p>We&#8217;ve come across few solutions over the course of the CORS battle, and I&#8217;ll be sharing two of the solutions we&#8217;ve found and those that we tried with our project. <\/p>\n\n\n\n<p>One way is using the <a href=\"https:\/\/www.npmjs.com\/package\/cors\">cors<\/a> package, the other way is to simply define a proxy.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/www.npmjs.com\/package\/cors\">cors<\/a> package<\/h3>\n\n\n\n<p><a href=\"https:\/\/www.npmjs.com\/\">npm<\/a> is a package manager for Javascript programming language and you can find useful packages created by other developers for you to use in your code.  <a href=\"https:\/\/www.npmjs.com\/package\/cors\">cors<\/a> is one of such handy packages and by installing it to your project, it enables CORS.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>CORS is a node.js package for providing a <a href=\"http:\/\/www.senchalabs.org\/connect\/\">Connect<\/a>\/<a href=\"http:\/\/expressjs.com\/\">Express<\/a> middleware that can be used to enable <a href=\"http:\/\/en.wikipedia.org\/wiki\/Cross-origin_resource_sharing\">CORS<\/a> with various options.<\/p><cite><a href=\"https:\/\/www.npmjs.com\/package\/cors\">&#8211; Readme of the cors Module<\/a><br><\/cite><\/blockquote>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"437\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6060\/files\/2022\/10\/image-5-1024x437.png\" alt=\"\" class=\"wp-image-50\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6060\/files\/2022\/10\/image-5-1024x437.png 1024w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6060\/files\/2022\/10\/image-5-300x128.png 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6060\/files\/2022\/10\/image-5-768x327.png 768w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6060\/files\/2022\/10\/image-5.png 1060w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>So you can install and mark as a dependency to your backend project directory (in package.json)<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"625\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6060\/files\/2022\/10\/image-6-1024x625.png\" alt=\"\" class=\"wp-image-51\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6060\/files\/2022\/10\/image-6-1024x625.png 1024w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6060\/files\/2022\/10\/image-6-300x183.png 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6060\/files\/2022\/10\/image-6-768x468.png 768w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6060\/files\/2022\/10\/image-6.png 1092w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Import cors into your file that&#8217;s running the backend server, and if you are using express, use it. Alternatively you can also try to specifically only accept a particular origin, e.g. localhost:3000\/<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"507\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6060\/files\/2022\/10\/image-7-1024x507.png\" alt=\"\" class=\"wp-image-52\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6060\/files\/2022\/10\/image-7-1024x507.png 1024w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6060\/files\/2022\/10\/image-7-300x149.png 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6060\/files\/2022\/10\/image-7-768x380.png 768w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6060\/files\/2022\/10\/image-7.png 1086w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Then from the frontend (this is the App.js file of the React app), point to the backend server<\/figcaption><\/figure>\n\n\n\n<p>Then, the <code>cors<\/code> module will handle the rest. It&#8217;ll allow the request coming from any origin (based on the wildcard <code><mark style=\"background-color:#7bdcb5\" class=\"has-inline-color\">*<\/mark><\/code>) or a specified origin (the commented out code example from above) and send the data back, allowing data to flow from backend to frontend.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Set the proxy<\/h3>\n\n\n\n<p>This was discovered after learning about the cors module. React actually has a very handy way of handling CORS issues by defining <code><mark style=\"background-color:#7bdcb5\" class=\"has-inline-color\">proxy<\/mark><\/code> in the <code><mark style=\"background-color:#7bdcb5\" class=\"has-inline-color\">package.json<\/mark><\/code> file. See details <a href=\"https:\/\/create-react-app.dev\/docs\/proxying-api-requests-in-development\/\">here<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"558\" height=\"202\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6060\/files\/2022\/10\/image-8.png\" alt=\"\" class=\"wp-image-53\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6060\/files\/2022\/10\/image-8.png 558w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6060\/files\/2022\/10\/image-8-300x109.png 300w\" sizes=\"auto, (max-width: 558px) 100vw, 558px\" \/><figcaption>Just define &#8220;proxy&#8221;&#8230; it&#8217;s that simple<\/figcaption><\/figure>\n\n\n\n<p>My teammate and I were a little dumbfounded by this simple solution; however, in the official documentation it is noted that this is only works in development (when we run the app using <code>npm start<\/code>). <\/p>\n\n\n\n<p>In fact, in the same documentation from create-react-app.dev website, there&#8217;s a link on <a href=\"https:\/\/enable-cors.org\/server_expressjs.html\">how to enable CORS on ExpressJS<\/a>. This is something I remember attempting to implement in my previous encounter before my capstone project, but did not work. I definitely need to try it out locally and make sure I understand what is going on.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Lesson Learned<\/h2>\n\n\n\n<p>After all these encounters with CORS-related shenanigan throughout my web dev study, I find myself realizing that I still got a lot to learn; however, I do find myself becoming better at finding potential solutions and being more comfortable discussing technical problems with my teammate, which is a great asset (skill) in itself. <\/p>\n\n\n\n<p>In fact, writing this blog post was a great way to better understand the solutions I have found at hand, and I&#8217;m excited to continue coding on my capstone project further. So, for now, thank you for reading and,<\/p>\n\n\n\n<p><strong>Until next time<\/strong>.<\/p>\n\n\n\n<p>\u2013 Fusako<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ever heard of CORS? For those who are not familiar with web development, CORS may be a new concept. It stands for &#8220;Cross-Origin Resource Sharing&#8221; and there are great online resources that go over the concept such on the MDN web docs or a quick overview provided on the wikipedia page. Here&#8217;s a brief description &hellip; <a href=\"https:\/\/blogs.oregonstate.edu\/obataf\/2022\/10\/28\/post-3-battle-of-the-cors\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Post #3: Battle of the CORS<\/span><\/a><\/p>\n","protected":false},"author":12815,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-41","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blogs.oregonstate.edu\/obataf\/wp-json\/wp\/v2\/posts\/41","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.oregonstate.edu\/obataf\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.oregonstate.edu\/obataf\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/obataf\/wp-json\/wp\/v2\/users\/12815"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/obataf\/wp-json\/wp\/v2\/comments?post=41"}],"version-history":[{"count":6,"href":"https:\/\/blogs.oregonstate.edu\/obataf\/wp-json\/wp\/v2\/posts\/41\/revisions"}],"predecessor-version":[{"id":59,"href":"https:\/\/blogs.oregonstate.edu\/obataf\/wp-json\/wp\/v2\/posts\/41\/revisions\/59"}],"wp:attachment":[{"href":"https:\/\/blogs.oregonstate.edu\/obataf\/wp-json\/wp\/v2\/media?parent=41"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/obataf\/wp-json\/wp\/v2\/categories?post=41"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/obataf\/wp-json\/wp\/v2\/tags?post=41"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}