首页 > 解决方案 > 在 firebase 函数中,获取当前的 https://domain 部分

问题描述

我正在编写一个 firebase 函数,以便它将基于请求路径/url 呈现一个页面。

const functions = require('firebase-functions')

function buildHtml (path) {
  return HTML_BODY_WITH_path_BEING_CONSIDERED
}

export const rendering = functions.https.onRequest(function(req, res) {
  const path = req.path
  res.send(buildHtml(path))
})

当我将每个源代码重写为此函数时,我可以/colletion/1path访问https://MY-PROJECT.firebaseapp.com/collection/1.

我想不通的是如何获得MY-PROJECT.firebaseapp.com零件。

我取了req.domain,但它是null

任何想法?

标签: node.jsfirebaseexpressgoogle-cloud-functions

解决方案


由于 HTTPS 功能由 Express 提供(并接收一个 express 请求对象作为参数,因此您需要将请求hostname与请求一起使用protocol来构建可能对调用者有帮助的 URL。这些都是req参数上的属性在您给定的代码中,这是一个Express Request 对象


推荐阅读