Optionaloptions: RestRouterOptionsProtectedcontextProtected ReadonlyerrorProtected ReadonlyisProtected 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 ReadonlyrouteRegisters 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.
Override Behaviors:
import { Router } from '@aws-lambda-powertools/event-handler/experimental-rest';
const todosRouter = new Router();
todosRouter.get('/todos', async () => {
// List API
});
todosRouter.get('/todos/{todoId}', async () => {
// Get API
});
const app = new Router();
app.includeRouter(todosRouter);
export const handler = async (event: unknown, context: Context) => {
return app.resolve(event, context);
};
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
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);
Whether the router is running in development mode.