Firebase功能:koa.js服务器如何部署

问题描述 投票:1回答:4

我已经有一个使用koa服务器编写的构建版本在MERN堆栈中编写的应用程序。我的主节点文件由node server.js命令运行以启动整个应用程序看起来像this

在每个教程中,我都看到我需要在编码开始时添加functions.https.request等(或至少假设这样做)。我怎样才能在firebase上托管我的应用程序,就像我在heroku上一样 - 整个服务器端?

node.js firebase google-cloud-functions koa firebase-hosting
4个回答
-1
投票

您无法在云功能上部署和运行任意节点应用程序。您必须使用产品定义的不同类型的触发器。

请参阅Cloud Functions for Firebase main page以查看列表。

  • Cloud Firestore触发器
  • 实时数据库触发器
  • Firebase身份验证触发器
  • Google Analytics for Firebase触发器
  • Crashlytics触发器
  • 云存储触发器
  • 云发布/子触发器
  • HTTP触发器

5
投票

实际上,可以使用firebase函数来托管Koa应用程序,我在一些沉重的Google搜索和分析之后弄明白了。

这是来自my project的一段代码,它现在托管了firebase函数:

const Koa = require('koa');
const app = new Koa();

// ... routes code here ...

const server = app.listen(config.port, () => {
  console.log(`HITMers-server is running on port ${config.port}`);
});

// This is just for running Koa and testing on local machine
module.exports = server;

exports.api = functions.https.onRequest(app.callback());

你可以看到the docstutorial video获取更多信息。

顺便说一下,这里是another example将Koa部署到now.sh版本2。


1
投票

您可以使用firebase托管运行快速应用程序,以通过firebase功能提供动态内容。但是,您目前无法使用Koa.js。 functions.https.onRequest要求您传递HTTP请求处理程序或从express()返回的快速应用程序。

以下是Firebase中有关从函数提供动态内容的相关文章。 https://firebase.google.com/docs/hosting/functions

以下是Firebase使用express的视频教程。 https://www.youtube.com/watch?v=LOeioOKUKI8


1
投票

你实际上可以完全跳过listen调用,并使用app.callback()。这听起来比听一个从未真正被击中的随机端口更有意义。

const functions = require('firebase-functions');
const app = new Koa();
... // set up your koa app however you normally would
app.use(router.routes());
module.exports.api = functions.https.onRequest(app.callback());
© www.soinside.com 2019 - 2024. All rights reserved.