如何通过firebase云功能部署服务器

时间:2018-06-19 02:56:45

标签: typescript firebase express mongoose google-cloud-functions

我按照basic example设置了快速服务器来访问托管在Google云平台上的mongo实例。但是当我运行命令时

firebase deploy --only functions

除了mongoServer函数之外我的所有函数都部署了,我收到错误:

  

函数:指定了以下过滤器但不匹配任何过滤器   项目中的函数:mongoServer

basic example

很奇怪

我做错了什么?

这是我的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

这是我的(精简版)mongo-server.ts文件:

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;
    });
}

1 个答案:

答案 0 :(得分:1)

您无法将快速应用部署到管理其自身连接的云功能。 (直接使用快递不是&#34;基本示例&#34;正如您所引用的那样。)您可以使用快速设置路由,并允许云功能向这些路由发送请求。云功能直接管理所有自己的传入连接。

请参阅this example了解更多涉及快递的基本内容。