title: How to solve CORS error in NodeJS independent of CORS middleware
body: <p>I am facing a CORS error when trying to make a request from the client to a node server. I want to solve the issue independent of the CORS middleware. Here is the server-side code. Please let me know if I am missing something. Thanks.</p>
  <pre class="lang-js prettyprint-override"><code>const express = require(&quot;express&quot;);
  const app = express();
  
  app.get(&quot;/&quot;, (req, res) =&gt; {
    res.setHeader(&quot;Access-Control-Allow-Origin&quot;, &quot;http://127.0.0.1:5500&quot;);
    res.setHeader(&quot;Access-Control-Allow-Methods&quot;, &quot;POST, GET, PUT&quot;);
    res.setHeader(&quot;Access-Control-Allow-Headers&quot;, &quot;application/json&quot;);
    res.send(&quot;&lt;h1&gt;Server is running at port 3000!&lt;h1&gt;&quot;);
  });
  app.listen(3000);
  
  </code></pre>
  <p>The error on the console:</p>
  <blockquote>
  <p>Access to fetch at 'http://localhost:3000/' from origin 'http://127.0.0.1:5500' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.</p>
  </blockquote>