Application Load Balancer
Same as API Gateway (REST)
caution
This page is a work in progress. If you want to help us to make this page better, please consider contributing on GitHub.
AWS Documentation
Example
import middy from '@middy/core'
import httpRouterHandler from '@middy/http-router'
import errorLoggerMiddleware from '@middy/error-logger'
import inputOutputLoggerMiddleware from '@middy/input-output-logger'
import httpContentNegotiationMiddleware from '@middy/http-content-negotiation'
import httpContentEncodingMiddleware from '@middy/http-content-encoding'
import httpCorsMiddleware from '@middy/http-cors'
import httpErrorHandlerMiddleware from '@middy/http-error-handler'
import httpEventNormalizerMiddleware from '@middy/http-event-normalizer'
import httpHeaderNormalizerMiddleware from '@middy/http-header-normalizer'
import httpJsonBodyParserMiddleware from '@middy/http-json-body-parser'
import httpMultipartBodyParserMiddleware from '@middy/http-multipart-body-parser'
import httpPartialResponseMiddleware from '@middy/http-partial-response'
import httpResponseSerializerMiddleware from '@middy/http-response-serializer'
import httpSecurityHeadersMiddleware from '@middy/http-security-headers'
import httpUrlencodeBodyParserMiddleware from '@middy/http-urlencode-body-parser'
import httpUrlencodePathParametersParserMiddleware from '@middy/http-urlencode-path-parser'
import warmupMiddleware from 'warmup'
import { handler as getHandler } from './handlers/get-user.js'
import { handler as postHandler } from './handlers/get-user.js'
const routes = [
{
method: 'GET',
path: '/user/{id}',
handler: getHandler
},
{
method: 'POST',
path: '/user',
handler: postHandler
}
]
export const handler = middy({
timeoutEarlyResponse: () => {
return {
statusCode: 408
}
}
})
.use(warmupMiddleware())
.use(httpEventNormalizerMiddleware())
.use(httpHeaderNormalizerMiddleware())
.use(
httpContentNegotiationMiddleware({
availableLanguages: ['en-CA', 'fr-CA'],
availableMediaTypes: ['application/json']
})
)
.use(httpUrlencodePathParametersParserMiddleware())
// Start oneOf
.use(httpUrlencodeBodyParserMiddleware())
.use(httpJsonBodyParserMiddleware())
.use(httpMultipartBodyParserMiddleware())
// End oneOf
.use(httpSecurityHeadersMiddleware())
.use(httpCorsMiddleware())
.use(httpContentEncodingMiddleware())
.use(
httpResponseSerializerMiddleware({
serializers: [
{
regex: /^application\/json$/,
serializer: ({ body }) => JSON.stringify(body)
}
],
default: 'application/json'
})
)
.use(httpPartialResponseMiddleware())
.use(httpErrorHandlerMiddleware())
.handler(httpRouterHandler(routes))