<p>I have built a microservice backend deployed on kubernetes on Digital Ocean.</p>
<p>I am trying to connect my react code to the backend and getting the below error:</p>
<pre><code>Access to XMLHttpRequest at 'http://cultor.dev/api/users/signin' from origin 'http://localhost:3000'
has been blocked by CORS policy: Response to preflight request doesn't pass access control check: 
Redirect is not allowed for a preflight request.
</code></pre>
<blockquote>
<p><strong>Index.ts</strong> settings:</p>
</blockquote>
<pre class="lang-js prettyprint-override"><code>app.use((req, res, next) =&gt; {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Headers', '*');
  res.header('Access-Control-Request-Headers', '*');
  if (req.method === &quot;OPTIONS&quot;) {
    res.header('Access-Control-Allow-Methods', '*');
    return res.status(200).json({});
  }
  next();
});
</code></pre>
<p>I would really appreciate the help. Thanks!</p>

Karan Kumar
<p>Install the cors middleware with</p>
<pre><code>npm install cors
</code></pre>
<p>And you can use it directly like this:</p>
<pre><code>const cors = require('cors');  
app.use(cors());
</code></pre>
<p>Or you can set specific options like this:</p>
<pre><code>const cors = require('cors');    
const corsOpts = {
    origin: '*',
    credentials: true,
    methods: ['GET','POST','HEAD','PUT','PATCH','DELETE'],
    allowedHeaders: ['Content-Type'],
    exposedHeaders: ['Content-Type']
};
app.use(cors(corsOpts));
</code></pre>
<p>You can replace origin with your website and allowedHeaders with the headers you're going to use.</p>
