为 Cloud Functions 更改 HTTP URL

Change HTTP URL for Cloud Function

我正在使用 Cloud Functions for Firebase 并编写 HTTP 触发器。我想将函数 URL 更改为:

https://us-central1-[projectID].cloudfunctions.net/helloWorld

至:

https://us-central1-[projectID].cloudfunctions.net/api/helloWorld

这可能吗?

如果要指定 URL 的路径,则不能使用普通的旧 HTTP 函数。您必须改为创建一个 Express 应用程序并将其移交给 Cloud Functions 进行服务。代码看起来像这样:

const functions = require('firebase-functions');
const express = require('express');
const app = express();

app.get('/helloWorld', (req, res) => {
    // your function code here
    res.send("hello")
})

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

请注意,/api 来自导出的名称,/helloWorld 来自 app.get 中的路径。

您还必须安装 express 模块:

npm install express