首页 > 解决方案 > 如何修复 NodeJS 中的“重定向问题”错误?

问题描述

我有SPA。我使用 nodejs + mongoDb。在我的 SPA 结束时,我有联系表格。当访问者填写联系表格并单击提交按钮时。我收到一封电子邮件。一切都很好,直到这里。但是,表单不会重定向。在标题栏加载指示器总是转动。我只想回调同一页面。

我尝试了这些,但它不起作用。

res.render("/",{Error:"block"}); req.redirect('/');

这是我的 Ejs 代码 Index.ejs

       <form class="cf" method="POST" action="/">
          <div class="half left cf">
            <input type="text" id="input-name" placeholder="Name" name="nameContact">
            <input type="email" id="input-email" placeholder="Email address" name="emailContact">
            <input type="text" id="input-subject" placeholder="Subject" name="subjectContact">
          </div>
          <div class="half right cf">
            <textarea type="text" id="input-message" placeholder="Message" name="messageContact"></textarea>
          </div>  
          <input type="submit" id="input-submit">
        </form>

这是我的帖子方法。

module.exports = function (app) {

    app.get('/', function (req, res) {
        Resume.find({}, function (err, data) {
            console.log(data)
            if (err) throw err;
            res.render('index', { resumes: data });
        });
    });

    app.post('/', urlencodedParser, function (req, res) {
        console.log(req.body);
        console.log(req.body.nameContact);
        main(req.body.nameContact, req.body.emailContact, req.body.subjectContact, req.body.messageContact)
            .then(() => {
                console.log("Email Sended");            
            }, err => { console.log("Email Error: " + err) })
            .catch(console.error);
    });
};

async function main(nameContact, emailContact, subjectContact, messageContact) {

    // Generate test SMTP service account from ethereal.email
    // Only needed if you don't have a real mail account for testing
    //let testAccount = await nodemailer.createTestAccount();

    // create reusable transporter object using the default SMTP transport
    let transporter = nodemailer.createTransport({
        host: "smtp.office365.com",
        port: 587,
        secure: false, // true for 465, false for other ports
        auth: {
            user: contactFormController.CONTACT_USER, // generated ethereal user
            pass: contactFormController.CONTACT_PASS // generated ethereal password
        }
    });

    // send mail with defined transport object
    let info = await transporter.sendMail({
        from: contactFormController.CONTACT_USER,
        replyTo: '<"' + emailContact + '">', // sender address
        to: "email", // list of receivers
        subject: subjectContact, // Subject line
        text: messageContact, // plain text body
        html: "<b>'" + messageContact + "'</b>" // html body
    });

    console.log("Message sent: %s", info.messageId);
    // Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com>

    // Preview only available when sending through an Ethereal account
    console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
    // Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
}

标签: node.js

解决方案


看起来你正在使用快递。在 expressredirect方法中,response对象而不是request对象可用。express 文档在这里

用法是response.redirect('/path')

此外,您不能同时进行渲染和重定向(headers只能发送一次):

res.render("/",{Error:"block"});
res.redirect('/');

推荐阅读