[AWS] Lambda Middleware

You can find more information about Middy library and all available middleware here

Here is an example for how to use cors Middy middleware in Lambda:

import * as middy from 'middy'
import { cors } from 'middy/middlewares'

export const handler = middy(
  async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
     ...
  }
)

handler
  .use(cors())

 

Middlewares have two phases: before and after.

The before phase, happens before the handler is executed. In this code the response is not created yet, so you will have access only to the request.

The after phase, happens after the handler is executed. In this code you will have access to both the request and the response.

 

posted @ 2021-05-24 14:38  Zhentiw  阅读(56)  评论(0编辑  收藏  举报