Google Cloud Platform HTTP 函数是否支持路由参数? [英] Do Google Cloud Platform HTTP Functions Support Route Parameters?

查看:27
本文介绍了Google Cloud Platform HTTP 函数是否支持路由参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题比我想提出的问题要简单一些,但我一直在努力寻找答案,但我绝对不能-

This is a bit simpler a question than I tend to like to come here with but I've been driving myself up the wall trying to find an answer to this and I absolutely cannot-

谷歌云平台 HTTP 函数是否支持路由参数,如这里?http://expressjs.com/en/guide/routing.html#route-parameters

Do Google Cloud Platform HTTP Functions support Route Parameters, as here? http://expressjs.com/en/guide/routing.html#route-parameters

具体来说,我看到 Google Cloud Platform HTTP Functions 似乎使用 Express 作为基础,但我看到的所有函数都已经运行了 req 和 res 参数,仅此而已.我可以访问正文中的数据,但这不允许我从路由中提取参数,例如在传递给/users/:userId/books/:bookId"的请求中查找书籍 ID.我无法看到如何将它们填充到 req.params 中,而无法在此处指定路径的哪一部分对应于哪个名称.

Specifically, I see that Google Cloud Platform HTTP Functions appear to use Express as a base, yet all functions I see any example of already just run off of req and res parameters and nothing else. I can access data within the body, but that doesn't allow me to pull parameters from the route like finding the book ID in a request passed to "/users/:userId/books/:bookId". I can't see how they could be populated into req.params without the ability to specify which part of the path corresponds to which name as here.

我知道我总是可以通过另一种方式传递它们,但这更简洁,更符合我们想要使用的设置,所以如果可能的话,我真的很想让它工作.有什么方法可以做到这一点,我完全不知道还是根本不支持?

I understand that I can always pass them in another way, but this is cleaner and more in keeping with the setup we'd like to use, so I'd really like to make it work if possible. Is there some way to do this that I'm completely missing or is this not supported at all?

推荐答案

我能够为此联系支持小组,看来是的,这是受支持的 - 您只需要使用 req.path 即可拉出完整路径,然后以某种方式解析它(我使用了 path-to-regexp)

I was able to reach out to the support group for this and it appears that yes, this is supported - you just have to use req.path in order to pull the full path and then parse it in some way (I used path-to-regexp)

示例代码:

exports.myFunction = function(req, res) {
    var keys = [];
    var re = pathToRegexp('/:paramA/:paramB/not-a-param/:paramC/also-not-a-param/:paramD?', keys, {strict: false});
    var pathVars = re.exec(req.path);
    if (pathVars) {
        console.log(JSON.stringify(pathVars));
        var paramA = pathVars[1];
        var paramB = pathVars[2];
        var paramC = pathVars[3];
        var paramD = pathVars[4];
        // Do stuff with the rest of your functionality here
        res.status(200).send('Whatever you wanna send');
    }
}

部署它的命令行代码看起来类似于 gcloud beta functions deploy myFunction --stage-bucket;--trigger-http(此命令的完整文档此处).您的新端点 URL 将是 https://<YOUR_REGION>-<YOUR_PROJECT_ID>.cloudfunctions.net/myFunction,然后您可以在实际制作时将后续查询或路由参数附加到它调用(例如,对 https://-.cloudfunctions.net/myFunction/paramA/paramB/not-a-param/paramC 进行 get 调用/also-not-a-param/paramD).

The command line code to deploy this would then look something like gcloud beta functions deploy myFunction --stage-bucket<STORAGE_BUCKET> --trigger-http (Full documentation for this command here). Your new endpoint URL will then be https://<YOUR_REGION>-<YOUR_PROJECT_ID>.cloudfunctions.net/myFunction, and you can then append subsequent query or route parameters to it when actually making your call (e.g., making a get call to https://<YOUR_REGION>-<YOUR_PROJECT_ID>.cloudfunctions.net/myFunction/paramA/paramB/not-a-param/paramC/also-not-a-param/paramD).

请注意:

  1. 除非您使用 --entry-point 标志,否则您的函数应该以在 CLI 中使用的相同名称导出.此名称将用于生成的 URL.
  2. --stage-bucket 命令是可选的,但我一直使用它.
  3. Cloud Functions 将自动查找名为 index.jsfunction.js 的文件以查找您的函数,但如果您提供 package.json 文件,其中包含 main 条目,然后 Cloud Functions 将查找它.
  4. 我认为这将在某个时候离开测试版,届时您应该更新到新的命令工具.
  1. Your function should be exported under the same name as used in the CLI unless you use the --entry-point flag. This name will be used in your resulting URL.
  2. The --stage-bucket <STORAGE_BUCKET> command is optional, but I've always used it.
  3. Cloud Functions will automatically look to a file named index.js or function.js to find your function, but if you provide a package.json file which contains a main entry, then Cloud Functions will look for it instead.
  4. This will, I assume, leave beta at some point, at which you should update to the new command tools.

这篇关于Google Cloud Platform HTTP 函数是否支持路由参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆