claudiajs / example-projects

Simple example projects that show how to use ClaudiaJs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

claudia update fails with latest fastify version

veedeo opened this issue · comments

commented
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)

any fix for this?

commented

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

commented

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