为什么我通过axios.post()收到此404错误?使用Express / Node?

时间:2019-10-10 13:58:22

标签: javascript node.js axios nodemailer

我正在尝试与Nodemailer进行联系,这是我第一次遇到404错误。

我希望这不是要分享的令人讨厌的代码,我会尽力减少它的数量。

Server.js

 const transporter = nodemailer.createTransport({
    host: "smtp.ethereal.email",
    port: 587,
    secure: false, 
    auth: {
        user: creds.USER, // Generated by ethereal
        pass: creds.PASS // Generated by ethereal
    }
});


transporter.verify((err, success) => {
    if(err) {
        console.log(err)
    } else {
        console.log("Server is ready to take messages")
    }
})

router.post("/send", (req, res, next) => {
    let name = req.body.name
    let email = req.body.email
    let message = req.body.message
    let content = `name: ${name} \n email: ${email} \n message: ${message}`


    let mail = {
        from: name,
        to: "jlbroughton88@gmail.com",
        subject: "Test from contact form",
        text: content
    }

    transporter.sendMail(mail, (err, data) => {
        if (err) {
            res.json({
                msg: "Message failed"
            })
        } else {
            res.json({
                msg: "Message succeeded!"
            })
        }
    })
})

ContactForm.js(反应组件)

我已经修剪了JSX部分,提交触发器工作正常。一旦到达axios.post()方法,它就会中断。

    handleSubmit(e) {
    e.preventDefault();
    const name = document.getElementById('name').value;
    const email = document.getElementById('email').value;
    const message = document.getElementById('message').value;
    axios.post("http://localhost:3002/send", {
        name,
        email,
        message
    })
    .then((response) => {
        // console.log(response)
        if (response.data.msg === 'success') {
            alert("Message Sent.");
            this.resetForm()
        } else if (response.data.msg === 'fail') {
            alert("Message failed to send.")
        }
    })
}

resetForm() {
    document.getElementById('contact-form').reset();
}

0 个答案:

没有答案