title: CORS error: even i enable it at API-Gateway and in my index.js file when uploading to Lamda function AWS
body: <p>i am looking to deploy the <code>backend -login functionality</code> using <code>Lamda function</code> i have already deployed <code>frontend(S3-bucket)</code> and <code>backend(RDS-mysql)</code>. i am getting an error when i submit the form with login details, i have also enabled the CORS in AWS <code>API gateway</code>. I am a bit new to AWS so i am not sure if i am missing something here. any suggestion please here.</p>
  <ul>
  <li><code>Error in console</code></li>
  </ul>
  <p><a href="https://i.stack.imgur.com/R9g0h.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/R9g0h.png" alt="enter image description here" /></a></p>
  <p>-<code>index.js</code> i have below code to enable<code>CORS</code> in the backend code</p>
  <pre><code>
      app.use(
        cors({
          credentials: true, // for cookies
          origin: &quot;*&quot;,
          optionsSuccessStatus: 200,
        })
      );
  
  </code></pre>
  <p>-<code>API-Gateway AWS</code> i enabled the CORS in AWS as well <a href="https://i.stack.imgur.com/gwjIi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gwjIi.png" alt="enter image description here" /></a></p>
  <p>-<code>Snippet of code from backend index.js</code> which i uploaded to <code>Lamda function</code>, this is the first time so i am not sure if i wrote the code below correct<code>( before tweaking in below i used it locally and it worked fine the code)</code></p>
  <pre><code>// bring in express
  const express = require(&quot;express&quot;);
  const mysqlx = require(&quot;mysql&quot;);
  const jwt = require(&quot;jsonwebtoken&quot;);
  const auth = require(&quot;./verifyTokenExisting&quot;);
  const authNew = require(&quot;./verifyTokenNew&quot;);
  const cors = require(&quot;cors&quot;);                // also using cors library here
  const cookieParser = require(&quot;cookie-parser&quot;);
  const pdf = require(&quot;html-pdf&quot;);
  const pdfTemplate = require(&quot;./documents/pdfTemplate&quot;);
  const fs = require(&quot;fs&quot;);
  const { isContext } = require(&quot;vm&quot;);
  
  const app = express();
  app.use(express.json());
  app.use(cookieParser());
  
  app.use(
    cors({
      credentials: true, // for cookies
      origin: &quot;*&quot;,
      optionsSuccessStatus: 200,
    })
  );
  // mysql connection
  
  let connection = mysqlx.createConnection({
    host: &quot;database-x.xxxxxxxxxxx.eu-west-2.rds.amazonaws.com&quot;,
    user: &quot;xxxx&quot;,
    password: &quot;xxxxxxxxxxxx&quot;,
    database: &quot;join_us&quot;,
    insecureAuth: true,
  });
  
  ////!  LOGIN &amp; LOGOUT
  exports.handler = (event, contect, callback) =&gt; {
    Context.callbackWaitsForEmptyEventLoop = false;
    //app.post(&quot;/api/newuser&quot;, (req, res) =&gt; {
    let x1 = event.body;
    console.log(&quot;144&quot;, x1);
  
    if (event.body.logout === false) {
      connection.query(&quot;SELECT * FROM  users WHERE email=?;&quot;, [x1.email], function (err, results) {
        
            if ((results[0].password == x1.password &amp;&amp; results[0].userloginStatus == false) || (results[0].password == x1.password &amp;&amp; results[0].userloginStatus == null)) {
              
              const payload = { email: results[0].email };
             // generate new token
              const token = jwt.sign(payload, &quot;lllfasdgfdadsfasdfdasfcadsf&quot;);
              //below are the cookies sent to user first time when he log in
              callback(
                null,
                cookie(&quot;yogaoutlet_access_token&quot;, token, {
                  maxAge: 25 * 24 * 60 * 60 * 1000,
                  httpOnly: true, // it will enable on frotend-javascript to not have access to cokkies
                   
                })
              );
  
            
          } else callback(null, redirect(&quot;http://localhost:3000/about&quot;));
        }
  
      
    } 
  
  // if event.body.logout === true then logout the user
  else {
      const payload = { email: event.body.email };
      console.log(&quot;339x&quot;, payload);
      const token = jwt.sign(payload, &quot;lllfasdgfdadsfasdfdasfcadsf&quot;);
  
      callback(null, clearCookie(&quot;yogaoutlet_access_token&quot;));
    }
    //});
  };
  </code></pre>