如何有一个Google云功能的多个API端点?

问题描述 投票:0回答:5
我有一个Google云功能,其中包含要在不同路径上调用的多个模块。 我正在使用无服务器框架来部署我的功能,但是它的限制只有一个函数的一个路径。

我想在一个函数中使用多个路径,就像我们在AWS无服务器框架中一样。

支持云函数将具有两个路径

user

以及

/user/add

/user/remove

;这两个路径都应调用相同的功能。
像这样的东西:
serverless.yml

functions: user: handler: handle events: - http: user/add - http: user/remove

我可以为一个GCF拥有多个API端点?

是的,确实没有实际的休息服务来备份Google Cloud功能。它使用盒子http触发器。
要努力,我正在使用请求有效负载来确定要执行的操作。在身体中,我正在添加一个名为

"path"
的键。
,例如,考虑函数用户。

添加用户:

google-cloud-functions serverless-framework
5个回答
5
投票

删除用户:

{ "path":"remove", "body":{ "first":"Jhon", "last":"Doe" } }

如果您的操作纯粹是Crud,则可以使用

request.method

提供诸如

GET

POST
PUT

DELETE
等动词来确定操作。
    

您可以使用Firebase托管重写URL。

在您的
Firebase.json
文件:
"hosting": {
    "rewrites": [
          {
            "source": "/api/v1/your/path/here",
            "function": "your_path_here"
          }
    ]
}
保留这是一个解决方法,它有一个主要的缺点:您将为双重打击而付费。如果您的应用必须扩展,请考虑这一点。
    
您可以在不同的luntimes
中写下您的功能。

Node.js

3
投票
Express

框架。因此,您可以使用其路由器在单个功能中构建不同的路由。

依赖性

npm install [email protected]

以下示例正在使用Typescript。遵循这些
GUIDELINES
启动打字稿项目。

// index.ts import { HttpFunction } from '@google-cloud/functions-framework'; import * as express from 'express'; import foo from './foo'; const app = express(); const router = express.Router(); app.use('/foo', foo) const index: HttpFunction = (req, res) => { res.send('Hello from the index route...'); }; router.get('', index) app.use('/*', router) export const api = app


2
投票

部署运行: gcloud functions deploy YOUR_NAME \ --runtime nodejs16 \ --trigger-http \ --entry-point api \ --allow-unauthenticated

    
目前,在Google中,每个功能仅允许一个事件定义。 

更多

可以使用
npm i express

安装express,然后导入并或多或少作为正常处理以处理路由: const express = require("express"); const app = express(); // enable CORS if desired app.use((req, res, next) => { res.set("Access-Control-Allow-Origin", "*"); next(); }); app.get("/", (req, res) => { res.send("hello world"); }); exports.example = app; // `example` is whatever your GCF entrypoint is

如果出于某种原因不是Express的选项,或者用例非常简单,则自定义路由器可能就足够了。
如果涉及参数或通配符,请考虑使用

route-parser

。一个删除答案
建议thisApp

0
投票
Express请求对象有一些有用的参数,您可以利用:

req.method

0
投票

req.path

在没有查询字符串的情况下提供路径

req.query
解析的键值查询字符串的对象

req.body

解析的json身体

there是一个简单的概念证明:
const routes = { GET: { "/": (req, res) => { const name = (req.query.name || "world"); res.send(`<!DOCTYPE html> <html lang="en"><body><h1> hello ${name.replace(/[\W\s]/g, "")} </h1></body></html> `); }, }, POST: { "/user/add": (req, res) => { // TODO stub res.json({ message: "user added", user: req.body.user }); }, "/user/remove": (req, res) => { // TODO stub res.json({message: "user removed"}); }, }, }; exports.example = (req, res) => { if (routes[req.method] && routes[req.method][req.path]) { return routes[req.method][req.path](req, res); } res.status(404).send({ error: `${req.method}: '${req.path}' not found` }); }; usage: $ curl https://us-east1-foo-1234.cloudfunctions.net/example?name=bob <!DOCTYPE html> <html lang="en"><body><h1> hello bob </h1></body></html> $ curl -X POST -H "Content-Type: application/json" --data '{"user": "bob"}' \ > https://us-east1-foo-1234.cloudfunctions.net/example/user/add {"message":"user added","user":"bob"}

如果您遇到CORS和/或前飞行问题的麻烦,请参见

GoogleCloud功能启用CORS?

  • yes,至少在云功能中,您可以。您可以使用
  • request.path
  • 属性在单个功能上创建自己的路径:
    示例:
  • import { onRequest } from "firebase-functions/v2/https" export const user = onRequest((request, response) => { if (request.path === '/add') { addUser(request, response) } else if (request.path === '/remove') { removeUser(request, response) } })
    添加端点的地址将是:
  • https://us-east1-my-cloud-function.cloudfunctions.net/user/add

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.