我想在一个函数中使用多个路径,就像我们在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"
的键。,例如,考虑函数用户。
添加用户:
删除用户:
{
"path":"remove",
"body":{
"first":"Jhon",
"last":"Doe"
}
}
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
Express
框架。因此,您可以使用其路由器在单个功能中构建不同的路由。
依赖性
npm install [email protected]
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
部署运行:
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
如果涉及参数或通配符,请考虑使用
route-parser
。一个删除答案建议thisApp
req.path
在没有查询字符串的情况下提供路径
req.query
解析的键值查询字符串的对象req.body
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?
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