The first setup modifies the functions configuration to use the Nest **/dist**
output, as opposed to the default functions directory. This option is ideal if you have an existing Nest app.
nest generate app server
Add functions, then delete the automatically generated directory.
npm i -g firebase-tools
firebase init functions
rm -rf functions
Now update the firebase config to point to the nest app.
"functions": {
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run build"
],
"source": "server" // <-- here
}
}
firebase.json
cd server
npm i firebase-functions firebase-admin express @nestjs/platform-express
Add the following lines to your package.json
.
{
// ...
"main": "dist/index.js",
"engines": {
"node": "8"
}
}
Create a new file named **src/index.ts**
that creates an exress app and wraps it with Nest.
import { NestFactory } from '@nestjs/core';
import { ExpressAdapter } from '@nestjs/platform-express';
import { AppModule } from './app.module';
import * as express from 'express';
import * as functions from 'firebase-functions';
const server = express();
export const createNestServer = async (expressInstance) => {
const app = await NestFactory.create(
AppModule,
new ExpressAdapter(expressInstance),
);
return app.init();
};
createNestServer(server)
.then(v => console.log('Nest Ready'))
.catch(err => console.error('Nest broken', err));
export const api = functions.https.onRequest(server);
npm run build
firebase serve --only functions
firebase deploy --only functions
In this setup, we perform a fresh install of Nest in the Functions source code. This is a good approach if you have existing background functions, but want to wrap Nest as an HTTP function.
Initialize Cloud Functions making sure to choose the TypeScript option.
npm i -g firebase-tools
firebase init functions
Install Nest. If you have an existing project, also copy over the other dependencies from your Package.json.
cd functions
npm i --save @nestjs/core @nestjs/common rxjs reflect-metadata express @nestjs/platform-express
One of the best features in Nest is the CLI. Let’s add support by creating the following file:
functions/nest-cli.json
{
"language": "ts",
"collection": "@nestjs/schematics",
"sourceRoot": "src"
}
Nest uses TypeScript features that are not enabled in Cloud Functions by default. Let’s change that.
functions/tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"noImplicitReturns": true,
"noUnusedLocals": true,
"outDir": "lib",
"sourceMap": true,
"strict": false,
"target": "es2017",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"declaration": true,
"removeComments": true,
"baseUrl": "./",
"incremental": true,
"esModuleInterop": true
},
"compileOnSave": true,
"include": [
"src"
]
}
nest generate module app --flat
nest generate controller egg
The file structure of Nest + Cloud Functions
Lastly, create the Nest server and wrap it in a Cloud Function. It’s purpose is to export an ExpressJS app and expose a function that wraps it with Nest.
functions/src/index.ts
import * as functions from 'firebase-functions';
import { NestFactory } from '@nestjs/core';
import { ExpressAdapter } from '@nestjs/platform-express';
import { AppModule } from './app.module';
import express from 'express';
const server = express();
const createNestServer = async (expressInstance) => {
const app = await NestFactory.create(
AppModule,
new ExpressAdapter(expressInstance),
);
return app.init();
};
createNestServer(server)
.then(v => console.log('Nest Ready'))
.catch(err => console.error('Nest broken', err));
export const api = functions.https.onRequest(server);
cd functions
npm run serve
firebase deploy --only functions
The should give you a URL that looks like [**http://localhost:5000/YOUR-PROJECT/REGION/api/eggs**](http://localhost:5000/YOUR-PROJECT/REGION/api/eggs "**http://localhost:5000/YOUR-PROJECT/REGION/api/eggs**")
where you can start testing the API. Happy Nesting 🥚🥚🥚!
Recommended Reading
☞ Building a Node.js App with TypeScript Tutorial
☞ How to Role-Based Access Control in a Node.js application
☞ How to Build Node.js Modules with Rust
☞ Node.js Tutorial for Beginners in 2020
☞ Introduce to Mutation and Database Access in GraphQL
#node-js #javascript