Skip to content

Instantly share code, notes, and snippets.

@adrai
Last active November 13, 2023 08:40
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save adrai/ca6237c013f5a746cdfd043bb6e02185 to your computer and use it in GitHub Desktop.
Save adrai/ca6237c013f5a746cdfd043bb6e02185 to your computer and use it in GitHub Desktop.
aws-serverless-fastify
const fastify = require('fastify');
function init(serverFactory) {
const app = fastify({ serverFactory });
app.get('/', (request, reply) => reply.send({ hello: 'world' }));
return app;
}
if (require.main === module) {
// called directly i.e. "node app"
init().listen(3000, (err) => {
if (err) console.error(err);
console.log('server listening on 3000');
});
} else {
// required as a module => executed on aws lambda
module.exports = init;
}
const awsServerlessExpress = require('aws-serverless-express');
const fastify = require('./app');
let server;
const serverFactory = (handler) => {
server = awsServerlessExpress.createServer(handler);
return server;
}
const app = fastify(serverFactory);
exports.handler = (event, context, callback) => {
context.callbackWaitsForEmptyEventLoop = false;
app.ready((e) => {
if (e) return console.error(e.stack || e);
awsServerlessExpress.proxy(server, event, context, 'CALLBACK', callback);
});
};
@adrai
Copy link
Author

adrai commented Jul 5, 2017

@adrai
Copy link
Author

adrai commented Apr 15, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment