Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

610
Visualizações
Error: Request failed with status code 404, React, Axios

I have built a form which I want to be able to send emails, to do this I have attempted to follow this youtube tutorial: https://www.youtube.com/watch?v=_3-By9QfFa0

However I am running into an issue where I am getting the error in the question title which is coming up in my console web browser when trying to submit the form. I realize the issue might have something to do with one of the routes somewhere but I just cant figure it out (unless it's something completely different).

schoolForm.js

const handleSubmit = async(e) => {
e.preventDefault();
try { //I also tried using only: "/send_mail" here like I have in server.js but it didnt work
  await axios.post("http://localhost:3000/send_mail", {
    name
  });
}
catch (error) {
  console.log(error);
  }
}

server.js

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(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());

app.use(cors());

app.post("/send_mail", cors(), async (req, res) => {
  let {text} = req.body;
  const transport = nodemailer.createTransport({
    host: "smtp-mail.outlook.com",
    port: 587,
    auth: {
      user: "someone@hotmail.com",
      pass: "password"
    },
    tls: {
      rejectUnauthorized: false
    }
  });

 await transport.sendMail({
    from: "someone@hotmail.com",
    to: "someone@hotmail.com",
    subject: "subject",
    html: `<p>${text}</p>`
  })

});

app.listen(4000, () => {
  console.log("Server is listening on port 4000");
});

Edit: The error I get in the browser:

enter image description here

Is there anyone that can help me solve this issue? Help would be greatly appreciated!

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Your server is listening on port 4000. // server.js

app.listen(4000, () => {
  console.log("Server is listening on port 4000");
});

You should use below URL for Axios call. // schoolForm.js

await axios.post("http://localhost:4000/send_mail", {    // use port 4000 not 3000
    name
});
about 4 years ago · Juan Pablo Isaza Relatório

0

You've set up your server to listen to port 4000 but your axios request is to port 3000. Make sure you send the request to the correct port:

await axios.post("http://localhost:4000/send_mail", {
    name
  });

Also note the body-parser is deprecated and you should use express built-in middleware. so instead of:

app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());

you should have:

app.use(express.urlencoded({extended: true}));
app.use(express.json());

and you can remove body-parser dependency.

You can read here to learn more about express built-in middleware and their optional properties.

EDIT:

The reason it wasn't working is because body is an object created by the middleware itself. Therefore req.body is undefined since the body object doesn't exists

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda