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

查看:78
本文介绍了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-

Google Cloud Platform 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函数似乎以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<STORAGE_BUCKET> --trigger-http(此命令的完整文档此处).这样,新的端点URL将为https://<YOUR_REGION>-<YOUR_PROJECT_ID>.cloudfunctions.net/myFunction,然后您可以在实际拨打电话(例如,对https://<YOUR_REGION>-<YOUR_PROJECT_ID>.cloudfunctions.net/myFunction/paramA/paramB/not-a-param/paramC/also-not-a-param/paramD进行get呼叫)时向其附加后续查询或路由参数.

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 <STORAGE_BUCKET>命令是可选的,但我一直使用它.
  3. Cloud Functions将自动查找名为index.jsfunction.js的文件以找到您的功能,但是如果您提供包含main条目的package.json文件,则Cloud Functions将查找它.
  4. 我认为这会在某个时候离开beta,在那一刻您应该更新到新的命令工具.
  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天全站免登陆