Nest: Firebase Cloud Functions NestJS6

Created on 26 Mar 2019  路  6Comments  路  Source: nestjs/nest

In previous version of NestJS was opportunity to deploy firebase function like in issue #1281
But how to make it in new version with adapters especially with FastifyAdapter ?

question 馃檶

Most helpful comment

It should work equivalently as in #1281 with a one small difference - you no longer can pass expressInstance directly (you can wrap it in the new ExpressAdapter(expressInstance) though). Also, you can use HTTP adapter (.getInstance()). We're trying to provide serveless docs as soon as possible :)

All 6 comments

This is example with native fastify. Maybe it will be useful for clarifying :

/////////fast.ts

const http = require('http')
const Fastify = require('fastify')

let handleRequest = null

const serverFactory = (handler, opts) => {
  handleRequest = handler
  return http.createServer()
}
const fastify = Fastify({serverFactory})

fastify.get('/test', (req, reply) => {
  reply.send({ hello: 'world' })
})

module.exports = (req, res) => {
  req = Object.assign({ip: ''}, {...req});

  fastify.ready(err => {
    if (err) throw err;
    handleRequest(req, res);
  });
};

////// index.ts

const functions = require('firebase-functions');

const fast = require('./fast');

exports.app = functions.https.onRequest(fast);


It should work equivalently as in #1281 with a one small difference - you no longer can pass expressInstance directly (you can wrap it in the new ExpressAdapter(expressInstance) though). Also, you can use HTTP adapter (.getInstance()). We're trying to provide serveless docs as soon as possible :)

Hi @kamilmysliwiec any progress on the docs? Minimal working example would be really useful.
From which version of NestJS we need to wrap it and from which #1281 should work?

@kss943 confirmed, got it working as @kamilmysliwiec suggested, the appropriate doc is here.

Snippet code:
main.ts

import * as express from 'express';
import * as functions from 'firebase-functions';
import { AppModule } from './app.module';
import { Express } from 'express';
import { ExpressAdapter } from '@nestjs/platform-express';
import { NestFactory } from '@nestjs/core';

const server: Express = express();

// Create and init Nest server based on Express instance.
const createNestServer = async (expressInstance: Express) => {
  const app = await NestFactory.create(
    AppModule,
    new ExpressAdapter(expressInstance)
  );
  app.init(); // Use when deploying & testing with Firebase Cloud Functions.
  // app.listen(4048); // Use when testing locally without Firebase Cloud Functions solely on NestJS.
};

createNestServer(server);
exports.angularUniversalFunction = functions.https.onRequest(server); // Export Firebase Cloud Functions to work on

app.module.ts (server AppModule, client is different)

import { AngularUniversalModule, applyDomino } from '@nestjs/ng-universal';
import { join } from 'path';
import { Module } from '@nestjs/common';

// Get working directory of client bundle.

const BROWSER_DIR = join(process.cwd(), '..', 'ditectrev-browser'); // Use when deploying & testing with Firebase Cloud Functions.
applyDomino(global, join(BROWSER_DIR, 'index.html'));

@Module({
  imports: [
    AngularUniversalModule.forRoot({
      bundle: require('./../dist/apps/ditectrev-server/main'), // Bundle is created dynamically during build process.
      liveReload: true,
      viewsPath: BROWSER_DIR
    })
  ]
})
export class AppModule {}

On top of this of course you need to bundle this using webpack, in my case I had to remember about libraryTarget: 'umd' and webpack-node-externals. The last one only if you have some 3rd party libraries which access DOM or window directly.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings