[object Object]

← back to Voice Chat

add 404 guard middleware after express.static

621724fe8244f24d3b87053478a2d83128cc1e00 · 2026-05-19 21:30:55 -0700 · Steve Abrams

Any path that falls past the static handler now returns a structured
404 (JSON for /api/*, plain text otherwise) instead of Express's
default HTML page — so callers can't mistake a typo'd route for a 200.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

Files touched

Diff

commit 621724fe8244f24d3b87053478a2d83128cc1e00
Author: Steve Abrams <steve@designerwallcoverings.com>
Date:   Tue May 19 21:30:55 2026 -0700

    add 404 guard middleware after express.static
    
    Any path that falls past the static handler now returns a structured
    404 (JSON for /api/*, plain text otherwise) instead of Express's
    default HTML page — so callers can't mistake a typo'd route for a 200.
    
    Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
---
 server.js | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/server.js b/server.js
index 79379e2..3b0f32e 100644
--- a/server.js
+++ b/server.js
@@ -170,6 +170,16 @@ app.post("/api/chat", async (req, res) => {
 
 app.use(express.static(join(__dirname, "public")));
 
+// 404 guard — any path that fell past express.static (e.g. mistyped /api/*
+// or a stale clean-URL) returns a structured 404 instead of Express's
+// default HTML page, so callers don't mistake it for a 200.
+app.use((req, res) => {
+  if (req.path.startsWith("/api/")) {
+    return res.status(404).json({ error: "not found", path: req.path });
+  }
+  res.status(404).type("txt").send("404 not found: " + req.path);
+});
+
 app.listen(PORT, () => {
   console.log(`voice-chat ready on http://localhost:${PORT}`);
   console.log(`  backend=${OLLAMA_URL} model=${MODEL}`);

← aad10fa broaden .gitignore with snapshot/build excludes  ·  back to Voice Chat  ·  Add per-site favicon (kills /favicon.ico 404) c5494dc →