提交电子邮件后如何将用户重定向到主页?

时间:2018-10-28 12:08:23

标签: node.js recaptcha ejs nodemailer

我正在使用nodemailer和Google reCaptcha提交联系表。表单提交并发送了电子邮件,但是联系页面的url / ejs文件被挂起,不会重定向回主页。我在控制台中没有收到任何错误消息,并且堆栈将其发送到console.log(“成功发送邮件”)。 这是控制台输出:

improviser14:~/workspace $ node app.js
Successfully sent mail

问题似乎来自此代码块,尤其是**内的行:

smtpTransport.sendMail(mailOptions, function(err, info){
          if(err) {
            console.log(err);
            req.flash("error", "Something went wrong... Please try again later!");
            res.redirect("/contact");
          } else {
            console.log("Successfully sent mail");
            req.flash("success", "Your email has been sent, we will respond within 24 hours.");
            *res.redirect("/");*
          }
        });
    });

以下是可供参考的入口代码:

  var express = require('express'),
    app     = express(),
    serveStatic = require('serve-static'),
    bodyParser = require('body-parser'),
    express    = require("express"),
    flash      = require("connect-flash"),
    nodemailer = require("nodemailer"),
    request    = require("request"),
    router     = express.Router(),
    contactRoutes = require("./routes/contact");

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

app.set('view engine', 'ejs');

app.use(express.static('public/'));

app.get('/', function(req, res){
    res.render('home');
});

app.get('/contact', function(req, res){
    res.render('contactMe');
});

app.use("/contact", contactRoutes);

app.listen(process.env.PORT, process.env.IP);

在此先感谢您的帮助。

更新:注释掉以下行。现在,我试图弄清楚为什么req.flash无法正常工作。

req.flash("success", "Your email has been sent, we will respond within 24 hours.");

0 个答案:

没有答案