The stylish Node.js middleware engine for AWS Lambda
Middy, the stylish Node.js middleware engine for AWS Lambda
Simple but powerfulA middleware engine makes your code more organised, removes duplication and uniforms the handling of non-functional concerns like authentication, authorization, validation, and serialization.Focus on what mattersBy pushing all the non-functional code to middlewares, you can be productive and focus on what matters the most: the business logic!Small coreMiddy comes with a very small core and an unobtrusive API to add the minimum amount of overhead to your code.Batteries includedMiddy comes with a large set of official middlewares and utilities that can be used out of the box to address the most common non-functional use cases.Blazing fastMiddy has been engineered to keep your lambda as fast as possible. The minimal core keeps your lambda size small and your cold starts under control. Add only what you need!Show me the code!The following abstract example illustrates the difference of style when using Middy:Without Middyhandler.jsexport const handler = (event, context) => {  // BOILERPLATE!  // E.g. decrypt environment variables with KMS  // deserialize the content of the event  // validate input, authentication, authorization    // REAL BUSINESS LOGIC  let response = doSomethingUsefulWith(event)    // MORE BOILERPLATE  // E.g.  // validate output  // serialize response  // handle errors  return response}Without Middy all your non-functional code is mixed up with the actual business logic in your handler.The code is hard to understand and maintain. What if you need to reuse some of the non-functional logic in another handler?With Middyhandler.jsimport middy from '@middy/core'const lambdaHandler = (event, context) => {  // REAL BUSINESS LOGIC  return doSomethingUsefulWith(event)}export const handler = middy(lambdaHandler)  .use(/* Your own behaviour in a reusable fashion */)  .use(/* logging and tracing */)  .use(/* input validation */)  .use(/* authentication */)  .use(/* error handling */)  .use(/* other behaviour */)Middy helps you to keep all the non-functional code outside from your handler function.The business logic in your handler remains pure and testable. All the non functional logic is isolated and reusable.Do you want to see some more realistic examples?API Gateway (HTTP) API Gateway (REST) API Gateway (WebSockets) Function URL S3 Object Response SQS Sponsors