每当我想在我的视图(ejs)中填写表单并发送到数据库时,我的终端就会出现此错误。
MongoDB Connected
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:485:11)
at ServerResponse.header (/home/codabae/Documents/mailmonster/api/v1/node_modules/express/lib/response.js:767:10)
at ServerResponse.send (/home/codabae/Documents/mailmonster/api/v1/node_modules/express/lib/response.js:170:12)
at done (/home/codabae/Documents/mailmonster/api/v1/node_modules/express/lib/response.js:1004:10)
at tryHandleCache (/home/codabae/Documents/mailmonster/api/v1/node_modules/ejs/lib/ejs.js:257:5)
at View.exports.renderFile [as engine] (/home/codabae/Documents/mailmonster/api/v1/node_modules/ejs/lib/ejs.js:482:10)
at View.render (/home/codabae/Documents/mailmonster/api/v1/node_modules/express/lib/view.js:135:8)
at tryRender (/home/codabae/Documents/mailmonster/api/v1/node_modules/express/lib/application.js:640:10)
at Function.render (/home/codabae/Documents/mailmonster/api/v1/node_modules/express/lib/application.js:592:3)
at ServerResponse.render (/home/codabae/Documents/mailmonster/api/v1/node_modules/express/lib/response.js:1008:7)
at /home/codabae/Documents/mailmonster/api/v1/node_modules/express-ejs-layouts/lib/express-layouts.js:113:20
at tryHandleCache (/home/codabae/Documents/mailmonster/api/v1/node_modules/ejs/lib/ejs.js:257:5)
at View.exports.renderFile [as engine] (/home/codabae/Documents/mailmonster/api/v1/node_modules/ejs/lib/ejs.js:482:10)
at View.render (/home/codabae/Documents/mailmonster/api/v1/node_modules/express/lib/view.js:135:8)
at tryRender (/home/codabae/Documents/mailmonster/api/v1/node_modules/express/lib/application.js:640:10)
at Function.render (/home/codabae/Documents/mailmonster/api/v1/node_modules/express/lib/application.js:592:3)
这是我的代码
我看来
<h1 class="mt-4">Dashboard</h1>
<div class="row mt-5">
<div class="col-md-6 m-auto">
<div class="card card-body">
<h1 class="text-center mb-3">
<i class="fas fa-user-plus"></i> Add Smtp
</h1>
<% include ./partials/messages %>
<form action="/users/mail" method="POST">
<div class="form-group">
<label for="smtpUsername">smtpUsername</label>
<input
type="name"
id="smtpUsername"
name="smtpUsername"
class="form-control"
placeholder="Enter smtpUsername"
value="<%= typeof smtpUsername != 'undefined' ? smtpUsername : '' %>"
/>
</div>
<div class="form-group">
<label for="smtpPassword">smtpPassword</label>
<input
type="name"
id="smtpPassword"
name="smtpPassword"
class="form-control"
placeholder="Enter smtpPassword"
value="<%= typeof smtpPassword != 'undefined' ? smtpPassword : '' %>"
/>
</div>
<div class="form-group">
<label for="smtpUrl">smtpUrl</label>
<input
type="name"
id="smtpUrl"
name="smtpUrl"
class="form-control"
placeholder="Enter smtpUrl"
value="<%= typeof smtpUrl != 'undefined' ? smtpUrl : '' %>"
/>
</div>
<button type="submit" class="btn btn-primary btn-block">
Add Smtp
</button>
</form>
</div>
</div>
</div>
<div class="row mt-5">
<div class="col-md-6 m-auto">
<div class="card card-body">
<h1 class="text-center mb-3">
<i class="fas fa-user-plus"></i> send mail
</h1>
<% include ./partials/messages %>
<form action="/users/mail" method="POST">
<div class="form-group">
<label for="to">to</label>
<input
type="name"
id="to"
name="to"
class="form-control"
placeholder="Enter to"
value="<%= typeof to != 'undefined' ? to : '' %>"
/>
</div>
<div class="form-group">
<label for="bcc">bcc</label>
<input
type="name"
id="bcc"
name="bcc"
class="form-control"
placeholder="Enter bcc"
value="<%= typeof bcc != 'undefined' ? bcc : '' %>"
/>
</div>
<div class="form-group">
<label for="cc">cc</label>
<input
type="name"
id="cc"
name="name"
class="form-control"
placeholder="Enter cc"
value="<%= typeof cc != 'undefined' ? cc : '' %>"
/>
</div>
<div class="form-group">
<label for="subject">subject</label>
<input
type="name"
id="subject"
name="subject"
class="form-control"
placeholder="Enter subject"
value="<%= typeof subject != 'undefined' ? subject : '' %>"
/>
</div>
<div class="form-group">
<label for="message">message</label>
<input
type="name"
id="message"
name="message"
class="form-control"
placeholder="Enter message"
value="<%= typeof message != 'undefined' ? message : '' %>"
/>
</div>
<button type="submit" class="btn btn-primary btn-block">
Register
</button>
</form>
</div>
</div>
</div>
<a href="/users/logout" class="btn btn-secondary">Logout</a>
我的路线
router.get('/mail', forwardAuthenticated, (req, res) =>
res.render('mail', {
user: req.user,
mail: req.mail
})
);
router.post('/mail', (req, res) => {
const { to, cc, bcc, subject, message, attachment,smtpUrl, smtpUsername, smtpPassword } = req.body;
console.log(smtpUrl)
console.log(smtpPassword)
console.log(smtpUsername)
let errors = [];
if (!smtpUrl || !smtpUsername || !smtpPassword) {
errors.push({ msg: 'Add an account' });
res.render('mail', {
smtpUrl,
smtpPassword,
smtpUsername
});
}else{
console.log(smtpUrl)
console.log(smtpPassword)
console.log(smtpUsername)
const newSmtp = new Smtp({
smtpUrl,
smtpPassword,
smtpUsername
});
newSmtp
.save()
.then(mail => {
req.flash(
'success_msg',
'Account Added'
);
})
.catch(err => console.log(err));
}
if (!to || !subject || !message) {
errors.push({ msg: 'Please enter all fields' });
}
if (errors.length > 0) {
res.render('mail', {
errors,
to,
cc,
bcc,
subject,
message,
attachment,
});
} else {
const newMail = new Mail({
to,
cc,
bcc,
subject,
message,
attachment,
});
let transporter = nodemailer.createTransport({
service: smtpUrl,
auth: {
user: smtpUsername,
pass: smtpPassword
}
});
let mailOptions = {
from: smtpUsername,
to: to,
subject: subject,
text: `${message}`
};
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
} else {
newMail
.save()
.then(mail => {
req.flash(
'success_msg',
'mail sent'
);
})
.catch(err => console.log(err));
console.log('Email sent: ' + info.response);
}
});
}
})
// Login
router.post('/login', (req, res, next) => {
passport.authenticate('local', {
successRedirect: '/users/mail',
failureRedirect: '/users/login',
failureFlash: true
})(req, res, next);
});
// Logout
router.get('/logout', (req, res) => {
req.logout();
req.flash('success_msg', 'You are logged out');
res.redirect('/users/login');
});
我的架构
const mongoose = require('mongoose');
const MailSchema = new mongoose.Schema({
to: {
type: String,
},
cc: {
type: String,
},
bcc: {
type: String,
},
subject: {
type: String,
},
message: {
type: String,
},
attachment: {
type: String,
},
date: {
type: Date,
default: Date.now
},
});
const SmtpSchema = new mongoose.Schema({
smtpUrl: {
type: String,
required: true
},
smtpUsername: {
type: String,
required: true
},
smtpPassword: {
type: String,
required: true
},
date: {
type: Date,
default: Date.now
},
});
const Mail = mongoose.model('Mail', MailSchema);
const Smtp = mongoose.model('Smtp', SmtpSchema);
module.exports = Mail;
module.exports = Smtp;
每当我想在我的视图(ejs)中填写表单并发送到数据库时,我的终端就会出现此错误。
错误[ERR_HTTP_HEADERS_SENT]:无法在发送到客户端后设置标头,我无法发送表单错误[ERR_HTTP_HEADERS_SENT]:无法在发送到客户端后设置标头,我无法发送表单错误[ERR_HTTP_HEADERS_SENT]:无法在发送到客户端后设置标头,我无法发送表单错误[ERR_HTTP_HEADERS_SENT]:发送到客户端后无法设置标题,我无法发送表单
2条答案
按热度按时间g6ll5ycj1#
当您在
router.post('/mail'
中添加smtp部分时出现错误时,您将继续发送邮件,因此在某些情况下,您应该向客户端发送两次响应,这是不可能的。你的根本问题是你在一个路由器路径和方法中做两个不同的工作。我强烈建议将两者分开。我看到你有另一个问题,有2个形式在一个页面上具有相同的动作(这是POST
在路径/mail
),与分离的问题将得到解决。3npbholx2#
我也遇到了同样的问题,为了解决这个问题,您应该在发送响应后添加一个return语句,以确保不会发送其他响应
举例说明:
通过在
res.redirect('/')
之前添加return,可以确保函数在发送重定向响应后立即退出,并且不再执行任何代码