|
1 | | -// index.js - api |
2 | | - |
3 | | -const http = require('http'); |
4 | | -const https = require('https'); |
5 | | -const dns = require('dns'); |
6 | 1 | const express = require('express'); |
7 | 2 | const bodyParser = require('body-parser'); |
8 | | -const url = require('url'); |
9 | | -const now = require('performance-now'); |
10 | 3 | const cors = require('cors'); |
11 | | -const { isURL } = require('validator'); |
| 4 | +const analyzeURL = require('./modules/analyze'); |
12 | 5 |
|
13 | 6 | const app = express(); |
14 | 7 | app.use(bodyParser.json()); |
15 | 8 | app.use(cors()); |
16 | 9 |
|
17 | | -app.post('/api/analyze', (req, res) => { |
18 | | - try { |
19 | | - if (!req.body.url) { |
20 | | - throw new Error('URL is missing'); |
21 | | - } |
22 | | - |
23 | | - const urlToAnalyze = req.body.url; |
24 | | - |
25 | | - if (!isURL(urlToAnalyze)) { |
26 | | - throw new Error('Invalid URL'); |
27 | | - } |
28 | | - |
29 | | - const hostname = url.parse(urlToAnalyze).hostname; |
30 | | - const protocol = url.parse(urlToAnalyze).protocol === 'https:' ? https : http; |
31 | | - const options = { |
32 | | - method: 'HEAD', |
33 | | - timeout: 5000, |
34 | | - }; |
35 | | - |
36 | | - const start = now(); |
37 | | - |
38 | | - protocol.request(urlToAnalyze, options, (httpResponse) => { |
39 | | - const isUp = httpResponse.statusCode >= 200 && httpResponse.statusCode < 400; |
40 | | - if (!isUp) { |
41 | | - return res.json({ isUp: false, ipAddress: null, uptime: 0, responseTime: 0 }); |
42 | | - } |
43 | | - |
44 | | - dns.lookup(hostname, (dnsError, ipAddress) => { |
45 | | - if (dnsError) { |
46 | | - console.error(`Failed to lookup IP address for ${urlToAnalyze}: ${dnsError}`); |
47 | | - return res.status(500).json({ error: 'Internal server error' }); |
48 | | - } |
49 | | - |
50 | | - // Calculate responseTime |
51 | | - const end = now(); |
52 | | - const responseTime = ((end - start) / 1000).toFixed(2); |
53 | | - |
54 | | - // calculate uptime |
55 | | - const totalSeconds = (end - start) / 1000; // convert to seconds |
56 | | - const availableSeconds = totalSeconds - (totalSeconds * 0.01); // assuming 99.99% uptime |
57 | | - const uptime = ((availableSeconds / totalSeconds) * 100).toFixed(2); |
58 | | - |
59 | | - console.log(`isUp: ${isUp}, ipAddress: ${ipAddress}, uptime: ${uptime}%, responseTime: ${responseTime}s`); |
60 | | - return res.json({ isUp: true, ipAddress, uptime, responseTime }); |
61 | | - }); |
62 | | - }).on('error', (error) => { |
63 | | - console.error(`Failed to request ${urlToAnalyze}: ${error}`); |
64 | | - return res.json({ isUp: false, ipAddress: null, uptime: 0, responseTime: 0 }); |
65 | | - }).end(); |
66 | | - } catch (error) { |
67 | | - console.error(`Error analyzing URL: ${error.message}`); |
68 | | - return res.status(400).json({ error: error.message }); |
69 | | - } |
70 | | -}); |
| 10 | +app.post('/api/analyze', analyzeURL); |
71 | 11 |
|
72 | 12 | const port = process.env.PORT || 5000; |
73 | 13 | app.listen(port, () => { |
|
0 commit comments