Google Cloud Functions:如何共享源代码? [英] Google Cloud Functions: How do you share source code?

查看:22
本文介绍了Google Cloud Functions:如何共享源代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个节点服务器和多个控制器,它们在该目录中执行数据库操作和帮助程序(例如电子邮件).

I have a Node server and multiple controllers that perform DB operations and helpers (For e-mail, for example) within that directory.

我想在我的函数中使用该目录中的源代码.假设如下目录结构:

I'd like to use source from that directory within my functions. Assuming the following directory structure:

src/
  server/
    /app/controllers/email_helper.js
  fns/
    send-confirm/

在 send-confirm 函数中使用 email_helper 的最佳方式是什么?

What's the best way to use email_helper within the send-confirm function?

我试过了:

  • 象征性地链接服务器"目录
  • 将本地仓库添加到 send-confirm/package.json

以上都不行.

推荐答案

原则上,您的 Cloud Functions 可以使用任何其他 Node.js 模块,就像任何标准 Node.js 服务器一样.但是,由于 Cloud Functions 需要在云中构建您的模块,它需要能够从云中找到这些依赖模块.这就是问题所在.

In principle, your Cloud Functions can use any other Node.js module, the same way any standard Node.js server would. However, since Cloud Functions needs to build your module in the cloud, it needs to be able to locate those dependency modules from the cloud. This is where the issue lies.

Cloud Functions 可以从以下任一位置加载模块:

Cloud Functions can load modules from any one of these places:

  • 任何公共 npm 存储库.
  • 任何网络可见的 URL.
  • firebase init 为您生成并上传到 firebase deployfunctions/ 目录中的任何位置.
  • Any public npm repository.
  • Any web-visible URL.
  • Anywhere in the functions/ directory that firebase init generates for you, and which gets uploaded on firebase deploy.

在您的情况下,从 functions/package.json 的角度来看,../server/ 目录不属于任何这些类别,因此Cloud Functions 无法使用您的模块.不幸的是,firebase deploy 不遵循符号链接,这就是该解决方案不起作用的原因.

In your case, from the perspective of functions/package.json, the ../server/ directory doesn't fall under any of those categories, and so Cloud Functions can't use your module. Unfortunately, firebase deploy doesn't follow symlinks, which is why that solution doesn't work.

我看到了两个可能的即时修复:

I see two possible immediate fixes:

  • 将您的 server/ 目录移动到 functions/ 下.我意识到这不是最漂亮的目录布局,但它是黑客攻击时最简单的修复.在 functions/package.json 中,您可以对 ./server 进行本地依赖.
  • 在某处的 URL 后面公开您的代码.例如,您可以打包一个 .tar 并将其放在 Google Drive 或 Firebase Cloud Storage 上.或者,您可以使用公共 git 存储库.
  • Move your server/ directory to be under functions/. I realize this isn't the prettiest directory layout, but it's the easiest fix while hacking. In functions/package.json you can then have a local dependency on ./server.
  • Expose your code behind a URL somewhere. For example, you could package up a .tar and put that on Google Drive, or on Firebase Cloud Storage. Alternatively, you can use a public git repository.

将来,如果 firebase deploy 遵循符号链接,我会很高兴.我已经在 Firebase 的内部错误跟踪器中提交了一个功能请求.

In the future, I'd love it if firebase deploy followed symlinks. I've filed a feature request for that in Firebase's internal bug tracker.

这篇关于Google Cloud Functions:如何共享源代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆