我正在从我的node.js服务器发送多封电子邮件,我想在前端显示已发送多少邮件的进度。
我编写了仅在发送完每封电子邮件后才发送响应的代码,但是我需要通知前端有关进度。 据我所知,我无法在单个http请求上发送多个响应,所以我有点在这里迷失了。
我的电子邮件发送请求处理程序:
const send = async (req, res) => {
//parsing request body
const {user, recievers, subject, text} = req.body;
//Getting sender credentials from db
const user = await UserSettings.findOne({ user }, 'MAIL');
//creating nodemailer transporter
const transporter = nodemailer.createTransport({
service: user.MAIL.SERVICE,
auth: {
user: user.MAIL.USER,
pass: user.MAIL.PASSWORD,
},
});
//sending email to every reciever
recievers.forEach(reciever=> {
const mailOptions = {
from: user.MAIL.USER,
to: reciever,
subject,
text
};
return transporter.sendMail(mailOptions, (error, info) => {
if (!error) {
console.log(`Email sent: ${info.response}`);
} else {
console.log(error);
}
});
});
return res.sendStatus(204);
};