Я новичок в nodejs и создаю приложение для проверки действительных адресов электронной почты.
Код работает нормально в localhost, но когда я загружаю его на сервер (AWS с прокси-сервером nginx), приложение иногда работает, а когда я посещаю URL-адрес много раз, оно застревает при загрузке.
Я использую сетевой модуль. Он останавливается во время conn.on ('data', data => {. Ниже мой код:
module.exports.getCheckEmail = function(req, res){
var emailSender = '11wicketsfantasy@gmail.com';
var data = req.query;
var emailToCheck = data.email;
var i = 0;
var type = 'success';
var message = 'Email is valid'
// Finding domain of email address
let domain = emailToCheck.split('@')[1];
dns.resolveMx(domain, function(err, addresses) {
if(typeof addresses == 'undefined' || addresses == null) {
res.status(200).json({
type: "error",
message: 'No MX Records Found'
});
} else {
// If MX Records are found
var tempMx = JSON.stringify(addresses);
var mxRecords = JSON.parse(tempMx);
// Getting minimum mx priority value
var min = Math.min.apply( null, mxRecords.map((v) => v.priority));
var mxDomainArray = mxRecords.filter((it) => {
return it.priority === min;
});
var mxDomain = mxDomainArray[0].exchange;
console.log('1 mx: '+mxDomain);
const TELNET_PORT = 25;
const conn = net.createConnection(TELNET_PORT, mxDomain);
conn.setEncoding('ascii');
conn.setTimeout(6000);
conn.on('error', function() {
res.status(200).json({
type: "error",
message: 'Error! There was some error during connection to mail server'
});
conn.destroy();
});
conn.on('false', function () {
res.status(200).json({
type: "error",
message: 'False! There was some error during connection to mail server'
});
conn.removeAllListeners();
conn.destroy();
});
console.log('before connect');
conn.on('connect', () => {
console.log('inside connect');
conn.on('prompt', function () {
const EOL = '\r\n';
conn.write(`helo ${mxDomain}` + EOL);
conn.write(`mail from: <${emailSender}>` + EOL);
conn.write(`rcpt to: <${emailToCheck}>` + EOL);
conn.write('quit' + EOL);
});
console.log('after conn');
conn.on('undetermined', function () {
res.status(200).json({
type: "error",
message: 'Undetermined! Mail server was undetermined'
});
conn.removeAllListeners();
conn.destroy(); //destroy socket manually
});
conn.on('timeout', () => {
console.log('inside timeout');
// type = "timeout";
// messsage = "Your email server is not responding. Kindly use different email id";
res.status(200).json({
type: "timeout",
message: 'Your email server is not responding. Kindly use different email id.'
});
//console.log('timeout from conn.on');
conn.removeAllListeners();
conn.destroy();
});
conn.on('data', data => {
console.log(data);
const response = data.toString().trim();
// console.log("Data: "+response);
if (response.startsWith('220') || response.startsWith('250') || response.startsWith('221') ) {
conn.emit('prompt');
} else {
type = 'error';
message = data;
}
});
conn.on('end', () => {
res.status(200).json({
type: type,
message: message
});
conn.removeAllListeners();
conn.destroy();
});
});
}
});
} //end module