-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (61 loc) · 2.16 KB
/
index.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const express = require('express');
const fs = require('fs');
const app = express();
// Read TLD data from tld.txt
const readTldData = () => {
try {
const data = fs.readFileSync('tld.txt', 'utf8');
return data.split('\n').filter(Boolean); // Filter out empty lines
} catch (error) {
console.error(error);
throw new Error('Failed to read TLD data from tld.txt file.');
}
};
// API endpoint to check if a TLD is valid
app.get('/is-a-tld', (req, res) => {
try {
const queryTld = req.query.tld;
if (!queryTld) {
res.status(400).json({ error: 'Bad request', message: 'Missing query parameter "tld".' });
return;
}
const tlds = readTldData();
const uppercaseTld = queryTld.toUpperCase();
if (tlds.includes(uppercaseTld)) {
res.json({ message: `.${uppercaseTld.toLowerCase()} is a valid TLD.` });
} else {
res.json({ message: `.${uppercaseTld.toLowerCase()} is not a valid TLD.` });
}
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Internal server error', message: 'Failed to read TLD data.' });
}
});
// API endpoint to check if a link contains a TLD
app.get('/contains-tld', (req, res) => {
try {
const queryLink = req.query.link;
if (!queryLink) {
res.status(400).json({ error: 'Bad request', message: 'Missing query parameter "link".' });
return;
}
const tlds = readTldData();
const lowercaseLink = queryLink.toLowerCase();
const linkTlds = tlds.filter(tld => lowercaseLink.includes(`.${tld.toLowerCase()}`));
if (linkTlds.length > 0) {
res.json({ message: `The link "${lowercaseLink}" contains the following TLDs: ${linkTlds.join(', ')}` });
} else {
res.json({ message: `The link "${lowercaseLink}" does not contain any known TLDs.` });
}
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Internal server error', message: 'Failed to read TLD data.' });
}
});
// Serve index.html from the public directory for the main page
app.use('/', express.static('public'));
// Start the server
const port = 3000;
app.listen(port, () => {
console.log(`Server is listening on port ${port}`);
});