【问题标题】:Optimizing Firebase cloud functions structure in node.js在 node.js 中优化 Firebase 云函数结构
【发布时间】:2021-02-24 21:22:42
【问题描述】:

this official Firebase example 中描述了如何构建云功能以最大限度地减少冷启动时间。

主要思想在index.ts文件中似乎如下:

export const httpFn =
functions.https.onRequest(async (request, response) => {
    await (await import('./fn/httpFn')).default(request, response)
})

换句话说,使用 await(await import())。然而,这个例子是在打字稿中。这在 node.js 中看起来如何?我找到了一些例子。例如。 this one.

其中 index.js 文件有:

exports.helloWorld = require('./hello-world').helloWorld

各个函数文件如下所示:

const functions = require('firebase-functions')

exports.helloWorld = functions.https.onRequest((request, response) => {
    const output = {
        di: 'is cool',
    }
    response.send(output)
})

这是在 node.js 中最小化冷启动时间的最佳方法吗?鉴于我不能使用“等待导入”。这个结构是否只会为每个单独的函数加载必要的依赖项?或者它是否仍会加载所有必需(导入)文件的所有依赖项?

【问题讨论】:

    标签: javascript firebase google-cloud-functions


    【解决方案1】:

    您所指的那篇博文不是“官方示例”。这是对我个人想法和意见的描述。

    您在此处显示的内容与博文中显示的内容不同。如果你想要 TypeScript 异步导入的纯 JavaScript 实现,你将不得不编写代码来模仿它。 require()不是异步的,不能实现相同的目标。

    我建议阅读有关此主题的 Stack Overflow 上的其他问题:

    【讨论】:

      猜你喜欢
      • 2017-09-14
      • 2014-06-06
      • 2015-03-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-08
      • 2012-11-17
      • 2017-08-10
      • 2011-11-30
      相关资源
      最近更新 更多