I'm using nodemailer to send email after the user submits the contact form, and then the user will be directed to a thank you page. I worked on this project on cloud9 now. Here is my code:
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: "[email protected]",
clientId: "xxxxxx",
clientSecret: "xxxxx",
refreshToken: "xxxxx",
accessToken: "xxxxx"
}
});
app.post("/upload", function(req, res){
var mailOptions = {
from: "xxxx <[email protected]", // sender address
to: "[email protected]", // 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);
});
})
The email can be sent out successfully, and I can see it in a few seconds after submitting the form, but the page will keep loading forever until showing up some error (I attached the error message below). During this period, I will receive 1 or 2 more same emails.
<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: What should I do to fix this? QQQQQ: Is this the problem of my code or the cloud9 server?
That's because you never send a response back to the browser in your app.post()
handler. So, the browser just keeps waiting to hear back from your server and it never does - eventually timing out. This is entirely your code, nothing to do with your cloud provider.
Inside your app.post()
handler, you need some sort of response content like res.send(xxx)
that will tell the browser the post request has finished. If this POST is from an ajax call, then you can just send whatever response you want back to your client-side Javascript. If the POST request is a form submission in the browser, then you need to send back an HTML page that the browser will load and show to the user.