【问题标题】:How to deploy a server via firebase cloud functions如何通过 Firebase 云功能部署服务器
【发布时间】:2018-06-19 02:56:45
【问题描述】:

我已经关注basic example 设置了一个快速服务器来访问托管在谷歌云平台上的 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;
    });
}

【问题讨论】:

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


    【解决方案1】:

    您无法将快速应用部署到管理自己的连接的 Cloud Functions。 (正如您引用的那样,直接使用 express 根本不是“基本示例”的一部分。)您可以使用 express 设置路由,并允许 Cloud Functions 向这些路由发送请求。 Cloud Functions 直接管理自己的所有传入连接。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-18
      • 2020-11-24
      • 2021-12-01
      • 2021-11-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多