Skip to content

Instantly share code, notes, and snippets.

@jgcmarins
Created June 13, 2020 22:03
Show Gist options
  • Select an option

  • Save jgcmarins/47c0577576f1078fbc4bcdbdf94a4dbd to your computer and use it in GitHub Desktop.

Select an option

Save jgcmarins/47c0577576f1078fbc4bcdbdf94a4dbd to your computer and use it in GitHub Desktop.
import { createServer, proxy } from 'aws-serverless-express';
import connectDatabase from './database';
import app from './app';
export const handler = (event, ctx) => {
// This will re-use `cachedMongoConn` between function calls.
// See https://www.mongodb.com/blog/post/serverless-development-with-nodejs-aws-lambda-mongodb-atlas
// See https://docs.atlas.mongodb.com/best-practices-connecting-to-aws-lambda/
// https://mongoosejs.com/docs/lambda.html
ctx.callbackWaitsForEmptyEventLoop = false;
console.log('Connecting to database...');
connectDatabase()
.then(() => proxy(createServer(app.callback()), event, ctx))
.catch(error => {
console.error('Could not connect to database', { error });
throw error;
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment