只有 POST 请求可用于 Google Cloud Functions HTTP 触发器

Are only POST requests available with Google Cloud Functions HTTP Triggers

我希望将其余的 API 部署到 Google Cloud Functions,但是部署文档似乎表明只能使用 POST 请求:

Every HTTP POST request to the function's endpoint (web_trigger.url parameter of the deploy output) will trigger function execution. Result of the function execution will be returned in response body. - https://cloud.google.com/functions/docs/deploying/

理想情况下,我希望将路径与通配符相关联,并跨不同的 HTTP 方法,例如

POST /user
GET  /user/:id
PUT  /user/:id
DEL  /user/:id

使用通配符值填充函数上下文中的某些参数对象,如 Rails、Hapijs 等

想知道 Cloud Functions 是否可以实现上述类似功能,如果不能,将来是否会实现?

POST-only 是文档中的错字(糟糕!);我会更新的。 Google Cloud Function HTTP 函数支持 GET、PUT、POST、DELETE 和 OPTIONS。

(请参阅 https://cloud.google.com/functions/docs/writing/http 处的 HTTP 函数文档)

If function needs to handle multiple HTTP methods (GET, PUT, POST, and so on), you can simply inspect the method property of the request.

您可以通过 req.method 检查 HTTP 方法,即

switch (req.method) {
  case 'GET':
    handleGET(req, res);
    break;
  case 'PUT':
    handlePUT(req, res);
    break;
  default:
    res.status(500).send({ error: 'Something blew up!' });
    break;
}

至于您问题的 routing/mapping 部分,目前作为 GCF 的一部分,路由没有任何额外内容。一如既往,敬请关注我们不断开发新功能!