我想要一个像https://<my-project-id>.firebaseapp.com/parameter
这样的自定义网址,以便我有一个接受值'参数'的函数。
以下是我对firebase.json重写的内容:
"rewrites": [
{
"source": "/:bar*", "function": "foo"
},
{
"source": "**",
"destination": "/index.html"
}
]
然后对于functions / index.js这里是我的'foo'函数:
exports.foo = functions.https.onRequest((req, res) => {
console.log('bar = '+req.query.bar);
console.info(req.query.bar);
res.status(200).send(`<!doctype html>
<head>
<title>Test</title>
</head>
<body>
Bar = ${req.query.bar}
</body>
</html>`);
});
如果我将firebase.json更改为以下内容:
"rewrites": [
{
"source": "/**", "function": "foo"
},
{
"source": "**",
"destination": "/index.html"
}
]
并写下这样的网址:
https://<my-project-id>.firebaseapp.com/anything?bar=test
然后它正确获取 bar 参数。但是,我需要能够缩短网址。
答案 0 :(得分:1)
在其他答案之后,这是我为使“ getUser”请求工作所做的:
exports.getUser = (req, res) => {
// https://us-central1-project.cloudfunctions.net/api/user/diegovfeder
// console.log(req.url);
let userId = req.url.split("/");
userId = userId[userId.length - 1];
//console.log(userId);
...
答案 1 :(得分:0)
尝试console.log(req.url);
获取网址。然后你只需要在/