forked from PedroFonsecaDEV/URLpilgrim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
urlFuncs.js
45 lines (40 loc) · 1.15 KB
/
urlFuncs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const fetch = require("node-fetch");
const ora = require("ora");
const { printLog } = require("../consoleMsg/consoleMsg");
const checkDuplicateUrl = (url, urlArray) => {
let found = false;
for (let i = 0; i < urlArray.length; i++) {
if (urlArray[i] == url) {
found = true;
i = urlArray.length + 1;
}
}
return found;
};
const testUrl = async (urlArray) => {
const protocolRegex = /^www(.+)/gi;
const spinner = ora("URL PILGRIM - Checking URLs Status").start();
for (let url of urlArray) {
if (protocolRegex.test(url)) {
url = "https://" + url;
}
fetch(url, { method: "head" })
.then((urlTest) => {
if (urlTest.status == 200) {
printLog(`URL: ${url} Status: ${urlTest.status}`, "good");
} else if (urlTest.status == 400 || urlTest.status == 404) {
printLog(`URL: ${url} Status: ${urlTest.status}`, "bad");
} else {
printLog(`URL: ${url} Status: Unknown`, "unknown");
}
})
.catch((error) => {
printLog(`URL: ${url} Status: Unknown`, "unknown");
});
}
spinner.stop();
};
module.exports = {
checkDuplicateUrl,
testUrl,
};