example-projects icon indicating copy to clipboard operation
example-projects copied to clipboard

claudia update fails with latest fastify version

Open veedeo opened this issue 6 years ago • 8 comments

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)

veedeo avatar May 29 '18 17:05 veedeo

any fix for this?

kwalski avatar Oct 16 '18 08:10 kwalski

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 avatar Oct 16 '18 08:10 veedeo

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

kwalski avatar Oct 16 '18 11:10 kwalski

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

kwalski avatar Oct 16 '18 23:10 kwalski

Can you share the code?

veedeo avatar Oct 17 '18 04:10 veedeo

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);
  });
}

kwalski avatar Oct 17 '18 04:10 kwalski

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 avatar Apr 02 '19 14:04 adrai

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

adrai avatar Apr 15 '19 09:04 adrai