claudiajs / example-projects

Simple example projects that show how to use ClaudiaJs
650 stars 235 forks source link

claudia update fails with latest fastify version #28

Open veedeo opened 6 years ago

veedeo commented 6 years ago
TypeError: "listener" argument must be a function
    at _addListener (events.js:239:11)
    at Server.addListener (events.js:297:10)
    at new Server (_http_server.js:269:10)
    at Object.createServer (http.js:34:10)
kwalski commented 6 years ago

any fix for this?

veedeo commented 6 years ago

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);
}
kwalski commented 6 years ago

@veedeo your guidance has been mighty helpful! Thank you 👍

kwalski commented 6 years ago

I recommend calling awsServerlessExpress.proxy inside app.ready callback, as in my case, proxy was calling fastify routes prematurely

veedeo commented 6 years ago

Can you share the code?

kwalski commented 6 years ago

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);
  });
}
adrai commented 5 years ago

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

adrai commented 5 years ago

https://www.fastify.io/docs/master/Serverless/