I have node.js app that upon click of a button send post request and on post request email should be sent to the admin. The page link from where I initiate request can be found here: https://blockmmerce.io/views/contact.
As soon as I click the button the event listener is called but it tries to send the request and returns back with 502 error code.
any possible fix?
contact.js:
$(document).ready(function () {
console.log("here...")
var enquirybutton = document.querySelector('#sendEnquiry');
enquirybutton.addEventListener('click', function (event) {
alert("clicked!!");
let name = $('#name').val();
let email = $('#email').val();
let message = $('#message').val();
event.preventDefault();
$.ajax({
url: "/sendEnquiry",
dataType: "json",
type: "post",
cache: false,
data: ({
"name": name,
"email": email,
"message": message
}),
success: function (response) {
alert("Enquiry sent successfully !!!")
},
error: function (textStatus, errorThrown) {
console.log("Error::::" + errorThrown);
console.log("status::::" + String(textStatus));
}
});
});
});
Index.js:
var express = require('express');
var router = express.Router();
var emailConfig = require('../config/email.config.js');
router.post('/sendEnquiry', function (req, res, next){
let name = req.body.name;
let email = req.body.email;
let message = req.body.message;
emailConfig.sendEmail(
'admin@blockmmerce.io',
'Enquiry',
`this is first query`
);
res.json({});
console.log("hello from send enquiry");
});
modules.exports = export;
email.config.js:
var nodemailer = require('nodemailer');
//--------------------------------------------------------
// Declarations
//--------------------------------------------------------
var senderEmail = 'info@server119.web-hosting.com';
// var password = '----';
//--------------------------------
// Mail Sending Email
//--------------------------------------------------------
var transporter = nodemailer.createTransport({
host: 'server119-3.web-hosting.com',
secure: true,
port: 465,
auth: {
user: 'info@blockmmerce.io',
pass: '------'
}
});
// ================================================================
// handle Sendmail
// ================================================================
var sendEMail = (emailId, subject, mailcontent) => {
let mailOptions = {
from: senderEmail,
to: emailId,
subject: subject,
html: mailcontent
};
return new Promise(function (resolve, reject) {
transporter.sendMail(mailOptions, function (err, info) {
if (err) {
reject(err);
} else {
resolve(info);
}
});
});
}
//--------------------------------------------------------
// Exporting All functions
//--------------------------------------------------------
module.exports = {
transporter,
senderEmail,
sendEMail
}
You have to add content type and submit data as string
$.ajax({
contentType: 'application/json',
data: JSON.stringify({
name:"Bob",
...
})
});
Also you have to return response to user from the server side, also don't use await and return response immediately to user, no need to wait till the mail to be submitted, because it will take time.
router.post('/sendEnquiry', function (req, res, next){
let name = req.body.name;
let email = req.body.email;
let message = req.body.message;
emailConfig.sendEmail(
'admin@blockmmerce.io',
'Enquiry',
// `Hi, I have an enquiry that: ${message}. You can reply back on my this email ${email}. Thanks, ${name}`
`${message}`
);
res.json({});
});