Booking backend project · Chapter 12 of 12

Hardening and the take-home

By the end of this chapter you can write the middleware every API needs before it faces the internet: a limit on login tries that answers 429 with Retry-After, the security headers a JSON API sends, and one log line per request with an id a support ticket can quote. Then you take the whole codebase home, ready to push to GitHub. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Ready for production
  • 3 tasks
  • TypeScript
  • In the all labs plan

The first 2 chapters of Booking backend project are free. This one comes with the all labs plan.

Why you need this

Diwali week starts on Friday 6 November, and Parda's API goes public: Rukhsana's site calls it from every member's phone, and anyone on the internet can call it too. Three things are missing, and Nikhil has a story for each.

Guessing passwords. Login checks a bcrypt hash, about 86 ms a try on the lab's machine. Nothing stops a script from trying Neha's email with a list of common passwords, all day. Here are six wrong tries in a row with this chapter's starter, which is the app as chapter 11 left it:

401
401
401
401
401
401

Every one is answered, and the seventh would be too.

Headers. The starter's /health answers with no security headers at all:

HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 98
ETag: W/"62-ki25Zkx61LtAKsvyKhymdKYyfm0"
Date: Sat, 24 Oct 2026 01:51:01 GMT
Connection: keep-alive
Keep-Alive: timeout=5

A browser is left to guess what it may do with the answer: sniff its type, show it inside another site's frame, send the full address of the page that called it to the next site.

Logs. When Datta forwards a member's message ("I paid and got an error at 9:12 pm"), nobody can find that request: the server prints "listening" and nothing else. And when someone adds logging in a hurry, the first version logs the query string, with its tokens, or the body, with its passwords.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1 · interview question

    Interview: rate-limit.ts, a limit on login tries

    Write rateLimit({ limit, windowMs, key, now }) in src/middleware/rate-limit.ts. It returns a middleware that counts requests per key in fixed windows: a key's window starts at its first request and lasts windowMs milliseconds; the first limit requests in it go on with next(); every later one in the same window calls next with an AppError of status 429, code too_many_requests and the message Too many tries: wait <s> seconds, and sets the header Retry-After to <s>, the whole seconds left in the window, rounded up. A request after the window has ended starts a new window. Each key counts on its own. key(req) defaults to req.ip, now() to Date.now: always read the time through now().

    The Check uses limit 3 and a 60 s window: requests at 1 s, 6 s and 11 s go on, one at 21 s is refused with Retry-After 40, another key still goes on, and a request at 61 s goes on again. Then, on the whole app (login has 5 tries a minute per address and email), 5 wrong passwords for Pranav get 401, the 6th gets 429 with Retry-After and the code too_many_requests, and Neha can still log in.

    Nikhil will ask: these counts live in this process's memory. What goes wrong when Parda runs two servers, and what would you keep them in instead?

  2. Task 2

    security-headers.ts: the headers every answer carries

    Write securityHeaders({ production }) in src/middleware/security-headers.ts. It returns a middleware that sets these response headers and then calls next(): X-Content-Type-Options nosniff; X-Frame-Options DENY; Content-Security-Policy default-src 'none'; frame-ancestors 'none' (one value, a space after the semicolon); Referrer-Policy no-referrer; Cross-Origin-Resource-Policy same-origin. Only when production is true, it also sets Strict-Transport-Security max-age=31536000; includeSubDomains. It removes X-Powered-By.

    The Check calls it with production false and true, then sends GET /health and a path that does not exist to the whole app and reads the headers (app.ts already puts this middleware second, after the request log).

  3. Task 3

    request-log.ts: one log line per request

    Write requestLog({ write, now }) in src/middleware/request-log.ts. It returns a middleware that gives each request an id: the request's x-request-id header when it matches the given REQUEST_ID pattern, otherwise a new randomUUID(). It sends that id back in the x-request-id response header, then calls next(). When the answer has gone out (the response's finish event), it calls write once with an object { requestId, method, path, status, ms }, plus userId when res.locals.user is set: path is the URL without its query string, status the status that was sent, ms is now() at finish minus now() at the start, rounded. Never put the body, the cookies or the query string in the line.

    The Check mounts it on a small app with a clock it controls: GET /shows?q=ghar&token=secret-123 must log path /shows, status 200, ms 42 and a UUID that is also the x-request-id header; a request sent with x-request-id ticket-Q581-neha keeps that id and logs userId 7; a request with an id that does not match the pattern gets a new UUID. Then GET /health on the whole app must answer with an x-request-id.

What you will learn

  • Middleware factories: a function that takes options and returns a middleware, with state that lives as long as the app.
  • Rate limiting: fixed windows, 429 Too Many Requests and Retry-After, and an injected clock that makes it testable.
  • Security headers: what each one tells the browser, and why Strict-Transport-Security is for production only.
  • Request logs: the response's finish event, a request id, and what never goes in a log.
  • The take-home: Parda's reference codebase, run on your own machine.

More in Ready for production

Build "Hardening and the take-home" yourself

Parda sells tickets for Pune's own cinemas and stages. You build its booking API, one module a chapter, and take the whole codebase home. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Booking backend project are free. This one comes with the all labs plan.