Setup Nest and Firebase Functions
What is Nest?
Nest (NestJS) is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with and fully supports TypeScript (yet still enables developers to code in pure JavaScript) and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Reactive Programming)
What is Firebase Functions?
Cloud Functions for Firebase is a serverless framework that lets you automatically run backend code in response to events triggered by Firebase features and HTTPS requests. Your JavaScript or TypeScript code is stored in Google’s cloud and runs in a managed environment. There’s no need to manage and scale your own servers.
How to mix Nest and Firebase Functions in a new project
Step 1: Create a nest project
Install nest cli and create a new project, in the steps choose your favorite package manager, I use npm
for the rest of this article.
npm i -g @nestjs/cli
nest new my-awesome-project
then run
cd my-awesome-project
npm run start:dev
To make sure if everything is working till here, open a browser and open this URL http://localhost:3000
then you should see “Hello World!” in your browser.
Your project folder structure will look like this
Step 2: Add Firebase Functions
install firebase tools and init the functions and follow the steps.
npm i -g firebase-tools
firebase init functions
Step 3: Get rid of functions folder
By running above command a new folder called functions
will be added to the project.
Delete the functions
folder
rm -rf functions
Step 4: Install dependencies
Now you need to install some packages by running
npm i firebase-admin firebase-functions express @nestjs/platform-express
and
npm i firebase-functions-test npm-run-all --save-dev
Step 5: Update firebase.json file
Find firebase.json
file and update it like below
{
"functions": {
"source": "."
}
}
Step 6: Update package.json
Open package.json
and add below lines to it
"main": "dist/main.js",
"engines": {
"node": "12"
},
update scripts
section to below
"scripts": {
"prenest:build": "rimraf dist",
"start": "npm-run-all --parallel nest:start:dev fb:serve",
"deploy": "npm-run-all nest:build fb:deploy",
"nest:build": "nest build",
"nest:start": "nest start",
"nest:start:dev": "nest start --watch",
"nest:start:debug": "nest start --debug --watch",
"nest:start:prod": "node dist/main",
"fb:build": "tsc",
"fb:serve": "firebase serve --only functions",
"fb:shell": "firebase functions:shell",
"fb:start": "npm run fb:shell",
"fb:deploy": "firebase deploy --only functions",
"fb:logs": "firebase functions:log",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
},
Essentially what we are doing here is to rename existing nest commands to nest:xxx
and adding some fb:xxx
command, and running both nest:start:dev
and fb:serve
in parallel by using npm-run-all
Step 7: Update main.ts
Open main.ts
file from src
folder, and update it to below
import {NestFactory} from '@nestjs/core';
import {ExpressAdapter, NestExpressApplication} from '@nestjs/platform-express';
import * as express from 'express';
import * as functions from 'firebase-functions';import {AppModule} from './app.module';const server: express.Express = express();export const createNestServer = async (expressInstance: express.Express) => {
const adapter = new ExpressAdapter(expressInstance);
const app = await NestFactory.create<NestExpressApplication>(
AppModule, adapter, {},
); app.enableCors(); return app.init();
};createNestServer(server)
.then(v => console.log('Nest Ready'))
.catch(err => console.error('Nest broken', err));export const api: functions.HttpsFunction = functions.https.onRequest(server);
Step 8: Run the project
Run the project by
npm start
Your project should be running and you will see something like below
To check if everything is working till now, find your URL as I highlighted in above picture and paste it to a new browser tab, you should see “Hello World!” again.
Step 9: Confirm Watch mode is working
While your project is running, open app.service.ts
in src
folder, and change return 'Hello World!';
to below and save.
return 'Together we will beat COVID-19';
Now refresh your browser, and you should see the changes without rebuilding the project manually.
Step 10: deploy to firebase
Run npm run deploy
and you will see the output as below
Step 11: Confirm the deployment works
Now open your project in firebase console and navigate to Functions menu, you should see the api
function deployed as below
Now copy the URL and paste to a new browser. You should see “Together we will beat COVID-19” and it means you did an awesome job.
Final Project Folder Structure
Full Code
Full code is available at https://github.com/RezaRahmati/nest-firebase-function-seed