title: Not able to send an axios post to a node js server - net::ERR_CONNECTION_REFUSED on localhost
body: <p>I'm trying to send a form from a react/node/axios website on my localhost:3000 and a node/nodemailer server running on localhost: 4000, when a submit the form/axios.post it take some seconds then i receive this error on clg:</p>
  <pre><code>Error: Network Error at createError (createError.js:16) at XMLHttpRequest.handleError 
  (xhr.js:84) xhr.js:177 POST http://localhost:4000/send_mail net::ERR_CONNECTION_REFUSED
  </code></pre>
  <p>I have tried a lot of things on code and also on my machine like, disabling firewall/protections... nothing worked.</p>
  <p>This is the react component i'm using:</p>
  <pre><code>import React from 'react';
  import { Link } from 'react-router-dom';
  import ReCAPTCHA from 'react-google-recaptcha';
  import axios from 'axios';
  
  import './style.css';
  
  const ContactForms = () =&gt; {
      const [isWork, setIsWork] = React.useState(false);
  
      const [name, setName] = React.useState('');
      const [telephone, setTelephone] = React.useState('');
      const [email, setEmail] = React.useState('');
      const [file, setFile] = React.useState();
      const [message, setMessage] = React.useState('');
  
      const [sent, setSent] = React.useState(false);
      const [msg, setMsg] = React.useState('');
      const recaptchaRef = React.createRef();
  
      console.log(sent);
      console.log(name);
  
      const handleSand = async (e) =&gt; {
          e.preventDefault();
  
          const token = await recaptchaRef.current.executeAsync();
  
          // const formData = new FormData();
          // formData.append('name', name);
          // formData.append('telephone', telephone);
          // formData.append('email', email);
          // formData.append('token', token);
          // formData.append('file', file);
          // formData.append('message', message);
  
          axios
              .post('http://localhost:4000/send_mail', {
                  name,
                  telephone,
                  email,
                  token,
                  file,
              })
              .then((r) =&gt; {
                  setSent(true);
                  setMsg(r.data.message);
              })
              .catch((e) =&gt; {
                  console.log(e);
              });
      };
  
      console.log(file);
  
      if (sent) return &lt;div&gt;{msg}&lt;/div&gt;;
      return (
          &lt;&gt;
              {!sent ? (
                  &lt;section className=&quot;faleConosco&quot; id=&quot;forms&quot;&gt;
                      &lt;div className=&quot;faleConosco__background&quot;&gt;&lt;/div&gt;
                      &lt;div className=&quot;faleConosco__formulario&quot;&gt;
                          &lt;div className=&quot;faleConosco__buttons&quot;&gt;
                              &lt;button
                                  className=&quot;button--amarelo amarelo&quot;
                                  onClick={() =&gt; setIsWork(false)}
                              &gt;
                                  Fale conosco
                              &lt;/button&gt;
                          &lt;/div&gt;
                          &lt;div className=&quot;faleConosco__info&quot;&gt;
                              &lt;h2 className=&quot;faleConosco__title title--style&quot;&gt;
                                  Faça parte do nosso time
                              &lt;/h2&gt;
                              &lt;span className=&quot;faleConosco__subtitle&quot;&gt;
                                  E transforme a gestão de telecom com a gente
                              &lt;/span&gt;
  
                              &lt;form className=&quot;formularioContato&quot; onSubmit={handleSand}&gt;
                                  &lt;input
                                      type=&quot;text&quot;
                                      placeholder=&quot;Nome completo&quot;
                                      value={name}
                                      onChange={(e) =&gt; setName(e.target.value)}
                                      required
                                  /&gt;
                                  &lt;button type=&quot;submit&quot; className=&quot;button--azul&quot;&gt;
                                      Enviar
                                  &lt;/button&gt;
  
                                  &lt;label&gt;
                                      &lt;input type=&quot;checkbox&quot; required /&gt;
                                      Eu li, estou ciente das condições de tratamento dos meus dados
                                      pessoais e dou meu consentimento, quando aplicável, conforme
                                      descrito nesta política de privacidade
                                  &lt;/label&gt;
  
                                  &lt;ReCAPTCHA
                                      sitekey=&quot;6Lf3EpAaAAAAAOaWFeNFGhqaQk0Fab4gMfDclxx0&quot;
                                      size=&quot;invisible&quot;
                                      ref={recaptchaRef}
                                  /&gt;
                              &lt;/form&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/section&gt;
              ) : (
                  &lt;section className=&quot;faleConosco&quot; id=&quot;forms&quot;&gt;
                      &lt;div className=&quot;faleConosco__background&quot;&gt;&lt;/div&gt;
                      &lt;div className=&quot;faleConosco__formulario&quot;&gt;
                          &lt;h1&gt;enviado&lt;/h1&gt;
                      &lt;/div&gt;
                  &lt;/section&gt;
              )}
          &lt;/&gt;
      );
  };
  
  export default ContactForms;
  </code></pre>
  <p>This is my server code:</p>
  <pre><code>const express = require('express');
  const app = express();
  
  require('dotenv').config();
  
  const bodyParser = require('body-parser');
  const cors = require('cors');
  const nodemailer = require('nodemailer');
  
  app.use(express.urlencoded({ extended: true }));
  app.use(express.json());
  
  app.use(cors());
  
  app.post('/send_mail', cors(), async (req, res) =&gt; {
      let { text } = req.body;
      const transport = nodemailer.createTransport({
          service: process.env.MAIL_SERVICE,
          auth: {
              user: process.env.MAIL_USER,
              pass: process.env.MAIL_PASS,
          },
          tls: {
              rejectUnauthorized: false,
          },
      });
  
      await transport.sendMail({
          from: `Leads Tel&amp;Com &lt;${process.env.MAIL_USER}&gt;`,
          to: `${process.env.MAIL_USER}`,
          subject: 'teste',
          text: 'teste',
      });
  });
  
  app.listen(
      (process.env.PORT || 4000,
      () =&gt; {
          console.log('Server is listening on port 4000');
      })
  );
  </code></pre>