In this lesson, you followed one POST /shows through the request line’s API, from the socket to the data file and back, then followed the same request with a bad date to see where it stops.
Key Points
- Everything starts in
server.ts. Itsapp.listen()call starts a Node HTTP server, and each request the server reads from the socket becomes one call to the app. - The layers run in the order
createApp()mounts them: the request logger, then CORS, then the JSON parser, then the shows router, then the 404 fallback, and last, the error handler. - On the way down, the middleware prepare the request and the response. The request logger makes an id and sets
X-Request-Idon the response. It asks to be told once the response is sent, and logs only then. - CORS and the parser add their part. CORS sees the request form’s origin and adds
Access-Control-Allow-Origin, which tells the browser the page may read the answer. CORS never turns a request away. The JSON parser turns the body from text into an object inreq.body. - The router hands the request to one handler. The path starts with
/shows, so the app passes the request to the shows router, and itsPOSThandler takes over. - The handler checks, then calls. It checks
req.bodywith theNewShowschema, then callsaddShow()with the checked show. That function knows nothing about HTTP. - The store saves the change.
addShow()hands one change to the store’s queue, so two shows posted together can’t overwrite each other. When its turn comes, the store readsrequest-line.json, the new show joins the list with a fresh id, and the store saves the file. It now holds three shows. - The way back doesn’t pass through the middleware again. The handler sets
Locationand sends201with the show. The headers the logger and CORS set on the way down are already on the response. Node writes it to the socket, and curl prints the answer. Once Node has finished its part, the logger’s listener writes the log line, with status201and the same request id as the header. - A show with
"date": "Sunday"stops at the schema. It passes the logger, CORS and the parser as before. TheNewShowschema in thePOSThandler decides it’s wrong. The check fails, and the handler throws anHttpErrorwith422.addShow()never runs, so the store never touches the file, which still holds three shows. - The error handler only writes the answer. Express 5 hands the thrown error straight to it and skips the 404 fallback, because only middleware with four parameters receives errors. The error handler sends
422in the error shape, with the id and CORS headers already set, and this time the log line is a warning ("level":"warn"). - The rule: each layer has one job, so ask what a new rule needs to know.
- Only dates on a Sunday? That needs only the date, so it goes in the
NewShowschema. - One show per date? That needs the stored shows, so
addShow()checks it insidestore.change(), during its turn. A second post waits in the store’s queue. Its turn starts after the first one saves, so it finds the date taken. - A header on every response, like the request id, goes in middleware, mounted in
createApp()before the routers. - A new resource, like presenters, gets its own router.
The Two Requests
The good request sends the show on December 20 with the request form’s origin. The answer is trimmed to the headers the video follows: