简体   繁体   English

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

[英]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. 我已经按照一个基本示例设置了一个快速服务器,以访问托管在Google云平台上的mongo实例。 But when I run the command 但是当我运行命令时

firebase deploy --only functions

All my functions deploy except for the mongoServer function and I get the error: mongoServer函数外,所有函数mongoServer部署,但出现错误:

functions: the following filters were specified but do not match any functions in the project: mongoServer 功能:指定了以下过滤器,但与项目中的任何功能都不匹配:mongoServer

It's odd that the basic example 基本示例很奇怪

What am I doing wrong? 我究竟做错了什么?

here is my functions/index.ts 这是我的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: 这是我的(精简的) 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;
    });
}

You can't deploy an express app to Cloud Functions that manages its own connections. 您无法将快速应用程序部署到管理自己的连接的Cloud Functions中。 (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. (正如您引用的那样,直接使用express并不是“基本示例”的全部。)Express所能做的就是设置路由,并允许Cloud Functions将请求发送到这些路由。 Cloud Functions manages all its own incoming connections directly. Cloud Functions直接管理其自己的所有传入连接。

See this example for something more basic that involves express. 有关更基本的涉及表达的内容,请参见此示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM