In this lesson, you followed requests down the assistant’s Express app, function by function, and moved a route that never answered to where it could.
Key Points
- A route below the fallback never runs. A
/healthroute added at the bottom ofserver.js, below the download’s 404 fallback, answers404with{"error":"Not found"}. The timer’s line still shows up in the server’s terminal, because the functions at the top ran. - Express sees the app as a list of functions, in the order the file registers them:
requestId,timer,express.json(),express.static("public")(the file server, which sends the page’s files frompublic, andpublic/index.htmlfor/), the/askroute withpromptVersion("v2")before its handler, then the 404 fallback, which answers404Not found to any request that reaches it. - A request enters at the top. Each function either answers it or calls
next()to hand it to the function below. - Express skips a route whose method or path doesn’t match.
app.post("/ask", ...)only takes POST requests to/ask. - The fallback has no path, so it matches every request that gets that far, and it answers. Nothing below it sees the request.
- Watch out: Express doesn’t gather all its middleware and run it before the routes. It runs everything in file order, so
express.json()registered below/askwould come too late, and the route would findreq.bodyundefined. - Moving the
/healthroute above the fallback fixes it. The route then answers200with{"ok":true}, and the fallback still catches every address nothing above it answers. - A hiker’s question,
POST /askwith a JSON body, runs six functions. First comerequestId,timer,express.json(), which reads the question intoreq.body, andexpress.static(), which only serves files to GET and HEAD requests, so it passes the request on. Inside the/askroute, the middleware thatpromptVersion("v2")returns setsX-Prompt-Version: v2, and then the handler answers. The page,GET /, runs four:requestId,timer,express.json()(no body), andexpress.static(), which findspublic/index.htmland answers. - Express runs your functions from top to bottom, and the first one that answers ends the request. Functions every request needs go at the top, routes below them, and the fallback last.
Final Code
server.js, with the /health route above the fallback (the /ask route’s body is unchanged):
server.js
import express from "express";
import { requestId, timer, promptVersion } from "./middleware.js";
const app = express();
app.use(requestId);
app.use(timer);
app.use(express.json());
app.use(express.static("public"));
app.post("/ask", promptVersion("v2"), async (req, res) => {
// ...
});
app.get("/health", (req, res) => res.json({ ok: true }));
app.use((req, res) => res.status(404).json({ error: "Not found" }));
app.listen(3000, () => console.log("Ridgeline Huts assistant on http://localhost:3000"));
Check it with curl in a second terminal while the server runs:
$ curl -i http://localhost:3000/health
HTTP/1.1 200 OK
X-Powered-By: Express
X-Request-Id: 3e1839d9-4047-45dc-a3a9-2d72e30e744f
...
{"ok":true}
Next, in Log One JSON Line per Request, you’ll decide what’s worth writing down about each request.