title: client (fetch) and server (nodejs http) don't understand each other?
body: <p>I try to wrap my mind around nodejs at the moment.</p>
  
  <p>So I've created a client:</p>
  
  <pre><code>let myHeaders = {
   'Content-Type': 'application/json',
   'Accept': 'application/json'
  };
  
  let myBody = {
    aString: "Test"
  };
  
  fetch("http://localhost:8099/", {
    method: 'post',
    mode: 'no-cors',
    headers: myHeaders,
    body: JSON.stringify(myBody)
  })
  .then(result =&gt; {
    return result.text();
  })
  .then(text =&gt; {
    // do stuff with text from server
  });
  </code></pre>
  
  <p>And I have created a server:</p>
  
  <pre><code>// request needed modules
  const http = require('http');
  
  // init server
  let server = http.createServer(logic);
  server.listen(8099);
  
  // server logic
  function logic (req, res) {
    var body = req.body;
    res.end("Hello");
  }
  </code></pre>
  
  <p>Two problems:</p>
  
  <p>1) The sever does not get the body (req.body is undefined).</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>See my answer below.</p>
  
  <p><strong>--</strong></p>
  
  <p>2) The client does not receive "Hello" (result.text() returns "").</p>
  
  <p><strong>UPDATE</strong>
  2 is solved by:</p>
  
  <p>Changing this on the client</p>
  
  <pre><code>fetch("http://localhost:8099/", {
    method: 'post',
    mode: 'no-cors', &lt;-- CHANGE to: mode: 'cors'
  ...
  </code></pre>
  
  <p>Adding this on server</p>
  
  <pre><code>res.setHeader('Access-Control-Allow-Origin', '*');
  res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
  </code></pre>
  
  <p><strong>--</strong></p>
  
  <p>I don't get what I do wrong here...</p>