13th June 2025 at 1:22am
-
Entry Point:
Router.handle()
- Takes HTTP/HTTP2 request and response objects
- Wraps
handleRequest()
in error handling
-
Main Request Processing:
Router.handleRequest()
- Emit middleware event
- Apply Helmet middleware
- Create Streamer instance
- Call
handleStreamer()
-
Stream Processing:
Router.handleStreamer()
- Emits streamer event
- Finds matching route using
findRoute()
- Performs security checks (CSRF protection)
- Processes request body based on
bodyFormat
- Creates
ServerRequest
instance - Calls
handleRoute()
-
Route Matching:
Router.findRoute()
andfindRouteRecursive()
- Recursively matches URL path against defined routes
- Handles nested routes
- Matches HTTP methods
- Returns array of matched route segments
-
Route Handling:
Router.handleRoute()
- Executes handlers for matched routes in sequence
- Emits handle event
- Falls back to 404 if no handler sends response
Body Format Processing
The router supports multiple body formats:
stream
: Raw streaming datastring
: UTF-8 stringjson
: Parsed JSON databuffer
: Raw bufferwww-form-urlencoded
: Parsed form data as objectwww-form-urlencoded-urlsearchparams
: Form data as URLSearchParamsignore
: Ignores request body (default for GET/HEAD)
Security Features
- Built-in Helmet middleware for security headers
- CSRF protection via
x-requested-with
header checks - JSON security parsing (protects against prototype pollution)
- Method matching validation
- Path validation
The routes are hierarchical, allowing for nested routes with inherited properties and progressive URL path matching.
It is important that the entire request path be awaited and eventually resolve or reject. A promise should never be left hanging. The Router class takes care of making sure every request has finished with some response, but if the promise never resolves or rejects, the request will eventually time out.