title: Cant enable CORS on the server side NodeJS
body: <p>I can't enable <code>CORS</code> on the server-side. My frontend and backend servers have different ports. Here is how server-side is implemented:</p>
  <pre class="lang-js prettyprint-override"><code>http
    .createServer(function (req, res) {
      // .. Here you can create your data response in a JSON format
      // const { headers, method, url } = req;
      // let body = [];
      res.setHeader('Access-Control-Allow-Origin', '*');
      res.setHeader('Access-Control-Request-Method', '*');
      res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET');
      res.setHeader('Access-Control-Allow-Headers', '*');
      if (req.method === 'OPTIONS') {
        res.writeHead(200);
        res.end();
        return;
      }
  
      // const responseBody = { headers, method, url, body: JSON.stringify(data) };
  
      response.write('{asd: 123}'); // Write out the default response
      res.end(); //end the response
    })
    .listen(port);
  
  </code></pre>
  <p>And I call the <code>fetch</code> function from the frontend-side like this one:</p>
  <pre class="lang-js prettyprint-override"><code>fetch('http://localhost:3035', {
        method: 'POST',
        mode: 'same-origin', // no-cors, *cors, same-origin
        cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
        credentials: 'include', // include, *same-origin, omit
        headers: {
          'Content-Type': 'application/json',
          // 'Content-Type': 'application/x-www-form-urlencoded',
        },
        body: JSON.stringify(line), // body data type must match &quot;Content-Type&quot; header
      })
        .then((response) =&gt; response.json())
        .then((data) =&gt; console.log(data))
        .catch((error) =&gt; console.log(error));
  </code></pre>
  <p>But still getting errors:</p>
  <p><code>Security Error: Content at http://localhost:3030/ may not load data from http://localhost:3035/.</code></p>
  <p><code>TypeError: &quot;NetworkError when attempting to fetch resource.&quot;</code></p>