Created
August 14, 2019 13:33
-
-
Save greg-nagy/c2fd82e221b57639c4e584ea68db429f to your computer and use it in GitHub Desktop.
nestjs 5.5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Handler, Context } from 'aws-lambda'; | |
import { Server } from 'http'; | |
import { createServer, proxy } from 'aws-serverless-express'; | |
import { eventContext } from 'aws-serverless-express/middleware'; | |
import { NestFactory } from '@nestjs/core'; | |
import { AppModule } from './app.module'; | |
// NOTE: If you get ERR_CONTENT_DECODING_FAILED in your browser, this is likely | |
// due to a compressed response (e.g. gzip) which has not been handled correctly | |
// by aws-serverless-express and/or API Gateway. Add the necessary MIME types to | |
// binaryMimeTypes below | |
const binaryMimeTypes: string[] = []; | |
let cachedServer: Server; | |
process.on('unhandledRejection', (reason) => { | |
console.error(reason); | |
}); | |
process.on('uncaughtException', (reason) => { | |
console.error(reason); | |
}); | |
async function bootstrapServer(): Promise<Server> { | |
if (!cachedServer) { | |
try { | |
const expressApp = require('express')(); | |
const nestApp = await NestFactory.create(AppModule, expressApp); | |
nestApp.use(eventContext()); | |
await nestApp.init(); | |
cachedServer = createServer(expressApp, undefined, binaryMimeTypes); | |
} | |
catch (error) { | |
return Promise.reject(error); | |
} | |
} | |
return Promise.resolve(cachedServer); | |
} | |
export const handler: Handler = async (event: any, context: Context) => { | |
cachedServer = await bootstrapServer(); | |
return proxy(cachedServer, event, context, 'PROMISE').promise; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment