Files
main.tsmain
What you'll learn
- Write middleware with createMiddleware(): code before await next() runs before the handler, code after it runs after
- Pass a value from middleware to the handler with c.set() and c.get(), typed through the app's Variables
- Scope middleware to a path with app.use('/cats/*', …) and end a request early by returning a response
Press Run tests to start the app. Its log appears here.Graded endpoints
The request id middleware runs for every route: the id sent in is echoed back, and x-status is set after next() from the response
The guard on /cats/* answers 401 itself and never calls next(); the outer middleware still tags the response
A key that does not match is refused the same way
The guard calls next() and the handler answers
The handler gets the request id from c.get(), set by the middleware that ran before it
The handler's own 404 passes back through the middleware, which reads its status
The guard is scoped to /cats/*, so the root does not ask for a key