【问题标题】:Error: Request failed with status code 404, React, Axios错误:请求失败,状态码 404,React,Axios
【发布时间】:2022-01-07 17:18:39
【问题描述】:

我已经建立了一个我希望能够发送电子邮件的表单,为此我尝试遵循这个 youtube 教程:https://www.youtube.com/watch?v=_3-By9QfFa0

但是,我遇到了一个问题,即在尝试提交表单时,我的控制台 Web 浏览器中出现了问题标题中的错误。我意识到这个问题可能与某处的某条路线有关,但我就是想不通(除非它完全不同)。

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");
});

编辑:我在浏览器中遇到的错误:

有没有人可以帮我解决这个问题?非常感谢您的帮助!

【问题讨论】:

  • 你的反应应用在哪个端口运行?
  • 什么是错误信息?
  • @OmarBerrami localhost:3000
  • @jkaczmarkiewicz 我不知道你的问题是什么?我收到的错误消息是标题中的错误消息:错误:请求失败,状态码为 404
  • @random1234 您的问题是您的节点应用程序正在端口 3000 上运行,而您的反应应用程序正在端口 3000 上运行,因此请更改主题之一

标签: javascript node.js reactjs axios nodemailer


【解决方案1】:

您的服务器正在侦听端口 4000。 // server.js

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

您应该使用以下 URL 进行 Axios 调用。 // schoolForm.js

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

【讨论】:

  • 它确实有效,但是,你知道为什么我的req.body 给了我undefined吗?
  • 你确定 req.body 是未定义的吗?请再次检查app.post("/send_mail", cors(), async (req, res) =&gt; { let {text} = req.body; //use {name} instead
  • 非常感谢,我将 {text} 替换为我从前端发送的变量 {name} 等...它成功了,再次感谢!
【解决方案2】:

您已将服务器设置为侦听端口 4000,但您的 axios 请求是端口 3000。 确保将请求发送到正确的端口:

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

还要注意body-parser 已弃用,您应该使用express 内置中间件。所以而不是:

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

你应该有:

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

你可以删除body-parser的依赖。

您可以阅读here 了解有关 express 内置中间件及其可选属性的更多信息。

编辑:

它不起作用的原因是因为body 是由中间件本身创建的对象。因此req.bodyundefined,因为body 对象不存在

【讨论】:

  • 嘿,谢谢您的回答,它确实有效,但是 Alpesh Patil 给出了相同的答案,并且他在您之前写了他的答案,因此我授予他赏金并接受了他的答案。我很感激你也通知我body-parser 的弃用。
  • 也许你能帮我解决这个问题?你知道为什么我的req.body 给了我undefined吗?
  • 别担心,我是来帮忙不收集积分的 :) 它不起作用的原因是因为body 是由中间件本身创建的对象。因此req.bodyundefined,因为body 对象不存在。
猜你喜欢
  • 2020-04-05
  • 2019-08-15
  • 2021-04-10
  • 1970-01-01
  • 2021-01-12
  • 2020-09-18
  • 2019-10-12
  • 2022-11-13
  • 2020-03-29
相关资源
最近更新 更多