title: How to send form data to the server with JavaScript
body: <p>I am working with two servers, one for <strong>React</strong> (on address <code>http://localhost:3000/contact</code>) and another for <strong>Express</strong> (on address <code>http://localhost:5000/</code>). I want to send form data object through some HTTP request approach as a POST method but I get empty object on the "backend" side.</p>
  
  <p>I have a simple form with an <code>onSubmit</code> event which firstly creates an object with the form data values:</p>
  
  <pre><code>const data = {
      firstInput: evt.target.elements.firstInput.value,
      secondInput: evt.target.elements.secondInput.value
  }
  </code></pre>
  
  <p><strong>Note:</strong> I tested If get all the data until here with DevTools and React Dev tools, until here it works great.</p>
  
  <p>And the second server with Express that just have a simple endpoint which should receive this data or at least print what I sent in the <code>req.body</code> object:</p>
  
  <pre><code>server.post("/", (req, res) =&gt; {
      res.end(req.body);
  });
  </code></pre>
  
  <p><strong>Note 2:</strong> Also tested this endpoint and it works fine but <code>req.body</code> gets an empty object.</p>
  
  <p>I have tested several methods like:</p>
  
  <blockquote>
    <p>Native <code>fetch</code> API:</p>
  
  <pre><code>fetch("http://localhost:5000/", {
      method: "POST",
      headers: {
          Accept: "application/json",
          "Content-Type": "application/json"
      },
      body: JSON.stringify(data)
      }).then(res =&gt; {
          console.log(res);
      });
  </code></pre>
    
    <p><strong><em>Error</em></strong>:</p>
    
    <p><img src="https://i.imgur.com/d6w7dYc.png" alt="Error"></p>
  </blockquote>
  
  <p>Also, tried with <code>async</code> / <code>await</code> approach on <code>fetch API</code> but I wasn't sure about using it on React component.</p>
  
  <p>I also have tried <a href="https://nodejs.org/api/http.html" rel="nofollow noreferrer">http API</a> but I got with the same.</p>
  
  <p>I guess my first question is how to send properly formatted the data from the component side to the server side. Thanks anyway.</p>