diff --git a/server/index.js b/server/index.js index 1a3157a..5179275 100644 --- a/server/index.js +++ b/server/index.js @@ -86,7 +86,14 @@ export function createApp(dbPath = DEFAULT_DB_PATH) { try { // 1. Health check - if (req.method === "GET" && pathname === "/api/health") { + if ((req.method === "GET" || req.method === "HEAD") && pathname === "/api/health") { + if (req.method === "HEAD") { + res.writeHead(200, { + "Content-Type": "application/json; charset=utf-8", + "Access-Control-Allow-Origin": "*" + }); + return res.end(); + } return sendJson(res, 200, { status: "ok", app: "spectra", @@ -278,7 +285,7 @@ export function createApp(dbPath = DEFAULT_DB_PATH) { } // 10. Static files - if (req.method === "GET") { + if (req.method === "GET" || req.method === "HEAD") { let filePath = pathname === "/" ? "/index.html" : pathname; const resolvedPath = path.join(PUBLIC_DIR, filePath); @@ -290,23 +297,27 @@ export function createApp(dbPath = DEFAULT_DB_PATH) { if (fs.existsSync(resolvedPath) && fs.statSync(resolvedPath).isFile()) { const ext = path.extname(resolvedPath).toLowerCase(); const contentType = MIME_TYPES[ext] || "application/octet-stream"; - const content = fs.readFileSync(resolvedPath); + const stat = fs.statSync(resolvedPath); res.writeHead(200, { "Content-Type": contentType, - "Content-Length": content.length, + "Content-Length": stat.size, "Cache-Control": ext === ".html" ? "no-cache" : "public, max-age=3600" }); + if (req.method === "HEAD") return res.end(); + const content = fs.readFileSync(resolvedPath); return res.end(content); } // Fallback for SPA routing const indexHtmlPath = path.join(PUBLIC_DIR, "index.html"); if (fs.existsSync(indexHtmlPath)) { - const content = fs.readFileSync(indexHtmlPath); + const stat = fs.statSync(indexHtmlPath); res.writeHead(200, { "Content-Type": "text/html; charset=utf-8", - "Content-Length": content.length + "Content-Length": stat.size }); + if (req.method === "HEAD") return res.end(); + const content = fs.readFileSync(indexHtmlPath); return res.end(content); } diff --git a/test/api.test.js b/test/api.test.js index 6cda489..ec07404 100644 --- a/test/api.test.js +++ b/test/api.test.js @@ -41,6 +41,10 @@ describe("Spectra YouTube Suggest Engine - Test Suite", () => { assert.equal(data.status, "ok"); assert.equal(data.database, "healthy"); assert.equal(data.app, "spectra"); + + // Also verify HEAD request for Homepage / uptime monitoring + const headRes = await fetch(`${baseUrl}/api/health`, { method: "HEAD" }); + assert.equal(headRes.status, 200); }); it("2. GET /api/topics returns curated starter topics and people", async () => {