Open veedeo opened 6 years ago
any fix for this?
I don't remember what was the problem exactly. but this is correct entry point for latest version:
import Fastify from 'fastify';
import awsServerlessExpress from 'aws-serverless-express';
import api from './api';
const app = Fastify({
serverFactory(handlerMethod) {
return awsServerlessExpress.createServer(handlerMethod);
},
});
api(app);
app.ready(() => console.log('Ready'));
export function handler(event, context) {
return awsServerlessExpress.proxy(app.server, event, context);
}
@veedeo your guidance has been mighty helpful! Thank you 👍
I recommend calling awsServerlessExpress.proxy inside app.ready callback, as in my case, proxy was calling fastify routes prematurely
Can you share the code?
in my api, I was creating a database connection during fastify initialization which was delaying fastify readiness before awsServerlessExpress.proxy call and fastify's route hanler was not being called. The following change will ensure fastify is ready
import Fastify from 'fastify';
import awsServerlessExpress from 'aws-serverless-express';
import api from './api';
const app = Fastify({
serverFactory(handlerMethod) {
return awsServerlessExpress.createServer(handlerMethod);
},
});
api(app);
export function handler(event, context) {
app.ready((err) => {
console.log(err?err:'Ready');
return awsServerlessExpress.proxy(app.server, event, context);
});
}
https://github.com/adrai/fastify/blob/master/docs/Serverless.md PR is in progress: https://github.com/fastify/fastify/pull/1564 claudia example is updated: https://github.com/claudiajs/example-projects/tree/master/fastify-app-lambda