All Challenges
2 PointsBasic
00:00

MEAN Stack: Express.js Composable Middleware Chain & RFC 7807 Error Boundary

Build an Express middleware pipeline runner supporting async NextFunction, JWT bearer parsing, NoSQL sanitization, and RFC 7807 error responses.

### Problem Overview In the MEAN stack backend, Express.js uses an onion-style middleware pipeline. Senior backend interviews frequently test your ability to implement an asynchronous middleware runner that executes sequential handlers, supports early exits, extracts and validates JWT Bearer headers, sanitizes NoSQL injection keys, and formats uncaught exceptions into standard RFC 7807 Problem Details JSON payloads. ### Functional Requirements 1. **`use(middleware: MiddlewareFn): this`**: - Registers a middleware function with signature `(req, res, next) => Promise<void> | void`. 2. **`execute(req: RequestContext): Promise<ResponseContext>`**: - Executes the registered middleware pipeline in strict sequential order. - Calling `next()` proceeds to the next middleware. - Calling `next(error)` or throwing an error routes to the RFC 7807 error handler. - Calling `res.send()` or `res.json()` ends execution immediately (short-circuit). 3. **Built-in Security Middleware (`noSqlSanitizer` & `rfc7807ErrorHandler`):** - **NoSQL Sanitizer**: Recursively strips out object keys starting with `$` (e.g., `{ "$gt": "" }`) from `req.body` and `req.query`. - **RFC 7807 Error Handler**: Returns standard error format: `{ type, title, status, detail, instance, timestamp }`.
AI Review Rubric Dimensions (1–10 Points):
  • Async next() chaining
  • Early exit handling
  • RFC 7807 error formatting
  • NoSQL key sanitization
solution.tsTYPESCRIPT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
36 lines•866 characters•
AI Review Ready