gpt4 book ai didi

node.js - 如何修复 NodeJS 中的 'Redirect problem' 错误?

转载 作者:太空宇宙 更新时间:2023-11-03 23:58:04 24 4
gpt4 key购买 nike

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

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

res.render("/",{错误:" 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...
}

最佳答案

看来您正在使用 express 。在 Express 中,redirect 方法可用于 response 对象,而不是 request 对象。 express docs here

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

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

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

关于node.js - 如何修复 NodeJS 中的 'Redirect problem' 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56092047/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com