发送邮件后,Nodemailer速度很慢

在用户提交联系表单之后,我使用nodemailer发送邮件,然后用户将被定向到一个感谢页面。 我现在在cloud9上开发了这个项目。 这是我的代码:

var express = require("express"), bodyParse = require("body-parser"), multer = require("multer"), nodemailer = require("nodemailer"); var app = express(); app.use(bodyParse.urlencoded({extended: true})); var smtpTransport = nodemailer.createTransport({ service: "Gmail", auth: { type: "OAuth2", user: "xxxxx@gmail.com", clientId: "xxxxxx", clientSecret: "xxxxx", refreshToken: "xxxxx", accessToken: "xxxxx" } }); app.post("/upload", function(req, res){ var mailOptions = { from: "xxxx <xxxx@gmail.com", // sender address to: "xxxx@gmail.com", // list of receivers subject: "xxxx", // Subject line text: "xxxx", attachments: [ { // file on disk as an attachment filename: 'xxxx.txt', path: './uploads/' + req.body.filename // stream this file } ] }; // send mail with defined transport object smtpTransport.sendMail(mailOptions, (error, info) => { if (error) { return console.log(error); } console.log('Message %s sent: %s', info.messageId, info.response); }); }) 

电子邮件可以发送成功,我可以在提交表单后几秒钟内看到它,但页面将继续加载,直到显示一些错误(我附上了错误消息下面)。 在此期间,我将收到1或2个相同的电子邮件。

 <html> <head> <meta charset='utf-8'> <title>Error 502 - Bad Gateway</title> <link rel="stylesheet" type="text/css" href="https://cdn.c9.io/errors/style.css" /> <style type="text/css"> .error_content { background: rgba(255, 255, 255, 0.23); padding: 10px; width: 641px; margin: 25px 0; display: none; } #error-msg { display: block; } </style> </head> <body class="errorUnknown light"> <div id="wrapper"> <h1>Error 502 - Bad Gateway</h1> <div class="error_content" id="error-msg"> <p>Please click <a href="javascript:location.reload(true)">here</a> to try again, if the issue persists please contact <a href="https://c9.io/support">support</a></p> </div> <a href="http://status.c9.io">Status Page</a> | <a href="https://c9.io/support">Support</a> | <a href="https://c9.io/dashboard.html">Dashboard</a> | <a href="https://c9.io">Home</a> </div> </body> </html> 

QQQQQ:我该怎么办才能解决这个问题? QQQQQ:这是我的代码或cloud9服务器的问题吗?

这是因为你永远不会在你的app.post()处理程序中发回一个响应给浏览器。 所以,浏览器只是不停地等待从服务器收到回应,而且永远不会 – 最终超时。 这完全是您的代码,与您的云提供商无关。

在你的app.post()处理程序中,你需要一些类似res.send(xxx)的响应内容,告诉浏览器发布的请求已经完成。 如果这个POST来自ajax调用,那么你可以发送你想要的任何回应给客户端的Javascript。 如果POST请求是浏览器中的表单提交,那么您需要发回浏览器将加载并显示给用户的HTML页面。