ProtectedcontextProtected ReadonlyerrorProtected ReadonlyisWhether the router is running in development mode.
Protected ReadonlyloggerA logger instance to be used for logging debug, warning, and error messages.
When no logger is provided, we'll only log warnings and errors using the global console object.
Protected ReadonlymiddlewareProtected Optional ReadonlyprefixThe base prefix to be used for all routes registered using this Router.
Protected ReadonlyrouteReadonlysharedA shared store that persists across requests for the lifetime of the Router instance.
Registers a custom error handler for specific error types.
The error constructor(s) to handle
The error handler that returns an error response
Registers a custom error handler for specific error types.
The error constructor(s) to handle
ProtectedhandleHandles errors by finding a registered error handler or falling back to a default handler.
The error to handle
Error resolve options including request context and scope
A Response object with appropriate status code and error details
Merges the routes, context and middleware from the passed router instance into this router instance.
Returns this with a widened type that includes the included router's store types,
allowing calls to be chained in a fluent style. When chaining multiple includeRouter
calls, the resulting type is the intersection of all store environments — giving
handlers type-safe access to every sub-router's store keys.
Override Behaviors:
The current router instance, typed as Router<MergeEnv<[TEnv, TOther]>>
import { Router } from '@aws-lambda-powertools/event-handler/http';
type AuthEnv = { store: { request: { userId: string } } };
type FeatureEnv = { store: { shared: { maxResults: number } } };
const authRouter = new Router<AuthEnv>();
const featureRouter = new Router<FeatureEnv>();
// Chained calls merge store types automatically
const app = new Router()
.includeRouter(authRouter)
.includeRouter(featureRouter);
// Handlers on `app` can now access both `userId` and `maxResults`
app.get('/profile', (reqCtx) => {
const userId = reqCtx.get('userId');
const maxResults = reqCtx.shared.get('maxResults');
return { userId, maxResults };
});
Registers a custom handler for 405 Method Not Allowed errors.
The error handler that returns an error response
Registers a custom handler for 405 Method Not Allowed errors.
Registers a custom handler for 404 Not Found errors.
The error handler that returns an error response
Registers a custom handler for 404 Not Found errors.
Resolves an API Gateway event by routing it to the appropriate handler and converting the result to an API Gateway proxy result. Handles errors using registered error handlers or falls back to default error handling (500 Internal Server Error).
The Lambda event to resolve
The Lambda context
Optionaloptions: ResolveOptionsOptional resolve options for scope binding
An API Gateway proxy result (V1 or V2 format depending on event version)
Resolves an API Gateway event by routing it to the appropriate handler and converting the result to an API Gateway proxy result. Handles errors using registered error handlers or falls back to default error handling (500 Internal Server Error).
The Lambda event to resolve
The Lambda context
Optionaloptions: ResolveOptionsOptional resolve options for scope binding
An API Gateway proxy result (V1 or V2 format depending on event version)
Resolves an API Gateway event by routing it to the appropriate handler and converting the result to an API Gateway proxy result. Handles errors using registered error handlers or falls back to default error handling (500 Internal Server Error).
The Lambda event to resolve
The Lambda context
Optionaloptions: ResolveOptionsOptional resolve options for scope binding
An API Gateway proxy result (V1 or V2 format depending on event version)
Resolves an API Gateway event by routing it to the appropriate handler and converting the result to an API Gateway proxy result. Handles errors using registered error handlers or falls back to default error handling (500 Internal Server Error).
The Lambda event to resolve
The Lambda context
Optionaloptions: ResolveOptionsOptional resolve options for scope binding
An API Gateway proxy result (V1 or V2 format depending on event version)
Resolves an API Gateway event by routing it to the appropriate handler and streaming the response directly to the provided response stream. Used for Lambda response streaming.
The Lambda event to resolve
The Lambda context
Stream resolve options including the response stream
Registers a global middleware function that will be executed for all routes.
Global middleware executes before route-specific middleware and follows the onion model
where middleware executes in registration order before next() and in reverse order after next().
The middleware function to register globally
const authMiddleware: Middleware = async ({ params, reqCtx, next }) => {
// Authentication logic
if (!isAuthenticated(reqCtx.req)) {
return new Response('Unauthorized', { status: 401 });
}
await next();
// Cleanup or logging after request completion
console.log('Request completed');
};
router.use(authMiddleware);
Deprecated
This property is deprecated and will be removed in a future major version, please use
requestContext.sharedinstead.