代码之家  ›  专栏  ›  技术社区  ›  Nate May

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

  •  0
  • Nate May  · 技术社区  · 7 年前

    basic example 设置一个Express服务器来访问托管在Google云平台上的Mongo实例。但当我执行命令时

    firebase deploy --only functions

    mongoServer

    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 回复  |  直到 7 年前
        1
  •  1
  •   Doug Stevenson    7 年前