【问题标题】:Nodemailer returns 404 when using POST formNodemailer 在使用 POST 表单时返回 404
【发布时间】:2019-07-30 06:16:41
【问题描述】:

我正在尝试使用 Nodemailer 创建一个联系我的表单。每当我使用表单发布时,它总是返回 404 错误,我怀疑我的 Async/Await 代码中有错误。我的代码输出第一个 console.log,然后返回 404。我很新,根本找不到我的错误,关于使用异步和等待 Nodemailer 的文档很少,这使得它变得更加困难。我尝试用他们在 github 上的测试代码替换我的代码,它工作正常。

邮件控制器 |导出到邮件路由

module.exports = {
    send: async (req, res, next) => {
        try {
        console.log(`Preparing Email`);
            const transporter = nodemailer.createTransport({
                service: `gmail`,
                auth: {
                    user:`psuedoEmail@gmail.com`,
                    pass:`password`
                }
            });
            console.log(`reading mail options...`);
            const mailOptions = {
                from: req.body.from,
                to: `psuedoEmail@gmail.com`,
                subject: req.body.subject,
                text: req.body.text
            };
            console.log(`Attempting to send Mail`);
            const info = await transporter.sendMail(mailOptions, (err, response) =>{
                if(err){
                    console.log(err);
                    res.redirect(`/contact`);
                } else {
                    console.log(`Info: `, info, response);
                    transporter.close();
                    res.redirect(`/`);
                }
            });
        } catch (error){
            next();
        }
    }
};

使用路线和表格更新

表格

<form action="/" method="POST">
  <div class="form-group">
    <label for="name">Name:</label>
    <input type="email" class="form-control" id="from">
  </div>
  <div class="form-group">
    <label for="subject">Subject:</label>
    <input type="text" class="form-control" id="subject">
  </div>
  <div class="form-group">
    <label for="text">Text:</label>
    <input type="text" class="form-control" id="text">
  </div>
  <button type="submit" class="btn btn-default">Submit</button>
</form>

路线

const express = require(`express`);
const router = express.Router();
const Mailer = require(`../controllers/mailer`);

router.route(`/contact`)
    .post(Mailer.send);

module.exports = router;

评论员要求的错误

GET /contact 304 7.008 ms - -
{ Error: Something Happened!
    at app.use (home/project/app.js:29:17)
    at Layer.handle [as handle_request] /node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix 
/node_modules/express/lib/router/index.js:317:13)
    at 
/node_modules/express/lib/router/index.js:284:7
    at Function.process_params /node_modules/express/lib/router/index.js:335:12)
    at next 
/node_modules/express/lib/router/index.js:275:10)
    at 
/node_modules/express/lib/router/index.js:635:15
    at next 
/node_modules/express/lib/router/index.js:260:14)
    at Function.handle 
/node_modules/express/lib/router/index.js:174:3)
    at router 
/node_modules/express/lib/router/index.js:47:12) status: 404 }
POST / 404 0.200 ms - 43

【问题讨论】:

  • 请删除const info = await 并将其更改为仅transporter.sendMail(mailOptions, (err, response) =&gt;{ 并尝试。
  • 同样的错误,但它现在在第一个 console.log 之前就中断了
  • 请发布您遇到的错误。
  • 您在回调中使用 response 作为参数,但使用 res 关键字进行引用
  • 还是什么都没有,而且是第二次回调,应该不一样吧?

标签: javascript node.js express


【解决方案1】:

您是否已将身份验证更改为您的凭据(您的电子邮件和密码)?如果是,请允许非安全应用从 gmail 设置访问 gmail,否则将不允许访问您的 gmail 帐户

【讨论】:

  • 一切设置正确,密码和邮箱是假的,放在这里
猜你喜欢
  • 2019-01-05
  • 1970-01-01
  • 2021-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-08
  • 1970-01-01
相关资源
最近更新 更多