十年匠心定制 · 商业建站与技术教学双线并行 咨询热线:400-886-1026 service@lmnt.cn
ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

functions-samples多语言实战:Node、Python、Dart三种语言编写同一个Cloud Function

functions-samples多语言实战:Node、Python、Dart三种语言编写同一个Cloud Function functions-samples多语言实战Node、Python、Dart三种语言编写同一个Cloud Function【免费下载链接】functions-samplesCollection of sample apps showcasing popular use cases using Cloud Functions for Firebase项目地址: https://gitcode.com/gh_mirrors/fu/functions-samplesCloud Function 多语言实战来了functions-samples 仓库汇集了 Cloud Functions for Firebase 的经典示例应用同一个函数比如返回服务器时间的 HTTPS 触发器分别用Node.js、Python、Dart三种语言实现是理解 Cloud Function 触发器机制的最佳多语言教程。 仓库结构一览三大语言目录怎么分工打开仓库顶层目录语言划分非常清晰目录语言/代次内容Node/Node.js 2nd gen官方主推功能最全Jest 测试、OpenTelemetry、Task Queues 等Node-1st-gen/Node.js 1st gen60 个存量示例涵盖 Stripe 支付、图像审核、FCM 推送等Python/Python 2nd gen与 Node 同构的 quickstart官方公开预览Dart/Dart 2nd gen最新加入基于firebase_functionsDart SDKfunctions-samples/ ├── Node/ # Node 2nd gen 示例 ├── Node-1st-gen/ # Node 1st gen 示例 ├── Python/ # Python 示例 └── Dart/ # Dart 示例其中 quickstarts 是每种语言的重叠区uppercase-firestore、callable-functions、https-time-server这几个入门示例在三种语言里都有对应版本方便横向对比。⏰ 同一个 Cloud Function三种写法长什么样以 HTTPS 触发器 Time Server 为例——接收format参数、返回格式化后的当前时间Node、Python、Dart 三个版本功能完全一致语言入口文件触发器写法Nodeindex.jsonRequest(...)导出函数Pythonmain.pyhttps_fn.on_request装饰器Dartserver.dartfirebase.https.onRequest三种风格的核心差异各摘几行感受一下Node 2nd gen—— 模块导出式exports.date onRequest({timeoutSeconds: 1200}, (req, res) { const formattedDate moment().format(req.query.format); res.status(200).send(formattedDate); });Python—— 装饰器式https_fn.on_request(corsoptions.CorsOptions(cors_origins*)) def date(req: https_fn.Request) - https_fn.Response: formatted_date datetime.now().strftime(format) return https_fn.Response(formatted_date)Dart—— 注册式main()里统一挂载路由void main() { runFunctions((firebase) { firebase.https.onRequest(name: date, (request) async { final formattedDate DateFormat(format).format(DateTime.now()); return Response.ok(formattedDate); }); }); } 观察要点请求解析Node 用req.query/req.bodyPython 用req.args/req.get_json()Dart 用request.url.queryParameters 手动jsonDecode错误处理三者都拦截了PUT请求并返回 403Node/Python 用res.status(403)Dart 直接调用内置的Response.forbidden()日期格式库Node 用moment、Python 用标准库datetime、Dart 用intl包——格式字符串语法互不通用跨语言移植时要特别注意。 Callable Function 三语言对比callable-functions 示例实现两数相加 敏感词过滤Nodeindex.jsonCall((request) ...)参数校验失败抛HttpsError(invalid-argument, ...)Dartserver.dartfirebase.https.onCall(name: addNumbers, ...)校验失败抛InvalidArgumentErrorPython见 Python/quickstarts/callable-functions装饰器写法与 HTTPS 版本同理。Dart 版本还额外演示了如何读取request.auth?.uid、token?[email]和 Node 的request.auth几乎一一对应方便验证认证信息在各语言下如何传递。 三步跑起来clone、配置、部署1. 克隆仓库git clone https://gitcode.com/gh_mirrors/fu/functions-samples2. 进入目标示例目录并关联 Firebase 项目cd Node/quickstarts/https-time-server # 或 Python/、Dart/ 对应目录 firebase use --add3. 按语言安装依赖并部署语言安装依赖部署命令Nodecd functions npm installfirebase deployPythonpip install -r functions/requirements.txtfirebase deployDartdart pub getfirebase deploy部署完成后CLI 会输出函数 URL形如https://name-hash.region.run.app浏览器打开即可看到服务器当前时间。Dart 版还支持firebase emulators:start在本地模拟器中调试见 Dart README。⚠️ 注意所有示例部署都需要 Firebase 的 Blaze 按量计费套餐见根 README。 仓库里还有哪些真家伙quickstart 之外Node 1st gen 目录藏着不少生产级玩法例如Stripe / Paypal 支付Firebase Auth 实时数据库 支付网关集成demo.gif 展示了完整下单流程图像流水线convert-images上传自动转 JPEG、moderate-imagesAI 检测 打码、exif-images元数据提取全部由 Storage 事件触发数据一致性child-count、limit-children、delete-old-child-nodes 演示用函数维护数据库不变式测试模板test-functions-jest 和 test-functions-mocha 提供 Jest / Mocha 单测脚手架写 Cloud Function 前先学会测它。❓ 常见问题QPython 版本可以用吗可以用但官方标注为公开预览public preview接口可能向后不兼容生产项目建议优先选 Node 2nd gen参考根 README 说明。QDart 示例会取代 Node 吗不会。Dart 目录目前只覆盖callable-functions、https-time-server等少数 quickstart见 Dart/quickstarts属于官方 SDK 生态扩展适合 Flutter/Dart 技术栈用户。Q1st gen 和 2nd gen 我该学哪个新项目直接学 Node/2nd gen区域、超时可配置Python/Dart 也都属于 2nd gen1st gen 的价值在于示例数量最多适合找现成业务模式参考。 总结functions-samples 的价值在于**同一个函数多语言对照**Time Server、Callable AddNumbers 等示例在 Node、Python、Dart 三套目录下逐一对应配合 quickstarts 清单可以 30 分钟内把每种语言的触发器、请求解析、错误处理风格全部过一遍再顺手把 Stripe、图像审核等实战示例搬进自己的项目。【免费下载链接】functions-samplesCollection of sample apps showcasing popular use cases using Cloud Functions for Firebase项目地址: https://gitcode.com/gh_mirrors/fu/functions-samples创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表