Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Follow One Request Through the API

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. Its app.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-Id on 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 in req.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 its POST handler takes over.
  • The handler checks, then calls. It checks req.body with the NewShow schema, then calls addShow() 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 reads request-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 Location and sends 201 with 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 status 201 and 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. The NewShow schema in the POST handler decides it’s wrong. The check fails, and the handler throws an HttpError with 422. 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 422 in 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 NewShow schema.
  • One show per date? That needs the stored shows, so addShow() checks it inside store.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:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Your shows slice answers a POST with 201 Created. From the outside, that’s one command and one answer. Behind it, the request passed through every file you wrote.

00:12 Gloria’s request line will bring rules of its own. To know where each one goes, follow one request through the API. Then follow one that fails.

00:23 Everything starts in server.ts, the file you run. Its listen call starts a Node HTTP server. Each request the server reads from the socket becomes one call to the app.

00:36 Inside the app, the request meets the layers in the order createApp() mounts them. The request logger comes first, then CORS, then the JSON parser. Next comes the shows router, then the 404 fallback, and last, the error handler.

00:53 Here’s the request: Gloria’s show on December 20, sent with the request form’s origin. The request logger runs first. It makes an id and sets the X-Request-Id header on the response.

01:07 It asks to be told once the response is sent, and logs only then. CORS sees the form’s origin and adds Access-Control-Allow-Origin. That tells the browser the page may read the answer.

01:20 CORS never turns a request away. The JSON parser turns the body from text into an object in req.body.

01:29 The path starts with slash shows, so the app hands the request to the shows router. Its POST handler takes over.

01:39 The handler checks req.body with the NewShow schema. With a title, a presenter and a real date, it passes. Then it calls addShow() with the checked show. That function knows nothing about HTTP.

01:53 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 reads request-line.json, the new show joins the list with a fresh id, and the store saves the file.

02:09 It now holds three shows.

02:13 The new show comes back out of addShow(). The handler sets Location and sends 201 with the show. The response doesn’t climb back up the column. The headers the logger and CORS set on the way down are already on it.

02:28 Node writes it to the socket, and curl prints the answer. Once Node has finished its part, the request logger’s listener runs. Its line has status 201 and the same request id as the header.

02:42 Now send it again with one change: the date is Sunday. It passes the logger, CORS and the parser, as before. So which part decides it’s wrong? Your turn: pause the video and predict it.

03:01 It’s the schema, in the POST handler. Sunday isn’t a date, so the check fails, and the handler throws an HttpError with 422.

03:11 addShow() never runs, so the store never touches the file. It still holds three shows.

03:19 The error handler only writes the answer. Express 5 hands it the thrown error. It skips the 404 fallback, because only middleware with four parameters receives errors.

03:31 The error handler sends 422 in the error shape, with the id and CORS headers already set. This time, the log line is a warning.

03:42 Each part 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 NewShow schema. One show per date?

03:54 That needs the stored shows, so addShow() checks it inside store.change(), during its turn. Remember the second post waiting in the queue? Its turn starts after the first one saves, so it finds the date taken.

04:08 A header on every response, like the request id, goes in middleware, mounted before the routers. A new resource, like presenters, gets its own router. You’ve followed one request down through the layers and back, and seen where a failing one stops.

04:26 Next, in Project: Riverside FM’s Request Line, Gloria’s brief brings new resources and rules, and you decide where each one goes.

Become a Member to join the conversation.