improvement

This commit is contained in:
Vadher
2026-07-19 16:39:49 +05:30
parent 803efec35a
commit 5db5d5c7d3
7 changed files with 2007 additions and 18764 deletions
+1918 -18692
View File
File diff suppressed because it is too large Load Diff
+18 -9
View File
@@ -1,15 +1,24 @@
[
"cloudflare.com",
"postgresql.org",
"github.com",
"mozilla.org",
"amazon.com",
"example.invalid",
"google.com",
"myfakewebsite123.xyz",
"postgresql.org",
"notarealdomain.test",
"github.com",
"docker.com",
"wikipedia.org",
"duckduckgo.com",
"nodejs.org",
"amns.in",
"invalid-site-999.com",
"mozilla.org",
"amazon.in",
"amdocs.com"
"thisdoesnotexistabc123.com",
"nodejs.org",
"amdocs.com",
"fake-company-domain.org",
"wikipedia.org",
"nonexistentdomain987654.net",
"duckduckgo.com",
"amazon.com",
"amns.in",
"missingwebsite.fake",
"randominvaliddomain12345.io"
]
+6
View File
@@ -4,6 +4,12 @@ const url_path = path.join(__dirname, "../database", "db.json");
const StoreRecord = async (urlRecord) => {
try {
try {
await fs.access(url_path);
} catch {
await fs.writeFile(url_path, "[]");
}
const rawData = await fs.readFile(url_path, "utf8");
const data = JSON.parse(rawData);
+10 -5
View File
@@ -6,7 +6,6 @@ const { StoreRecord } = require("./models/db_store_controller");
const { checkUrl } = require("./models/health_status_controller");
const { init, broadcast } = require("./utils/utils");
// const urlPath = "./database/urls.json";
const urlPath = path.join(__dirname, "database", "urls.json");
dotenv.config();
@@ -35,11 +34,17 @@ const checkAllUrl = async (Store) => {
async function setupWebSocket(wss) {
init(wss);
let Store = [];
checkAllUrl(Store);
setInterval(async () => {
Store = [];
wss.on("connection", (ws) => {
checkAllUrl(Store);
}, 2000);
});
setInterval(
async () => {
Store = [];
checkAllUrl(Store);
},
process.env.TIME || 60 * 1000,
);
}
module.exports = { setupWebSocket };
+1
View File
@@ -15,6 +15,7 @@
<h1>URL HEALTH MONITOR</h1>
<h4>WebSocket: <span id="websocket_status">Connecting...</span></h4>
<h4>Page will fetch latest update every 60 second </h4>
<h4 id="status"></h4>
</div>
+51 -54
View File
@@ -1,23 +1,22 @@
const tbody = document.getElementById("content-display");
async function init() {
try {
const response = await fetch("/api/url");
try {
const response = await fetch("/api/url");
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
const urls = await response.json();
const urls = await response.json();
urls.forEach((url) => {
urls.forEach((url) => {
const id = url.replace(/[^a-zA-Z0-9]/g, "_");
const id = url.replace(/[^a-zA-Z0-9]/g, "_");
const row = document.createElement("tr");
row.id = id;
const row = document.createElement("tr");
row.id = id;
row.innerHTML = `
row.innerHTML = `
<td>${url}</td>
<td class="status">Waiting...</td>
<td class="statusCode">-</td>
@@ -25,12 +24,11 @@ async function init() {
<td class="error">-</td>
`;
tbody.appendChild(row);
});
} catch (err) {
console.error(err);
}
tbody.appendChild(row);
});
} catch (err) {
console.error(err);
}
}
document.addEventListener("DOMContentLoaded", init);
@@ -43,60 +41,59 @@ console.log(socket);
const websocket_status = document.getElementById("websocket_status");
socket.onopen = () => {
websocket_status.textContent = "Connected";
websocket_status.textContent = "Connected";
};
let counter = 0
let counter = 0;
socket.onmessage = (event) => {
// console.log(counter)
const data = JSON.parse(event.data);
if (data.type === "checking") {
counter = counter + 1
console.log(counter)
document.getElementById("status").textContent = "Refreshed " + counter + " Times";
return;
}
// console.log(counter)
const data = JSON.parse(event.data);
if (data.type === "checking") {
counter = counter + 1;
document.getElementById("status").textContent =
"Refreshed " + counter + " Times";
return;
}
const id = data.url.replace(/[^a-zA-Z0-9]/g, "_");
const row = document.getElementById(id);
if (!row) return;
const id = data.url.replace(/[^a-zA-Z0-9]/g, "_");
const row = document.getElementById(id);
if (!row) return;
row.querySelector(".status").textContent = data.status;
row.querySelector(".statusCode").textContent = data.statusCode;
row.querySelector(".responseTime").textContent = data.responseTime != null ? `${data.responseTime} ms` : "-";
row.querySelector(".error").textContent = data.error || "-";
row.querySelector(".status").textContent = data.status;
row.querySelector(".statusCode").textContent = data.statusCode;
row.querySelector(".responseTime").textContent =
data.responseTime != null ? `${data.responseTime} ms` : "-";
row.querySelector(".error").textContent = data.error || "-";
};
socket.onclose = () => {
websocket_status.textContent = "Disconnected";
websocket_status.textContent = "Disconnected";
};
document.getElementById("addBtn").addEventListener("click", async () => {
const url = document.getElementById("urlInput").value.trim();
if (!url) return;
const url = document.getElementById("urlInput").value.trim();
if (!url) return;
const response = await fetch("/api/url/register", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ url })
});
const response = await fetch("/api/url/register", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ url }),
});
if (response.ok) {
document.getElementById("urlInput").value = "";
if (response.ok) {
document.getElementById("urlInput").value = "";
const id = url.replace(/[^a-zA-Z0-9]/g, "_");
if (!document.getElementById(id)) {
const row = document.createElement("tr");
row.id = id;
row.innerHTML = `
const id = url.replace(/[^a-zA-Z0-9]/g, "_");
if (!document.getElementById(id)) {
const row = document.createElement("tr");
row.id = id;
row.innerHTML = `
<td>${url}</td>
<td class="status">Waiting...</td>
<td class="statusCode">-</td>
<td class="responseTime">-</td>
<td class="error">-</td>
`;
tbody.appendChild(row);
}
tbody.appendChild(row);
}
}
});
+2 -3
View File
@@ -2,10 +2,9 @@
"name": "uptime_monitor",
"version": "1.0.0",
"description": "A lightweight **URL Health Monitoring** application built with **Node.js, Express, WebSocket, Axios, and Docker**. The application periodically checks the health of registered URLs and updates the frontend in real time using WebSockets.",
"main": "app.js",
"main": "./backend/app.js",
"scripts": {
"start": "node backend/app.js",
"test": "echo \"Error: no test specified\" && exit 1"
"start": "node backend/app.js"
},
"keywords": [],
"author": "Akshay Vadher",