A visitor writes: "Orders said Something went wrong." You open the server's log and find many error lines that look alike. Which one is theirs? A request id ties the two together. The server gives each request a label as it arrives, writes it in every log line, and sends it back in a header, X-Request-Id. The page shows it next to the message, and you search the log for it.
Think of the ticket at a deli counter: when number 5 is called, you and the clerk both know who that is. In this course the id is a counter too: req-1, req-2, and so on. A middleware, a function with (req, res, next), hands it out. Express calls it for every request once it's given to app.use, and it must call next() at the end, or the request never reaches its route.
The client side is given: api() puts the header into the ApiError, and the Orders page adds it to the message. The orders query still fails on purpose. The visitor's first four requests are the login page and the login, so the next one is number 5. Your server must give:
click "Orders" → GET /api/orders → 500
server log: req-5 GET /api/orders Error: no such column: order_items.price
page: Something went wrong (req-5)
click "Try again" → page: Something went wrong (req-6)
POST /api/login → 200 | X-Request-Id: req-7 {"email": "ana@example.com"}
GET /api/orders → 500 | X-Request-Id: req-8 {"error": "Something went wrong"}
The id is safe to show: it's only a number that means something in your log.
Your task: in server/app.js, add the counter lastRequestId and the middleware requestId, which sets req.id and the X-Request-Id header. Put req.id first in handleError's log line, and register requestId before every other middleware.
Assignment
Follow the Coding Tutorial to give every request an id, send it back as X-Request-Id and write it in the error log.
Hint
The counter lives outside the function, with let, so it keeps counting from one request to the next. If the log line starts with undefined, requestId isn't given to app.use yet. If a request hangs, next() is missing.
Free plan: 30 tutor messages. PRO is unlimited.
