• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

290
Views
Export Fastify server as firebase cloud functions

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.

almost 3 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error