Nate May
Nate May

Reputation: 4062

How to deploy a server via firebase cloud functions

I've followed a basic example to set up an express server to access a mongo instance hosted on google cloud platform. But when I run the command

firebase deploy --only functions

All my functions deploy except for the mongoServer function and I get the error:

functions: the following filters were specified but do not match any functions in the project: mongoServer

It's odd that the basic example

What am I doing wrong?

here is my functions/index.ts

import * as admin from 'firebase-admin';
import * as functions from 'firebase-functions';
import { mongoApp } from './mongo/mongo-server';
import { onSendNotification } from './notifications/send-notification';
import { onImageSave } from './resize-image/onImageSave';
admin.initializeApp();

export const onFileChange = functions.storage.object().onFinalize(onImageSave);
export const sendNotification = functions.https.onRequest(onSendNotification);
export const mongoServer = functions.https.onRequest(mongoApp); // this one fails

and here is my (stripped down) mongo-server.ts file:

import * as bodyParser from 'body-parser';
import * as express from 'express';
import * as mongoose from 'mongoose';
import { apiFoods } from './foods.api';
import { Mongo_URI, SECRET_KEY } from './mongo-config';

const path = require('path');

export const mongoApp = express();

mongoApp.set('port', (process.env.PORT || 8090));
mongoApp.use(bodyParser.json());
mongoApp.use(bodyParser.urlencoded({ extended: false }));

connect()
  .then((connection: mongoose.Connection) => {
    connection.db
      .on('disconnected', connect)
      .once('open', () => {

        console.log('Connected to MongoDB');
        apiFoods(mongoApp);

        mongoApp.listen(mongoApp.get('port'), () => {
          console.log('Listening on port ' + mongoApp.get('port'));
        });

      });
  }).catch(console.log)

function connect(): Promise<mongoose.Connection> {
  return mongoose
    .connect(Mongo_URI)
    .then((goose) => { return goose.connection })
    .catch(err => {
      console.log(err)
      return null;
    });
}

Upvotes: 0

Views: 311

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317497

You can't deploy an express app to Cloud Functions that manages its own connections. (The direct use of express is not at all part of the "basic example" as you cite.) All you can do with express is set up routes, and allow Cloud Functions to send requests to those routes. Cloud Functions manages all its own incoming connections directly.

See this example for something more basic that involves express.

Upvotes: 1

Related Questions