Update index.js

This commit is contained in:
William Harrison authored and GitHub committed 2023-05-27 13:55:29 +08:00
1 parent c69e790329
commit 46efb9ba07
1 file changed
+33 -37
+33 -37
View File
@@ -5,45 +5,41 @@ const util = require("util");
const argv = minimist(process.argv.slice(2));
async function validate() {
const email = argv.email;
if(!email) return throw new Error("No email provided!");
const domain = email.split("@").pop();
const validEmail = validateEmail(email);
if(!validEmail) return throw new Error("The email address does not match the correct format!");
const getMXRecords = util.promisify(dns.resolveMx);
let mxRecords = [];
try {
const email = argv.email;
if(!email) return throw new Error("No email provided!");
const domain = email.split("@").pop();
const validEmail = validateEmail(email);
if(!validEmail) return throw new Error("The email address does not match the correct format!");
const getMXRecords = util.promisify(dns.resolveMx);
let mxRecords = [];
try {
mxRecords = await getMXRecords(domain);
} catch (err) {
throw new Error(`Error fetching MX records for the domain ${domain}: ${err.message}`);
}
if(!mxRecords.length) return throw new Error(`No MX records exist for the domain ${domain}!`);
const result = {
"success": true,
"email": email,
"test_results": {
"matches_format": true,
"mx_exists": true
},
"results": {
"domain": domain,
"mx_records": mxRecords
}
}
console.log(result);
} catch(err) {
throw new Error(err.message);
mxRecords = await getMXRecords(domain);
} catch (err) {
throw new Error(`Error fetching MX records for the domain ${domain}: ${err.message}`);
}
if(!mxRecords.length) return throw new Error(`No MX records exist for the domain ${domain}!`);
const result = {
"success": true,
"email": email,
"test_results": {
"matches_format": true,
"mx_exists": true
},
"results": {
"domain": domain,
"mx_records": mxRecords
}
}
console.log(result);
}
validate();