I want to export Fastify server to firebase cloud functions (so I can run them through emulators), Following is my code:
import { ValidationPipe } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import {
FastifyAdapter,
NestFastifyApplication,
} from '@nestjs/platform-fastify';
import { AppModule } from './app.module';
import * as functions from 'firebase-functions';
const http = require('http')
const Fastify = require('fastify')
let handleRequest = null
const serverFactory = (handler, opts) => {
handleRequest = handler
return http.createServer()
}
const fastify = Fastify({serverFactory,});
export const createFastifyServer = async (fastifyInstance) => {
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
new FastifyAdapter()
);
app.useGlobalPipes(new ValidationPipe());
await app.init()
}
createFastifyServer(fastify)
.then(() => console.log('Nest Ready'))
.catch((err) => console.error('Nest broken', err));
exports.app = functions.https.onRequest((req, res) => {
fastify.ready((err) => {
if (err) throw err
handleRequest(req, res)
})
})
And when I try to access the exported functions from the path, e.g. http://localhost:port/firebase project/regionapp/app
after running the local emulators then I get route not found exception e.g. {"message":"Route GET:/ not found","error":"Not Found","statusCode":404}
.
I'm aware of using get/post with fastify directly e.g.
fastify.get('/', (req, reply) => {
console.log('Hello World')
reply.send({ hello: 'world' })
})
but want to export my already created express server app (created through NestFastifyApplication) as fastify, in express we simply do export const api: functions.HttpsFunction = firebase.https.onRequest(express_server);
but what is the alternate for fastify? Thanks.