# Middy — Middleware Engine for AWS Lambda in Node.js > A lightweight middleware framework for AWS Lambda that simplifies input parsing, validation, error handling, and other cross-cutting concerns. ## Install Save in your project root: # Middy — Middleware Engine for AWS Lambda in Node.js ## Quick Use ```javascript import middy from "@middy/core"; import jsonBodyParser from "@middy/http-json-body-parser"; import httpErrorHandler from "@middy/http-error-handler"; import validator from "@middy/validator"; const baseHandler = async (event) => { const { name } = event.body; return { statusCode: 200, body: JSON.stringify({ message: `Hello ${name}` }) }; }; export const handler = middy(baseHandler) .use(jsonBodyParser()) .use(validator({ eventSchema: mySchema })) .use(httpErrorHandler()); ``` ## Introduction Middy is a middleware engine for AWS Lambda functions in Node.js. It applies the onion model to Lambda handlers, letting developers compose reusable layers for parsing, validation, caching, error handling, and other cross-cutting concerns. This keeps business logic clean while moving boilerplate into composable middleware. ## What Middy Does - Wraps Lambda handlers with a before/after/onError middleware chain - Provides official middleware for common tasks: JSON parsing, validation, CORS, SSM parameter fetching, and more - Normalizes event and response objects across AWS event sources - Catches errors and formats them into structured HTTP responses - Supports ESM and CommonJS module formats ## Architecture Overview Middy wraps a Lambda handler and maintains an ordered list of middleware objects. Each middleware can define `before`, `after`, and `onError` hooks. On invocation, Middy runs `before` hooks in order, then the handler, then `after` hooks in reverse. If any step throws, `onError` hooks run in reverse. The chain fully supports async/await with minimal cold-start overhead. ## Self-Hosting & Configuration - Install via npm: `npm install @middy/core` plus desired middleware packages - No server to manage — it runs inside your Lambda function - Configure middleware by passing options to each middleware factory function - Compatible with AWS SAM, Serverless Framework, CDK, SST, and other deploy tools - Works with Node.js 18+ in both ESM and CommonJS ## Key Features - Onion-model middleware with before, after, and onError lifecycle hooks - Over 30 official middleware packages covering parsing, validation, caching, and secrets - TypeScript support with full type definitions - Minimal cold-start overhead with lazy-loading capabilities - Simple API for writing custom middleware ## Comparison with Similar Tools - **Lambda Powertools** — AWS-maintained utilities for logging, tracing, and metrics; Middy focuses on the middleware pattern - **Serverless Express** — Adapts Express apps to Lambda; Middy is Lambda-native without HTTP framework overhead - **DAZN Lambda Powertools** — Opinionated middleware set; Middy provides a flexible engine with a la carte middleware - **Hono** — Web framework that runs on Lambda; Middy wraps raw Lambda handlers without routing assumptions ## FAQ **Q: Does Middy add significant cold-start latency?** A: Middy's core is lightweight. Cold-start impact depends on which middleware you attach; each is a separate package to keep bundles small. **Q: Can I write my own middleware?** A: Yes. A middleware is an object with optional `before`, `after`, and `onError` async functions. No special registration is needed. **Q: Does Middy work with TypeScript?** A: Yes. Middy and all official middleware packages ship with TypeScript type definitions. ## Sources - https://github.com/middyjs/middy - https://middy.js.org --- Source: https://tokrepo.com/en/workflows/asset-c9146a67 Author: AI Open Source