Do Google Cloud Platform HTTP Functions Support Route Parameters?

强颜欢笑 提交于 2020-05-25 17:23:41

问题


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-

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

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?


回答1:


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)

Sample code:

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');
    }
}

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).

Please note that:

  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.



回答2:


You could try modofun: https://modofun.js.org

Which is a router for multiple operations based on the request path, and also supports automatic parameter parsing from the path.

It parses the URL path into an operation name and a list of parameters which are populated into req.params, same as with Express when using regex. So you could do something like this:

var modofun = require('modofun')

exports.bookstore = modofun(
  {
    /**
     * /users/:userId/:bookId
     */
    users: (req, res) => {
      var [userId, bookId] = req.params
      // ...
      res.json(data)
    }
  },
  { mode: 'reqres' }
)

Or, you can also have the parameters expanded into function arguments and work with pure functions, like this:

var modofun = require('modofun')

exports.bookstore = modofun(
  {
    /**
     * /users/:userId/:bookId
     */
    users: (userId, bookId) => {
      // ...
      return data
    }
  },
  { mode: 'function' }
)

I made it for a Google Cloud Functions deployment I have in production, mostly because I don't need the large dependency trail from Express, and I wanted something more lightweight.

But remember that you can also just use an Express together with Google Cloud Functions. Nothing stops you from creating an Express app with the usual route matching rules and then passing that as the export for your GCloud Function.

Hope that helps.




回答3:


If you don't want to pass data in the body, you can always put it into a query parameter of the url. Like:

http://yourGoogleProject.cloudfunctions.net/users?userid={userId}&bookId={bookid}

And in the cloud function you simply access the query parameter from the req object, like:

exports.users = (req, res) => {
  res.status(200).send(`Hello from user:  + $(req.query.userid) your bookId: $(req.query.bookid)`);
  }


来源:https://stackoverflow.com/questions/42145759/do-google-cloud-platform-http-functions-support-route-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!