<?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: Kartikey Jaiswal</title>
    <description>The latest articles on Forem by Kartikey Jaiswal (@kartikeykjjaiswal).</description>
    <link>https://forem.com/kartikeykjjaiswal</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%2F1264138%2Fcdd81d3e-de72-4494-a6bf-7c5476413697.png</url>
      <title>Forem: Kartikey Jaiswal</title>
      <link>https://forem.com/kartikeykjjaiswal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/kartikeykjjaiswal"/>
    <language>en</language>
    <item>
      <title>How to handle CORS issues when deploying a nodeJS express app on vercel??</title>
      <dc:creator>Kartikey Jaiswal</dc:creator>
      <pubDate>Tue, 23 Jan 2024 22:07:26 +0000</pubDate>
      <link>https://forem.com/kartikeykjjaiswal/how-to-handle-cors-issues-when-deploying-a-nodejs-express-app-on-vercel-10kh</link>
      <guid>https://forem.com/kartikeykjjaiswal/how-to-handle-cors-issues-when-deploying-a-nodejs-express-app-on-vercel-10kh</guid>
      <description>&lt;p&gt;When we deploy a simple nodejs express app to vercel, we are most likely to encounter cors issues most of the times. Personally, I often get cors errors each time I deploy to vercel.&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%2Fvo4u6ciatxmua2mozbb8.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%2Fvo4u6ciatxmua2mozbb8.png" alt="VERCEL CORS ERROR"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Till yesterday, I was getting intimidated by these errors very much. Even, I have spent more than 2 days in resolving this error. The app works on my local machine but as soon I deploy it on vercel, it gets cors issue. &lt;/p&gt;

&lt;p&gt;There are multiple solutions of this problem. Some of you will say using CORS node js module will work fine. But it does not. It tackle some of the errors but not all and not every time.&lt;br&gt;
The more effective solution is giving vercel the headache of your CORS issues.&lt;/p&gt;

&lt;p&gt;So, This is the folder structure of most of the simple apps.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwolgvsq77ategjxlnvvk.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%2Fwolgvsq77ategjxlnvvk.png" alt="FOLDER STRUCTURE"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have a root folder in which we have our index.js, package.json, package-lock.json.&lt;br&gt;
Our index.json has all our express server code.&lt;/p&gt;

&lt;p&gt;Now, to deploy it on vercel, we make vercel.json file in the root folder. Generally, this is the vercel.json code we write for our routes and build. This works most of the time and almost everyone uses this copy-paste template to deploy on vercel.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

 {
   "version": 2,
   "builds": [
     {
       "src": "index.js",
       "use": "@vercel/node"
     }
   ],
   "routes": [
     {
       "src": "/(.*)",
       "dest": "/"
     }
   ]
 }


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The problem I got in my projects was, even though I was using cors module in my code but then to I was having cors error sometimes. I went to vercel documentation and found out that we can fix this error using vercel.json. I tried solutions given on vercel (copied and pasted the code) but it was failing the checks of deployment. The solutions given on vercel are for specific cases. The solutions in the vercel documentation about vercel.json properties did not mention where or with which property we dont have to use these headers or properties. Encountering this problem took me more than 2 days to understand whats wrong I was doing. I tried searching on stackoverflow, reddit and other discussion portals and communities but didn’t found a good and simple solution. Now, when I have handled the issue I want to share this post so that developers dont have to go here and there for resolving and waste their time trying and handling errors.&lt;/p&gt;

&lt;p&gt;Here, is the new updated vercel.json file handling cors issues.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

{
    "version": 2,
    "builds": [
      {
        "src": "*.js",
        "use": "@vercel/node"
      }
    ],
    "routes": [
      {
         "src": "/(.*)",
         "dest": "/",
          "methods": ["GET","POST", "PUT", "DELETE", "PATCH", "OPTIONS"],
          "headers": {
             "Access-Control-Allow-Origin": "*",
             "Access-Control-Allow-Credentials": "true",
             "Access-Control-Allow-Headers": "X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version"
         }
     }
   ]
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This JSON code can save you lots and lots of time when deploying your code on vercel. The methods and header properties ensure that vercl handle all the cors issues.&lt;/p&gt;

&lt;p&gt;You can adjust routes and builds according to your file structure.&lt;/p&gt;

&lt;p&gt;Feel free to ask any questions and leave your feedback. Hope you find this post helpful. I will be regularly posting the problems I face in my development journey, that dont have a staright solution available on internet and their solutions I get from my experiments.&lt;/p&gt;

&lt;p&gt;Thank you so much for giving time and reading my post.&lt;br&gt;
Kartikey Jaiswal&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>node</category>
      <category>backend</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
