Add HEAD request support for uptime monitoring

This commit is contained in:
chrisf committed 2026-10-29 22:58:40 -06:00
1 parent e90505a39b
commit 707d969da4
2 files changed
+21 -6

No files matched your search

+17 -6
View File
@@ -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);
}
+4
View File
@@ -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 () => {