Last active
November 13, 2023 08:40
-
-
Save adrai/ca6237c013f5a746cdfd043bb6e02185 to your computer and use it in GitHub Desktop.
aws-serverless-fastify
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
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; | |
} |
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
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); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
example with claudia.js https://github.com/claudiajs/example-projects/tree/master/fastify-app-lambda