title: CORS preflight request error ExpressJS and Angular
body: <p>I am trying to create a simple login using an ExpressJS backend, just to test to see how this would work.
  But for some weird reason an OPTIONS call is done on to my Angular front-end and throws a CORS error.</p>
  <p><a href="https://i.stack.imgur.com/RvHix.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RvHix.png" alt="Network Error" /></a></p>
  <p>The request headers of the OPTIONS call can be seen in the image below.</p>
  <p><a href="https://i.stack.imgur.com/5HWx4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5HWx4.png" alt="The OPTIONS call's request headers" /></a></p>
  <p><a href="https://i.stack.imgur.com/8Ri1b.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8Ri1b.png" alt="The console output" /></a></p>
  <p>My ExpressJS Cors setup:</p>
  <pre class="lang-js prettyprint-override"><code>// There are 2 front-end applications, only the second is used now but it needs to work with both.
  env.redirectURL = ['http://localhost:4200', 'http://localhost:5200'];
  
  const app = express();
  app.use(cors({ credentials: true, origin: env.redirectURL }));
  </code></pre>
  <p>In this code below I create a JWT token and send it to the front-end using redirection. This then for some reason calls an &quot;options&quot; request on my front-end which fails.</p>
  <pre class="lang-js prettyprint-override"><code>export default (req: Request, res: Response) =&gt; {
    if (!req.user) {
      if (env.logging) console.error('No user found!');
      res.status(500).send();
      return;
    }
  
    const expiresIn = 30 * 60 * 24 * 60000;
  
    jwt.sign(req.user, env.jwt.secret, { expiresIn }, (err, token) =&gt; {
      if (err) {
        if (env.logging) console.error(err);
        res.sendStatus(500); // Handle Error Better
      } else {
        res.cookie('token', token, {
          sameSite: false,
          httpOnly: true,
          secure: env.jwt.secure,
          expires: new Date(new Date().getTime() + expiresIn),
        });
  
        res.redirect(env.redirectURL[1]);
      }
    });
  };
  </code></pre>
  <p>I have been stuck on this for multiple days now and tried to do the same thing months ago as well. I feel stupid for not being capable of figuring this out and this is my last resort.</p>